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 | f4161376f9732ca13713f53cff241b89 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
/*
Прокрастинирую
*/
public class Main {
static FastReader in;
static PrintWriter out;
static Random rand = new Random();
static final int INF = (int) (1e9 + 10);
static final int MOD = (int) (1e9 + 7);
static final int N = (int) (1e5 + 5);
static final int LOGN = 20;
static void solve() {
int n = in.nextInt();
char[] s = in.next().toCharArray();
int h = 0, v = 0, l = -1, r = -1;
TreeMap<Long, Integer> tm = new TreeMap<>();
tm.put(0L, 0);
for (int i = 0; i < n; i++) {
if (s[i] == 'R') {
h++;
} else if (s[i] == 'U') {
v++;
} else if (s[i] == 'L') {
h--;
} else if (s[i] == 'D') {
v--;
}
if (tm.containsKey((long) h * n + v)) {
int j = tm.get((long) h * n + v);
if ((l == -1 && r == -1) || (i - j < r - l)) {
l = j + 1;
r = i + 1;
}
tm.put((long) h * n + v, i + 1);
} else {
tm.put((long) h * n + v, i + 1);
}
}
if (l == -1 && r == -1) {
out.println(-1);
} else {
out.println(l + " " + r);
}
}
public static void main(String[] args) throws FileNotFoundException {
in = new FastReader(System.in);
// in = new FastReader(new FileInputStream("sometext.txt"));
out = new PrintWriter(System.out);
// out = new PrintWriter(new FileOutputStream("output.txt"));
int q = in.nextInt();
while (q-- > 0) {
solve();
}
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
Integer nextInt() {
return Integer.parseInt(next());
}
Long nextLong() {
return Long.parseLong(next());
}
Double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(nextLine());
}
return st.nextToken();
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
}
} | Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 58187cb86ae6a67bf4e7db6d025dde5a | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
/*
procrastinating
*/
public class Main {
static FastReader in;
static PrintWriter out;
static Random rand = new Random();
static final int INF = (int) (1e9 + 10), MOD = (int) (998244353), LOGN = 60;
static final long IINF = (long) (2e18 + 10);
static int n;
static char[] s;
static class Treap {
class Node {
long x, y;
int val;
Node left, right;
Node(long x, long y, int val) {
this.x = x;
this.y = y;
this.val = val;
}
}
Node root;
Node merge(Node t1, Node t2) {
if (t1 == null) return t2;
if (t2 == null) return t1;
if (t1.y >= t2.y) {
t1.right = merge(t1.right, t2);
return t1;
} else {
t2.left = merge(t1, t2.left);
return t2;
}
}
Node[] split(Node t, long x) {
if (t == null) return new Node[]{null, null};
if (t.x <= x) {
Node[] tt = split(t.right, x);
t.right = tt[0]; tt[0] = t;
return tt;
} else {
Node[] tt = split(t.left, x);
t.left = tt[1]; tt[1] = t;
return tt;
}
}
int find(Node t, long x) {
if (t == null) return -1;
if (t.x == x) return t.val;
return t.x < x ? find(t.right, x) : find(t.left, x);
}
void insert(long x, int val) {
Node[] tt1 = split(root, x - 1);
Node[] tt2 = split(tt1[1], x);
Node t = new Node(x, rand.nextLong(), val);
root = merge(tt1[0], merge(t, tt2[1]));
}
void erase(long x) {
Node[] tt1 = split(root, x - 1);
Node[] tt2 = split(tt1[1], x);
root = merge(tt1[0], tt2[1]);
}
int find(long x) {
return find(root, x);
}
}
static long hash(int x, int y) {
return 1L * y * n + x;
}
static void solve() {
n = in.nextInt();
s = in.next().toCharArray();
int x = 0, y = 0, ans1 = -1, ans2 = -1;
Treap tr = new Treap();
tr.insert(hash(x, y), 0);
for (int i = 0; i < n; i++) {
if (s[i] == 'U') y++;
else if (s[i] == 'R') x++;
else if (s[i] == 'D') y--;
else if (s[i] == 'L') x--;
long h = hash(x, y);
int pos = tr.find(h);
if (pos != -1 && (ans1 == -1 || i + 1 - pos < ans2 - ans1 + 1)) {
ans1 = pos + 1;
ans2 = i + 1;
}
tr.insert(h, i + 1);
}
if (ans1 == -1) {
out.println(ans1);
} else {
out.println(ans1 + " " + ans2);
}
}
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
in = new FastReader(System.in);
// in = new FastReader(new FileInputStream("input.txt"));
out = new PrintWriter(System.out);
// out = new PrintWriter(new FileOutputStream("output.txt"));
Thread thread = new Thread(null, () -> {
int tests = 1;
tests = in.nextInt();
while (tests-- > 0) {
solve();
}
}, "Go", 1 << 28);
thread.start();
thread.join();
// out.flush();
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
Integer nextInt() {
return Integer.parseInt(next());
}
Long nextLong() {
return Long.parseLong(next());
}
Double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(nextLine());
}
return st.nextToken();
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
}
} | Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 21badc72a2def8b536985de68968e79f | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes | //package Contest1296;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class YetAnotherButWithDPThisTime {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int t = 0; t < T; t++) {
HashMap<Integer, HashMap<Integer, Integer>> dp = new HashMap<>();
int N = Integer.parseInt(br.readLine());
char[] directions = br.readLine().toCharArray();
HashMap<Integer, Integer> zero = new HashMap<>();
zero.put(0, 0);
dp.put(0, zero);
List<int[]> points = new ArrayList<>();
int[] origin = new int[3];
points.add(origin);
int startIndex = Integer.MAX_VALUE;
int len = Integer.MAX_VALUE;
for (int i = 0; i < N; i++) {
// System.out.println(Arrays.deepToString(points.toArray()));
int[] current = new int[3];
current = lastArrayCopied(points);
addValue(current, directions[i]);
if (dp.containsKey(current[0])) {
if (dp.get(current[0]).containsKey(current[1])) {
int currentLenth = current[2] - dp.get(current[0]).get(current[1]);
if (currentLenth < len) {
startIndex = dp.get(current[0]).get(current[1]);
len = currentLenth;
}
dp.get(current[0]).replace(current[1], current[2]);
} else {
dp.get(current[0]).put(current[1], current[2]);
}
} else {
HashMap<Integer, Integer> subMap = new HashMap<>();
dp.put(current[0], subMap);
dp.get(current[0]).put(current[1], current[2]);
}
points.add(current);
}
if (startIndex == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println((startIndex + 1) + " " + (startIndex + len));
}
}
br.close();
}
public static int[] lastArrayCopied(List<int[]> points) {
int[] pushArray = points.get(points.size() - 1);
int[] returnArray = new int[pushArray.length];
System.arraycopy(pushArray, 0, returnArray, 0, pushArray.length);
returnArray[2]++;
return returnArray;
}
public static void addValue(int[] arr, char a) {
if (a == 'L') {
arr[0]--;
}
if (a == 'R') {
arr[0]++;
}
if (a == 'U') {
arr[1]++;
}
if (a == 'D') {
arr[1]--;
}
}
}
| Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 6f26235ce33c81bd831a705d4279f717 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
/**
*
* @author Saju
*
*/
public class Main {
private static int dx[] = { -2, -3, -2, -1, -1, 1};
private static int dy[] = { 1, -1, -1, -2, -3, -2};
private static final long INF = (long) (1e15);
private static final int INT_INF = Integer.MAX_VALUE;
private static final long NEG_INF = Long.MIN_VALUE;
private static final double EPSILON = 1e-10;
private static final int MAX = 1007;
private static final int MOD = 10007;
private static final int MAXN = 100007;
private static final int MAXA = 10000009;
private static final int MAXLOG = 22;
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
/*
1
5
RRUDU
*/
int test = in.nextInt();
for (int t = 1; t <= test; t++) {
int n = in.nextInt();
String str = in.next();
int final_x = 0;
int final_y = 0;
Map<String, Integer> map = new HashMap<>();
String s = final_x + "*" + final_y;
map.put(s, 0);
int minCircle = INT_INF;
int start = -1;
int end = -1;
char ch[] = str.toCharArray();
for(int i = 0; i < n; i++) {
char c = ch[i];
if(i + 1 < n) {
int c1 = ch[i + 1];
if((c == 'L' && c1 == 'R') || (c == 'R' && c1 == 'L') ||
(c == 'U' && c1 == 'D') || (c == 'D' && c1 == 'U')) {
start = i + 1;
end = i + 2;
break;
}
}
if(c == 'L') {
final_x--;
}
else if(c == 'R') {
final_x++;
}
else if(c == 'U') {
final_y++;
}
else if(c == 'D') {
final_y--;
}
s = final_x + "*" + final_y;
if(map.containsKey(s)) {
int prev = map.get(s);
int diff = (i + 1) - prev;
if(minCircle > diff) {
minCircle = diff;
start = prev;
start++;
end = i + 1;
}
}
map.put(s, i + 1);
// System.out.println(final_x + " " + final_y);
}
// System.out.println(start + " " + end);
if(start == -1) {
out.println("-1");
}
else {
out.println(start + " " + end);
}
}
out.flush();
out.close();
System.exit(0);
}
private static void generatePrime(int n) {
//O(NloglogN)
boolean arr[] = new boolean[n + 5];
Arrays.fill(arr, true);
for(int i = 2; i * i <=n; i++){
if(arr[i] == true){
for(int j = i * i; j <= n; j+=i){
arr[j] = false;
}
}
}
int count = 0;
int start = 0;
for(int i = 2; i <= n; i++){
if(arr[i] == true){
// System.out.println(i + " ");
count++;
}
if(count == (start * 100) + 1) {
// System.out.println(i);
start++;
}
}
System.out.println();
System.out.println(count);
}
private static Map<Integer, Integer> primeFactorization(long n){
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i = 2; i <= Math.sqrt(n); i++){
if(n % i == 0){
int count = 0;
while(n % i == 0){
count++;
n = n / i;
}
map.put(i, count);
// System.out.println("i: " + i + ", count: " + count);
}
}
if(n != 1) {
// System.out.println(n);
map.put((int)n, 1);
}
return map;
}
private static class Pair<T>{
T a;
T b;
Pair(T a, T b){
this.a = a;
this.b = b;
}
}
private static class SegmentTree{
int n;
private final int MAXN = 100007;
private long[] tree = new long[MAXN << 2];
private long[] lazy = new long[MAXN << 2];
private long[] arr = new long[MAXN];
/***
*
* arr is 1 based index.
*
* @param arr
*
*/
SegmentTree(int n, long[] arr){
this.n = n;
for(int i = 0; i < arr.length; i++) {
this.arr[i] = arr[i];
}
}
void build(int index, int left, int right) {
if(left == right) {
tree[index] = arr[left];
}
else {
int mid = (left + right) / 2;
build(index * 2, left, mid);
build((index * 2) + 1, mid + 1, right);
tree[index] = max(tree[(index * 2)], tree[(index * 2) + 1]);
}
}
long query(int node, int left, int right, int start, int end) {
if(left > end || right < start) {
return NEG_INF;
}
if(left >= start && right <= end) {
return tree[node];
}
int mid = (left + right) / 2;
long val1 = query(2 * node, left, mid, start, end);
long val2 = query(2 * node + 1, mid + 1, right, start, end);
return max(val1, val2);
}
void update(int node, int left, int right, int idx, long val) {
if(left == right) {
tree[node] += val;
}
else {
int mid = (left + right) / 2;
if(idx <= mid) {
update(2 * node, left, mid, idx, val);
}
else {
update(2 * node + 1, mid + 1, right, idx, val);
}
tree[node] = max(tree[(2 * node) + 1], tree[(2 * node)]);
}
}
void updateRange(int node, int start, int end, int l, int r, long val)
{
if(lazy[node] != 0)
{
// This node needs to be updated
tree[node] = lazy[node]; // Update it
if(start != end)
{
lazy[node*2] = lazy[node]; // Mark child as lazy
lazy[node*2+1] = lazy[node]; // Mark child as lazy
}
lazy[node] = 0; // Reset it
}
if(start > end || start > r || end < l) // Current segment is not within range [l, r]
return;
if(start >= l && end <= r)
{
// Segment is fully within range
tree[node] = val;
if(start != end)
{
// Not leaf node
lazy[node*2] = val;
lazy[node*2+1] = val;
}
return;
}
int mid = (start + end) / 2;
updateRange(node*2, start, mid, l, r, val); // Updating left child
updateRange(node*2 + 1, mid + 1, end, l, r, val); // Updating right child
tree[node] = max(tree[node*2], tree[node*2+1]); // Updating root with max value
}
long queryRange(int node, int start, int end, int l, int r)
{
if(start > end || start > r || end < l)
return 0; // Out of range
if(lazy[node] != 0)
{
// This node needs to be updated
tree[node] = lazy[node]; // Update it
if(start != end)
{
lazy[node*2] = lazy[node]; // Mark child as lazy
lazy[node*2+1] = lazy[node]; // Mark child as lazy
}
lazy[node] = 0; // Reset it
}
if(start >= l && end <= r) // Current segment is totally within range [l, r]
return tree[node];
int mid = (start + end) / 2;
long p1 = queryRange(node*2, start, mid, l, r); // Query left child
long p2 = queryRange(node*2 + 1, mid + 1, end, l, r); // Query right child
return max(p1, p2);
}
void buildRange(int node, int low, int high){
if(low == high){
tree[node] = arr[low];
return;
}
int mid = (low + high) / 2;
int left = node << 1;
int right = left | 1;
buildRange(left, low, mid);
buildRange(right, mid + 1, high);
tree[node] = max(tree[left], tree[right]);
}
void printSegmentTree() {
System.out.println(Arrays.toString(tree));
}
}
private static class KMP{
private static char[] t;
private static char[] s;
public int kmp(char[]t, char[]s) {
this.t = t;
this.s = s;
return this.kmp();
}
private int kmp() {
List<Integer> prefixTable = getPrefixTable(s);
int match = 0;
int i = 0;
int j = 0;
int n = t.length;
int m = s.length;
while(i < n) {
if(t[i] == s[j]) {
if(j == m - 1) {
match++;
j = prefixTable.get(j - 1);
continue;
}
i++;
j++;
}
else if(j > 0) {
j = prefixTable.get(j - 1);
}
else {
i++;
}
}
return match;
}
/***
1. We compute the prefix values π[i] in a loop by iterating <br/>
from i=1 to i=n−1 (π[0] just gets assigned with 0). <br/><br/>
2. To calculate the current value π[i] we set the variable j <br/>
denoting the length of the best suffix for i−1. Initially j=π[i−1]. <br/>
3. Test if the suffix of length j+1 is also a prefix by <br/><br/>
comparing s[j] and s[i]. If they are equal then we assign π[i]=j+1, <br/>
otherwise we reduce j to π[j−1] and repeat this step. <br/><br/>
4. If we have reached the length j=0 and still don't have a match, <br/>
then we assign π[i]=0 and go to the next index i+1. <br/><br/>
@param
pattern(String)
***/
private List<Integer> getPrefixTable(char[] pattern){
List<Integer> prefixTable = new ArrayList<Integer>();
int n = pattern.length;
for(int i = 0; i < n; i++) {
prefixTable.add(0);
}
for(int i = 1; i < n; i++) {
for(int j = prefixTable.get(i - 1); j >= 0;) {
if(pattern[j] == pattern[i]) {
prefixTable.set(i, j + 1);
break;
}
else if(j > 0){
j = prefixTable.get(j - 1);
}
else {
break;
}
}
}
return prefixTable;
}
}
private static class Point {
int x;
int y;
Point(int x, int y){
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
Point ob = (Point) obj;
if(this.x == ob.x && this.y == ob.y){
return true;
}
return false;
}
@Override
public String toString() {
return this.x + " " + this.y;
}
@Override
public int hashCode() {
return 0;
}
}
private static long pow(int base, int pow) {
long val = 1L;
for(int i = 1; i <= pow; i++) {
val *= base;
}
return val;
}
private static int log(int x, int base) {
return (int) (Math.log(x) / Math.log(base));
}
private static int log(long x, int base) {
return (int) (Math.log(x) / Math.log(base));
}
private static long max(long a, long b) {
if (a >= b) {
return a;
}
return b;
}
private static long abs(long a) {
if (a < 0) {
return -a;
}
return a;
}
private static int abs(int a) {
if (a < 0) {
return -a;
}
return a;
}
private static int max(int a, int b) {
if (a >= b) {
return a;
}
return b;
}
private static int min(int a, int b) {
if (a <= b) {
return a;
}
return b;
}
private static long min(long a, long b) {
if (a <= b) {
return a;
}
return b;
}
private static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
private static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
private static long bigMod(long n, long k, long m) {
long ans = 1;
while (k > 0) {
if ((k & 1) == 1) {
ans = (ans * n) % m;
}
n = (n * n) % m;
k >>= 1;
}
return ans;
}
/*
* Returns an iterator pointing to the first element in the range [first,
* last] which does not compare less than val.
*
*/
private static int lowerBoundNew(long[] arr, long num) {
int start = 0;
int end = arr.length - 1;
int index = 0;
int len = arr.length;
int mid = 0;
while (true) {
if (start > end) {
break;
}
mid = (start + end) / 2;
if (arr[mid] > num) {
end = mid - 1;
} else if (arr[mid] < num) {
start = mid + 1;
} else {
while (mid >= 0 && arr[mid] == num) {
mid--;
}
return mid + 1;
}
}
if (arr[mid] < num) {
return mid + 1;
}
return mid;
}
/*
* upper_bound() is a standard library function in C++ defined in the header
* . It returns an iterator pointing to the first element in the range
* [first, last) that is greater than value, or last if no such element is
* found
*
*/
private static int upperBoundNew(long[] arr, long num) {
int start = 0;
int end = arr.length - 1;
int index = 0;
int len = arr.length;
int mid = 0;
while (true) {
if (start > end) {
break;
}
mid = (start + end) / 2;
if (arr[mid] > num) {
end = mid - 1;
} else if (arr[mid] < num) {
start = mid + 1;
} else {
while (mid < len && arr[mid] == num) {
mid++;
}
if (mid == len - 1 && arr[mid] == num) {
return mid + 1;
} else {
return mid;
}
}
}
if (arr[mid] < num) {
return mid + 1;
}
return mid;
}
private static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (IOException e) {
return null;
}
return tokenizer.nextToken();
}
public String nextLine() {
String line = null;
try {
tokenizer = null;
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public boolean hasNext() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
}
} | Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | df838b0d62311bd9aa8880a4b25831a5 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
/**
*
* @author Saju
*
*/
public class Main {
private static int dx[] = { -2, -3, -2, -1, -1, 1};
private static int dy[] = { 1, -1, -1, -2, -3, -2};
private static final long INF = (long) (1e15);
private static final int INT_INF = Integer.MAX_VALUE;
private static final long NEG_INF = Long.MIN_VALUE;
private static final double EPSILON = 1e-10;
private static final int MAX = 1007;
private static final int MOD = 10007;
private static final int MAXN = 100007;
private static final int MAXA = 10000009;
private static final int MAXLOG = 22;
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
// PrintWriter out = new PrintWriter(System.out);
/*
1
5
RRUDU
*/
int test = in.nextInt();
for (int t = 1; t <= test; t++) {
int n = in.nextInt();
String str = in.next();
int final_x = 0;
int final_y = 0;
Map<String, Integer> map = new HashMap<>();
String s = final_x + "*" + final_y;
map.put(s, 0);
int minCircle = INT_INF;
int start = -1;
int end = -1;
char ch[] = str.toCharArray();
for(int i = 0; i < n; i++) {
char c = ch[i];
if(i + 1 < n) {
int c1 = ch[i + 1];
if((c == 'L' && c1 == 'R') || (c == 'R' && c1 == 'L') ||
(c == 'U' && c1 == 'D') || (c == 'D' && c1 == 'U')) {
start = i + 1;
end = i + 2;
break;
}
}
if(c == 'L') {
final_x--;
}
else if(c == 'R') {
final_x++;
}
else if(c == 'U') {
final_y++;
}
else if(c == 'D') {
final_y--;
}
s = final_x + "*" + final_y;
if(map.containsKey(s)) {
int prev = map.get(s);
int diff = (i + 1) - prev;
if(minCircle > diff) {
minCircle = diff;
start = prev;
start++;
end = i + 1;
}
}
map.put(s, i + 1);
// System.out.println(final_x + " " + final_y);
}
// System.out.println(start + " " + end);
if(start == -1) {
System.out.println("-1");
}
else {
System.out.println(start + " " + end);
}
}
// out.flush();
// out.close();
System.exit(0);
}
private static void generatePrime(int n) {
//O(NloglogN)
boolean arr[] = new boolean[n + 5];
Arrays.fill(arr, true);
for(int i = 2; i * i <=n; i++){
if(arr[i] == true){
for(int j = i * i; j <= n; j+=i){
arr[j] = false;
}
}
}
int count = 0;
int start = 0;
for(int i = 2; i <= n; i++){
if(arr[i] == true){
// System.out.println(i + " ");
count++;
}
if(count == (start * 100) + 1) {
// System.out.println(i);
start++;
}
}
System.out.println();
System.out.println(count);
}
private static Map<Integer, Integer> primeFactorization(long n){
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i = 2; i <= Math.sqrt(n); i++){
if(n % i == 0){
int count = 0;
while(n % i == 0){
count++;
n = n / i;
}
map.put(i, count);
// System.out.println("i: " + i + ", count: " + count);
}
}
if(n != 1) {
// System.out.println(n);
map.put((int)n, 1);
}
return map;
}
private static class Pair<T>{
T a;
T b;
Pair(T a, T b){
this.a = a;
this.b = b;
}
}
private static class SegmentTree{
int n;
private final int MAXN = 100007;
private long[] tree = new long[MAXN << 2];
private long[] lazy = new long[MAXN << 2];
private long[] arr = new long[MAXN];
/***
*
* arr is 1 based index.
*
* @param arr
*
*/
SegmentTree(int n, long[] arr){
this.n = n;
for(int i = 0; i < arr.length; i++) {
this.arr[i] = arr[i];
}
}
void build(int index, int left, int right) {
if(left == right) {
tree[index] = arr[left];
}
else {
int mid = (left + right) / 2;
build(index * 2, left, mid);
build((index * 2) + 1, mid + 1, right);
tree[index] = max(tree[(index * 2)], tree[(index * 2) + 1]);
}
}
long query(int node, int left, int right, int start, int end) {
if(left > end || right < start) {
return NEG_INF;
}
if(left >= start && right <= end) {
return tree[node];
}
int mid = (left + right) / 2;
long val1 = query(2 * node, left, mid, start, end);
long val2 = query(2 * node + 1, mid + 1, right, start, end);
return max(val1, val2);
}
void update(int node, int left, int right, int idx, long val) {
if(left == right) {
tree[node] += val;
}
else {
int mid = (left + right) / 2;
if(idx <= mid) {
update(2 * node, left, mid, idx, val);
}
else {
update(2 * node + 1, mid + 1, right, idx, val);
}
tree[node] = max(tree[(2 * node) + 1], tree[(2 * node)]);
}
}
void updateRange(int node, int start, int end, int l, int r, long val)
{
if(lazy[node] != 0)
{
// This node needs to be updated
tree[node] = lazy[node]; // Update it
if(start != end)
{
lazy[node*2] = lazy[node]; // Mark child as lazy
lazy[node*2+1] = lazy[node]; // Mark child as lazy
}
lazy[node] = 0; // Reset it
}
if(start > end || start > r || end < l) // Current segment is not within range [l, r]
return;
if(start >= l && end <= r)
{
// Segment is fully within range
tree[node] = val;
if(start != end)
{
// Not leaf node
lazy[node*2] = val;
lazy[node*2+1] = val;
}
return;
}
int mid = (start + end) / 2;
updateRange(node*2, start, mid, l, r, val); // Updating left child
updateRange(node*2 + 1, mid + 1, end, l, r, val); // Updating right child
tree[node] = max(tree[node*2], tree[node*2+1]); // Updating root with max value
}
long queryRange(int node, int start, int end, int l, int r)
{
if(start > end || start > r || end < l)
return 0; // Out of range
if(lazy[node] != 0)
{
// This node needs to be updated
tree[node] = lazy[node]; // Update it
if(start != end)
{
lazy[node*2] = lazy[node]; // Mark child as lazy
lazy[node*2+1] = lazy[node]; // Mark child as lazy
}
lazy[node] = 0; // Reset it
}
if(start >= l && end <= r) // Current segment is totally within range [l, r]
return tree[node];
int mid = (start + end) / 2;
long p1 = queryRange(node*2, start, mid, l, r); // Query left child
long p2 = queryRange(node*2 + 1, mid + 1, end, l, r); // Query right child
return max(p1, p2);
}
void buildRange(int node, int low, int high){
if(low == high){
tree[node] = arr[low];
return;
}
int mid = (low + high) / 2;
int left = node << 1;
int right = left | 1;
buildRange(left, low, mid);
buildRange(right, mid + 1, high);
tree[node] = max(tree[left], tree[right]);
}
void printSegmentTree() {
System.out.println(Arrays.toString(tree));
}
}
private static class KMP{
private static char[] t;
private static char[] s;
public int kmp(char[]t, char[]s) {
this.t = t;
this.s = s;
return this.kmp();
}
private int kmp() {
List<Integer> prefixTable = getPrefixTable(s);
int match = 0;
int i = 0;
int j = 0;
int n = t.length;
int m = s.length;
while(i < n) {
if(t[i] == s[j]) {
if(j == m - 1) {
match++;
j = prefixTable.get(j - 1);
continue;
}
i++;
j++;
}
else if(j > 0) {
j = prefixTable.get(j - 1);
}
else {
i++;
}
}
return match;
}
/***
1. We compute the prefix values π[i] in a loop by iterating <br/>
from i=1 to i=n−1 (π[0] just gets assigned with 0). <br/><br/>
2. To calculate the current value π[i] we set the variable j <br/>
denoting the length of the best suffix for i−1. Initially j=π[i−1]. <br/>
3. Test if the suffix of length j+1 is also a prefix by <br/><br/>
comparing s[j] and s[i]. If they are equal then we assign π[i]=j+1, <br/>
otherwise we reduce j to π[j−1] and repeat this step. <br/><br/>
4. If we have reached the length j=0 and still don't have a match, <br/>
then we assign π[i]=0 and go to the next index i+1. <br/><br/>
@param
pattern(String)
***/
private List<Integer> getPrefixTable(char[] pattern){
List<Integer> prefixTable = new ArrayList<Integer>();
int n = pattern.length;
for(int i = 0; i < n; i++) {
prefixTable.add(0);
}
for(int i = 1; i < n; i++) {
for(int j = prefixTable.get(i - 1); j >= 0;) {
if(pattern[j] == pattern[i]) {
prefixTable.set(i, j + 1);
break;
}
else if(j > 0){
j = prefixTable.get(j - 1);
}
else {
break;
}
}
}
return prefixTable;
}
}
private static class Point {
int x;
int y;
Point(int x, int y){
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
Point ob = (Point) obj;
if(this.x == ob.x && this.y == ob.y){
return true;
}
return false;
}
@Override
public String toString() {
return this.x + " " + this.y;
}
@Override
public int hashCode() {
return 0;
}
}
private static long pow(int base, int pow) {
long val = 1L;
for(int i = 1; i <= pow; i++) {
val *= base;
}
return val;
}
private static int log(int x, int base) {
return (int) (Math.log(x) / Math.log(base));
}
private static int log(long x, int base) {
return (int) (Math.log(x) / Math.log(base));
}
private static long max(long a, long b) {
if (a >= b) {
return a;
}
return b;
}
private static long abs(long a) {
if (a < 0) {
return -a;
}
return a;
}
private static int abs(int a) {
if (a < 0) {
return -a;
}
return a;
}
private static int max(int a, int b) {
if (a >= b) {
return a;
}
return b;
}
private static int min(int a, int b) {
if (a <= b) {
return a;
}
return b;
}
private static long min(long a, long b) {
if (a <= b) {
return a;
}
return b;
}
private static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
private static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
private static long bigMod(long n, long k, long m) {
long ans = 1;
while (k > 0) {
if ((k & 1) == 1) {
ans = (ans * n) % m;
}
n = (n * n) % m;
k >>= 1;
}
return ans;
}
/*
* Returns an iterator pointing to the first element in the range [first,
* last] which does not compare less than val.
*
*/
private static int lowerBoundNew(long[] arr, long num) {
int start = 0;
int end = arr.length - 1;
int index = 0;
int len = arr.length;
int mid = 0;
while (true) {
if (start > end) {
break;
}
mid = (start + end) / 2;
if (arr[mid] > num) {
end = mid - 1;
} else if (arr[mid] < num) {
start = mid + 1;
} else {
while (mid >= 0 && arr[mid] == num) {
mid--;
}
return mid + 1;
}
}
if (arr[mid] < num) {
return mid + 1;
}
return mid;
}
/*
* upper_bound() is a standard library function in C++ defined in the header
* . It returns an iterator pointing to the first element in the range
* [first, last) that is greater than value, or last if no such element is
* found
*
*/
private static int upperBoundNew(long[] arr, long num) {
int start = 0;
int end = arr.length - 1;
int index = 0;
int len = arr.length;
int mid = 0;
while (true) {
if (start > end) {
break;
}
mid = (start + end) / 2;
if (arr[mid] > num) {
end = mid - 1;
} else if (arr[mid] < num) {
start = mid + 1;
} else {
while (mid < len && arr[mid] == num) {
mid++;
}
if (mid == len - 1 && arr[mid] == num) {
return mid + 1;
} else {
return mid;
}
}
}
if (arr[mid] < num) {
return mid + 1;
}
return mid;
}
private static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (IOException e) {
return null;
}
return tokenizer.nextToken();
}
public String nextLine() {
String line = null;
try {
tokenizer = null;
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public boolean hasNext() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
}
} | Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 9297a3c224c1b8a239ed807d67ba137c | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.lang.Math;
import java.util.Arrays;
import java.util.Comparator;
public class Main
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static int binarySearch(int a[] ,int k,int l,int h){
while(l<=h){
int mid = (l+h)/2;
if(a[mid]==k) return mid;
else if(a[mid]>k) h=mid-1;
else if(a[mid]<k) l=mid+1;
}
return -1;
}
static int binarySearch(ArrayList<Integer> a ,int k,int l,int h){
while(l<=h){
int mid = (l+h)/2;
if(a.get(mid)==k) return mid;
else if(a.get(mid)>k) h=mid-1;
else if(a.get(mid)<k) l=mid+1;
}
return -1;
}
static String reverse(String input)
{
char[] a = input.toCharArray();
int l, r = 0;
r = a.length - 1;
for (l = 0; l < r; l++, r--)
{
// Swap values of l and r
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
static int lcm(int a, int b)
{
return (a*b)/gcd(a, b);
}
static int solve(int A, int B)
{
int count = 0;
for (int i = 0; i < 21; i++) {
if (((A >> i) & 1) != ((B >> i) & 1)) {
count++;
}
}
return count;
}
static long nCr(int n, int r)
{
return fact(n) / (fact(r) *
fact(n - r));
}
static long fact(int n)
{
long res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
public static long count(long k) {
return k * (k - 1) / 2;
}
static boolean isPrime(int n) {
// if(n==1) return false;
if(n==2) return true;
if (n%2==0) return false;
int l = (int)Math.sqrt(n);
for(int i=3;i<=l;i+=2) {
if(n%i==0)
return false;
}
return true;
}
static int negMod(int n){
int a = (n % 1000000007 + 1000000007) % 1000000007;
return a;
}
public static int sum(long x) {
int sum = 0;
while (x > 0) {
sum += x % 10;
x /= 10;
}
return sum;
}
static ArrayList<Long> a = new ArrayList<>();
static void genLucky(long val , int f , int s){
if(val>10000000000l) return ;
if(f==s) a.add(val);
genLucky(val*10+4, f+1, s);
genLucky(val*10+7, f, s+1);
}
public static int max(int x, int y, int z)
{
return (int)Math.max(Math.max(x, y), z);
}
public static int min(int x, int y, int z)
{
return (int)Math.min(Math.min(x, y), z);
}
static int mod=1000003;
public static void main(String[] args) throws Exception
{
OutputStream outputStream = System.out;
PrintWriter w = new PrintWriter(outputStream);
FastReader sc = new FastReader();
// Scanner sc = new Scanner(new File("input.txt"));
// PrintWriter out = new PrintWriter(new File("output.txt"));
int i,j;
int t = sc.nextInt();
while(t>0){
int n = sc.nextInt();
String s = sc.nextLine();
HashMap<String,Integer> h = new HashMap<>();
h.put("0/0",0);
int x=0,y=0;
int l = -1,r=-1,fl=-1,fr=-1;
int len = Integer.MAX_VALUE;
int tlen=0;
String temp;
for(i=0;i<n;i++){
char c = s.charAt(i);
if(c=='L') x--;
else if(c=='R') x++;
else if(c=='U') y++;
else if(c=='D') y--;
temp = String.valueOf(x)+"/"+ String.valueOf(y);
if(h.containsKey(temp)){
l=h.get(temp);
r=i+1;
tlen=r-l;
if(tlen<len){
len=tlen;
fl=l+1;
fr=r;
}
}
h.put(temp,i+1);
}
if(fl==-1) w.println("-1");
else w.println(fl+" "+fr);
t--;
}
w.close();
}
}
// System.out.println();
| Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 30aa7e0927bb51fb266a4b9da7aff1a9 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".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.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class YetAnotherWalkingRobot { // Template for CF
public static class ListComparator implements Comparator<List<Integer>> {
@Override
public int compare(List<Integer> l1, List<Integer> l2) {
for (int i = 0; i < l1.size(); ++i) {
if (l1.get(i).compareTo(l2.get(i)) != 0) {
return l1.get(i).compareTo(l2.get(i));
}
}
return 0;
}
}
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int T = Integer.parseInt(f.readLine());
for (int i = 0; i < T; i++) {
int N = Integer.parseInt(f.readLine());
String str = f.readLine();
int l = -1;
int r = N;
Map<String, Integer> path = new HashMap<>();
// List<Integer> curr = new ArrayList<>();
// curr.add(0);
// curr.add(0);
// path.put(curr, 0);
int x = 0;
int y = 0;
path.put(x + " " + y, 0);
for (int j = 0; j < N; j++) {
if (str.charAt(j) == 'U') {
// curr.set(1, (curr.get(1) + 1));
y++;
} else if (str.charAt(j) == 'D') {
// curr.set(1, (curr.get(1) - 1));
y--;
} else if (str.charAt(j) == 'L') {
// curr.set(0, (curr.get(0) - 1));
x--;
} else if (str.charAt(j) == 'R') {
// curr.set(0, (curr.get(0) + 1));
x++;
}
if (path.containsKey(x + " " + y)) {
if (r - l > j - path.get(x + " " + y)) {
l = path.get(x + " " + y);
r = j;
}
}
path.put(x + " " + y, j + 1);
}
if (l == -1) {
out.println(-1);
} else {
out.println((l + 1) + " " + (r + 1));
}
}
out.close();
}
} | Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 115aff3ff869768f692da0995ed4d693 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".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.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class YetAnotherWalkingRobot { // Template for CF
public static class ListComparator implements Comparator<List<Integer>> {
@Override
public int compare(List<Integer> l1, List<Integer> l2) {
for (int i = 0; i < l1.size(); ++i) {
if (l1.get(i).compareTo(l2.get(i)) != 0) {
return l1.get(i).compareTo(l2.get(i));
}
}
return 0;
}
}
public static class Pair {
int first = 0;
int second = 0;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int T = Integer.parseInt(f.readLine());
for (int i = 0; i < T; i++) {
int N = Integer.parseInt(f.readLine());
String str = f.readLine();
int l = -1;
int r = N;
Map<List<Integer>, Integer> path = new HashMap<>();
// List<Integer> curr = new ArrayList<>();
// curr.add(0);
// curr.add(0);
// path.put(curr, 0);
int x = 0;
int y = 0;
List<Integer> first = new ArrayList<>();
first.add(0);
first.add(0);
path.put(first, 0);
for (int j = 0; j < N; j++) {
if (str.charAt(j) == 'U') {
// curr.set(1, (curr.get(1) + 1));
y++;
} else if (str.charAt(j) == 'D') {
// curr.set(1, (curr.get(1) - 1));
y--;
} else if (str.charAt(j) == 'L') {
// curr.set(0, (curr.get(0) - 1));
x--;
} else if (str.charAt(j) == 'R') {
// curr.set(0, (curr.get(0) + 1));
x++;
}
List<Integer> temp = new ArrayList<>();
temp.add(x);
temp.add(y);
if (path.containsKey(temp)) {
if (r - l > j - path.get(temp)) {
l = path.get(temp);
r = j;
}
}
path.put(temp, j + 1);
}
if (l == -1) {
out.println(-1);
} else {
out.println((l + 1) + " " + (r + 1));
}
}
out.close();
}
} | Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | a3ccf058bf726000cca7b44b5186640e | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".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.HashMap;
import java.util.Stack;
public class Main {
static int n;
static String s;
static class POINT {
char c;
int pos;
public POINT(char c, int pos) {
this.c = c;
this.pos = pos;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int t = 0; t < T; t++) {
n = Integer.parseInt(br.readLine());
s = br.readLine();
solve();
}
}
private static void solve() {
HashMap<String, Integer> map = new HashMap<>();
int x = 0, y = 0;
map.put(x + " " + y, 0);
int start = -1, end = -1;
int min = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'R')
x++;
else if (s.charAt(i) == 'L')
x--;
else if (s.charAt(i) == 'U')
y--;
else if (s.charAt(i) == 'D')
y++;
String key = x + " " + y;
if (map.containsKey(key)) {
int l = map.get(key) + 1;
int r = i + 1;
int len = r - l + 1;
if (len < min) {
start = l;
end = r;
min = len;
}
}
map.put(key, (i + 1));
}
if (start == -1 && end == -1) {
System.out.println("-1");
} else {
System.out.println(start + " " + end);
}
}
} | Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | d69244132846ed2bc4593b5dd08901dc | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.stream.Collectors;
public class Main {
static Long max = (long) (2 * 10E5);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static Long getHash(long x, long y) {
return x * max + y;
}
public static void main(String[] args) throws IOException {
int t = Integer.parseInt(br.readLine());
for (int _t = 0; _t < t; _t++) {
int n = Integer.parseInt(br.readLine());
char[] s = br.readLine().toCharArray();
long x = 0, y = 0;
int minDist = Integer.MAX_VALUE;
int l = 0, r = 0;
HashMap<Long, Integer> xySet = new HashMap<>();
xySet.put(0l, 0);
for (int i = 0; i < s.length; i++) {
if (s[i] == 'R')
x += 1;
if (s[i] == 'L')
x -= 1;
if (s[i] == 'U')
y += 1;
if (s[i] == 'D')
y -= 1;
long xyHash = getHash(x, y);
if (xySet.containsKey(xyHash)) {
int prevI = xySet.get(xyHash);
if (minDist > i - prevI) {
minDist = i - prevI;
l = prevI;
r = i + 1;
}
}
xySet.put(xyHash, i + 1);
}
if (minDist != Integer.MAX_VALUE) {
l++;
bw.write((l) + " " + (r) + "\n");
} else
bw.write(-1 + "\n");
}
bw.flush();
}
} | Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | a3282f0683f41540e968c8543e949c0a | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.text.*;
public class Prac{
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int ni() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nl() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nia(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public String rs() {
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 String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
public static class Key {
private final int x;
private final int y;
public Key(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Key)) return false;
Key key = (Key) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
static PrintWriter w = new PrintWriter(System.out);
static long mod=998244353L,mod1=1000000007;
public static void main (String[] args)throws IOException{
InputReader sc=new InputReader(System.in);
int t=sc.ni();
while(t-->0){
int n=sc.ni();
//sc.nextLine();
Map<Key,Integer> set=new HashMap<>();
set=new HashMap<>();
String s=sc.nextLine();
char arr[]=s.toCharArray();
set.put(new Key(0,0),0);
int x=0,y=0;
int a1=-1,a2=-1,min=Integer.MAX_VALUE;
boolean f=false;
for(int i=0;i<n;i++){
if(arr[i]=='L'){
x--;
}
else if(arr[i]=='R'){
x++;
}
else if(arr[i]=='U'){
y++;
}
else if(arr[i]=='D'){
y--;
}
Key k=new Key(x,y);
if(set.containsKey(k)){
int l=set.get(k)+1,r=i+1;
if((r-l)<min){
min=r-l;
a1=l;a2=r;
set.replace(k,i+1);
f=true;
}
set.replace(k,i+1);
}
else set.put(k,i+1);
}
if(!f)w.println(-1);
else w.println(a1+" "+a2);
}
w.close();
}
} | Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 4f20b886fc24d6c902a87c2de5d19eee | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static PrintWriter pw;
static int[] visit;
static int max;
static int[] arr;
static void dfs(int i, int c) {
visit[i-1]=c;
max=Math.max(c, max);
int lim=arr.length/i;
for(int j=2; j<=lim; j++){
if(visit[i*j-1]<c+1 && arr[i*j-1]>arr[i-1]){
dfs(i*j, c+1);
}
}
}
public static long gcf(long x, long y) {
while(x%y!=0 && y%x!=0) {
if(x>y)
x%=y;
else
y%=x;
}
return x>y? y:x;
}
public static void main(String[] args) throws Exception {
Scanner sc= new Scanner(System.in);
pw = new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
tuple[] arr=new tuple[n+1];
String s=sc.next();
arr[0]=new tuple(0,0,0);
for(int i=1; i<=n; i++){
arr[i]=new tuple(arr[i-1].x, arr[i-1].y, i);
switch(s.charAt(i-1)){
case 'L':arr[i].x--;break;
case 'R':arr[i].x++;break;
case 'U':arr[i].y++;break;
case 'D':arr[i].y--;break;
}
}
Arrays.sort(arr);
boolean b=false;
int ans=n;
for(int i=1; i<=n; i++){
if(arr[i].equal(arr[i-1])){
b=true;
ans=Math.min(ans, arr[i].z-arr[i-1].z);
}
}
if(b){
for(int i=1; i<=n; i++){
if(arr[i].equal(arr[i-1]) && arr[i].z-arr[i-1].z==ans){
pw.println(arr[i-1].z+1+" "+arr[i].z);
break;
}
}
}else{
pw.println(-1);
}
}
pw.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
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 {
return Double.parseDouble(next());
}
public int[] nextArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++)
arr[i] = nextInt();
return arr;
}
public Integer[] nextsort(int n) throws IOException{
Integer[] arr=new Integer[n];
for(int i=0; i<n; i++)
arr[i]=nextInt();
return arr;
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int x, int y) {
this.x=x;
this.y=y;
}
public int hashCode() {
return (this.x+""+this.y).hashCode();
}
public int compareTo(Pair other) {
if(this.x!=other.x)
return this.x-other.x;
else {
return this.y-other.y;
}
}
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
Pair p = (Pair) obj;
return this.x == p.x && this.y == p.y;
}
}
static class tuple implements Comparable<tuple>{
int x;int y;int z;
public tuple(int x, int y, int z){
this.x=x;
this.y=y;
this.z=z;
}
public int compareTo(tuple other) {
if(this.x!=other.x)
return this.x-other.x;
else {
if(this.y!=other.y)
return this.y-other.y;
else
return this.z-other.z;
}
}
public boolean equal(tuple t){
return this.x==t.x && this.y==t.y;
}
}
static class visit {
int x;
boolean y;
public visit(int x, boolean y){
this.x=x;
this.y=y;
}
}
} | Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 7fe3f4a64ebd4473ed0fc4f8f9e840a0 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class Cf182 implements Runnable
{
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt()
{
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return readString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception
{
new Thread(null, new Cf182(),"Main",1<<27).start();
}
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
public static long findGCD(long arr[], int n)
{
long result = arr[0];
for (int i = 1; i < n; i++)
result = gcd(arr[i], result);
return result;
}
static void sortbycolomn(long arr[][], int col)
{
Arrays.sort(arr, new Comparator<long[]>() {
@Override
public int compare(final long[] entry1,
final long[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
});
}
public void run()
{
InputReader in = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int t=in.nextInt();
while(t--!=0)
{
int n=in.nextInt();
String s=in.next();
Map<Key,Integer> map=new HashMap<Key,Integer>();
Key[] k1=new Key[n+1];
int x=0,y=0;
Key k2=new Key("0","0");
map.put(k2,0);
int L=0,R=0;
int dis=100000000;
int l=0,r=0;
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='L')
x=x-1;
else if(s.charAt(i)=='R')
x=x+1;
else if(s.charAt(i)=='U')
y=y+1;
else if(s.charAt(i)=='D')
y=y-1;
k1[i]=new Key(Integer.toString(x),Integer.toString(y));
if(map.containsKey(k1[i])==true)
{
L=map.get(k1[i])+1;
R=i+1;
if(dis>R-L+1)
{
l=L;
r=R;
dis=R-L+1;
//map.remove(k1[i]);
//map.put(k1[i],i);
if(dis==2)
break;
}
}
map.put(k1[i],i+1);
}
if(dis==100000000)
w.println("-1");
else
{
w.print(l+" ");
w.print(r);
w.println();
}
}
w.flush();
w.close();
}
}
class Key<K1, K2> {
public K1 key1;
public K2 key2;
public Key(K1 key1, K2 key2) {
this.key1 = key1;
this.key2 = key2;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) return false;
if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) return false;
return true;
}
@Override
public int hashCode() {
int result = key1 != null ? key1.hashCode() : 0;
result = 31 * result + (key2 != null ? key2.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "[" + key1 + ", " + key2 + "]";
}
} | Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 4ed8563b1203427c7a15094149e17515 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".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.*;
public class Main {
static int t;
static int n;
static String s;
public static void main(String[] args) {
PrintWriter pw = new PrintWriter(System.out);
Input(new FastReader(), pw);
pw.flush();
pw.close();
}
public static void Input(FastReader input, PrintWriter pw) {
t = input.nextInt();
while (t-- > 0) {
n = input.nextInt();
s = input.nextLine();
int x = 0, y = 0, min = Integer.MAX_VALUE;
int temp1 = -1, temp2 = -1;
HashMap<String, Integer> map = new HashMap<>();
map.put(x + " " + y, 0);
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L')
x--;
if (s.charAt(i) == 'R')
x++;
if (s.charAt(i) == 'U')
y++;
if (s.charAt(i) == 'D')
y--;
String str = x + " " + y;
if (map.containsKey(str)) {
int l = map.get(str) + 1;
int r = i + 1;
int len = r - l + 1;
if (len < min) {
temp1 = l;
temp2 = r;
min = len;
}
}
map.put(str, (i + 1));
}
if (temp1 != -1 && temp2!=0)
System.out.println(temp1 + " " + temp2);
else
System.out.println(-1);
}
}
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String string = "";
try {
string = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return string;
}
char nextChar() {
return next().charAt(0);
}
String[] nextStringArray() {
String[] str = null;
try {
str = this.br.readLine().trim().split(" ");
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] nextIntArray() {
String[] data = nextStringArray();
int[] a = new int[data.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(data[i]);
}
return a;
}
Integer[] nextIntegerArray() {
String[] data = nextStringArray();
Integer[] a = new Integer[data.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(data[i]);
}
return a;
}
long[] nextLongArray() {
String[] data = nextStringArray();
long[] a = new long[data.length];
for (int i = 0; i < a.length; i++) {
a[i] = Long.parseLong(data[i]);
}
return a;
}
public boolean hasNext() throws IOException {
if (st != null && st.hasMoreTokens()) {
return true;
}
String s = br.readLine();
if (s == null || s.isEmpty()) {
return false;
}
st = new StringTokenizer(s);
return true;
}
}
}
| Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 1645f58c7877a7592e5d2c1f076fa8a5 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes |
import javafx.util.Pair;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main {
static int t;
static int n;
static String s;
public static void main(String[] args) {
PrintWriter pw = new PrintWriter(System.out);
Input(new FastReader(), pw);
pw.flush();
pw.close();
}
public static void Input(FastReader input, PrintWriter pw) {
t = input.nextInt();
while (t-- > 0) {
n = input.nextInt();
s = input.nextLine();
int x = 0, y = 0, min = Integer.MAX_VALUE;
int temp1 = -1, temp2 = -1;
HashMap<Pair<Integer,Integer>, Integer> map = new HashMap<>();
map.put(new Pair<>(0,0), 0);
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L')
x--;
if (s.charAt(i) == 'R')
x++;
if (s.charAt(i) == 'U')
y++;
if (s.charAt(i) == 'D')
y--;
Pair<Integer,Integer>p=new Pair<>(x,y);
if (map.containsKey(p)) {
int l = map.get(p) + 1;
int r = i + 1;
int len = r - l + 1;
if (len < min) {
temp1 = l;
temp2 = r;
min = len;
}
}
map.put(p, (i + 1));
}
if (temp1 != -1 && temp2!=0)
pw.println(temp1 + " " + temp2);
else
pw.println(-1);
}
}
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String string = "";
try {
string = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return string;
}
char nextChar() {
return next().charAt(0);
}
String[] nextStringArray() {
String[] str = null;
try {
str = this.br.readLine().trim().split(" ");
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] nextIntArray() {
String[] data = nextStringArray();
int[] a = new int[data.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(data[i]);
}
return a;
}
Integer[] nextIntegerArray() {
String[] data = nextStringArray();
Integer[] a = new Integer[data.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(data[i]);
}
return a;
}
long[] nextLongArray() {
String[] data = nextStringArray();
long[] a = new long[data.length];
for (int i = 0; i < a.length; i++) {
a[i] = Long.parseLong(data[i]);
}
return a;
}
public boolean hasNext() throws IOException {
if (st != null && st.hasMoreTokens()) {
return true;
}
String s = br.readLine();
if (s == null || s.isEmpty()) {
return false;
}
st = new StringTokenizer(s);
return true;
}
}
}
| Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | b412fcf79f9b7bc806079dc7664d20d3 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes |
import javafx.util.Pair;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main {
static int t;
static int n;
static String s;
public static void main(String[] args) {
PrintWriter pw = new PrintWriter(System.out);
Input(new FastReader(), pw);
pw.flush();
pw.close();
}
public static void Input(FastReader input, PrintWriter pw) {
t = input.nextInt();
while (t-- > 0) {
n = input.nextInt();
s = input.nextLine();
int x = 0, y = 0, min = Integer.MAX_VALUE;
int temp1 = -1, temp2 = -1;
Map<Pair<Integer,Integer>, Integer> map = new HashMap<>();
map.put(new Pair<>(0,0), 0);
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'L')
x--;
if (s.charAt(i) == 'R')
x++;
if (s.charAt(i) == 'U')
y++;
if (s.charAt(i) == 'D')
y--;
Pair<Integer,Integer>p=new Pair<>(x,y);
if (map.containsKey(p)) {
int l = map.get(p) + 1;
int r = i + 1;
int len = r - l + 1;
if (len < min) {
temp1 = l;
temp2 = r;
min = len;
}
}
map.put(p, (i + 1));
}
if (temp1 != -1 && temp2!=-1)
pw.println(temp1 + " " + temp2);
else
pw.println(-1);
}
}
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String string = "";
try {
string = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return string;
}
char nextChar() {
return next().charAt(0);
}
String[] nextStringArray() {
String[] str = null;
try {
str = this.br.readLine().trim().split(" ");
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] nextIntArray() {
String[] data = nextStringArray();
int[] a = new int[data.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(data[i]);
}
return a;
}
Integer[] nextIntegerArray() {
String[] data = nextStringArray();
Integer[] a = new Integer[data.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(data[i]);
}
return a;
}
long[] nextLongArray() {
String[] data = nextStringArray();
long[] a = new long[data.length];
for (int i = 0; i < a.length; i++) {
a[i] = Long.parseLong(data[i]);
}
return a;
}
public boolean hasNext() throws IOException {
if (st != null && st.hasMoreTokens()) {
return true;
}
String s = br.readLine();
if (s == null || s.isEmpty()) {
return false;
}
st = new StringTokenizer(s);
return true;
}
}
}
| Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 8482546ad6594d8b91a5e09eeab5b953 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".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.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class q3 {
public static void main(String[] args) {
FastReader s = new FastReader();
int t = s.nextInt();
while(t-- > 0)
{
int n = s.nextInt();
String inp = s.next();
int ans = Integer.MAX_VALUE;
HashMap<String,Integer> map = new HashMap<>();
Set<String> set = new HashSet<>();
int x = 0;
int y = 0;
int ansl = -1;
int ansr = -1;
set.add(0 + " " +0);
map.put(0 + " " + 0, -1);
for(int i=0;i<n;++i)
{
if(inp.charAt(i) == 'L')
x--;
else if(inp.charAt(i) == 'R')
x++;
else if(inp.charAt(i) == 'U')
y++;
else y--;
if(set.contains(x + " " + y))
{
int index = map.get(x + " " + y);
if(ans > i - index + 1)
{
ans = i - index + 1;
ansl = index+2;
ansr = i + 1;
}
}
set.add(x + " " + y);
map.put(x + " " + y, i);
}
if(ans == Integer.MAX_VALUE)
System.out.println(-1);
else System.out.println(ansl + " " + ansr);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 2b6cfd83d49bea27ae290cc481b2cd34 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes |
import java.util.*;
import javafx.util.Pair;
public class YetAnotherWalkingRobot {
static class Node {
int x;
int y;
int index;
public Node(int x, int y) {
this.x = x;
this.y = y;
// this.index=index;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x * 7 + (y * 3+5*(y-x) );
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Node other = (Node) obj;
if (x != other.x && y != other.y) {
return false;
}
return true;
}
}
/*static class Node_R {
int x;
int y;
int index;
public Node_R(int x,int y,int index ) {
this.x=x;
this.y=y;
this.index=index;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result +index;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node other = (Node) obj;
if (x!=other.x&&y!=other.y)
return false;
return true;
}
public int getR(){
return
}
}*/
public class Account {
private int accountNumber;
private String holderName;
public Account(int accountNumber) {
this.accountNumber = accountNumber;
}
public String getHolderName() {
return holderName;
}
public void setHolderName(String holderName) {
this.holderName = holderName;
}
public int getAccountNumber() {
return accountNumber;
}
//Depends only on account number
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + accountNumber;
return result;
}
//Compare only account numbers
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Account other = (Account) obj;
if (accountNumber != other.accountNumber) {
return false;
}
return true;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int test = scan.nextInt();
while (test-- != 0) {
int n = scan.nextInt();
String s = scan.next();
HashMap<Node, Integer> map = new HashMap<>();
map.put(new Node(0, 0), 0);
int x = 0;
int y = 0;
int index = -1;
int min_l = -1;
int min_r = -1;
int len = Integer.MAX_VALUE;
for (int i = 1; i <= n; i++) {
char ch = s.charAt(i - 1);
if (ch == 'L') {
y = y - 1;
}
if (ch == 'U') {
x = x + 1;
}
if (ch == 'D') {
x = x - 1;
}
if (ch == 'R') {
y = y + 1;
}
index = i;
Node check_pre = new Node(x, y);
if (map.containsKey(check_pre)) {
int pre_index = map.get(check_pre);
int temp = Math.abs(i - pre_index + 1);
if (temp < len) {
len = temp;
min_l = pre_index;
min_r = i;
}
}
map.put(new Node(x, y), index);
if (len == 2) {
break;
}
}
if (len == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println((min_l + 1) + " " + min_r);
}
// map.put(new Node(1,1,1), 1);
//System.out.println(map.get(new Node(1,1,1)));
}
}
}
| Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 4f46ab99a6e373c0b568bebcb94074fe | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes |
import java.util.*;
import javafx.util.Pair;
public class YetAnotherWalkingRobot {
static class Node {
int x;
int y;
int index;
public Node(int x, int y) {
this.x = x;
this.y = y;
// this.index=index;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x * 7 + (y * 3+5*(y-x) );
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Node other = (Node) obj;
if (x != other.x && y != other.y) {
return false;
}
return true;
}
}
/*static class Node_R {
int x;
int y;
int index;
public Node_R(int x,int y,int index ) {
this.x=x;
this.y=y;
this.index=index;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result +index;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node other = (Node) obj;
if (x!=other.x&&y!=other.y)
return false;
return true;
}
public int getR(){
return
}
}*/
public class Account {
private int accountNumber;
private String holderName;
public Account(int accountNumber) {
this.accountNumber = accountNumber;
}
public String getHolderName() {
return holderName;
}
public void setHolderName(String holderName) {
this.holderName = holderName;
}
public int getAccountNumber() {
return accountNumber;
}
//Depends only on account number
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + accountNumber;
return result;
}
//Compare only account numbers
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Account other = (Account) obj;
if (accountNumber != other.accountNumber) {
return false;
}
return true;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int test = scan.nextInt();
while (test-- != 0) {
int n = scan.nextInt();
String s = scan.next();
HashMap<Pair, Integer> map = new HashMap<>();
map.put(new Pair(0, 0), 0);
int x = 0;
int y = 0;
int index = -1;
int min_l = -1;
int min_r = -1;
int len = Integer.MAX_VALUE;
for (int i = 1; i <= n; i++) {
char ch = s.charAt(i - 1);
if (ch == 'L') {
y = y - 1;
}
if (ch == 'U') {
x = x + 1;
}
if (ch == 'D') {
x = x - 1;
}
if (ch == 'R') {
y = y + 1;
}
index = i;
Pair check_pre = new Pair(x, y);
if (map.containsKey(check_pre)) {
int pre_index = map.get(check_pre);
int temp = Math.abs(i - pre_index + 1);
if (temp < len) {
len = temp;
min_l = pre_index;
min_r = i;
}
}
map.put(new Pair(x, y), index);
if (len == 2) {
break;
}
}
if (len == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println((min_l + 1) + " " + min_r);
}
// map.put(new Node(1,1,1), 1);
//System.out.println(map.get(new Node(1,1,1)));
}
}
}
| Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 516cb7b005a300cae9ae35c9658543b7 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int j=0;j<t;j++)
{
int x=0,y=0,flag=0,len=0,w=0,r=0,min=Integer.MAX_VALUE;
int n=sc.nextInt();
String s=sc.next();
Pair arr[]=new Pair[n+1];
for(int i=0;i<n+1;i++)
arr[i]=new Pair(0,0);
HashMap<Pair, Integer>hm=new HashMap<>();
hm.put(arr[0],0);
for(int i=0;i<n;i++)
{
if(s.charAt(i)=='L')
{
x--;
}
else if(s.charAt(i)=='R'){
x++;
}
else if(s.charAt(i)=='U'){
y++;
}
else if(s.charAt(i)=='D'){
y--;
}
arr[i+1]=new Pair(x,y);
//System.out.println("ee"+arr[i+1].x+" "+arr[i+1].y+" "+hm.containsKey(arr[i+1]));
if(hm.containsKey(arr[i+1]))
{
flag=1;
len=i-hm.get(arr[i+1]);
if(len==1)
{
w=hm.get(arr[i+1])+1;
r=(i+1);
break;
}
min=Math.min(min,len);
if(min==len)
{
w=hm.get(arr[i+1])+1;
r=(i+1);
}
}
hm.put(arr[i+1],i+1);
}
/*hm.forEach((k,v) -> System.out.println("Key = "
+ k.x+" "+k.y+" " + ", Value = " + v)); */
if(flag==0)
System.out.println("-1");
else
System.out.println(w+" "+r);
}
}
}
class Pair{
int x;
int y;
public Pair(int x,int y){
this.x=x;
this.y=y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair pair = (Pair) o;
return x == pair.x && y == pair.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
} | Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 69422302b889807e432695bb3146524c | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes |
import java.util.HashMap;
import java.util.Scanner;
public class Robot {
public static void main(String[] args) {
int tc;
int n;
String str;
Scanner input = new Scanner(System.in);
tc = input.nextInt();
for(int i=0; i<tc; i++) {
n = input.nextInt();
input.nextLine();
str = input.nextLine();
//long start = System.currentTimeMillis();
HashMap<String, Integer> hm = new HashMap<String, Integer>();
hm.put("0-0",0);
int x=0;
int y=0;
int j;
int si = 0;
int ei = 0;
int len = Integer.MAX_VALUE;
for(j=0; j<n; j++) {
if(str.charAt(j) == 'L') {
x--;
String key = x + "-" + y;
if(hm.containsKey(key)) {
//System.out.println("Match");
if((j-hm.get(key)) < len) {
len = j-hm.get(key);
si = hm.get(key)+1;
ei = j+1;
}
hm.remove(key);
}
hm.put(key, j+1);
}
if(str.charAt(j) == 'R') {
x++;
String key = x + "-" + y;
if(hm.containsKey(key)) {
//System.out.println("Match");
if((j-hm.get(key)) < len) {
len = j-hm.get(key);
si = hm.get(key)+1;
ei = j+1;
}
hm.remove(key);
}
hm.put(key, j+1);
}
if(str.charAt(j) == 'U') {
y++;
String key = x + "-" + y;
if(hm.containsKey(key)) {
//System.out.println("Match");
if((j-hm.get(key)) < len) {
len = j-hm.get(key);
si = hm.get(key)+1;
ei = j+1;
}
hm.remove(key);
}
hm.put(key, j+1);
}
if(str.charAt(j) == 'D') {
y--;
String key = x + "-" + y;
if(hm.containsKey(key)) {
//System.out.println("Match");
if((j-hm.get(key)) < len) {
len = j-hm.get(key);
si = hm.get(key)+1;
ei = j+1;
}
hm.remove(key);
}
hm.put(key, j+1);
}
//System.out.println("Temp at j:"+j+" is :[" +x+","+y+"]");
}
if(len == Integer.MAX_VALUE)
System.out.println("-1");
else {
System.out.println(si+" "+ei);
}
//long end = System.currentTimeMillis();
//System.out.println((end - start) + " ms");
}
}
} | Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 9d1199ea73829aff72f028b0d70c2d88 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public final class C1296 {
// private static class Pair {
// int x;
// int y;
// Pair(int x, int y) {
// this.x = x;
// this.y = y;
// }
// @Override
// public int hashCode() {
// return x * 1000000 + y;
// }
// @Override
// public boolean equals(Object p) {
// Pair x = (Pair) p;
// return this.hashCode() == x.hashCode();
// }
// }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
String s = in.next();
Map<String, Integer> map = new HashMap<>();
map.put("0 0", 0);
int x = 0;
int y = 0;
int l = -1, r = -1, ans = Integer.MAX_VALUE;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'L') x--;
if (s.charAt(i) == 'R') x++;
if (s.charAt(i) == 'D') y--;
if (s.charAt(i) == 'U') y++;
String xx = x + " " + y;
Integer last = map.get(xx);
if (last != null && (i - last) < ans) {
l = last;
r = i + 1;
ans = i - last;
}
map.put(xx, i + 1);
}
if (ans == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println((l + 1) + " " + r);
}
}
}
}
| Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 4ee68feb7ab597177509ceada14c6e65 | train_001.jsonl | 1580826900 | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'.Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$.This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$).Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public final class C1296 {
private static class Pair {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
return x * 67 + y * 67 * 67;
}
@Override
public boolean equals(Object p) {
if (p == null) return false;
Pair pp = (Pair) p;
return this.x == pp.x && this.y == pp.y;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
String s = in.next();
Map<Pair, Integer> map = new HashMap<>();
map.put(new Pair(0, 0), 0);
int x = 0;
int y = 0;
int l = -1, r = -1, ans = Integer.MAX_VALUE;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'L') x--;
if (s.charAt(i) == 'R') x++;
if (s.charAt(i) == 'D') y--;
if (s.charAt(i) == 'U') y++;
Integer last = map.get(new Pair(x, y));
if (last != null && (i - last) < ans) {
l = last;
r = i + 1;
ans = i - last;
}
map.put(new Pair(x, y), i + 1);
}
if (ans == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println((l + 1) + " " + r);
}
}
}
}
| Java | ["4\n4\nLRUD\n4\nLURD\n5\nRRUDU\n5\nLLDDR"] | 1 second | ["1 2\n1 4\n3 4\n-1"] | null | Java 8 | standard input | [
"data structures",
"implementation"
] | 1fc1d5ee79aaf823b246db5471ba7836 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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,500 | For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 \le l \le r \le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | standard output | |
PASSED | 226ee039fa6f0b3004b8226fdb04b805 | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | import java.util.*;
import java.io.*;
public final class TestClass {
private static Set<Long> getPrimeSquares(){
Set<Long> primeSquares = new HashSet<>();
int n = 1000000;
boolean[] compBool = new boolean[n];
for (int i = 2; i < n; i++){
if (compBool[i]) continue;
Long square = ((long)i)*((long)i);
primeSquares.add(square);
for (int j = 2*i; j < n; j += i) compBool[j] = true;
}
return primeSquares;}
public static void main(String args[] ) throws Exception {
/* Sample code to perform I/O:
* Use either of these methods for input
//BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine(); // Reading input from STDIN
System.out.println("Hi, " + name + "."); // Writing output to STDOUT
//Scanner
Scanner s = new Scanner(System.in);
String name = s.nextLine(); // Reading input from STDIN
System.out.println("Hi, " + name + "."); // Writing output to STDOUT
*/
// Write your code here
Set<Long> primeSquares = getPrimeSquares();
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
String inp[] = br.readLine().split(" ");
for (int i = 0; i < n; i++){
long x = Long.parseLong(inp[i]);
if (primeSquares.contains(x)) bw.write("YES");
else bw.write("NO");
bw.newLine();
}
br.close();
bw.close();
}
catch (Exception e){
}
}
} | Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | 4529d8e3ab1bbb7ff1a3282ce983256e | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
public static boolean isPrime(long x)
{
if(x<2) return false;
else if(x==2) return true;
else if(x%2==0) return false;
int n=(int)Math.sqrt(x);
for(int i=3;i<=n;i=i+2)
if(x%i==0)
return false;
return true;
}
public static void main(String ardf[]) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
String s[]=br.readLine().split(" ");
for(int i=0;i<s.length;i++){
long x=Long.parseLong(s[i]);
long nu=(long)Math.sqrt(x);
if(nu*nu==x && isPrime(nu))
bw.write("YES\n");
else bw.write("NO\n");
}
bw.flush();
}
} | Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | f7f05650acf57f46c4122d22d3d57410 | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | import java.math.BigInteger;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
Scanner insert = new Scanner(System.in);
int n = insert.nextInt();
int[] a=new int[10000001];
for(int i=2;i<=1000000;i++)
{
a[i]=1;
}
for(int i=2;i<=1000;i++)
{
for(int j=2*i;j<=1000000;j+=i)
{
a[j]=0;
}
}
for(int i=0;i<n;i++)
{
long b=insert.nextLong();
double x=Math.sqrt(b);
if(x==(int)x && a[(int)x]==1)
System.out.println("YES");
else
System.out.println("NO");
}
}
}
| Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | 2d6f41ee1f6cb1e2b0cf2fa65b9891a4 | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | import com.sun.source.tree.Tree;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;
import java.util.regex.Pattern;
public class Example {
public static int Max(int[] p)
{
int Max=0;
int j=0;
for(int i=0;i<p.length;i++)
{
if(p[i]>Max)
{
Max = p[i];
j=i;
}
}
return j;
}
public static int Max(int[] a, int l)
{
int max=0;
for(int i=0;i<a.length-1;i++)
{
if(a[i+1]-a[i]>max)max=a[i+1]-a[i];
}
if(a[0]*2>max) max=a[0]*2;
if((l-a[a.length-1])*2>max)max=(l-a[a.length-1])*2;
return max;
}
public static int[] Sort(int[] a)
{
for(int i=0;i<a.length-1;i++)
{
for(int j=i+1;j<a.length;j++)
{
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
return a;
}
public static boolean Permutation(int[] p)
{
boolean[] temp= new boolean[p.length];
Arrays.fill(temp, false);
for(int i=0;i<p.length;i++)
{
for(int j=0;j<p.length;j++)
{
if(p[i]==j)
{
temp[j]=true;
break;
}
}
}
for(int j=0;j<p.length;j++)
{
if(!temp[j]) return false;
}
return true;
}
public static long Count(long n)
{
long count=0;
if (n>=100)
{
count=count+n/100;
n=n -(n/100)*100;
}
if(n>=20)
{
count=count+n/20;
n=n-(n/20)*20;
}
if(n>=10)
{
count=count+n/10;
n=n-(n/10)*10;
}
if(n>=5)
{
count=count+n/5;
n=n-(n/5)*5;
}
count=count+n;
return count;
}
public static int Min(int a, int b, int c)
{
if(a<=b&&a<=c) return a;
else if(b<=a&&b<=c) return b;
else return c;
}
public static int Ribbon(int[] x,int n)
{
int max=0;
for(int i=n/x[0];i>=0;i--)
{
for (int j=0;j<=n/x[1];j++)
{
int k=n-(i*x[0] +j*x[1]);
if(k%x[2]==0&&k>=0)
{
int temp=k/x[2];
if ((i+j+temp)>max)max=i+j+temp;
}
}
}
return max;
}
public static boolean Prime(long n)
{
if(n==1) return false;
if(n==2||n==3) return true;
for(int i=2;i<=Math.sqrt((double) n);i++)
{
if(n%i==0) return false;
}
return true;
}
public static void main(String[] args) {
int n;
Scanner sc= new Scanner(System.in);
Set<Long> a = new HashSet<>();
boolean[] check= new boolean[1000000+1];
for(int i=2;i<=1000000;i++)
{
check[i]=true;
}
for(int i=2;i<=1000000;i++) {
if (check[i]) {
for (int j = 2 * i; j <= 1000000; j += i) {
check[j]=false;
}
}
}
for (int i=2;i<=1000000;i++)
{
if(check[i]) a.add((long)i*i);
}
n=sc.nextInt();
long[] x= new long[n];
for(int i=0;i<n;i++)
{
x[i]=sc.nextLong();
if(a.contains(x[i])) System.out.println("YES");
else System.out.println("NO");
}
}
}
| Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | 14e19b2789e38e1e3d945aca074a2e9e | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | import java.util.*;
import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
public class Nfactor {
public static boolean[] countPrimes(int n) {
boolean[] k = new boolean[n + 1];
if (n == 0) return k;
if (n == 1 || n == 2 ) {k[2] = false;return k;}
if (n == 3) return k;
if (n == 4) return k;
k[0] = true;
k[1] = true; int ans = n - 1;
for (int i = 2; i*i < n; i ++){
if (k[i] == false){
for (int j = i*i; j < n; j += i){
if (j % i == 0 && k[j] == false) {
ans -= 1;
k[j] = true;
}
}
}
}
return k;
}
public static void main(String args[]) throws IOException{
Reader.init(System.in);
boolean[] k = countPrimes(1000000 + 1);
// int q = list.size();
int test = Reader.nextInt();
for (int u = 0; u < test; u ++) {
long a = Reader.nextLong();
int t = (int) Math.pow(a, 0.5);
if ((long) t*t == a) {
if (k[(int) t] == false) {System.out.println("YES");}
else {
System.out.println("NO");
}
}
else {
System.out.println("NO");
}
}
}
}
/** Class for buffered reading int and double values */
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException
{
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
| Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | ed0b89840126065c1bd5459b816b6575 | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader inb = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(inb.readLine());
int a = 1000007;
boolean[] prime = new boolean[a];
int c = (int) Math.sqrt(a) + 2;
prime[0] = true;
prime[1] = true;
for (int i = 2; i <= c; i++) {
if (!prime[i]) {
for (int j = i * i; j < a; j += i) {
prime[j] = true;
}
}
}
String[] str = inb.readLine().split(" ");
for (int i = 0; i < n; i++) {
long k = Long.parseLong(str[i]);
int sqr = (int) Math.sqrt(k);
if ((long) sqr * (long) sqr == k && !prime[sqr]) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}
| Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | 74e169b05286018085fe21d934d8b4cd | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a = 1000007;
boolean[] prime = new boolean[a];
int c = (int) Math.sqrt(a) + 2;
prime[0]=true;
prime[1]=true;
for (int i = 2; i <= c; i++) {
if (!prime[i]) {
for (int j = i * i; j < a; j+=i) {
prime[j] = true;
}
}
}
for (int i = 0; i < n; i++) {
long k = in.nextLong();
int sqr = (int) Math.sqrt(k);
if ((long) sqr * (long) sqr == k && !prime[sqr]) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
} | Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | 295d9087413094909509205b4c83d855 | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | import java.util.*;
public class MyClass
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long a[]=new long[n];
long var;
for(int i=0;i<n;i++)
{
a[i]=sc.nextLong();
}
long[] prime = new long[10000001];
prime[0]=1;
prime[1]=1;
for(long i=2;i*i<=10000000;i++)
{
if(prime[(int) i]==0)
{
for(long j=i;i*j<=1000000;j++)
{
prime[(int) (i*j)]=1;
}
}
}
for(long i=0;i<n;i++)
{
var = (long) Math.sqrt(a[(int) i]);
if(var*var==a[(int) i])
{
if(prime[(int) var]==0)
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
else
{
System.out.println("NO");
}
}
}
} | Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | e57b4f1422e1fd26b372bf0f8abdd76d | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class test {
public static void main(String[] args) {
int test = fs.nextInt();
// int test = 1;
for (int cases = 0; cases < test; cases++) {
long n = fs.nextLong();
double sq = Math.sqrt(n);
if (Math.floor(sq) == Math.ceil(sq)) {
int s = (int) sq;
if (isPrime(s)) {
op.print("YES" + "\n");
} else {
op.print("NO" + "\n");
}
} else {
op.print("NO" + "\n");
}
op.flush();
}
}
static boolean isPrime(int n) {
int skip = 0;
if (n < 2)
return false;
else if (n == 2)
return true;
int limit = (int) Math.sqrt(n);
if (n % 2 == 0)
return false;
for (int j = 3; j <= limit; j += 2) {
if (n % j == 0)
return false;
}
return true;
}
static int countDifferentBits(int a, int b) {
int count = 0;
for (int i = 0; i < 32; i++) {
if (((a >> i) & 1) != ((b >> i) & 1)) {
++count;
}
}
return count;
}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sortMyMapusingValues(HashMap<String, Integer> hm) {
List<Map.Entry<String, Integer>> capitalList = new LinkedList<>(hm.entrySet());
Collections.sort(capitalList, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
HashMap<String, Integer> result = new HashMap<>();
for (Map.Entry<String, Integer> entry : capitalList) {
result.put(entry.getKey(), entry.getValue());
}
}
static boolean ispowerof2(long num) {
if ((num & (num - 1)) == 0)
return true;
return false;
}
static void primeFactors(int n) {
while (n % 2 == 0) {
System.out.print(2 + " ");
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
System.out.print(i + " ");
n /= i;
}
}
if (n > 2)
System.out.print(n);
}
// static boolean isPrime(long n) {
// // Corner cases
// if (n <= 1)
// return false;
// if (n <= 3)
// return true;
// if (n % 2 == 0 || n % 3 == 0)
// return false;
//
// for (int i = 5; i * i <= n; i = i + 6)
// if (n % i == 0 || n % (i + 2) == 0)
// return false;
//
// return true;
// }
static class Graph {
HashMap<Integer, LinkedList<Integer>> hm = new HashMap<Integer, LinkedList<Integer>>();
private void addVertex(int vertex) {
hm.put(vertex, new LinkedList<>());
}
private void addEdge(int source, int dest, boolean bi) {
if (!hm.containsKey(source))
addVertex(source);
if (!hm.containsKey(dest))
addVertex(dest);
hm.get(source).add(dest);
if (bi) {
hm.get(dest).add(source);
}
}
private boolean uniCycle(int i, HashSet<Integer> visited, int parent) {
visited.add(i);
LinkedList<Integer> list = hm.get(i);
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
Integer integer = (Integer) it.next();
if (!visited.contains(integer)) {
if (uniCycle(integer, visited, i))
return true;
} else if (integer != parent) {
return true;
}
}
return false;
}
private boolean uniCyclic() {
HashSet<Integer> visited = new HashSet<Integer>();
Set<Integer> set = hm.keySet();
for (Integer integer : set) {
if (!visited.contains(integer)) {
if (uniCycle(integer, visited, -1)) {
return true;
}
}
}
return false;
}
private boolean isbiCycle(int i, HashSet<Integer> visited, HashSet<Integer> countered) {
if (countered.contains(i))
return true;
if (visited.contains(i))
return false;
visited.add(i);
countered.add(i);
LinkedList<Integer> list = hm.get(i);
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
Integer integer = (Integer) it.next();
if (isbiCycle(integer, visited, countered)) {
return true;
}
}
countered.remove(i);
return false;
}
private boolean isbiCyclic() {
HashSet<Integer> visited = new HashSet<Integer>();
HashSet<Integer> countered = new HashSet<Integer>();
Set<Integer> set = hm.keySet();
for (Integer integer : set) {
if (isbiCycle(integer, visited, countered)) {
return true;
}
}
return false;
}
}
static class Node {
Node left, right;
int data;
public Node(int data) {
this.data = data;
}
public void insert(int val) {
if (val <= data) {
if (left == null) {
left = new Node(val);
} else {
left.insert(val);
}
} else {
if (right == null) {
right = new Node(val);
} else {
right.insert(val);
}
}
}
public boolean contains(int val) {
if (data == val) {
return true;
} else if (val < data) {
if (left == null) {
return false;
} else {
return left.contains(val);
}
} else {
if (right == null) {
return false;
} else {
return right.contains(val);
}
}
}
public void inorder() {
if (left != null) {
left.inorder();
}
System.out.print(data + " ");
if (right != null) {
right.inorder();
}
}
public int maxDepth() {
if (left == null)
return 0;
if (right == null)
return 0;
else {
int ll = left.maxDepth();
int rr = right.maxDepth();
if (ll > rr)
return (ll + 1);
else
return (rr + 1);
}
}
public int countNodes() {
if (left == null)
return 1;
if (right == null)
return 1;
else {
return left.countNodes() + right.countNodes() + 1;
}
}
public void preorder() {
System.out.print(data + " ");
if (left != null) {
left.inorder();
}
if (right != null) {
right.inorder();
}
}
public void postorder() {
if (left != null) {
left.inorder();
}
if (right != null) {
right.inorder();
}
System.out.print(data + " ");
}
public void levelorder(Node node) {
LinkedList<Node> ll = new LinkedList<Node>();
ll.add(node);
getorder(ll);
}
public void getorder(LinkedList<Node> ll) {
while (!ll.isEmpty()) {
Node node = ll.poll();
System.out.print(node.data + " ");
if (node.left != null)
ll.add(node.left);
if (node.right != null)
ll.add(node.right);
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Pair {
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
private static final Scanner sc = new Scanner(System.in);
private static final FastReader fs = new FastReader();
private static final OutputWriter op = new OutputWriter(System.out);
static int[] getintarray(int n) {
int ar[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = fs.nextInt();
}
return ar;
}
static long[] getlongarray(int n) {
long ar[] = new long[n];
for (int i = 0; i < n; i++) {
ar[i] = fs.nextLong();
}
return ar;
}
static int[][] get2darray(int n, int m) {
int ar[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ar[i][j] = fs.nextInt();
}
}
return ar;
}
static Pair[] getpairarray(int n) {
Pair ar[] = new Pair[n];
for (int i = 0; i < n; i++) {
ar[i] = new Pair(fs.nextInt(), fs.nextInt());
}
return ar;
}
static void printarray(int ar[]) {
for (int i : ar) {
op.print(i + " ");
}
op.flush();
}
static int fact(int n) {
int fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
//
// function to find largest prime factor
}
| Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | 6e92d6f7c7ee1531d47e8fbc6c2fa5ea | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class test {
public static void main(String[] args) {
int test = fs.nextInt();
// int test = 1;
for (int cases = 0; cases < test; cases++) {
long n = fs.nextLong();
double sq = Math.sqrt(n);
if (Math.floor(sq) == Math.ceil(sq)) {
long s = (int) sq;
if (isPrime(s)) {
op.print("YES" + "\n");
} else {
op.print("NO" + "\n");
}
} else {
op.print("NO" + "\n");
}
op.flush();
}
}
static int countDifferentBits(int a, int b) {
int count = 0;
for (int i = 0; i < 32; i++) {
if (((a >> i) & 1) != ((b >> i) & 1)) {
++count;
}
}
return count;
}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sortMyMapusingValues(HashMap<String, Integer> hm) {
List<Map.Entry<String, Integer>> capitalList = new LinkedList<>(hm.entrySet());
Collections.sort(capitalList, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
HashMap<String, Integer> result = new HashMap<>();
for (Map.Entry<String, Integer> entry : capitalList) {
result.put(entry.getKey(), entry.getValue());
}
}
static boolean ispowerof2(long num) {
if ((num & (num - 1)) == 0)
return true;
return false;
}
static void primeFactors(int n) {
while (n % 2 == 0) {
System.out.print(2 + " ");
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
System.out.print(i + " ");
n /= i;
}
}
if (n > 2)
System.out.print(n);
}
static boolean isPrime(long n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static class Graph {
HashMap<Integer, LinkedList<Integer>> hm = new HashMap<Integer, LinkedList<Integer>>();
private void addVertex(int vertex) {
hm.put(vertex, new LinkedList<>());
}
private void addEdge(int source, int dest, boolean bi) {
if (!hm.containsKey(source))
addVertex(source);
if (!hm.containsKey(dest))
addVertex(dest);
hm.get(source).add(dest);
if (bi) {
hm.get(dest).add(source);
}
}
private boolean uniCycle(int i, HashSet<Integer> visited, int parent) {
visited.add(i);
LinkedList<Integer> list = hm.get(i);
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
Integer integer = (Integer) it.next();
if (!visited.contains(integer)) {
if (uniCycle(integer, visited, i))
return true;
} else if (integer != parent) {
return true;
}
}
return false;
}
private boolean uniCyclic() {
HashSet<Integer> visited = new HashSet<Integer>();
Set<Integer> set = hm.keySet();
for (Integer integer : set) {
if (!visited.contains(integer)) {
if (uniCycle(integer, visited, -1)) {
return true;
}
}
}
return false;
}
private boolean isbiCycle(int i, HashSet<Integer> visited, HashSet<Integer> countered) {
if (countered.contains(i))
return true;
if (visited.contains(i))
return false;
visited.add(i);
countered.add(i);
LinkedList<Integer> list = hm.get(i);
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
Integer integer = (Integer) it.next();
if (isbiCycle(integer, visited, countered)) {
return true;
}
}
countered.remove(i);
return false;
}
private boolean isbiCyclic() {
HashSet<Integer> visited = new HashSet<Integer>();
HashSet<Integer> countered = new HashSet<Integer>();
Set<Integer> set = hm.keySet();
for (Integer integer : set) {
if (isbiCycle(integer, visited, countered)) {
return true;
}
}
return false;
}
}
static class Node {
Node left, right;
int data;
public Node(int data) {
this.data = data;
}
public void insert(int val) {
if (val <= data) {
if (left == null) {
left = new Node(val);
} else {
left.insert(val);
}
} else {
if (right == null) {
right = new Node(val);
} else {
right.insert(val);
}
}
}
public boolean contains(int val) {
if (data == val) {
return true;
} else if (val < data) {
if (left == null) {
return false;
} else {
return left.contains(val);
}
} else {
if (right == null) {
return false;
} else {
return right.contains(val);
}
}
}
public void inorder() {
if (left != null) {
left.inorder();
}
System.out.print(data + " ");
if (right != null) {
right.inorder();
}
}
public int maxDepth() {
if (left == null)
return 0;
if (right == null)
return 0;
else {
int ll = left.maxDepth();
int rr = right.maxDepth();
if (ll > rr)
return (ll + 1);
else
return (rr + 1);
}
}
public int countNodes() {
if (left == null)
return 1;
if (right == null)
return 1;
else {
return left.countNodes() + right.countNodes() + 1;
}
}
public void preorder() {
System.out.print(data + " ");
if (left != null) {
left.inorder();
}
if (right != null) {
right.inorder();
}
}
public void postorder() {
if (left != null) {
left.inorder();
}
if (right != null) {
right.inorder();
}
System.out.print(data + " ");
}
public void levelorder(Node node) {
LinkedList<Node> ll = new LinkedList<Node>();
ll.add(node);
getorder(ll);
}
public void getorder(LinkedList<Node> ll) {
while (!ll.isEmpty()) {
Node node = ll.poll();
System.out.print(node.data + " ");
if (node.left != null)
ll.add(node.left);
if (node.right != null)
ll.add(node.right);
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Pair {
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
private static final Scanner sc = new Scanner(System.in);
private static final FastReader fs = new FastReader();
private static final OutputWriter op = new OutputWriter(System.out);
static int[] getintarray(int n) {
int ar[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = fs.nextInt();
}
return ar;
}
static long[] getlongarray(int n) {
long ar[] = new long[n];
for (int i = 0; i < n; i++) {
ar[i] = fs.nextLong();
}
return ar;
}
static int[][] get2darray(int n, int m) {
int ar[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ar[i][j] = fs.nextInt();
}
}
return ar;
}
static Pair[] getpairarray(int n) {
Pair ar[] = new Pair[n];
for (int i = 0; i < n; i++) {
ar[i] = new Pair(fs.nextInt(), fs.nextInt());
}
return ar;
}
static void printarray(int ar[]) {
for (int i : ar) {
op.print(i + " ");
}
op.flush();
}
static int fact(int n) {
int fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
//
// function to find largest prime factor
}
| Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | dc0ba2dc0d8641f6a593a3f7e35bb6a1 | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class test {
public static void main(String[] args) {
int test = fs.nextInt();
// int test = 1;
for (int cases = 0; cases < test; cases++) {
long n = fs.nextLong();
double sq = Math.sqrt(n);
if (Math.floor(sq) == Math.ceil(sq)) {
int s = (int) sq;
if (isPrime(s)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
} else {
System.out.println("NO");
}
op.flush();
}
}
static boolean isPrime(int n) {
int skip = 0;
if (n < 2)
return false;
else if (n == 2)
return true;
int limit = (int) Math.sqrt(n);
if (n % 2 == 0)
return false;
for (int j = 3; j <= limit; j += 2) {
if (n % j == 0)
return false;
}
return true;
}
static int countDifferentBits(int a, int b) {
int count = 0;
for (int i = 0; i < 32; i++) {
if (((a >> i) & 1) != ((b >> i) & 1)) {
++count;
}
}
return count;
}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sortMyMapusingValues(HashMap<String, Integer> hm) {
List<Map.Entry<String, Integer>> capitalList = new LinkedList<>(hm.entrySet());
Collections.sort(capitalList, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
HashMap<String, Integer> result = new HashMap<>();
for (Map.Entry<String, Integer> entry : capitalList) {
result.put(entry.getKey(), entry.getValue());
}
}
static boolean ispowerof2(long num) {
if ((num & (num - 1)) == 0)
return true;
return false;
}
static void primeFactors(int n) {
while (n % 2 == 0) {
System.out.print(2 + " ");
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
while (n % i == 0) {
System.out.print(i + " ");
n /= i;
}
}
if (n > 2)
System.out.print(n);
}
// static boolean isPrime(long n) {
// // Corner cases
// if (n <= 1)
// return false;
// if (n <= 3)
// return true;
// if (n % 2 == 0 || n % 3 == 0)
// return false;
//
// for (int i = 5; i * i <= n; i = i + 6)
// if (n % i == 0 || n % (i + 2) == 0)
// return false;
//
// return true;
// }
static class Graph {
HashMap<Integer, LinkedList<Integer>> hm = new HashMap<Integer, LinkedList<Integer>>();
private void addVertex(int vertex) {
hm.put(vertex, new LinkedList<>());
}
private void addEdge(int source, int dest, boolean bi) {
if (!hm.containsKey(source))
addVertex(source);
if (!hm.containsKey(dest))
addVertex(dest);
hm.get(source).add(dest);
if (bi) {
hm.get(dest).add(source);
}
}
private boolean uniCycle(int i, HashSet<Integer> visited, int parent) {
visited.add(i);
LinkedList<Integer> list = hm.get(i);
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
Integer integer = (Integer) it.next();
if (!visited.contains(integer)) {
if (uniCycle(integer, visited, i))
return true;
} else if (integer != parent) {
return true;
}
}
return false;
}
private boolean uniCyclic() {
HashSet<Integer> visited = new HashSet<Integer>();
Set<Integer> set = hm.keySet();
for (Integer integer : set) {
if (!visited.contains(integer)) {
if (uniCycle(integer, visited, -1)) {
return true;
}
}
}
return false;
}
private boolean isbiCycle(int i, HashSet<Integer> visited, HashSet<Integer> countered) {
if (countered.contains(i))
return true;
if (visited.contains(i))
return false;
visited.add(i);
countered.add(i);
LinkedList<Integer> list = hm.get(i);
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
Integer integer = (Integer) it.next();
if (isbiCycle(integer, visited, countered)) {
return true;
}
}
countered.remove(i);
return false;
}
private boolean isbiCyclic() {
HashSet<Integer> visited = new HashSet<Integer>();
HashSet<Integer> countered = new HashSet<Integer>();
Set<Integer> set = hm.keySet();
for (Integer integer : set) {
if (isbiCycle(integer, visited, countered)) {
return true;
}
}
return false;
}
}
static class Node {
Node left, right;
int data;
public Node(int data) {
this.data = data;
}
public void insert(int val) {
if (val <= data) {
if (left == null) {
left = new Node(val);
} else {
left.insert(val);
}
} else {
if (right == null) {
right = new Node(val);
} else {
right.insert(val);
}
}
}
public boolean contains(int val) {
if (data == val) {
return true;
} else if (val < data) {
if (left == null) {
return false;
} else {
return left.contains(val);
}
} else {
if (right == null) {
return false;
} else {
return right.contains(val);
}
}
}
public void inorder() {
if (left != null) {
left.inorder();
}
System.out.print(data + " ");
if (right != null) {
right.inorder();
}
}
public int maxDepth() {
if (left == null)
return 0;
if (right == null)
return 0;
else {
int ll = left.maxDepth();
int rr = right.maxDepth();
if (ll > rr)
return (ll + 1);
else
return (rr + 1);
}
}
public int countNodes() {
if (left == null)
return 1;
if (right == null)
return 1;
else {
return left.countNodes() + right.countNodes() + 1;
}
}
public void preorder() {
System.out.print(data + " ");
if (left != null) {
left.inorder();
}
if (right != null) {
right.inorder();
}
}
public void postorder() {
if (left != null) {
left.inorder();
}
if (right != null) {
right.inorder();
}
System.out.print(data + " ");
}
public void levelorder(Node node) {
LinkedList<Node> ll = new LinkedList<Node>();
ll.add(node);
getorder(ll);
}
public void getorder(LinkedList<Node> ll) {
while (!ll.isEmpty()) {
Node node = ll.poll();
System.out.print(node.data + " ");
if (node.left != null)
ll.add(node.left);
if (node.right != null)
ll.add(node.right);
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Pair {
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
private static final Scanner sc = new Scanner(System.in);
private static final FastReader fs = new FastReader();
private static final OutputWriter op = new OutputWriter(System.out);
static int[] getintarray(int n) {
int ar[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = fs.nextInt();
}
return ar;
}
static long[] getlongarray(int n) {
long ar[] = new long[n];
for (int i = 0; i < n; i++) {
ar[i] = fs.nextLong();
}
return ar;
}
static int[][] get2darray(int n, int m) {
int ar[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ar[i][j] = fs.nextInt();
}
}
return ar;
}
static Pair[] getpairarray(int n) {
Pair ar[] = new Pair[n];
for (int i = 0; i < n; i++) {
ar[i] = new Pair(fs.nextInt(), fs.nextInt());
}
return ar;
}
static void printarray(int ar[]) {
for (int i : ar) {
op.print(i + " ");
}
op.flush();
}
static int fact(int n) {
int fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
//
// function to find largest prime factor
}
| Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | 3d6053826e41f72a4b39418683a56119 | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class P1 implements Runnable
{
public static void main(String args[]) throws Exception
{
new Thread(null, new P1(),"P1",1<<27).start();
}
public void run()
{
InputReader sc = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
//Seive
int temp=1000001;
boolean[] flag=new boolean[temp];
Arrays.fill(flag,true);
flag[0]=false;
flag[1]=false;
for (int i=2;i<=Math.sqrt(temp);++i) {
if (flag[i]) {
for (int j=2;i*j<temp;++j) {
flag[i*j]=false;
}
}
}
//Actual Program
int n=sc.nextInt();
long[] a=new long[n];
for (int i=0;i<n;++i) {
a[i]=sc.nextLong();
double x=Math.ceil(Math.sqrt(a[i]));
double y=Math.floor(Math.sqrt(a[i]));
// System.out.println(x+" "+y);
if (x==y) {
if (flag[(int)x]) {
w.println("YES");
}
else{
w.println("NO");
}
}
else{
w.println("NO");
}
System.out.flush();
}
w.close();
}
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt()
{
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return readString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
// if modulo is required set value accordingly
public static long[][] matrixMultiply2dL(long t[][],long m[][])
{
long res[][]= new long[t.length][m[0].length];
for(int i=0;i<t.length;i++)
{
for(int j=0;j<m[0].length;j++)
{
res[i][j]=0;
for(int k=0;k<t[0].length;k++)
{
res[i][j]+=t[i][k]+m[k][j];
}
}
}
return res;
}
static long combination(long n,long r)
{
long ans=1;
for(long i=0;i<r;i++)
{
ans=(ans*(n-i))/(i+1);
}
return ans;
}
// **just change the name of class from Main to reuquired**
} | Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | 2fab2ac45cc62615ae7693bdd7c0675e | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | //note: 0<=|int|<=2 * 10^9
//note: 0<=|long|<= 9 * 10^18
import java.io.*;
import java.util.*;
public class MyProgram{
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
boolean[] prime = new boolean[(int) 1e6];
for(int i = 0; i < prime.length; i++){
prime[i] = true;
}
int count = 2;
while(count < prime.length){
for(int i = 2; i*count < prime.length; i++){
prime[i*count] = false;
}
count++;
}
Set<Integer> primes = new HashSet<>();
for(int i = 2; i < prime.length; i++){
if(prime[i] == true){
primes.add(i);
}
}
int n = sc.nextInt();
for(int i = 0; i < n; i++){
long k = sc.nextLong();
if(Math.sqrt(k) % 1 == 0){
if(primes.contains((int) Math.sqrt(k))){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
else{
System.out.println("NO");
}
}
out.close();
}
public static void TP(long k){
boolean ok = true;
if(ok){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
public static boolean isPrime(long p){
int count = 0;
for(long i = 1; i*i <= p ;i++){
if(p % i == 0){
count++;
}
}
if(count == 2){
return true;
}
return false;
}
public static boolean isPS(int k){
if(Math.sqrt(k) % 1 == 0){
return true;
}
return false;
}
//-----------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 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | eb8f6df9b45788ceb6ed5db75625fcd9 | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | import java.util.*;
public class TPrimes {
static void sieve(boolean a[]) {
int n = 1_000_000;
for(int i =0;i<n;i++) {
a[i] = true;
}
a[0] = a[1] = false;
for(int i =2;i<n;i++) {
if(a[i] == true) {
long j = (long)i*i;
while(j<n) {
a[(int)j] = false;
j+=i;
}
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
boolean[] a = new boolean[1_000_001];
sieve(a);
while(t-->0){
long n = scan.nextLong();
long x = (long)Math.ceil(Math.sqrt(n));
if(x*x==n&&a[(int)x]) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
}
| Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | 3f5d8553337930a6ae228842955f0132 | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Saturday{
public static void main(String args[]) throws IOException{
Scanner in = new Scanner(System.in);
long n = in.nextLong();
long x;
int rt;
boolean prime;
boolean ar[] = new boolean[10000000];
ar[1] = true;
for(int i = 2; i <= 10000 ; i++ ){
if(ar[i] == false)
for(long j = (i*i) ; j <= 1000000 ; j += i){
ar[(int)j] = true;
}
}
for(int i = 0; i<n ; i++){
x = in.nextLong();
rt =(int) Math.sqrt((double)x);
boolean status = (rt*rt == (int)x) && (ar[rt] == false) ? true : false;
if(status)
System.out.println("YES");
else
System.out.println("NO");
}
}
} | Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | 3c8907a3adab3e44c496483320166f2b | train_001.jsonl | 1349105400 | We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integer t Т-prime, if t has exactly three distinct positive divisors.You are given an array of n positive integers. For each of them determine whether it is Т-prime or not. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Saturday{
public static void main(String args[]) throws IOException{
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
long n = in.nextInt();
long x;
int rt;
boolean prime;
boolean ar[] = new boolean[10000000];
ar[1] = true;
for(int i = 2; i <= 10000 ; i++ ){
if(ar[i] == false)
for(long j = (i*i) ; j <= 1000000 ; j += i){
ar[(int)j] = true;
}
}
for(int i = 0; i<n ; i++){
x = (long)in.nextLong();
rt =(int) Math.sqrt((double)x);
boolean status = (rt*rt == (int)x) && (ar[rt] == false) ? true : false;
if(status)
System.out.println("YES");
else
System.out.println("NO");
}
}
} | Java | ["3\n4 5 6"] | 2 seconds | ["YES\nNO\nNO"] | NoteThe given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO". | Java 11 | standard input | [
"binary search",
"number theory",
"implementation",
"math"
] | 6cebf9af5cfbb949f22e8b336bf07044 | The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line contains n space-separated integers xi (1 ≤ xi ≤ 1012). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | 1,300 | Print n lines: the i-th line should contain "YES" (without the quotes), if number xi is Т-prime, and "NO" (without the quotes), if it isn't. | standard output | |
PASSED | aa13a4a51139c5c5cdc6fa4232a11b65 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Baskets {
private static void outputOdd(int totalBaskets, int totalBalls) {
int centre = (totalBaskets + 1)/2;
int offset = 0;
for(int i = 0; i<totalBalls; i++) {
if(offset==0) {
System.out.println(centre);
offset = 1 % centre;
}
else {
System.out.println((centre-offset));
if((i+1) < totalBalls)
System.out.println((centre + offset));
offset = (offset + 1) % centre;
i++;
}
}
}
private static void outputEven (int totalBaskets, int totalBalls) {
int mid1 = totalBaskets/2;
int mid2 = mid1 +1;
int offset = 0;
for(int i =0 ; i<totalBalls; i++) {
if(offset == 0) {
System.out.println(mid1);
if((i+1) < totalBalls)
System.out.println(mid2);
offset = 1 % mid1;
i++;
}
else {
System.out.println((mid1-offset));
if((i+1) < totalBalls)
System.out.println((mid2 + offset));
offset = (offset + 1) % mid1;
i++;
}
}
}
public static void main(String...string) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] args = br.readLine().split(" ");
int totalBalls = Integer.parseInt(args[0]);
int totalBaskets = Integer.parseInt(args[1]);
if(totalBaskets%2==0) outputEven(totalBaskets, totalBalls);
else outputOdd(totalBaskets, totalBalls);
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 63d00b93abdcceebd9c06935c3a4032d | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Round123_B {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
String[] f = r.readLine().split(" ");
int n = Integer.parseInt(f[0]);
int m = Integer.parseInt(f[1]);
int balls = n;
if (m % 2 != 0) {
while (balls > 0) {
int right = (m + 1) / 2 + 1;
int left = (m + 1) / 2 - 1;
out.println((m + 1) / 2);
out.flush();
balls--;
while (right <= m && left > 0 && balls - 2 >= 0) {
out.println(left);
out.flush();
out.println(right);
out.flush();
right++;
left--;
balls -= 2;
}
while (left > 0 && balls-- > 0) {
out.println(left--);
out.flush();
}
while (right <= m && balls-- > 0) {
out.println(right++);
out.flush();
}
}
} else {
while (balls > 0) {
int right = (m + 1) / 2 + 2;
int left = (m + 1) / 2 - 1;
out.println((m + 1) / 2);
out.flush();
balls--;
if (balls > 0) {
out.println((m + 1) / 2 + 1);
out.flush();
balls--;
}
while (right <= m && left > 0 && balls - 2 >= 0) {
out.println(left);
out.flush();
out.println(right);
out.flush();
right++;
left--;
balls -= 2;
}
while (left > 0 && balls-- > 0) {
out.println(left--);
out.flush();
}
while (right <= m && balls-- > 0) {
out.println(right++);
out.flush();
}
}
}
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | c06bbd98346e1fe48babe6807378a543 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class con123_B {
public static void main( final String[] args ) throws IOException {
final BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
final String[] parts = br.readLine().split( " " );
final int n = Integer.parseInt( parts[ 0 ] );
final int m = Integer.parseInt( parts[ 1 ] );
System.out.println( solve( n, m ) );
}
public static String solve( final int n, final int m ) {
final int[] seq = new int[n];
int f, l; // used;
seq[ 0 ] = m / 2;
f = seq[ 0 ];
l = f;
int idx = 1;
if ( m % 2 == 0 ) {
--seq[ 0 ];
--f;
if ( n > 1 )
seq[ 1 ] = seq[ 0 ] + 1;
l = seq[ 0 ] + 1;
++idx;
}
while ( idx < n ) {
seq[ idx++ ] = --f;
if ( idx < n ) seq[ idx++ ] = ++l;
}
final StringBuilder buff = new StringBuilder();
for ( int i = 0; i < n; ++i ) {
buff.append( seq[ i % m ] + 1 ).append( '\n' );
}
return buff.toString();
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 54f190fef5cf6552a4dbbb89516cbd5f | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
FastScanner in = new FastScanner(System.in);
int n = in.nextInt(), m = in.nextInt();
int last = m, c;
for (int i = 1; i <= n; i++) {
if (m % 2 == 0) {
if (last == m) c = m / 2;
else if (last > m / 2) c = m - last;
else c = m - last + 1;
} else {
if (last == m) c = m / 2 + 1;
else if (last >= m / 2 + 1) c = m - last;
else c = m - last + 1;
}
System.out.println(c);
last = c;
}
}
}
final class FastScanner {
private StringTokenizer stringTokenizer = null;
private BufferedReader bufferedReader;
public FastScanner(InputStream in) {
bufferedReader = new BufferedReader(new InputStreamReader(in));
try {
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
public String next() {
while (!stringTokenizer.hasMoreTokens())
try {
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
} catch (IOException e) {
e.printStackTrace();
return null;
}
return stringTokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public boolean hasNext() {
return stringTokenizer.hasMoreTokens();
}
public void nextLine() {
try {
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 26a0384cfde318cc6758934899ae1a04 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes |
import java.util.PriorityQueue;
import java.util.Scanner;
public class two {
public static void main(String[] args) {
PriorityQueue<obj> q = new PriorityQueue<obj>();
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
for (int i = 1; i <= m; i++)
q.add(new obj(i));
StringBuilder ans = new StringBuilder("");
for (int i = 1; i <= n; i++) {
obj cur = q.poll();
ans.append(cur.index + "\n");
cur.balls++;
q.add(cur);
}
System.out.print(ans);
}
static double n;
static double m;
static class obj implements Comparable<obj> {
int index;
int balls;
public obj(int index) {
balls = 0;
this.index = index;
}
@Override
public int compareTo(obj arg0) {
if (balls != arg0.balls)
return balls - arg0.balls;
else {
double x = Math.abs((m+1) / 2.0 - index);
double y = Math.abs((m+1) / 2.0 - arg0.index);
if (x != y)
return Double.compare(x, y);
else
return index - arg0.index;
}
}
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | f1d5bb583cf15739a237e68d1b9eb300 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.util.Scanner;
public class Prob195B {
public static void main(String[] Args) {
Scanner in = new Scanner(System.in);
int balls = in.nextInt();
int baskets = in.nextInt();
int[] arr = new int[baskets];
if (baskets % 2 == 0) {
arr[0] = (baskets) / 2;
arr[1] = (baskets) / 2 + 1;
for (int i = 2; i < baskets; i++)
if (i % 2 == 0)
arr[i] = arr[0] - i / 2;
else
arr[i] = arr[1] + i / 2;
} else {
arr[0] = (baskets + 1) / 2;
for (int i = 1; i < baskets; i++)
if (i % 2 == 1)
arr[i] = arr[0] - (i + 1) / 2;
else
arr[i] = arr[0] + (i / 2);
}
for (int i = 0; i < balls; i++)
System.out.println(arr[i % baskets]);
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | c7b937841bb6230fac3245d6ba34d9a7 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.PrintWriter;
import java.util.Scanner;
public class B {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
PrintWriter out=new PrintWriter(System.out);
if(m%2 == 0){
for(int ii=1;ii<=n;ii++){
int i=ii%m;
if(i==0)
i=m;
if(i%2 == 1){
//left
int res = m/2 - i/2 ;
out.println(res);
}
else{
//right
int res = m/2 + i/2;
out.println(res);
}
}
}
else{
for(int ii=1;ii<=n;ii++){
int i=ii%m;
if(i==0)
i=m;
if(i==1){
out.println((m+1)/2);
continue;
}
if(i%2 == 0){
//left
int res = (m+1)/2 - i/2;
out.println(res);
}
else{
//right
int res = (m+1)/2 + i/2;
out.println(res);
}
}
}
out.flush();
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | fefab308e2f11a366eeebd55a86d1ca8 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main implements Runnable {
void solution() throws IOException {
int n = nextInt();
int m = nextInt();
int[] p = new int[m];
int k = 0;
if (m % 2 == 1) {
int middle = m / 2;
p[k++] = middle;
int l = middle - 1, r = middle + 1;
while (l >= 0 && r < m) {
p[k++] = l--;
p[k++] = r++;
}
} else {
int middle = m / 2 - 1;
int l = middle, r = middle + 1;
while (l >= 0 && r < m) {
p[k++] = l--;
p[k++] = r++;
}
}
if (k != m) throw new RuntimeException();
for (int i = 0; i < n; i++)
out.println(p[i % m] + 1);
}
///////////////////// Template definitions //////////////////////////
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String l = in.readLine();
if (l == null) return null;
st = new StringTokenizer(l);
}
return st.nextToken();
}
public static void main(String args[]) {
//Locale.setDefault(Locale.UK);
new Thread(new Main()).start();
}
public void run() {
try {
boolean online = true;
Reader reader = online
? new InputStreamReader(System.in)
: new FileReader("my.in");
in = new BufferedReader(reader);
out = new PrintWriter(System.out);
solution();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(202);
}
}
BufferedReader in;
StringTokenizer st;
PrintWriter out;
} | Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 560f85792b3a57f7a56b8dc35e484667 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
BufferedReader br;
int[] a;
int n, m, mid;
public class Node implements Comparable<Node> {
int id;
public Node(int id) {
this.id = id;
}
public int compareTo(Node node) {
if (Math.abs(mid - 2 * this.id) < Math.abs(mid - 2 * node.id))
return -1;
if (Math.abs(mid - 2 * this.id) > Math.abs(mid - 2 * node.id))
return 1;
if (this.id < node.id)
return -1;
if (this.id > node.id)
return 1;
return 0;
}
}
public void go() throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
String[] ss = br.readLine().split(" ");
n = Integer.parseInt(ss[0]);
m = Integer.parseInt(ss[1]);
mid = m + 1;
Node[] nodes = new Node[m];
for (int i = 0; i < m; i++)
nodes[i] = new Node(i+1);
Arrays.sort(nodes);
a = new int[m];
for (int i = 0; i < m; i++)
a[i] = nodes[i].id;
for (int i = 0; i < n; i++)
System.out.println(a[i%m]);
}
public static void main(String[] args) throws Exception {
new Main().go();
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 3bc68070ab32ffeb10af04e703c2b6f4 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class B195 {
public static void main(String[] args) throws IOException {
solve();
}
public static void solve()throws IOException{
Scanner scan = new Scanner(new BufferedInputStream(System.in));
int n = scan.nextInt();
int m = scan.nextInt();
Pair p[] = new Pair[m];
double d = (m+1) / 2.0;
for (int i = 1; i <= m; i++) {
p[i-1] = new Pair();
p[i-1].basket = i;
p[i-1].idx = Math.abs(d - i);
}
Arrays.sort(p, new Comparator<Pair>() {
public int compare(Pair o1, Pair o2) {
if(o1.idx != o2.idx)
return (int)(o1.idx - o2.idx);
else
return o1.basket - o2.basket;
}
});
for (int i = 0, j = 0; i < n; i++) {
System.out.println(p[j].basket);
j = (j+1) % m ;
}
}
}
class Pair{
int basket;
double idx;
public String toString(){
return basket + "," + idx;
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | b4b385a8ca22c8a13e732c0d393241fb | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class B implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer st;
Random rnd;
int[] already, distances;
void solve() throws IOException {
int n = nextInt(), m = nextInt();
already = new int[m];
distances = new int[m];
for(int i = 1; i <= m; i++) {
distances[i - 1] = Math.abs((m + 1) / 2 - i);
if(m % 2 == 0 && i > (m / 2)) {
--distances[i - 1];
}
//out.println(i + " " + distances[i - 1]);
}
PriorityQueue<Integer> q = new PriorityQueue<Integer>(m, new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
if(already[a] != already[b]) {
return already[a] - already[b];
} else if(distances[a] != distances[b]) {
return distances[a] - distances[b];
} else {
return a - b;
}
}
});
for(int i = 0; i < m; i++) {
q.add(i);
}
for(int i = 0; i < n; i++) {
int u = q.poll();
already[u]++;
q.add(u);
out.println((u + 1));
}
}
public static void main(String[] args) {
final boolean oldChecker = false;
if(oldChecker) {
new Thread(null, new B(), "yarrr", 1 << 24).start();
} else {
new B().run();
}
}
public void run() {
try {
final String className = this.getClass().getName().toLowerCase();
try {
in = new BufferedReader(new FileReader(className + ".in"));
out = new PrintWriter(new FileWriter(className + ".out"));
} catch (FileNotFoundException e) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
rnd = new Random();
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(42);
}
}
String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | aa9a4f4ac34347e5dce5b49b82bdc432 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.util.*; import java.text.*; import java.io.BufferedReader;import java.math.*; import java.util.regex.*; import java.awt.geom.*; import static java.lang.Math.*; import static java.lang.Character.*; import static java.lang.Integer.*; import static java.lang.Double.*; import static java.lang.Long.*; import static java.lang.System.*; import static java.util.Arrays.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
public class B {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintStream out = System.out;
static int m = 0, n = 0;
static int result = 0;
static void readInput() throws IOException {
int[]x = readArrInt();
n = x[0];
m = x[1];
}
static void process() {
int[]a = new int[m];
int mid = (m-1)/2, u = 1, v=1;
if (m%2==0) {
u=-u;
v = -1;
}
// debug(mid);
a[0] = mid;
for (int i = 1; i < m; i+=2) {
try{
a[i] = mid-u;
a[i+1] = mid+u;
u+=v;
} catch (Exception e){
}
}
// debug (a);
for (int i = 0; i < n; i++) {
out.println(a[i%m]+1);
}
}
public static void main(String[] args) throws IOException {
readInput();
process();
}
public static void debug(Object...os) {
System.err.println(Arrays.deepToString(os));
}
static int[] readArrInt() throws IOException {
return parseArrInt(in.readLine());
}
static int readInt() throws IOException {
return new Integer(in.readLine());
}
static int[] parseArrInt(String s) {
StringTokenizer st = new StringTokenizer(s);
ArrayList<Integer> list = new ArrayList<Integer>();
while (st.hasMoreTokens()) {
list.add(Integer.parseInt(st.nextToken()));
}
Integer[] temp = list.toArray(new Integer[]{});
int[] res = new int[temp.length];
for (int i = 0; i < temp.length; i++) {
res[i] = temp[i];
}
return (res);
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 9e64f3d23ca8bf9fe330fb458bd254e4 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
*
*/
/**
* @author antonio081014
* @date Jun 10, 2012, 8:31:50 AM
*
*/
public class A {
public int[] map;
public int[] count;
public static void main(String[] args) throws Exception {
A main = new A();
main.run();
System.exit(0);
}
public void run() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split("\\s");
int N = Integer.parseInt(str[0]);
int M = Integer.parseInt(str[1]);
map = new int[N];
int mid = (M - 1) / 2;
int left = 0;
int right = 0;
boolean flag = true;
for (int i = 0; i < N; i++) {
if (mid - left < 0 && mid + right >= M) {
left = 0;
right = 0;
} else if (mid - left >= 0 && mid + right >= M) {
flag = true;
} else if (mid + right < M && mid - left < 0)
flag = false;
if (left == 0 && right == 0) {
map[i] = mid + 1;
left++;
right++;
if (M % 2 == 0)
flag = false;
} else if (flag) {
map[i] = mid - left + 1;
left++;
flag = false;
} else {
map[i] = mid + right + 1;
right++;
flag = true;
}
}
for (int i = 0; i < N; i++) {
System.out.println(map[i]);
}
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | dbd060b24a6750089d68c2e348f877a8 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.PrintWriter;
import java.util.Scanner;
public class AfterTraining {
public static void solve(int n, int m, PrintWriter out) {
if (m % 2 == 1) {
int middle = (m + 1) / 2;
int count = 0;
while (count < n) {
out.println(middle);
count++;
if (count == n) {
return;
}
for (int radius = 1; radius < m / 2 + 1; radius++) {
out.println(middle - radius);
count++;
if (count == n) {
return;
}
out.println(middle + radius);
count++;
if (count == n) {
return;
}
}
}
} else {
int middle = m / 2;
int count = 0;
while (count < n) {
out.println(middle);
count++;
if (count == n) {
return;
}
for (int radius = 1; middle - radius > 0; radius++) {
out.println(middle + radius);
count++;
if (count == n) {
return;
}
out.println(middle - radius);
count++;
if (count == n) {
return;
}
}
out.println(m);
count++;
if (count == n) {
return;
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
PrintWriter out = new PrintWriter(System.out);
solve(n, m, out);
out.flush();
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 8d26147a271af77457193b1213e5f40d | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt(), count = m, last = (m + 1) / 2;
Queue<Integer> que = new LinkedList<Integer>();
if (count % 2 == 1) {
que.add(last);
count--;
last--;
}
while (count > 0) {
que.add(last);
que.add(m - last + 1);
last--;
count -= 2;
}
for (int i = 1; i <= n; i++) {
int b = que.poll();
System.out.println(b);
que.add(b);
}
}
} | Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 5b48ac780a7e0dc58f42980735fd3f78 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes |
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author pttrung
*/
/*
* aaaaaaaaa
aaa
*/
public class B {
public static int Mod = 1000003;
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner();
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int m = in.nextInt();
Basket[] data = new Basket[m];
double mid = (double) (m + 1) / 2;
PriorityQueue<Basket> q = new PriorityQueue();
for (int i = 0; i < data.length; i++) {
double point = Math.abs(mid - (i + 1));
data[i] = new Basket(i + 1,0, point);
q.add(data[i]);
}
for (int i = 0; i < n; i++) {
Basket b = q.poll();
out.println(b.num);
b.value += 1;
q.add(b);
}
out.close();
}
static class Basket implements Comparable<Basket> {
int num, value;
double point;
public Basket(int num, int value, double point) {
this.num = num;
this.value = value;
this.point = point;
}
@Override
public int compareTo(Basket o) {
if (value != o.value) {
return value - o.value;
} else if (point != o.point) {
if (point < o.point) {
return -1;
}
return 1;
} else {
return num - o.num;
}
}
}
public static double solve(double dist, int vp, int vd) {
return (double) dist / (vd - vp);
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public static long pow(long a, int b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2);
if (b % 2 == 0) {
return val * val;
} else {
return val * (val * a);
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
//System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("square_detector.txt"))));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 33343155668b4de5e63fa8fe9f5900fb | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.*;
import java.util.*;
/**
* */
public class Solution {
public static Integer nBalls=0;
public static Integer nBaskets=0;
public class BasketComparator implements Comparator<Integer>{
@Override
public int compare(Integer arg0, Integer arg1) {
Double c0=Math.abs((((double)nBaskets+1)/2)-(double) arg0);
Double c1=Math.abs((((double)nBaskets+1)/2)-(double) arg1);
return (c0.equals(c1))? arg0.compareTo(arg1):c0.compareTo(c1);
}}
public BasketComparator newBasketComparator(){
return new BasketComparator();
}
/**
* @param args
*/
public static void main(String[] args){
try {
Solution solution=new Solution();
BufferedReader reader =new BufferedReader( new InputStreamReader( System.in)); // STDIN
String s[]=reader.readLine().split(" ", 2);
nBalls=Integer.valueOf(s[0].trim());
nBaskets=Integer.valueOf(s[1].trim());
List<Integer> list=Collections.synchronizedList(new ArrayList<Integer>(nBaskets));
for(int i=1;i<=nBaskets;i++)
list.add(i);
Collections.sort(list, solution.newBasketComparator());
for(int i=0;i!=nBalls;i++){
System.out.println(list.get(i%list.size()));
}
}catch(Exception e){
e.printStackTrace(System.err);
}
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 2871c87a427d3b7941428dd6ee6ff17c | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(),
m = sc.nextInt();
int cnt = 0;
int l = (int) Math.ceil(1d * n / m);
if ((m & 1) == 1) {
for (int i = 0; i < l; ++i)
for (int j = 0; j * 2 < m; ++j) {
if (cnt < n) {
System.out.println((m + 1) / 2 - j);
cnt += 1;
}
if (j > 0 & cnt < n) {
System.out.println((m + 1) / 2 + j);
cnt += 1;
}
}
} else {
for (int i = 0; i < l; ++i)
for (int j = 1; j * 2 <= m; ++j) {
if (cnt < n) {
System.out.println(m / 2 - j + 1);
cnt += 1;
}
if (cnt < n) {
System.out.println(m / 2 + j);
cnt += 1;
}
}
}
}
} | Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 377045bfa2329114c916c866390ff040 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.util.*;
import java.math.*;
import java.io.*;
public class Main
{
public static void main(String args[]) throws IOException
{
Scanner c=new Scanner(System.in);
int N=c.nextInt();
int M=c.nextInt();
PriorityQueue<basket> Q=new PriorityQueue<basket>();
for(int i=0;i<M;i++)
Q.add(new basket(0,i,M));
for(int i=0;i<N;i++)
{
basket bas=Q.poll();
System.out.println((bas.pos+1));
Q.add(new basket(bas.balls+1,bas.pos,M));
}
}
}
class basket implements Comparable
{
int balls;
int pos;
double mid;
public basket(int balls, int pos, int M)
{
this.balls=balls;
this.pos=pos;
this.mid=((double)(M-1))/2;
}
@Override
public int compareTo(Object a0)
{
basket bas=(basket)a0;
double d1=Math.abs(mid-this.pos);
double d2=Math.abs(mid-bas.pos);
if(this.balls<bas.balls)
return -1;
else if(this.balls==bas.balls)
{
if(d1<d2)
return -1;
if(d1==d2)
{
if(this.pos<bas.pos)
return -1;
}
return 1;
}
return 1;
}
}
//must declare new classes here | Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 63917387ac51fda13c979f882b4840e8 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class A {
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE")!=null;
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
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");
}
}
String readString() throws IOException{
while(!tok.hasMoreTokens()){
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException{
return Integer.parseInt(readString());
}
long readLong() throws IOException{
return Long.parseLong(readString());
}
double readDouble() throws IOException{
return Double.parseDouble(readString());
}
int[] readArr(int n) throws IOException{
int[] res = new int[n];
for(int i = 0; i < n; i++){
res[i] = readInt();
}
return res;
}
long[] readArrL(int n) throws IOException{
long[] res = new long[n];
for(int i = 0; i < n; i++){
res[i] = readLong();
}
return res;
}
public static void main(String[] args){
new A().run();
}
public void run(){
try{
long t1 = System.currentTimeMillis();
init();
solve();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = "+(t2-t1));
}catch (Exception e){
e.printStackTrace(System.err);
System.exit(-1);
}
}
class Basket implements Comparable<Basket>{
int num, col, n;
public Basket(int num, int col, int n) {
super();
this.num = num;
this.col = col;
this.n = n;
}
@Override
public int compareTo(Basket o) {
if(this.col > o.col) return 1;
if(this.col < o.col) return -1;
if(abs((this.n+1)*1.0/2.0 - num) > abs((this.n+1)*1.0/2.0 - o.num)) return 1;
if(abs((this.n+1)*1.0/2.0 - num) < abs((this.n+1)*1.0/2.0 - o.num)) return -1;
return this.num - o.num;
}
}
void solve() throws IOException{
int n = readInt();
int m = readInt();
PriorityQueue<Basket> q = new PriorityQueue<A.Basket>();
for(int i = 1; i <= m; i++)
q.add(new Basket(i, 0, m));
for(int i = 0; i < n; i++)
{
Basket b = q.poll();
out.println(b.num);
b.col++;
q.add(b);
}
}
void maxHepify(int[] a, int i, int length){
int l = (i<<1) + 1;
int r = (i<<1) + 2;
int largest = i;
if(l < length && a[l] > a[largest])
largest = l;
if(r < length && a[r] > a[largest])
largest = r;
if(largest != i){
a[largest] += a[i];
a[i] = a[largest] - a[i];
a[largest] -= a[i];
maxHepify(a, largest, length);
}
}
void buildMaxHeap(int[] a){
for(int i = a.length/2 - 1; i >= 0; i--){
maxHepify(a, i, a.length);
}
}
void heapSort(int[] a){
buildMaxHeap(a);
for(int i = a.length - 1; i > 0; i--){
a[i] += a[0];
a[0] = a[i] - a[0];
a[i] -= a[0];
maxHepify(a, 0, i);
}
}
int[] zFunction(char[] s){
int[] z = new int[s.length];
z[0] = 0;
for (int i=1, l=0, r=0; i<s.length; ++i) {
if (i <= r)
z[i] = min (r-i+1, z[i-l]);
while (i+z[i] < s.length && s[z[i]] == s[i+z[i]])
++z[i];
if (i+z[i]-1 > r){
l = i;
r = i+z[i]-1;
}
}
return z;
}
int[] prefixFunction(char[] s){
int[] pr = new int[s.length];
for (int i = 1; i< s.length; ++i) {
int j = pr[i-1];
while (j > 0 && s[i] != s[j])
j = pr[j-1];
if (s[i] == s[j]) ++j;
pr[i] = j;
}
return pr;
}
int ModExp(int a, int n, int mod){
int res = 1;
while (n!=0)
if ((n & 1) != 0) {
res = (res*a)%mod;
--n;
}
else {
a = (a*a)%mod;
n >>= 1;
}
return res;
}
public static class Utils {
private Utils() {}
public static void mergeSort(int[] a) {
mergeSort(a, 0, a.length - 1);
}
private static void mergeSort(int[] a, int leftIndex, int rightIndex) {
final int MAGIC_VALUE = 50;
if (leftIndex < rightIndex) {
if (rightIndex - leftIndex <= MAGIC_VALUE) {
insertionSort(a, leftIndex, rightIndex);
} else {
int middleIndex = (leftIndex + rightIndex) / 2;
mergeSort(a, leftIndex, middleIndex);
mergeSort(a, middleIndex + 1, rightIndex);
merge(a, leftIndex, middleIndex, rightIndex);
}
}
}
private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) {
int length1 = middleIndex - leftIndex + 1;
int length2 = rightIndex - middleIndex;
int[] leftArray = new int[length1];
int[] rightArray = new int[length2];
System.arraycopy(a, leftIndex, leftArray, 0, length1);
System.arraycopy(a, middleIndex + 1, rightArray, 0, length2);
for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) {
if (i == length1) {
a[k] = rightArray[j++];
} else if (j == length2) {
a[k] = leftArray[i++];
} else {
a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++];
}
}
}
private static void insertionSort(int[] a, int leftIndex, int rightIndex) {
for (int i = leftIndex + 1; i <= rightIndex; i++) {
int current = a[i];
int j = i - 1;
while (j >= leftIndex && a[j] > current) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = current;
}
}
}
boolean isPrime(int a){
for(int i = 2; i <= sqrt(a); i++)
if(a % i == 0) return false;
return true;
}
static double distance(long x1, long y1, long x2, long y2){
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
static long gcd(long a, long b){
if(min(a,b) == 0) return max(a,b);
return gcd(max(a, b) % min(a,b), min(a,b));
}
static long lcm(long a, long b){
return a * b /gcd(a, b);
}
}
/*class Treap<K extends Comparable<K>>{
public K x;
public double y;
public Treap<K> left;
public Treap<K> right;
public Treap(K x, double y, Treap<K> left, Treap<K> right) {
this.x = x;
this.y = y;
this.left = left;
this.right = right;
}
public static <K extends Comparable<K>> Treap<K> merge(Treap<K> l, Treap<K> r){
if(l == null) return r;
if(r == null) return l;
if(l.y > r.y){
return new Treap<K>(l.x, l.y, l.left, merge(l.right, r));
}
else{
return new Treap<K>(r.x, r.y, merge(l, r.left), r.right);
}
}
public void split(K x, Treap<K> left, Treap<K> right){
Treap<K> newTreap = null;
if(this.x.compareTo(x) <= 0){
if(this.right == null){
right = null;
}
else{
right.split(x, newTreap, right);
}
left = new Treap<K>(this.x, this.y, left, newTreap);
}
else{
if(this.left == null){
left = null;
}
else{
left.split(x, left, newTreap);
}
right = new Treap<K>(x, y, newTreap, right);
}
}
public Treap<K> add(K x){
Treap<K> left = null, right = null;
this.split(x, left, right);
Treap<K> temp = new Treap<K>(x, random(), null, null);
return merge(merge(left, temp), right);
}
@SuppressWarnings("null")
public Treap<K> remove(K x){
Treap<K> left = null, temp = null, right = null;
this.split(x, left, right);
right.split(x, temp, right);
return merge(left, right);
}
public static <K extends Comparable<K>> Treap<K> build(K[] a){
Treap<K> temp = new Treap<K>(a[0], random(), null, null);
for(int i = 1; i < a.length; i++){
temp = temp.add(a[i]);
}
return temp;
}
}*/ | Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 323d1e2a1aa228eb97b34d57019ab95e | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.util.Scanner;
public class B {
public int [] solve( int a , int b) {
int [] res = new int[a];
if( b == 1 ) {
for( int i=0 ; i<a ; i ++){
res[i] = 1;
}
return res;
}
int mid = ( b + 1 ) / 2;
int beg = mid ;
int end = mid+1 > b ? b : mid +1;
int suma = 0 ;
int sumb = 0 ;
for( int i =0;i < a ; i ++ ) {
if( suma == sumb && (b+1 - beg - end ) <= 0) {
res[i] = beg;
beg -- ;
if( beg == 0 ) {
beg = mid ;
suma ++ ;
}
}else{
res[i] = end;
end ++ ;
if( end > b ) {
end = mid + 1>b?b:mid+1;
sumb ++ ;
}
}
}
return res;
}
public static void main (String [] args){
B solution = new B();
Scanner cin = new Scanner(System.in);
while ( cin.hasNext()) {
int a = cin.nextInt();
int b = cin.nextInt();
int [] res = solution.solve(a,b);
for( int i =0 ; i< res.length ; i ++ ) {
System.out.println(res[i]);
}
}
cin.close();
}
// public List<Integer> test ( int a , int b){
// int mid = (b+1)/2;
// List<Integer> res = new ArrayList<Integer>();
// int [] buts = new int[b+1];
// for( int i=1 ;i <=a ; i ++ ) {
// int num = index(buts,mid);
// buts[num] ++ ;
// res.add(num);
// }
// return res;
// }
//
// int index( int [] a , int mid ) {
// int min = 1000000;
// int last = -1;
// for( int i = 1 ; i < a.length ; i ++ ) {
// if( a[i] < min) {
// min = a[i];
// last = i ;
// }else{
// if( a[i] == min) {
// int la = Math.abs(mid-i);
// int lb = Math.abs(mid-last);
// if( la < lb ) {
// last = i;
// }
// }
// }
// }
// return last;
// }
} | Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | b77b9dde2c72448658800a58e88b36a1 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | //package contest_123_div2;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.Writer;
import java.util.Arrays;
/**
* Date : 10 ����. 2012
* Time : 18:29:27
* Email : denys.astanin@gmail.com
*/
public class B {
public static void main(String[] args) throws IOException {
new B().run();
}
int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
String nextString() throws IOException {
in.nextToken();
return (String) in.sval;
}
StreamTokenizer in;
Writer writer;
Reader reader;
void run() throws IOException {
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
reader = oj ? new InputStreamReader(System.in, "ISO-8859-1")
: new FileReader("src/contest_123_div2/B.txt");
writer = new OutputStreamWriter(System.out, "ISO-8859-1");
in = new StreamTokenizer(new BufferedReader(reader));
PrintWriter out = new PrintWriter(writer);
int n = nextInt();
int m = nextInt();
int middle = (m + 1) / 2;
int leftPointer, rightPointer;
int[] counter = new int[m + 1];
leftPointer = middle;
rightPointer = middle + (m % 2 == 0 ? 1 : 0);
while (n > 0) {
if (rightPointer == leftPointer) {
out.println(leftPointer);
counter[leftPointer]++;
leftPointer--;
rightPointer++;
} else if (Math.abs(middle - leftPointer) <= Math.abs(middle + (m % 2 == 0 ? 1 : 0) - rightPointer)) {
if (counter[leftPointer] <= counter[rightPointer]) {
out.println(leftPointer);
counter[leftPointer]++;
leftPointer--;
} else {
out.println(rightPointer);
counter[rightPointer]++;
rightPointer++;
}
} else if (Math.abs(middle - leftPointer) > Math.abs(middle + (m % 2 == 0 ? 1 : 0) - rightPointer)) {
if (counter[leftPointer] > counter[rightPointer]) {
out.println(leftPointer);
counter[leftPointer]++;
leftPointer--;
} else {
out.println(rightPointer);
counter[rightPointer]++;
rightPointer++;
}
}
if (leftPointer == 0) {
leftPointer = middle;
}
if (rightPointer == m + 1) {
rightPointer = middle;
if (m % 2 == 0) {
rightPointer++;
}
}
n--;
}
out.flush();
out.close();
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 2774f6b7dd4887044222c94d9337d7fe | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | //package round123;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
public class B {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
final int n = ni(), m = ni();
final int[] d = new int[m];
Queue<Integer> q = new PriorityQueue<Integer>(100000, new Comparator<Integer>(){
public int compare(Integer a, Integer b){
if(d[a] != d[b])return d[a] - d[b];
int aa = Math.abs(m-1-2*a);
int bb = Math.abs(m-1-2*b);
if(aa != bb)return aa - bb;
return a - b;
}
});
for(int i = 0;i < m;i++){
q.add(i);
}
for(int i = 0;i < n;i++){
int cur = q.poll();
out.println(cur+1);
d[cur]++;
q.add(cur);
}
}
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 B().run();
}
public int ni()
{
try {
int num = 0;
boolean minus = false;
while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
if(num == '-'){
num = 0;
minus = true;
}else{
num -= '0';
}
while(true){
int b = is.read();
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
} catch (IOException e) {
}
return -1;
}
public long nl()
{
try {
long num = 0;
boolean minus = false;
while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
if(num == '-'){
num = 0;
minus = true;
}else{
num -= '0';
}
while(true){
int b = is.read();
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
}
} catch (IOException e) {
}
return -1;
}
public String ns()
{
try{
int b = 0;
StringBuilder sb = new StringBuilder();
while((b = is.read()) != -1 && (b == '\r' || b == '\n' || b == ' '));
if(b == -1)return "";
sb.append((char)b);
while(true){
b = is.read();
if(b == -1)return sb.toString();
if(b == '\r' || b == '\n' || b == ' ')return sb.toString();
sb.append((char)b);
}
} catch (IOException e) {
}
return "";
}
public char[] ns(int n)
{
char[] buf = new char[n];
try{
int b = 0, p = 0;
while((b = is.read()) != -1 && (b == ' ' || b == '\r' || b == '\n'));
if(b == -1)return null;
buf[p++] = (char)b;
while(p < n){
b = is.read();
if(b == -1 || b == ' ' || b == '\r' || b == '\n')break;
buf[p++] = (char)b;
}
return Arrays.copyOf(buf, p);
} catch (IOException e) {
}
return null;
}
double nd() { return Double.parseDouble(ns()); }
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 7e0a5172ffc3507c3cc991f522e3fc88 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author lwc626
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
MyInputReader in = new MyInputReader(inputStream);
MyOutputWriter out = new MyOutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, MyInputReader in, MyOutputWriter out) {
int n = in .nextInt() , m = in.nextInt() ;
int[] order = new int[m] ;
int cnt = 0;
order[cnt++] = (m + 1) / 2;
if( (m & 1) == 0)
order[cnt++] = ( m + 2 ) / 2;
for( int i = 1 ; i <= (m - 1) / 2 ; i ++ ){
order[cnt++] = (m+1)/2 - i ;
order[cnt++] = (m+2)/2 + i ;
}
for( int i = 0 ; i < n ; i ++ ){
out.printLine( order[ i % m ] );
}
}
}
class MyInputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public MyInputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt(){
return readInt() ;
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
class MyOutputWriter {
private final PrintWriter writer;
public MyOutputWriter(OutputStream outputStream) {
writer = new PrintWriter(outputStream);
}
public MyOutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | ebb52ae5fa28ebd72e954c0757078307 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.util.*;
public class P195B {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int M = in.nextInt();
double mid = (M+1)/2;
int pos = 0;
if (M%2 == 0) pos = -1;
for (int n = 0; n < N; ++n)
{
int val;
if (M%2 == 1)
val = (int)mid + pos;
else
val = (int)Math.round(mid + pos + (pos < 0 ? 0.5 : -0.5));
if (val < 1 || val > M)
{
pos = (M%2 == 0 ? -1: 0);
if (M%2 == 1)
val = (int)mid + pos;
else
val = (int)Math.round(mid + pos + (pos < 0 ? 0.5 : -0.5));
}
System.out.println(val);
if (pos < 0)
pos *= -1;
else
pos = (pos+1)*-1;
}
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 16c3f8c47bd7475eb00a1768653d95a4 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.SortedSet;
import java.util.StringTokenizer;
public class ProblemB {
public static void main(String[] args) {
solve();
}
public static void solve() {
BufferedReader br = null;
InputStreamReader is = null;
is = new InputStreamReader(System.in);
br = new BufferedReader(is);
try {
StringTokenizer tokenizer = new StringTokenizer(br.readLine(), " ");
int balls = Integer.valueOf(tokenizer.nextToken());
int baskets = Integer.valueOf(tokenizer.nextToken());
List<Pair> basketsList = new ArrayList<Pair>(baskets);
for (int index = 1; index <= baskets; ++index ) {
basketsList.add(new Pair(index, Math.abs(baskets + 1 - 2 * index)));
}
Collections.sort(basketsList, new Comparator<Pair>() {
@Override
public int compare(Pair pair1, Pair pair2) {
if (pair1.range > pair2.range)
return 1;
if (pair1.range < pair2.range)
return -1;
if (pair1.index > pair2.index)
return 1;
if (pair1.index < pair2.index)
return -1;
return 0;
}
});
for (int index =0 ; index < balls; ++index) {
System.out.println(basketsList.get(index % baskets).index);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null)
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class Pair {
int index;
int range;
public Pair(int index, int range) {
this.index = index;
this.range = range;
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | f3b20e8ce657a50c2e80348e43ddbd24 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Sourcecode {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int ballnumber= input.nextInt();
int basketnumber = input.nextInt();
getanswer(ballnumber,basketnumber);
}
private static void getanswer(int ballnumber, int basketnumber) {
for(int i=1;i<=ballnumber;i++){
int answer=putball(i,basketnumber);
System.out.println(answer);
}
}
private static int putball(int i, int basketnumber) {
int category = i%basketnumber;
if(category==0) category=basketnumber;
int answer=0;
float middle = (float) ((basketnumber+1)*0.5);
if(category%2==0){//Category is even
if(basketnumber%2==0){//Middle is not integer
//System.out.println("hi"+category);
answer = (int) (middle+0.5+(float)(category-2)/2);
}
else{//Middle is integer
answer=(int) (middle-(category/2));
}
}
else{//Category is Odd
if(basketnumber%2==0){//Middle is 3.5 or like this
answer = (int) (middle-0.5-(float)(category-1)/2);
}
else{ //Middle is integer
answer = (int) (middle+((category-1)/2));
}
}
return answer;
}
} | Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 43fec7c6e688b1cf01d535cb18f18625 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.*;
import java.util.*;
public class B
{
String line;
StringTokenizer inputParser;
BufferedReader is;
FileInputStream fstream;
DataInputStream in;
String FInput="";
void openInput(String file)
{
if(file==null)is = new BufferedReader(new InputStreamReader(System.in));//stdin
else
{
try{
fstream = new FileInputStream(file);
in = new DataInputStream(fstream);
is = new BufferedReader(new InputStreamReader(in));
}catch(Exception e)
{
System.err.println(e);
}
}
}
void readNextLine()
{
try {
line = is.readLine();
inputParser = new StringTokenizer(line, " ");
//System.err.println("Input: " + line);
} catch (IOException e) {
System.err.println("Unexpected IO ERROR: " + e);
}
catch (NullPointerException e)
{
line=null;
}
}
long NextLong()
{
String n = inputParser.nextToken();
long val = Long.parseLong(n);
return val;
}
int NextInt()
{
String n = inputParser.nextToken();
int val = Integer.parseInt(n);
//System.out.println("I read this number: " + val);
return val;
}
String NextString()
{
String n = inputParser.nextToken();
return n;
}
void closeInput()
{
try {
is.close();
} catch (IOException e) {
System.err.println("Unexpected IO ERROR: " + e);
}
}
public static void main(String [] argv)
{
String filePath=null;
if(argv.length>0)filePath=argv[0];
new B(filePath);
}
public void readFInput()
{
for(;;)
{
try
{
readNextLine();
FInput+=line+" ";
}
catch(Exception e)
{
break;
}
}
inputParser = new StringTokenizer(FInput, " ");
}
public B(String inputFile)
{
openInput(inputFile);
readNextLine();
int n=NextInt();
int m=NextInt();
int [] b = new int [m];
int idL=0, idR=m-1, id=m;
while(id>0)
{
b[idR--]=id--;
if(id>0)b[idL++]=id--;
}
int [] a = new int[m+1];
for(int i=0; i<m; i++)
a[b[i]]=i+1;
a[0]=a[m];
for(int i=1; i<=n; i++)
{
int x=a[i%m];
System.out.println(x);
}
closeInput();
}
private class C implements Comparable<C>
{
int resi, id;
C(int id, int resi)
{
this.id=id;
this.resi=resi;
}
public int compareTo( C d)
{
return d.resi - resi;
}
}
} | Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 559eb4f7f9219f0e374e922561e23b11 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.util.*;
/**
* Created by IntelliJ IDEA.
* User: piyushd
* Date: 3/26/11
* Time: 10:53 PM
* To change this template use File | Settings | File Templates.
*/
public class TaskB {
void run() {
int N = nextInt(), M = nextInt();
int [] pos = new int[M];
if (M % 2 == 0) {
int ix = 0;
for (int i = M / 2 - 1; i >= 0; i--) {
pos[i] = ix;
ix += 2;
}
ix = 1;
for (int i = M / 2; i < M; i++) {
pos[i] = ix;
ix += 2;
}
} else {
int ix = 1;
for (int i = M / 2 - 1; i >= 0; i--) {
pos[i] = ix;
ix += 2;
}
ix = 0;
for (int i = M / 2; i < M; i++) {
pos[i] = ix;
ix += 2;
}
}
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < pos.length; i++) map.put(pos[i], i + 1);
for (int i = 0; i < N; i++) {
System.out.println(map.get(i % M));
}
}
int nextInt(){
try{
int c = System.in.read();
if(c == -1) return c;
while(c != '-' && (c < '0' || '9' < c)){
c = System.in.read();
if(c == -1) return c;
}
if(c == '-') return -nextInt();
int res = 0;
do{
res *= 10;
res += c - '0';
c = System.in.read();
}while('0' <= c && c <= '9');
return res;
}catch(Exception e){
return -1;
}
}
long nextLong(){
try{
int c = System.in.read();
if(c == -1) return -1;
while(c != '-' && (c < '0' || '9' < c)){
c = System.in.read();
if(c == -1) return -1;
}
if(c == '-') return -nextLong();
long res = 0;
do{
res *= 10;
res += c-'0';
c = System.in.read();
}while('0' <= c && c <= '9');
return res;
}catch(Exception e){
return -1;
}
}
double nextDouble(){
return Double.parseDouble(next());
}
String next(){
try{
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while(Character.isWhitespace(c))
c = System.in.read();
do{
res.append((char)c);
}while(!Character.isWhitespace(c=System.in.read()));
return res.toString();
}catch(Exception e){
return null;
}
}
String nextLine(){
try{
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while(c == '\r' || c == '\n')
c = System.in.read();
do{
res.append((char)c);
c = System.in.read();
}while(c != '\r' && c != '\n');
return res.toString();
}catch(Exception e){
return null;
}
}
public static void main(String[] args){
new TaskB().run();
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 2bf565297d9edcbf5f971e5aff505091 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) throws Exception {
new B().solve();
}
void solve() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] sp = in.readLine().split(" ");
int n = Integer.parseInt(sp[0]);
int m = Integer.parseInt(sp[1]);
System.out.println(((m + 1) / 2));
int last = (m + 1) / 2;
n--;
for (; n > 0; n--) {
int next;
if (last == m) {
next = (m + 1) / 2;
} else if ((m + 1) - last > last) {
next = (m + 1) - last;
} else {
next = (m + 1) - (last + 1);
}
System.out.println(next);
last = next;
}
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | c783c1f403cf71400d300f5d137146ea | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int n=s.nextInt();
int N[]=new int[n];
int m=s.nextInt();
int mn=0;
int mid=(m-1)/2;
Boolean dec=false;
int incer=0;
if(m==2)
{
for(int i=0;i<n;i++)
{
if(i%2==0)
{
N[i]=0;
}
else
{
N[i]=1;
}
}
for(int i=0;i<n;i++)
{
System.out.println(++N[i]);
}
return;
}
if(m%2!=0)
{
for(int i=0;i<n;i++)
{
if((mn<m)&&(mn>0))
{
if(dec)
{
N[i]=mid+incer;
dec=false;
incer++;
mn++;
}
else
{
N[i]=mid-incer;
mn++;
dec=true;
}
}
else
{
if(mn>=m)
{
mn=0;
}
if(mn==0)
{
N[i]=mid;
incer=1;
mn++;
}
}
}
}
else
{
for(int i=0;i<n;i+=2)
{
if(mid+incer>=m-1)
{
incer=0;
}
N[i]=mid-incer;
if(i<n-1)
{
N[i+1]=mid+incer+1;
}
incer++;
}
}
for(int i=0;i<n;i++)
{
System.out.println(++N[i]);
}
}
} | Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | e7c6d58bf1246709648b5dcb80766a96 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
import java.util.TreeSet;
public class b123 {
public static void debug(Object... obs)
{
System.out.println(Arrays.deepToString(obs));
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int med = 10*(m+1)/2;
TreeSet<BS> pq = new TreeSet<BS>();
for(int i=0;i<m;i++)
{
pq.add(new BS(0,i+1,med));
}
for(int i=0;i<n;i++)
{
BS h = pq.pollFirst();
System.out.println(h.id);
h.bls++;
pq.add(h);
}
}
private static class BS implements Comparable<BS>
{
int bls = 0;
int id = 0;
int med = 0;
public BS(int bls, int id, int med)
{
super();
this.bls = bls;
this.id = id;
this.med = med;
}
public int compareTo(BS a)
{
if(bls - a.bls != 0)
return bls - a.bls;
int fm = Math.abs( med - 10*id );
int afm = Math.abs( med - 10*a.id );
if(fm - afm != 0)
return fm - afm;
return id - a.id;
}
}
} | Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 11c4fd07d8d5901839234aff7b47989e | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.util.*;
public class B {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
final int m = in.nextInt();
TreeSet<Integer> X = new TreeSet<Integer>(new Comparator<Integer>() {
public int compare(Integer A, Integer B) {
double sa = Math.abs((m+1)/2.0-A);
double sb = Math.abs((m+1)/2.0-B);
if(sa > sb) return 1;
if(sb > sa) return -1;
return A-B;
}
});
StringBuilder out = new StringBuilder();
while(n > 0) {
if(X.isEmpty()) {
for(int i=1; i<=m; i++)
X.add(i);
}
//System.out.println(X);
out.append(X.pollFirst()+"\n");
n--;
}
System.out.println(out);
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | e786ffa709b06d82030e812775912306 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.*;
public class B {
public static void main(String[] args) throws IOException {
BufferedReader r;
r = new BufferedReader(new InputStreamReader(System.in));
//r = new BufferedReader(new FileReader(new File("in.txt")));
String[] line = r.readLine().split(" ");
int n = Integer.parseInt(line[0]);
int m = Integer.parseInt(line[1]);
int q = 0;
int c = (m + 1) / 2;
boolean j = m % 2 == 0;
int s = j ? 1 : -1;
int z = 1;
System.out.println(c);
for (int i = 1; i < n; i++) {
int d = c + s * z;
if (j ^ (s == 1))
z++;
s = -s;
if (d < 1 || d > m) {
d = c;
s = j ? 1 : -1;
z = 1;
}
System.out.println(d);
}
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | d3787b194192037aec5ce47f69537fbb | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class B {
public static void main(String[] args) throws Exception {
int n = nextInt(), m = nextInt();
int mid = (m + 1) / 2;
int cur = mid;
for (int i = 0; i < n; i++) {
out.println(cur);
if (cur == m) {
cur = mid;
} else {
if ((m & 1) == 1) {
if (cur < mid) {
cur = mid + (mid - cur);
} else {
cur = mid - (cur - mid) - 1;
}
} else {
if (cur <= mid) {
cur = mid + (mid - cur) + 1;
} else {
cur = mid - (cur - mid - 1) - 1;
}
}
}
}
out.flush();
}
// ///////////////////////////////////////////////////////////////
// IO
// ///////////////////////////////////////////////////////////////
private static StreamTokenizer in;
private static PrintWriter out;
private static BufferedReader inB;
private static boolean FILE = false;
private static int nextInt() throws Exception {
in.nextToken();
return (int) in.nval;
}
private static String nextString() throws Exception {
in.nextToken();
return in.sval;
}
static {
try {
out = new PrintWriter(FILE ? (new FileOutputStream("output.txt"))
: System.out);
inB = new BufferedReader(new InputStreamReader(
FILE ? new FileInputStream("input.txt") : System.in));
} catch (Exception e) {
e.printStackTrace();
}
in = new StreamTokenizer(inB);
}
// ///////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////
// pre - written
// ///////////////////////////////////////////////////////////////
private static void println(Object o) throws Exception {
out.println(o);
out.flush();
}
private static void exit(Object o) throws Exception {
println(o);
exit();
}
private static void exit() {
System.exit(0);
}
private static final int INF = Integer.MAX_VALUE;
private static final int MINF = Integer.MIN_VALUE;
// ////////////////////////////////////////////////////////////////
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 618df0d43015c68c96af3a3e74789173 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.PrintStream;
import java.util.Scanner;
/** Nov 10, 2012 **/
/**
* @author DOAN Minh Quy
* @email mquy.doan@gmail.com
*/
public class B195 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new B195().run();
}
void run(){
Scanner scanner = new Scanner(System.in);
PrintStream printer = new PrintStream(System.out);
int ballCount = scanner.nextInt();
int basketCount = scanner.nextInt();
while(ballCount>0){
int left = 0, right = 0;
if ( basketCount % 2 == 1){
int middle = (basketCount+1) >> 1;
--ballCount;
printer.println(middle);
left = middle; right = middle;
}else{
right = (basketCount) >> 1;
left = right+1;
}
while(true){
--left;++right;
if ( left < 1 ){
break;
}
if ( ballCount < 1 ){
break;
}
printer.println(left);
--ballCount;
if ( ballCount < 1 ){
break;
}
printer.println(right);
--ballCount;
}
}
scanner.close();
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 086522f42d57b3a271a647fc8ff58136 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
int n, m;
class Pair implements Comparable< Pair > {
int idx, balls;
public Pair( int i, int b ) {
idx = i;
balls = b;
}
public int compareTo( Pair p ) {
if ( balls != p.balls ) {
return ( balls - p.balls ) ;
}
int t = Math.abs( m + 1 - 2 * idx ) - Math.abs( m + 1 - 2 * p.idx );
if ( t != 0 ) {
return t ;
}
return idx - p.idx;
}
public String toString( ) {
return idx + " " + balls;
}
}
public void solve( ) throws Throwable {
n = in.nextInt( );
m = in.nextInt( );
int balls[ ] = new int[ m ], ans[ ] = new int[ n ];
PriorityQueue< Pair > pq = new PriorityQueue< Pair >( );
for ( int i = 0; i < m; ++i ) {
pq.add( new Pair( 1 + i, 0 ) );
}
for ( int i = 0; i < n; ++i ) {
//debug( pq );
Pair cur = pq.poll( );
ans[ i ] = cur.idx;
pq.add( new Pair( cur.idx, 1 + cur.balls ) );
}
for ( int x : ans ) {
out.println( x );
}
}
public void run( ) {
in = new FastScanner( System.in );
out = new PrintWriter( new PrintStream( System.out ), true );
try {
solve( );
out.close( );
System.exit( 0 );
} catch( Throwable e ) {
e.printStackTrace( );
System.exit( -1 );
}
}
public void debug( Object...os ) {
System.err.println( Arrays.deepToString( os ) );
}
public static void main( String[ ] args ) {
( new Main( ) ).run( );
}
private FastScanner in;
private PrintWriter out;
private static class FastScanner {
private int charsRead;
private int currentRead;
private byte buffer[ ] = new byte[ 0x1000 ];
private InputStream reader;
public FastScanner( InputStream in ) {
reader = in;
}
public int read( ) {
if ( charsRead == -1 ) {
throw new InputMismatchException( );
}
if ( currentRead >= charsRead ) {
currentRead = 0;
try {
charsRead = reader.read( buffer );
} catch( IOException e ) {
throw new InputMismatchException( );
}
if ( charsRead <= 0 ) {
return -1;
}
}
return buffer[ currentRead++ ];
}
public int nextInt( ) {
int c = read( );
while ( isWhitespace( c ) ) {
c = read( );
}
if ( c == -1 ) {
throw new NullPointerException( );
}
if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int sign = c == '-' ? -1 : 1;
if ( sign == -1 ) {
c = read( );
}
if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int ans = 0;
while ( !isWhitespace( c ) && c != -1 ) {
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10 + num;
c = read( );
}
return ans * sign;
}
public long nextLong( ) {
int c = read( );
while ( isWhitespace( c ) ) {
c = read( );
}
if ( c == -1 ) {
throw new NullPointerException( );
}
if ( c != '-' && !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int sign = c == '-' ? -1 : 1;
if ( sign == -1 ) {
c = read( );
}
if ( c == -1 || !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
long ans = 0;
while ( !isWhitespace( c ) && c != -1 ) {
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10 + num;
c = read( );
}
return ans * sign;
}
public boolean isWhitespace( int c ) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == -1;
}
public String next( ) {
int c = read( );
StringBuffer ans = new StringBuffer( );
while ( isWhitespace( c ) && c != -1 ) {
c = read( );
}
if ( c == -1 ) {
return null;
}
while ( !isWhitespace( c ) && c != -1 ) {
ans.appendCodePoint( c );
c = read( );
}
return ans.toString( );
}
public String nextLine( ) {
String ans = nextLine0( );
while ( ans.trim( ).length( ) == 0 ) {
ans = nextLine0( );
}
return ans;
}
private String nextLine0( ) {
int c = read( );
if ( c == -1 ) {
return null;
}
StringBuffer ans = new StringBuffer( );
while ( c != '\n' && c != '\r' && c != -1 ) {
ans.appendCodePoint( c );
c = read( );
}
return ans.toString( );
}
public double nextDouble( ) {
int c = read( );
while ( isWhitespace( c ) ) {
c = read( );
}
if ( c == -1 ) {
throw new NullPointerException( );
}
if ( c != '.' && c != '-' && !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int sign = c == '-' ? -1 : 1;
if ( c == '-' ) {
c = read( );
}
double ans = 0;
while ( c != -1 && c != '.' && !isWhitespace( c ) ) {
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10.0 + num;
c = read( );
}
if ( !isWhitespace( c ) && c != -1 && c != '.' ) {
throw new InputMismatchException( );
}
double pow10 = 1.0;
if ( c == '.' ) {
c = read( );
}
while ( !isWhitespace( c ) && c != -1 ) {
pow10 *= 10.0;
if ( !( c >= '0' && c <= '9' ) ) {
throw new InputMismatchException( );
}
int num = c - '0';
ans = ans * 10.0 + num;
c = read( );
}
return ans * sign / pow10;
}
}
}
| Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | 90ddc4f790e35153cb9d6b734e5ae7f0 | train_001.jsonl | 1339342200 | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has n balls and m baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to m, correspondingly. The balls are numbered with numbers from 1 to n.Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where i is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.For every ball print the number of the basket where it will go according to Valeric's scheme.Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.Map.Entry;
public class Main {
public static void main(String[] args) throws IOException {
(new Main()).solve();
}
public void Main() {
}
void solve() throws IOException {
// BufferedReader in = new BufferedReader(new
// InputStreamReader(System.in));
// Scanner in = new Scanner(System.in);
MyReader in = new MyReader();
PrintWriter out = new PrintWriter(System.out);
// Scanner in = new Scanner(new FileReader("input.txt"));
// PrintWriter out = new PrintWriter("output.txt");
int n = in.nextInt();
int m = in.nextInt();
int s = (m + 1) / 2;
int d = 0;
for (int i = 0; i < n; i++) {
if ((d + m)%2 == 0) {
s -= d;
} else {
s += d;
}
if (s < 1 || s > m) {
s = (m + 1) / 2;
d = 0;
}
d++;
out.println(s);
}
out.close();
}
};
class MyReader {
private BufferedReader in;
String[] parsed;
int index = 0;
public MyReader() {
in = new BufferedReader(new InputStreamReader(System.in));
}
public int nextInt() throws NumberFormatException, IOException {
if (parsed == null || parsed.length == index) {
read();
}
return Integer.parseInt(parsed[index++]);
}
public long nextLong() throws NumberFormatException, IOException {
if (parsed == null || parsed.length == index) {
read();
}
return Long.parseLong(parsed[index++]);
}
public String nextString() throws IOException {
if (parsed == null || parsed.length == index) {
read();
}
return parsed[index++];
}
private void read() throws IOException {
parsed = in.readLine().split(" ");
index = 0;
}
public String readLine() throws IOException {
return in.readLine();
}
}; | Java | ["4 3", "3 1"] | 2 seconds | ["2\n1\n3\n2", "1\n1\n1"] | null | Java 6 | standard input | [
"data structures",
"implementation",
"math"
] | 907893a0a78a7444d26bdd793d9c7499 | The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of balls and baskets, correspondingly. | 1,300 | Print n numbers, one per line. The i-th line must contain the number of the basket for the i-th ball. | standard output | |
PASSED | ac06c4f1aaac56370240afff2475141d | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.util.*;
public class S {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
int n=sc.nextInt();
int a[]=new int[n];
for(int j=0;j<n;j++)
{
a[j]=sc.nextInt();
}
Arrays.sort(a);
int g=1;
int s=1;
for(int j=0;j<n;j++)
{
if(a[j]<=g)
{
g++;
s=g;
}
else if(s<a[j])
{
s++;
}
else if(s>=a[j])
{
s++;
g=s;
}
}
System.out.println(g);
}
}
}
| Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | 17203a61e598d0cb53c1d5864c6a7c24 | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.util.*;
public class T
{
public static void main(String[] args) {
int loop;
// System.out.println("1st");
Scanner sc = new Scanner(System.in);
loop = sc.nextInt();
ArrayList<Integer> ans=new ArrayList<Integer>();
while(loop>0){//System.out.println("2t");
int k=sc.nextInt();
ArrayList<Integer> a=new ArrayList<Integer>();
while(k>0){
int n = sc.nextInt();
a.add(n);k--;
}
Collections.sort(a);
int output=1;int j=-1;
for(int i=0;i<a.size();i++){
if(output>=a.get(i)){output++;
if(j!=-1)j=-1;}else {
if(output+a.size()-i-1>=a.get(i)){
if(j==-1)j=output;
output++;
}else{
break;
}
}
}
if(j!=-1&&output!=a.size()+1)ans.add(j);
else ans.add(output);
loop--;
}
sc.close();
for(Integer i:ans)System.out.println(i);
}
}
| Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | dd3ca7b6fa056d8cf8a3275b493af322 | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.util.* ;
public class SelfIsolation{
public static void main(String []args){
Scanner sc1 = new Scanner(System.in);
int t = sc1.nextInt();
while(t>0) {
int n = sc1.nextInt();
int array[] = new int[n] ;
for(int i=0; i<n; i++){
array[i] = sc1.nextInt();
}
Arrays.sort(array);
int sum = 0 ;
for(int i=1; i<=array.length; i++) {
if(array[i-1]<=i) {
sum = i ;
}
else {
continue ;
}
}
System.out.println(sum+1);
t-- ;
}
}
} | Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | 87e52ee3edd042e7fcf0f4650834692c | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void solve(InputReader in) {
int n = in.readInt();
int a[] = new int[n];
for(int i = 0; i<n; i++)
a[i] = in.readInt();
Arrays.sort(a);
for(int i = n-1; i>= 0; i--) {
if(a[i] <= i+1) {
System.out.println(i+2);
return;
}
}
System.out.println(1);
}
public static void main(String ...strings) {
InputReader in = new InputReader(System.in);
int t = in.readInt();
while(t-- > 0) {
solve(in);
}
}
}
class InputReader{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long readLong() {
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 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 | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | 44588e51bac57e95b0186f3322a7fcd4 | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void solve(InputReader in) {
int n = in.readInt();
int a[] = new int[n];
for(int i = 0; i<n; i++)
a[i] = in.readInt();
Arrays.sort(a);
int ans1 = 1;
for(int i = 0; i<n; i++) {
if(a[i] <= ans1) {
ans1++;
}
}
int ans2 = 1;
for(int i = n-1; i>= 0; i--) {
if(a[i] <= i+1) {
ans2 = i+2;
break;
}
}
System.out.println(Math.max(ans1, ans2));
}
public static void main(String ...strings) {
InputReader in = new InputReader(System.in);
int t = in.readInt();
while(t-- > 0) {
solve(in);
}
}
}
class InputReader{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long readLong() {
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 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 | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | 2f60f5ca3a5d7d52e0a880768697bed5 | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Tanzim Ibn Patowary
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, Scanner in, PrintWriter out) {
for (int test = 0; test < testNumber; test++) {
int tar = in.nextInt();
while (tar-- != 0) {
int n = in.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
Arrays.sort(a);
int sim = 1, go = 0;
for (int i = 0; i < n; i++) {
if (a[i] <= sim + go) {
sim = sim + go + 1;
go = 0;
} else {
go++;
}
}
out.println(sim);
}
}
}
}
}
| Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | afbe4a4dd740daa3ecccc79b6ad091c5 | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Train {
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
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());
}
}
static InputReader kb = new InputReader(System.in);
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) {
int n= kb.nextInt();
while(n>0){
int c = kb.nextInt();
int[] a = new int[c];
for(int i=0;i<c;i++){
a[i]=kb.nextInt();
}
Arrays.sort(a);
int res=0;
for(int i=1;i<=c;i++){
if(a[i-1]<=i){
res=i;
//System.out.println(a[i-1]);
}
}
System.out.println(res+1);
n--;
}
}
}
| Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | d3b893190ac2030b3088a2ce78fcc4be | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.util.*;
public class C645_PB {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int t=in.nextInt();
while(t-->0)
{
int n = in.nextInt();
int[] a = new int[n];
for(int i =0;i<n;i++)
a[i] = in.nextInt();
Arrays.sort(a);
int max = 0;
for(int i =0;i<n;i++)
{
//System.out.println(i+1+" "+a[i]);
if(i+1>=a[i])
max = i+1;
}
System.out.println(max+1);
}
}
}
| Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | 5ee480b89f81cb118c96e0f1fa94018c | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.util.*;
public class Solution
{
public static void main(String ar[])
{
Scanner scan=new Scanner(System.in);
int test=scan.nextInt();
while(test-->0)
{
int n=scan.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=scan.nextInt();
Arrays.sort(arr);
int count=1;
int x=0;
for(int i=0;i<n;i++)
{
if(count+x>=arr[i])
{ count+=x+1;
x=0;
}
else
x++;
}
System.out.println(count);
}
}
} | Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | babdddb2ec7719ec7b1762317dda6373 | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.time.LocalTime;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.math.*;
public class A {
public static void main(String[] args) {
FastScanner scan = new FastScanner();
int t = scan.nextInt();
for(int tt=0; tt<t; tt++) {
int n = scan.nextInt();
int [] arr = scan.readArray(n);
int count = 1;
sort(arr);
for(int i=n-1; i>=0; i--) {
if(arr[i] <= i+1) {
count = i+2;
break;
}
}
System.out.println(count);
}
}
public static void sort(int [] a) {
ArrayList<Integer> b = new ArrayList<>();
for(int i: a) b.add(i);
Collections.sort(b);
for(int i=0; i<a.length; i++) a[i]= b.get(i);
}
static class FastScanner{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while(!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e){
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int [] readArray(int n) {
int [] a = new int[n];
for(int i=0; i<n ; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | 54dd0713f3b383732153a1edcf9e04ea | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class Gd {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n =sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
int flag=0;
outer:for(int j=n-1;j>=0;j--)
{
if(arr[j]<=j+1)
{
flag=1;
System.out.println(j+2);
break outer;
}
}
if(flag==0)
{
System.out.println(1);
}
}
}
}
| Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | f1579216c2108c4f6de07834f5d070f4 | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class r645b {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int t=scan.nextInt();
for(int tt=0;tt<t;tt++) {
int n=scan.nextInt();
ArrayList<Integer> temp=new ArrayList<>();
int[] a=new int[n];
for(int i=0;i<n;i++) {
temp.add(scan.nextInt());
}
Collections.sort(temp);
for(int i=0;i<n;i++) a[i]=temp.get(i);
int g=1;
int id=0;
while(id<a.length) {
int see=g-1;
while(id<a.length) {
see++;
if(see>=a[id]) {
g+=see-(g-1);
id++;
break;
}
else id++;
}
}
System.out.println(g);
}
}
} | Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | 2ce3cefad5343c3e5dfafa9ab4dee9b7 | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Collections;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author sumit
*/
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);
BMariaBreaksTheSelfIsolation solver = new BMariaBreaksTheSelfIsolation();
solver.solve(1, in, out);
out.close();
}
static class BMariaBreaksTheSelfIsolation {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
int[] arr = in.nextIntArray(n);
ArrayList<Integer> list = new ArrayList();
for (int i = 0; i < n; i++) {
list.add(arr[i]);
}
Collections.sort(list);
int ans = 0;
for (int i = n - 1; i >= 0; i--) {
if (list.get(i) <= (i + 1)) {
ans = i + 1;
break;
}
}
out.printLine(ans + 1);
}
}
}
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() {
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 & 15;
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int arraySize) {
int array[] = new int[arraySize];
for (int i = 0; i < arraySize; i++)
array[i] = nextInt();
return array;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
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 close() {
writer.close();
}
public void printLine(int i) {
writer.println(i);
}
}
}
| Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | afe004bfdae3aa635ebc2114d458b85b | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.valueOf(reader.readLine());
while((t--) != 0) {
int n = Integer.valueOf(reader.readLine());
int[] info = new int[n];
String[] tmp = reader.readLine().split(" ");
for(int i=0; i<n; i++) {
info[i] = Integer.valueOf(tmp[i]);
}
Arrays.sort(info);
int i = n - 1;
for(i=n-1; i>=0; i--) {
if(info[i] > i+1) {
// 不来
} else {
break;
}
}
System.out.println(i+2);
}
}
} | Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | acd16d511aa36f27bd03d5b53881fdbf | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class Main{
public static void main(String[] args) throws Exception {
IO io = new IO();
PrintWriter out = new PrintWriter(System.out);
Solver sr = new Solver();
sr.solve(io,out);
out.flush();
out.close();
}
static class Solver
{
class Pair{
int granny, freq;
public Pair(int a, int b){
granny = a;
freq = b;
}
}
void solve(IO io, PrintWriter out)
{
int t = io.nextInt();
//int t = 1;
while(t-->0)
{
int n = io.nextInt();
int[] ar = new int[n];
for(int i=0 ; i<n ; i++)
ar[i] = io.nextInt();
Arrays.sort(ar);
ArrayList<Pair> list = new ArrayList<>();
int tmp=ar[0], count=1;
for(int i=1 ; i<n ; i++){
if(tmp==ar[i])
count++;
else{
//out.println(ar[i-1]+" "+count);
list.add(new Pair(ar[i-1],count));
count=1;
tmp=ar[i];
}
}
list.add(new Pair(ar[n-1],count));
Collections.sort(list, new Comparator<Pair>(){
public int compare(Pair a, Pair b){
return a.granny-b.granny;
}
});
// for(Pair p : list)
// out.println(p.granny+" "+p.freq);
tmp=1; int ans=1;
for(int i=0 ; i<list.size() ; i++){
tmp+=list.get(i).freq;
if(tmp-1>=list.get(i).granny)
ans=tmp;
}
out.println(ans);
}
}
}
//Special thanks to Petr (on codeforces) who inspired me to implement IO class!
static class IO
{
private static final int BUF_SIZE = 2048;
BufferedReader br;
private IO() {
br = new BufferedReader(new InputStreamReader(System.in));
}
private boolean isSpace(char c) {
return c == '\n' || c == '\r' || c == ' ';
}
String next() {
try {
StringBuilder sb = new StringBuilder();
int r;
while ((r = br.read()) != -1 && isSpace((char) r));
if (r == -1) {
return null;
}
sb.append((char) r);
while ((r = br.read()) != -1 && !isSpace((char) r)) {
sb.append((char) r);
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | ba4f8453a174d42956705e689485522c | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 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.StringTokenizer;
public class Main {
public static void main(String args[]) throws Exception {
MyScanner sc = new MyScanner();
PrintWriter pr = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int ar[] = new int[n];
for (int i = 0; i < n; i++)
ar[i] = sc.nextInt();
Arrays.parallelSort(ar);
int count = 1;
for(int i = 0; i < n; i++) {
if(ar[i] <= count)
count++;
else {
int tempcount = count;
int j = i;
boolean flag = false;
for(j = i; j < n; j++) {
if(tempcount < ar[j])
tempcount++;
else {
//System.out.println(1);
flag = true;
tempcount++;
break;
}
}
if(!flag)
break;
else {
i = j;
count = tempcount;
}
}
}
pr.println(count);
}
pr.close();
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | 2dff2c3e1fbbdcf9771ee369c50243f8 | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class my{
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];
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
ans[i] = i + 1;
}
Arrays.sort(arr);
int i = 0;
int val = 0;
for (i = 0; i < n; i++) {
if (arr[i] <= ans[i]) {
val = ans[i];
}
}
System.out.println(val + 1);
}
}
} | Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | e8e74e04e93a9cd14443c102652a2d61 | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.util.*;
import java.io.*;
public class Main
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(System.out);
int t=Integer.parseInt(br.readLine());
for(int x=0;x<t;x++)
{
int n=Integer.parseInt(br.readLine());
String str[]=br.readLine().split(" ");
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=Integer.parseInt(str[i]);
Arrays.sort(arr);
int cntr=1,rem=0;
for(int i=0;i<n;i++)
{
if(arr[i]<=cntr)
cntr++;
else if(cntr+rem>=arr[i])
{
cntr+=rem+1;
rem=0;
}
else
rem++;
}
pw.println(cntr);
}
pw.flush();
pw.close();
}
} | Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | 3d7958042fb410338cb3b11101a79972 | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main1 {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer = null;
int totalCases, testnum;
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws IOException {
new Main1().execute();
}
void debug(Object... os) {
System.out.println(Arrays.deepToString(os));
}
int ni() throws IOException {
return Integer.parseInt(ns());
}
long nl() throws IOException {
return Long.parseLong(ns());
}
double nd() throws IOException {
return Double.parseDouble(ns());
}
String ns() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(br.readLine());
}
return tokenizer.nextToken();
}
String nline() throws IOException {
tokenizer = null;
return br.readLine();
}
void execute() throws IOException {
totalCases = ni();
for (testnum = 1; testnum <= totalCases; testnum++) {
if (!input()) {
break;
}
solve();
}
out.close();
}
void solve() throws IOException {
int num = ni();
int[] arr = new int[num];
for (int j = 0; j < num; j++) {
arr[j] = ni();
}
Arrays.sort(arr);
for (int k = num - 1; k >= 0; k--) {
if (k + 1 >= arr[k]) {
out.println(k + 2);
return;
}
}
out.println(1);
}
boolean input() throws IOException {
return true;
}
}
| Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | 87abf343fb2e8dd727a31961af6ed7eb | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 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.Arrays;
import java.util.StringTokenizer;
public class Test2 {
static FastScanner sc = new FastScanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
int cnt = sc.nextInt();
for (int i = 0; i < cnt; i++) {
solve();
}
out.close();
}
private static void solve() {
int num = sc.nextInt();
int[] arr = new int[num];
for (int j = 0; j < num; j++) {
arr[j] = sc.nextInt();
}
Arrays.sort(arr);
for (int k = num - 1; k >= 0; k--) {
if (k + 1 >= arr[k]) {
out.println(k + 2);
return;
}
}
out.println(1);
}
static class FastScanner {
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
public String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | 43f199e98d6c7bd0cbfa628e2fc9ebc9 | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | //package Round;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class B {
InputStream is;
PrintWriter out;
String INPUT ="";
void solve()
{
for(int T = ni();T > 0;T--)go();
}
void go()
{
int n = ni();
int[] a = na(n);
Arrays.sort(a);
int c =1;
for(int i=0;i<a.length;i++){
if(a[i]<=c) c++;
else {
int j=i;
int sum = c;
while(j<n && a[j] > sum){
sum++;
j++;
}
if(j==n && a[j-1]>=sum){
out.println(c);
return;
}else{
i=j-1;
c=sum;
}
}
}
out.println(c);
}
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 B().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)); }
}
//Template by Uwi | Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | 22ae632687031ebc4bd13af4ddd61559 | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author @maxximus
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int t, n, m, b, c, x, y;
t = in.nextInt();
for (int i = 0; i < t; i++) {
n = in.nextInt();
int[] a = na(n, in);
Arrays.sort(a);
boolean flag = false;
for (int j = n - 1; j >= 0; j--) {
if (j + 1 >= a[j]) {
out.println(j + 2);
flag = true;
break;
}
}
if (!flag) out.println(1);
}
}
private int[] na(int n, Scanner in) {
int[] r = new int[n];
for (int i = 0; i < n; i++) {
r[i] = in.nextInt();
}
return r;
}
}
}
| Java | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output | |
PASSED | 3f94c0b1f14576b114b55a44d00751ae | train_001.jsonl | 1590503700 | Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$.Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 \le 2$$$ and $$$a_5=1 \le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 \le 5$$$, $$$a_3=4 \le 5$$$ and $$$a_4=5 \le 5$$$); the $$$6$$$-th granny cannot be called into the yard — therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). | 256 megabytes | import java.util.*;
import java.io.*;
public class maria
{
public static void main(String []Args)throws Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int arr[]=new int[n+1];
arr[0]=1;
for(int i=1;i<=n;i++){
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
int ans=1;
for(int i=n;i>=0;i--){
if(arr[i]<=i){
ans=i+1;
break;
}
}
System.out.println(ans);
}
}
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 | ["4\n5\n1 1 2 2 1\n6\n2 3 4 5 6 7\n6\n1 5 4 5 1 9\n5\n1 2 3 5 6"] | 2 seconds | ["6\n1\n6\n4"] | NoteIn the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.In the second test case in the example, no one can be in the yard, so Maria will remain there alone.The third test case in the example is described in the details above.In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total. | Java 8 | standard input | [
"sortings",
"greedy"
] | 718cea81f609055cece58cae5310f703 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 2\cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. | 1,000 | For each test case, print a single integer $$$k$$$ ($$$1 \le k \le n + 1$$$) — the maximum possible number of grannies in the courtyard. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.