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
sequencelengths 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 | 07d10643c7c1176ef4d6c1e27a8a6d97 | train_000.jsonl | 1532938500 | Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlasting city construction. From the window in your room, you see the sequence of n hills, where i-th of them has height ai. The Innopolis administration wants to build some houses on the hills. However, for the sake of city appearance, a house can be only built on the hill, which is strictly higher than neighbouring hills (if they are present). For example, if the sequence of heights is 5, 4, 6, 2, then houses could be built on hills with heights 5 and 6 only.The Innopolis administration has an excavator, that can decrease the height of an arbitrary hill by one in one hour. The excavator can only work on one hill at a time. It is allowed to decrease hills up to zero height, or even to negative values. Increasing height of any hill is impossible. The city administration wants to build k houses, so there must be at least k hills that satisfy the condition above. What is the minimum time required to adjust the hills to achieve the administration's plan?However, the exact value of k is not yet determined, so could you please calculate answers for all k in range ? Here denotes n divided by two, rounded up. | 512 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
private static final long INF = 1234567890987654321L;
public static void solve(FastIO io) {
int N = io.nextInt();
int K = (N + 1) >> 1;
long[] A = new long[N + 2];
for (int i = 1; i <= N; ++i) {
A[i] = io.nextLong();
}
A[0] = A[N + 1] = -INF;
long[] dp = new long[N + 2];
for (int i = 1; i <= K; ++i) {
long[] next = new long[N + 2];
Arrays.fill(next, INF);
long prevBestNoExcav = INF;
if (i == 1) {
prevBestNoExcav = 0;
}
long bestAns = INF;
for (int j = 1; j <= N; ++j) {
// construct house with j-3 or before, assuming j-1 is not yet excavated
if (j >= 3) {
prevBestNoExcav = Math.min(prevBestNoExcav, dp[j - 3]);
}
next[j] = Math.min(next[j], prevBestNoExcav + getTrim(A[j], A[j - 1]) + getTrim(A[j], A[j + 1]));
if (i > 1 && j >= 3) {
long prev = Math.min(A[j - 2] - 1, A[j - 1]);
next[j] = Math.min(next[j], dp[j - 2] + getTrim(A[j], prev) + getTrim(A[j], A[j + 1]));
}
bestAns = Math.min(bestAns, next[j]);
}
io.printf(bestAns + " ");
dp = next;
}
io.println();
// for (int i = 1; i <= K; ++i) {
// long best = INF;
// for (int j = 1; j <= N; ++j) {
// best = Math.min(best, dp[i][j]);
// }
// io.print(best + " ");
// }
// io.println();
}
private static long get(long[] dp, int i, int j) {
if (i == 0) {
return 0;
}
if (j < 0 || j >= dp.length) {
return INF;
}
return dp[j];
}
private static long getTrim(long ht, long neighbor) {
if (neighbor < ht) {
return 0;
}
return neighbor - (ht - 1);
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
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 String nextString() {
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 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 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 int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private 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 void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.flush();
}
} | Java | ["5\n1 1 1 1 1", "3\n1 2 3", "5\n1 2 3 2 2"] | 1 second | ["1 2 2", "0 2", "0 1 3"] | NoteIn the first example, to get at least one hill suitable for construction, one can decrease the second hill by one in one hour, then the sequence of heights becomes 1, 0, 1, 1, 1 and the first hill becomes suitable for construction.In the first example, to get at least two or at least three suitable hills, one can decrease the second and the fourth hills, then the sequence of heights becomes 1, 0, 1, 0, 1, and hills 1, 3, 5 become suitable for construction. | Java 8 | standard input | [
"dp"
] | 9534b468bfb6126fc16b896532ced8c5 | The first line of input contains the only integer n (1 ≤ n ≤ 5000)—the number of the hills in the sequence. Second line contains n integers ai (1 ≤ ai ≤ 100 000)—the heights of the hills in the sequence. | 1,900 | Print exactly numbers separated by spaces. The i-th printed number should be equal to the minimum number of hours required to level hills so it becomes possible to build i houses. | standard output | |
PASSED | f449d08ea0d2e1d468552432a10c8966 | train_000.jsonl | 1532938500 | Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlasting city construction. From the window in your room, you see the sequence of n hills, where i-th of them has height ai. The Innopolis administration wants to build some houses on the hills. However, for the sake of city appearance, a house can be only built on the hill, which is strictly higher than neighbouring hills (if they are present). For example, if the sequence of heights is 5, 4, 6, 2, then houses could be built on hills with heights 5 and 6 only.The Innopolis administration has an excavator, that can decrease the height of an arbitrary hill by one in one hour. The excavator can only work on one hill at a time. It is allowed to decrease hills up to zero height, or even to negative values. Increasing height of any hill is impossible. The city administration wants to build k houses, so there must be at least k hills that satisfy the condition above. What is the minimum time required to adjust the hills to achieve the administration's plan?However, the exact value of k is not yet determined, so could you please calculate answers for all k in range ? Here denotes n divided by two, rounded up. | 512 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
private static final long INF = 1234567890987654321L;
public static void solve(FastIO io) {
int N = io.nextInt();
int K = (N + 1) >> 1;
long[] A = new long[N + 2];
for (int i = 1; i <= N; ++i) {
A[i] = io.nextLong();
}
A[0] = A[N + 1] = -INF;
long[][] dp = new long[K + 1][N + 2];
for (int i = 1; i <= K; ++i) {
Arrays.fill(dp[i], INF);
}
for (int i = 1; i <= K; ++i) {
long prevBestNoExcav = INF;
for (int j = 1; j <= N; ++j) {
// construct house with j-3 or before, assuming j-1 is not yet excavated
prevBestNoExcav = Math.min(prevBestNoExcav, get(dp, i - 1, j - 3));
dp[i][j] = Math.min(dp[i][j], prevBestNoExcav + getTrim(A[j], A[j - 1]) + getTrim(A[j], A[j + 1]));
// construct house with j-2, assuming j-1 is excavated already
if (i > 1 && j >= 3) {
long prev = Math.min(A[j - 2] - 1, A[j - 1]);
dp[i][j] = Math.min(dp[i][j], get(dp, i - 1, j - 2) + getTrim(A[j], prev) + getTrim(A[j], A[j + 1]));
}
// skip construction
// dp[i][j] = Math.min(dp[i][j], dp[i][j - 1]);
}
// System.out.format("i = %d, dp[i] = %s\n", i, Arrays.toString(dp[i]));
}
for (int i = 1; i <= K; ++i) {
long best = INF;
for (int j = 1; j <= N; ++j) {
best = Math.min(best, dp[i][j]);
}
io.print(best + " ");
}
io.println();
}
private static long get(long[][] dp, int i, int j) {
if (i == 0) {
return 0;
}
if (j < 0 || j >= dp[i].length) {
return INF;
}
return dp[i][j];
}
private static long getTrim(long ht, long neighbor) {
if (neighbor < ht) {
return 0;
}
return neighbor - (ht - 1);
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
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 String nextString() {
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 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 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 int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private 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 void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
solve(io);
io.flush();
}
} | Java | ["5\n1 1 1 1 1", "3\n1 2 3", "5\n1 2 3 2 2"] | 1 second | ["1 2 2", "0 2", "0 1 3"] | NoteIn the first example, to get at least one hill suitable for construction, one can decrease the second hill by one in one hour, then the sequence of heights becomes 1, 0, 1, 1, 1 and the first hill becomes suitable for construction.In the first example, to get at least two or at least three suitable hills, one can decrease the second and the fourth hills, then the sequence of heights becomes 1, 0, 1, 0, 1, and hills 1, 3, 5 become suitable for construction. | Java 8 | standard input | [
"dp"
] | 9534b468bfb6126fc16b896532ced8c5 | The first line of input contains the only integer n (1 ≤ n ≤ 5000)—the number of the hills in the sequence. Second line contains n integers ai (1 ≤ ai ≤ 100 000)—the heights of the hills in the sequence. | 1,900 | Print exactly numbers separated by spaces. The i-th printed number should be equal to the minimum number of hours required to level hills so it becomes possible to build i houses. | standard output | |
PASSED | 1c75d3b9492a4dde7811fa2091c3e29d | train_000.jsonl | 1532938500 | Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlasting city construction. From the window in your room, you see the sequence of n hills, where i-th of them has height ai. The Innopolis administration wants to build some houses on the hills. However, for the sake of city appearance, a house can be only built on the hill, which is strictly higher than neighbouring hills (if they are present). For example, if the sequence of heights is 5, 4, 6, 2, then houses could be built on hills with heights 5 and 6 only.The Innopolis administration has an excavator, that can decrease the height of an arbitrary hill by one in one hour. The excavator can only work on one hill at a time. It is allowed to decrease hills up to zero height, or even to negative values. Increasing height of any hill is impossible. The city administration wants to build k houses, so there must be at least k hills that satisfy the condition above. What is the minimum time required to adjust the hills to achieve the administration's plan?However, the exact value of k is not yet determined, so could you please calculate answers for all k in range ? Here denotes n divided by two, rounded up. | 512 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
import java.util.stream.*;
public class C {
static final int INF = Integer.MAX_VALUE / 3;
int[] fast(int[] a) {
int n = a.length;
int[][] dp = new int[3][2];
for (int[] row : dp) {
Arrays.fill(row, INF);
}
// 0 - last, 1 - prelast, 2 - far
dp[0][1] = 0;
dp[2][0] = 0;
for (int idx = 1; idx < n; idx++) {
int max = idx / 2 + 1;
int[][] nxt = new int[3][max + 1];
for (int[] row : nxt) {
Arrays.fill(row, INF);
}
for (int pS = 0; pS < 3; pS++) {
for (int pB = 0; pB < dp[0].length; pB++) {
if (dp[pS][pB] == INF) {
continue;
}
// build
if (pS != 0) {
int cost;
if (pS == 1) {
int already = Math.min(a[idx - 2] - 1, a[idx - 1]);
cost = Math.max(already - (a[idx] - 1), 0);
} else {
cost = Math.max(a[idx - 1] - (a[idx] - 1), 0);
}
nxt[0][pB + 1] = Math.min(nxt[0][pB + 1], dp[pS][pB]
+ cost);
}
// skip
{
int cost;
if (pS == 0) {
cost = Math.max(a[idx] - (a[idx - 1] - 1), 0);
} else {
cost = 0;
}
int nS = Math.min(pS + 1, 2);
nxt[nS][pB] = Math.min(nxt[nS][pB], dp[pS][pB] + cost);
}
}
}
dp = nxt;
// System.err.println(Arrays.deepToString(dp));
}
int[] ret = new int[(n + 1) / 2 + 1];
Arrays.fill(ret, INF);
for (int i = 0; i < ret.length; i++) {
for (int j = 0; j < 3; j++) {
ret[i] = Math.min(ret[i], dp[j][i]);
}
}
return ret;
}
void submit() {
int n = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
int[] ret = fast(a);
for (int i = 1; i < ret.length; i++) {
out.print(ret[i] + " ");
}
out.println();
}
void test() {
int n = 5000;
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = rand(1, 100000);
}
out.println(Arrays.toString(fast(a)));
}
void stress() {
for (int tst = 0;; tst++) {
if (false) {
throw new AssertionError();
}
System.err.println(tst);
}
}
C() throws IOException {
is = System.in;
out = new PrintWriter(System.out);
submit();
// stress();
// test();
out.close();
}
static final Random rng = new Random();
static final int C = 5;
static int rand(int l, int r) {
return l + rng.nextInt(r - l + 1);
}
public static void main(String[] args) throws IOException {
new C();
}
private InputStream is;
PrintWriter out;
private byte[] buf = new byte[1 << 14];
private int bufSz = 0, bufPtr = 0;
private int readByte() {
if (bufSz == -1)
throw new RuntimeException("Reading past EOF");
if (bufPtr >= bufSz) {
bufPtr = 0;
try {
bufSz = is.read(buf);
} catch (IOException e) {
throw new RuntimeException(e);
}
if (bufSz <= 0)
return -1;
}
return buf[bufPtr++];
}
private boolean isTrash(int c) {
return c < 33 || c > 126;
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isTrash(b))
;
return b;
}
String nextToken() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!isTrash(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
String nextString() {
int b = readByte();
StringBuilder sb = new StringBuilder();
while (!isTrash(b) || b == ' ') {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
char nextChar() {
return (char) skip();
}
int nextInt() {
int ret = 0;
int b = skip();
if (b != '-' && (b < '0' || b > '9')) {
throw new InputMismatchException();
}
boolean neg = false;
if (b == '-') {
neg = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
ret = ret * 10 + (b - '0');
} else {
if (b != -1 && !isTrash(b)) {
throw new InputMismatchException();
}
return neg ? -ret : ret;
}
b = readByte();
}
}
long nextLong() {
long ret = 0;
int b = skip();
if (b != '-' && (b < '0' || b > '9')) {
throw new InputMismatchException();
}
boolean neg = false;
if (b == '-') {
neg = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
ret = ret * 10 + (b - '0');
} else {
if (b != -1 && !isTrash(b)) {
throw new InputMismatchException();
}
return neg ? -ret : ret;
}
b = readByte();
}
}
}
| Java | ["5\n1 1 1 1 1", "3\n1 2 3", "5\n1 2 3 2 2"] | 1 second | ["1 2 2", "0 2", "0 1 3"] | NoteIn the first example, to get at least one hill suitable for construction, one can decrease the second hill by one in one hour, then the sequence of heights becomes 1, 0, 1, 1, 1 and the first hill becomes suitable for construction.In the first example, to get at least two or at least three suitable hills, one can decrease the second and the fourth hills, then the sequence of heights becomes 1, 0, 1, 0, 1, and hills 1, 3, 5 become suitable for construction. | Java 8 | standard input | [
"dp"
] | 9534b468bfb6126fc16b896532ced8c5 | The first line of input contains the only integer n (1 ≤ n ≤ 5000)—the number of the hills in the sequence. Second line contains n integers ai (1 ≤ ai ≤ 100 000)—the heights of the hills in the sequence. | 1,900 | Print exactly numbers separated by spaces. The i-th printed number should be equal to the minimum number of hours required to level hills so it becomes possible to build i houses. | standard output | |
PASSED | dfbc7d39061bce45949e41c5cd28ef59 | train_000.jsonl | 1532938500 | Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlasting city construction. From the window in your room, you see the sequence of n hills, where i-th of them has height ai. The Innopolis administration wants to build some houses on the hills. However, for the sake of city appearance, a house can be only built on the hill, which is strictly higher than neighbouring hills (if they are present). For example, if the sequence of heights is 5, 4, 6, 2, then houses could be built on hills with heights 5 and 6 only.The Innopolis administration has an excavator, that can decrease the height of an arbitrary hill by one in one hour. The excavator can only work on one hill at a time. It is allowed to decrease hills up to zero height, or even to negative values. Increasing height of any hill is impossible. The city administration wants to build k houses, so there must be at least k hills that satisfy the condition above. What is the minimum time required to adjust the hills to achieve the administration's plan?However, the exact value of k is not yet determined, so could you please calculate answers for all k in range ? Here denotes n divided by two, rounded up. | 512 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
/**
* @author khokharnikunj8
*/
public class Main {
public static void main(String[] args) {
new Thread(null, new Runnable() {
public void run() {
new Main().solve();
}
}, "1", 1 << 26).start();
}
void solve() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CHills solver = new CHills();
solver.solve(1, in, out);
out.close();
}
static class CHills {
public void solve(int testNumber, ScanReader in, PrintWriter out) {
int n = in.scanInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = in.scanInt();
int[][][] dp = new int[2][(n / 2 + (n % 2)) + 1][2];
for (int i = 0; i <= (n / 2 + (n % 2)); i++) Arrays.fill(dp[0][i], 1000000000);
int[][] min = new int[2][(n / 2 + (n % 2)) + 1];
dp[0][0][0] = 0;
dp[0][1][1] = 0;
Arrays.fill(min[0], a[0]);
for (int i = 1; i < n; i++) {
for (int j = 0; j < (n / 2 + (n % 2)) + 1; j++) {
dp[i & 1][j][0] = 1000000000;
dp[i & 1][j][1] = 1000000000;
min[i & 1][j] = a[i];
//zero
if (dp[i & 1][j][0] > dp[(i - 1) & 1][j][0]) dp[i & 1][j][0] = dp[(i - 1) & 1][j][0];
if (dp[i & 1][j][0] >= dp[(i - 1) & 1][j][1] + (Math.max(0, a[i] - a[i - 1] + 1))) {
dp[i & 1][j][0] = dp[(i - 1) & 1][j][1] + (Math.max(0, a[i] - a[i - 1] + 1));
min[i & 1][j] = Math.min(min[i & 1][j], a[i - 1] - 1);
}
//one
if (j - 1 >= 0 && dp[i & 1][j][1] >= dp[(i - 1) & 1][j - 1][0] + (Math.max(0, min[(i - 1) & 1][j - 1] - a[i] + 1)))
dp[i & 1][j][1] = dp[(i - 1) & 1][j - 1][0] + (Math.max(0, min[(i - 1) & 1][j - 1] - a[i] + 1));
}
}
for (int i = 1; i <= (n / 2) + (n % 2); i++)
out.print(Math.min(dp[(n - 1) & 1][i][0], dp[(n - 1) & 1][i][1]) + " ");
}
}
static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (total <= 0) return -1;
}
return buf[index++];
}
public int scanInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
}
}
| Java | ["5\n1 1 1 1 1", "3\n1 2 3", "5\n1 2 3 2 2"] | 1 second | ["1 2 2", "0 2", "0 1 3"] | NoteIn the first example, to get at least one hill suitable for construction, one can decrease the second hill by one in one hour, then the sequence of heights becomes 1, 0, 1, 1, 1 and the first hill becomes suitable for construction.In the first example, to get at least two or at least three suitable hills, one can decrease the second and the fourth hills, then the sequence of heights becomes 1, 0, 1, 0, 1, and hills 1, 3, 5 become suitable for construction. | Java 8 | standard input | [
"dp"
] | 9534b468bfb6126fc16b896532ced8c5 | The first line of input contains the only integer n (1 ≤ n ≤ 5000)—the number of the hills in the sequence. Second line contains n integers ai (1 ≤ ai ≤ 100 000)—the heights of the hills in the sequence. | 1,900 | Print exactly numbers separated by spaces. The i-th printed number should be equal to the minimum number of hours required to level hills so it becomes possible to build i houses. | standard output | |
PASSED | b748562e15512b77e391d849d3ada3f4 | train_000.jsonl | 1532938500 | Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlasting city construction. From the window in your room, you see the sequence of n hills, where i-th of them has height ai. The Innopolis administration wants to build some houses on the hills. However, for the sake of city appearance, a house can be only built on the hill, which is strictly higher than neighbouring hills (if they are present). For example, if the sequence of heights is 5, 4, 6, 2, then houses could be built on hills with heights 5 and 6 only.The Innopolis administration has an excavator, that can decrease the height of an arbitrary hill by one in one hour. The excavator can only work on one hill at a time. It is allowed to decrease hills up to zero height, or even to negative values. Increasing height of any hill is impossible. The city administration wants to build k houses, so there must be at least k hills that satisfy the condition above. What is the minimum time required to adjust the hills to achieve the administration's plan?However, the exact value of k is not yet determined, so could you please calculate answers for all k in range ? Here denotes n divided by two, rounded up. | 512 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Solveaproblem {
private static int MAX = Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
StringTokenizer sToken = new StringTokenizer(reader.readLine());
int[] arr = new int[n+2];
for (int i=1; i<=n; i++) arr[i] = Integer.parseInt(sToken.nextToken());
int m = (n/2) + (n%2);
int[][] dp = new int[n+1][m+1];
int[][] res = new int[n+1][m+1];
for (int i=0; i<=n; i++) {
for (int j=0; j<=m; j++) {
dp[i][j] = MAX; res[i][j] = MAX;
}
}
for (int i=1; i<=n; i++) {
int x = Math.max(0, arr[i-1] - (arr[i] - 1)) +
Math.max(0, arr[i+1] - (arr[i] - 1));
dp[i][1] = x; res[i][1] = Math.min(res[i-1][1], dp[i][1]);
if (i<3) continue;
for (int j=2; j<=m; j++) {
int y = Math.max(0, arr[i-1] - (arr[i - 2] - 1)) +
Math.max(0, arr[i-1] - (arr[i] - 1)) -
Math.max(0, arr[i-1] - (Math.min(arr[i-2], arr[i]) - 1));
if (i==3) dp[i][j] = dp[i-2][j-1] + x - y;
else {
if (res[i-3][j-1]==MAX) dp[i][j] = dp[i-2][j-1] + x - y;
else if (dp[i-2][j-1]==MAX) dp[i][j] = res[i-3][j-1] + x;
else dp[i][j] = Math.min(res[i-3][j-1] + x, dp[i-2][j-1] + x - y);
}
if (dp[i][j]<0) dp[i][j] = MAX;
res[i][j] = Math.min(res[i-1][j], dp[i][j]);
}
}
StringBuilder ans = new StringBuilder();
for (int i=1; i<=m; i++) ans.append(res[n][i]).append(" ");
PrintWriter writer = new PrintWriter(System.out);
writer.print(ans);
writer.close();
}
} | Java | ["5\n1 1 1 1 1", "3\n1 2 3", "5\n1 2 3 2 2"] | 1 second | ["1 2 2", "0 2", "0 1 3"] | NoteIn the first example, to get at least one hill suitable for construction, one can decrease the second hill by one in one hour, then the sequence of heights becomes 1, 0, 1, 1, 1 and the first hill becomes suitable for construction.In the first example, to get at least two or at least three suitable hills, one can decrease the second and the fourth hills, then the sequence of heights becomes 1, 0, 1, 0, 1, and hills 1, 3, 5 become suitable for construction. | Java 8 | standard input | [
"dp"
] | 9534b468bfb6126fc16b896532ced8c5 | The first line of input contains the only integer n (1 ≤ n ≤ 5000)—the number of the hills in the sequence. Second line contains n integers ai (1 ≤ ai ≤ 100 000)—the heights of the hills in the sequence. | 1,900 | Print exactly numbers separated by spaces. The i-th printed number should be equal to the minimum number of hours required to level hills so it becomes possible to build i houses. | standard output | |
PASSED | f70616b37988d1c5bb2fdd2044b8e2f0 | train_000.jsonl | 1532938500 | Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlasting city construction. From the window in your room, you see the sequence of n hills, where i-th of them has height ai. The Innopolis administration wants to build some houses on the hills. However, for the sake of city appearance, a house can be only built on the hill, which is strictly higher than neighbouring hills (if they are present). For example, if the sequence of heights is 5, 4, 6, 2, then houses could be built on hills with heights 5 and 6 only.The Innopolis administration has an excavator, that can decrease the height of an arbitrary hill by one in one hour. The excavator can only work on one hill at a time. It is allowed to decrease hills up to zero height, or even to negative values. Increasing height of any hill is impossible. The city administration wants to build k houses, so there must be at least k hills that satisfy the condition above. What is the minimum time required to adjust the hills to achieve the administration's plan?However, the exact value of k is not yet determined, so could you please calculate answers for all k in range ? Here denotes n divided by two, rounded up. | 512 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {new Main().run();}
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
void run(){
work();
out.flush();
}
long mod=1000000007;
long gcd(long a,long b) {
return a==0?b:gcd(b%a,a);
}
long inf=Long.MAX_VALUE;
void work() {
int n=ni();
long[] A=na(n);
long[][] dp=new long[2503][3];//00 10 01
for(long[] d:dp) {
Arrays.fill(d, inf);
}
dp[1][2]=0;
dp[0][0]=0;
for(int i=0;i<n-1;i++) {
long[][] ndp=new long[2503][3];
for(long[] d:ndp) {
Arrays.fill(d, inf);
}
for(int j=0;j<=2501;j++) {
for(int k=0;k<3;k++) {
if(dp[j][k]==inf) {
continue;
}
if(k==0) {
ndp[j][0]=Math.min(ndp[j][0], dp[j][k]);
ndp[j+1][2]=Math.min(ndp[j+1][2], dp[j][k]+Math.max(A[i]-(A[i+1]-1), 0));
}else if(k==1) {
ndp[j][0]=Math.min(ndp[j][0], dp[j][k]);
long v=Math.min(A[i-1]-1, A[i]);
ndp[j+1][2]=Math.min(ndp[j+1][2], dp[j][k]+Math.max(v-(A[i+1]-1), 0));
}else {
ndp[j][1]=Math.min(ndp[j][1], dp[j][k]+Math.max(0, A[i+1]-(A[i]-1)));
}
}
}
dp=ndp;
}
for(int i=1;i<=(n+1)/2;i++) {
long v=Math.min(Math.min(dp[i][0], dp[i][1]), dp[i][2]);
out.print(v+" ");
}
}
//input
@SuppressWarnings("unused")
private ArrayList<Integer>[] ng(int n, int m) {
ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
int s=in.nextInt()-1,e=in.nextInt()-1;
graph[s].add(e);
graph[e].add(s);
}
return graph;
}
private ArrayList<long[]>[] ngw(int n, int m) {
ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong();
graph[(int)s].add(new long[] {e,w,i});
graph[(int)e].add(new long[] {s,w});
}
return graph;
}
private int ni() {
return in.nextInt();
}
private long nl() {
return in.nextLong();
}
private String ns() {
return in.next();
}
private long[] na(int n) {
long[] A=new long[n];
for(int i=0;i<n;i++) {
A[i]=in.nextLong();
}
return A;
}
private int[] nia(int n) {
int[] A=new int[n];
for(int i=0;i<n;i++) {
A[i]=in.nextInt();
}
return A;
}
}
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());
}
}
| Java | ["5\n1 1 1 1 1", "3\n1 2 3", "5\n1 2 3 2 2"] | 1 second | ["1 2 2", "0 2", "0 1 3"] | NoteIn the first example, to get at least one hill suitable for construction, one can decrease the second hill by one in one hour, then the sequence of heights becomes 1, 0, 1, 1, 1 and the first hill becomes suitable for construction.In the first example, to get at least two or at least three suitable hills, one can decrease the second and the fourth hills, then the sequence of heights becomes 1, 0, 1, 0, 1, and hills 1, 3, 5 become suitable for construction. | Java 8 | standard input | [
"dp"
] | 9534b468bfb6126fc16b896532ced8c5 | The first line of input contains the only integer n (1 ≤ n ≤ 5000)—the number of the hills in the sequence. Second line contains n integers ai (1 ≤ ai ≤ 100 000)—the heights of the hills in the sequence. | 1,900 | Print exactly numbers separated by spaces. The i-th printed number should be equal to the minimum number of hours required to level hills so it becomes possible to build i houses. | standard output | |
PASSED | 23a381c995af8b6fb37f5d6b77fe1507 | train_000.jsonl | 1532938500 | Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlasting city construction. From the window in your room, you see the sequence of n hills, where i-th of them has height ai. The Innopolis administration wants to build some houses on the hills. However, for the sake of city appearance, a house can be only built on the hill, which is strictly higher than neighbouring hills (if they are present). For example, if the sequence of heights is 5, 4, 6, 2, then houses could be built on hills with heights 5 and 6 only.The Innopolis administration has an excavator, that can decrease the height of an arbitrary hill by one in one hour. The excavator can only work on one hill at a time. It is allowed to decrease hills up to zero height, or even to negative values. Increasing height of any hill is impossible. The city administration wants to build k houses, so there must be at least k hills that satisfy the condition above. What is the minimum time required to adjust the hills to achieve the administration's plan?However, the exact value of k is not yet determined, so could you please calculate answers for all k in range ? Here denotes n divided by two, rounded up. | 512 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {new Main().run();}
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
void run(){
work();
out.flush();
}
long mod=1000000007;
long gcd(long a,long b) {
return a==0?b:gcd(b%a,a);
}
long inf=Long.MAX_VALUE;
void work() {
int n=ni();
long[] A=na(n);
long[][] dp=new long[2503][3];//00 10 01
for(long[] d:dp) {
Arrays.fill(d, inf);
}
dp[1][2]=0;
dp[0][0]=0;
for(int i=0;i<n-1;i++) {
long[][] ndp=new long[2503][3];
for(long[] d:ndp) {
Arrays.fill(d, inf);
}
for(int j=0;j<=(i+2)/2;j++) {
for(int k=0;k<3;k++) {
if(dp[j][k]==inf) {
continue;
}
if(k==0) {
ndp[j][0]=Math.min(ndp[j][0], dp[j][k]);
ndp[j+1][2]=Math.min(ndp[j+1][2], dp[j][k]+Math.max(A[i]-(A[i+1]-1), 0));
}else if(k==1) {
ndp[j][0]=Math.min(ndp[j][0], dp[j][k]);
long v=Math.min(A[i-1]-1, A[i]);
ndp[j+1][2]=Math.min(ndp[j+1][2], dp[j][k]+Math.max(v-(A[i+1]-1), 0));
}else {
ndp[j][1]=Math.min(ndp[j][1], dp[j][k]+Math.max(0, A[i+1]-(A[i]-1)));
}
}
}
dp=ndp;
}
for(int i=1;i<=(n+1)/2;i++) {
long v=Math.min(Math.min(dp[i][0], dp[i][1]), dp[i][2]);
out.print(v+" ");
}
}
//input
@SuppressWarnings("unused")
private ArrayList<Integer>[] ng(int n, int m) {
ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
int s=in.nextInt()-1,e=in.nextInt()-1;
graph[s].add(e);
graph[e].add(s);
}
return graph;
}
private ArrayList<long[]>[] ngw(int n, int m) {
ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong();
graph[(int)s].add(new long[] {e,w,i});
graph[(int)e].add(new long[] {s,w});
}
return graph;
}
private int ni() {
return in.nextInt();
}
private long nl() {
return in.nextLong();
}
private String ns() {
return in.next();
}
private long[] na(int n) {
long[] A=new long[n];
for(int i=0;i<n;i++) {
A[i]=in.nextLong();
}
return A;
}
private int[] nia(int n) {
int[] A=new int[n];
for(int i=0;i<n;i++) {
A[i]=in.nextInt();
}
return A;
}
}
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());
}
}
| Java | ["5\n1 1 1 1 1", "3\n1 2 3", "5\n1 2 3 2 2"] | 1 second | ["1 2 2", "0 2", "0 1 3"] | NoteIn the first example, to get at least one hill suitable for construction, one can decrease the second hill by one in one hour, then the sequence of heights becomes 1, 0, 1, 1, 1 and the first hill becomes suitable for construction.In the first example, to get at least two or at least three suitable hills, one can decrease the second and the fourth hills, then the sequence of heights becomes 1, 0, 1, 0, 1, and hills 1, 3, 5 become suitable for construction. | Java 8 | standard input | [
"dp"
] | 9534b468bfb6126fc16b896532ced8c5 | The first line of input contains the only integer n (1 ≤ n ≤ 5000)—the number of the hills in the sequence. Second line contains n integers ai (1 ≤ ai ≤ 100 000)—the heights of the hills in the sequence. | 1,900 | Print exactly numbers separated by spaces. The i-th printed number should be equal to the minimum number of hours required to level hills so it becomes possible to build i houses. | standard output | |
PASSED | c724444b598dd8609803ada886d12bf5 | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.*;
public class Solution {
public static void main(String args[] ) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
char s[] = {'a' , 'a' , 'b' , 'b'};
for(int i = 0; i < n ; i++)
System.out.print(s[i%4]);
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | dc4aec5fed9aa298a40c58a165d04dd1 | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if (n >= 4) {
int count = n / 4;
for (int i = 0; i < count; i++) {
System.out.print("aabb");
}
if (n % 4 != 0) {
System.out.print("aabb".substring(0, (n - (n / 4) * 4)));
}
} else {
System.out.print("aabb".substring(0, n));
}
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | 723b18eca87aeb403a5fffe0cf31f93d | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | //package HackerEarthA;
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import java.text.*;
import java.math.*;
import java.util.regex.*;
/**
*
* @author prabhat
*/
public class easy18{
public static long[] BIT;
public static long[] tree;
public static long[] sum;
public static int mod=1000000007;
public static int[][] dp;
public static boolean[][] isPalin;
public static int max1=1000005;
public static int[][] g;
public static LinkedList<Pair> l[];
public static int n,m,q,k,t,arr[],b[],cnt[],chosen[],pos[],ans;
public static ArrayList<Integer> adj[],al;
public static TreeSet<Integer> ts;
public static char[][] s;
public static int depth,mini,maxi;
public static long V[],low,high,min=Long.MAX_VALUE;
public static boolean visit[][],isPrime[],used[];
public static int[][] dist;
public static ArrayList<Integer>prime;
public static int[] x={0,1};
public static int[] y={-1,0};
public static ArrayList<Integer> divisor[]=new ArrayList[1500005];
public static int[][] subtree,parent,mat;
public static TreeMap<Integer,Integer> tm;
public static LongPoint[] p;
public static int[][] grid;
public static ArrayList<Pair> list;
public static TrieNode root;
static LinkedHashMap<String,Integer> map;
public static BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception
{
InputReader in = new InputReader(System.in);
PrintWriter pw=new PrintWriter(System.out);
n=in.ii();
String ans="";
char c='a';
int y=0;
char[] s={'a','a','b','b'};
for(int i=0;i<n;i++)
pw.print(s[i%4]);
pw.println(ans);
pw.close();
}
public static void dfs1(int cur)
{
used[cur]=true;
cnt[cur]=1;
for(int ver:adj[cur])
{
if(used[ver])continue;
dfs1(ver);
cnt[cur]+=cnt[ver];
}
}
static ArrayList<String> dfs(String s,String[] sarr,HashMap<String, ArrayList<String>> map)
{
if(map.containsKey(s))return map.get(s);
ArrayList<String> res=new ArrayList<>();
if(s.length()==0)
{
res.add("");
return res;
}
for(String word:sarr)
{
if(s.startsWith(word))
{
ArrayList<String > sub=dfs(s.substring(word.length()),sarr,map);
for(String s2:sub)
{
res.add(word+ (s2.isEmpty() ? "" : " ")+s2);
}
}
}
map.put(s,res);
return res;
}
static class SegmentTree{
int n;
int max_bound;
public SegmentTree(int n)
{
this.n=n;
tree=new long[4*n+1];
build(1,0,n-1);
}
void build(int c,int s,int e)
{
if(s==e)
{
tree[c]=arr[s];
return ;
}
int mid=(s+e)>>1;
build(2*c,s,mid);
build(2*c+1,mid+1,e);
tree[c]=(tree[2*c]+tree[2*c+1]);
}
void put(int c,int s, int e,int l,int r,int v) {
if(l>e||r<s||s>e)return ;
if (s == e)
{
tree[c] = arr[s]^v;
return;
}
int mid = (s + e) >> 1;
if (l>mid) put(2*c+1,m+1, e , l,r, v);
else if(r<=mid)put(2*c,s,m,l,r , v);
else{
}
}
long query(int c,int s,int e,int l,int r)
{
if(e<l||s>r)return 0L;
if(s>=l&&e<=r)
{
return tree[c];
}
int mid=(s+e)>>1;
return (query(2*c,s,mid,l,r)+query(2*c+1,mid+1,e,l,r));
}
}
static void update(int indx,long val)
{
while(indx<max1)
{
BIT[indx]+=val;
indx+=(indx&(-indx));
}
}
static long query1(int indx)
{
long sum=0;
while(indx>0)
{
sum+=BIT[indx];
// System.out.println(indx);
indx-=(indx&(-indx));
}
return sum;
}
public static ListNode removeNthFromEnd(ListNode a, int b) {
int cnt=0;
ListNode ra1=a;
ListNode ra2=a;
while(ra1!=null){ra1=ra1.next; cnt++;}
if(b>cnt)return a.next;
else if(b==1&&cnt==1)return null;
else{
int y=cnt-b+1;
int u=0;
ListNode prev=null;
while(a!=null)
{
u++;
if(u==y)
{
if(a.next==null)prev.next=null;
else
{
if(prev==null)return ra2.next;
prev.next=a.next;
a.next=null;
}
break;
}
prev=a;
a=a.next;
}
}
return ra2;
}
static ListNode rev(ListNode a)
{
ListNode prev=null;
ListNode cur=a;
ListNode next=null;
while(cur!=null)
{
next=cur.next;
cur.next=prev;
prev=cur;
cur=next;
}
return prev;
}
static class ListNode {
public int val;
public ListNode next;
ListNode(int x) { val = x; next = null; }
}
static class TrieNode
{
int value; // Only used in leaf nodes
TrieNode[] arr = new TrieNode[2];
public TrieNode() {
value = 0;
arr[0] = null;
arr[1] = null;
}
}
static void insert(int pre_xor)
{
TrieNode temp = root;
// Start from the msb, insert all bits of
// pre_xor into Trie
for (int i=31; i>=0; i--)
{
// Find current bit in given prefix
int val = (pre_xor & (1<<i)) >=1 ? 1 : 0;
// Create a new node if needed
if (temp.arr[val] == null)
temp.arr[val] = new TrieNode();
temp = temp.arr[val];
}
// Store value at leaf node
temp.value = pre_xor;
}
static int query(int pre_xor)
{
TrieNode temp = root;
for (int i=31; i>=0; i--)
{
// Find current bit in given prefix
int val = (pre_xor & (1<<i)) >= 1 ? 1 : 0;
// Traverse Trie, first look for a
// prefix that has opposite bit
if (temp.arr[val] != null)
temp = temp.arr[val];
// If there is no prefix with opposite
// bit, then look for same bit.
else if (temp.arr[1-val] != null)
temp = temp.arr[1-val];
}
return pre_xor^(temp.value);
}
public static String add(String s1,String s2)
{
int n=s1.length()-1;
int m=s2.length()-1;
int rem=0;
int carry=0;
int i=n;
int j=m;
String ans="";
while(i>=0&&j>=0)
{
int c1=(int)(s1.charAt(i)-'0');
int c2=(int)(s2.charAt(j)-'0');
int sum=c1+c2;
sum+=carry;
rem=sum%10;
carry=sum/10;
ans=rem+ans;
i--; j--;
}
while(i>=0)
{
int c1=(int)(s1.charAt(i)-'0');
int sum=c1;
sum+=carry;
rem=sum%10;
carry=sum/10;
ans=rem+ans;
i--;
}
while(j>=0)
{
int c2=(int)(s2.charAt(j)-'0');
int sum=c2;
sum+=carry;
rem=sum%10;
carry=sum/10;
ans=rem+ans;
j--;
}
if(carry>0)ans=carry+ans;
return ans;
}
public static String[] multiply(String A, String B) {
int lb=B.length();
char[] a=A.toCharArray();
char[] b=B.toCharArray();
String[] s=new String[lb];
int cnt=0;
int y=0;
String s1="";
q=0;
for(int i=b.length-1;i>=0;i--)
{
int rem=0;
int carry=0;
for(int j=a.length-1;j>=0;j--)
{
int mul=(int)(b[i]-'0')*(int)(a[j]-'0');
mul+=carry;
rem=mul%10;
carry=mul/10;
s1=rem+s1;
}
s1=carry+s1;
s[y++]=s1;
q=Math.max(q,s1.length());
s1="";
for(int i1=0;i1<y;i1++)s1+='0';
}
return s;
}
public static long nCr(long total, long choose)
{
if(total < choose)
return 0;
if(choose == 0 || choose == total)
return 1;
return nCr(total-1,choose-1)+nCr(total-1,choose);
}
static int get(long s)
{
int ans=0;
for(int i=0;i<n;i++)
{
if(p[i].x<=s&&s<=p[i].y)ans++;
}
return ans;
}
static class LongPoint {
public long x;
public long y;
public LongPoint(long x, long y) {
this.x = x;
this.y = y;
}
public LongPoint subtract(LongPoint o) {
return new LongPoint(x - o.x, y - o.y);
}
public long cross(LongPoint o) {
return x * o.y - y * o.x;
}
}
static int CountPs(String s,int n)
{
boolean b=false;
char[] S=s.toCharArray();
int[][] dp=new int[n][n];
boolean[][] p=new boolean[n][n];
for(int i=0;i<n;i++)p[i][i]=true;
for(int i=0;i<n-1;i++)
{
if(S[i]==S[i+1])
{
p[i][i+1]=true;
dp[i][i+1]=0;
b=true;
}
}
for(int gap=2;gap<n;gap++)
{
for(int i=0;i<n-gap;i++)
{
int j=gap+i;
if(S[i]==S[j]&&p[i+1][j-1]){p[i][j]=true;}
if(p[i][j])
dp[i][j]=dp[i][j-1]+dp[i+1][j]+1-dp[i+1][j-1];
else if(!p[i][j]) dp[i][j]=dp[i][j-1]+dp[i+1][j]-dp[i+1][j-1];
//if(dp[i][j]>=1)
// return true;
}
}
// return b;
return dp[0][n-1];
}
static void Seive()
{
isPrime=new boolean[1500005];
Arrays.fill(isPrime,true);
for(int i=0;i<1500005;i++){divisor[i]=new ArrayList<>();}
int u=0;
prime=new ArrayList<>();
for(int i=2;i<=(1500000);i++)
{
if(isPrime[i])
{
prime.add(i);
for(int j=i;j<1500000;j+=i)
{
divisor[j].add(i);
isPrime[j]=false;
}
/* for(long x=(divCeil(low,i))*i;x<=high;x+=i)
{
divisor[(int)(x-low)].add(i*1L);
}*/
}
}
}
static long divCeil(long a,long b)
{
return (a+b-1)/b;
}
static long root(int pow, long x) {
long candidate = (long)Math.exp(Math.log(x)/pow);
candidate = Math.max(1, candidate);
while (pow(candidate, pow) <= x) {
candidate++;
}
return candidate-1;
}
static long pow(long x, int pow)
{
long result = 1;
long p = x;
while (pow > 0) {
if ((pow&1) == 1) {
if (result > Long.MAX_VALUE/p) return Long.MAX_VALUE;
result *= p;
}
pow >>>= 1;
if (pow > 0 && p >= 4294967296L) return Long.MAX_VALUE;
p *= p;
}
return result;
}
static boolean valid(int i,int j)
{
if(i>=0&&i<n&&j>=0&&j<m)return true;
return false;
}
private static class DSU{
int[] parent;
int[] rank;
int cnt;
public DSU(int n){
parent=new int[n];
rank=new int[n];
for(int i=0;i<n;i++){
parent[i]=i;
rank[i]=1;
}
cnt=n;
}
int find(int i){
while(parent[i] !=i){
parent[i]=parent[parent[i]];
i=parent[i];
}
return i;
}
int Union(int x, int y){
int xset = find(x);
int yset = find(y);
if(xset!=yset)
cnt--;
if(rank[xset]<rank[yset]){
parent[xset] = yset;
rank[yset]+=rank[xset];
rank[xset]=0;
return yset;
}else{
parent[yset]=xset;
rank[xset]+=rank[yset];
rank[yset]=0;
return xset;
}
}
}
public static int[][] packU(int n, int[] from, int[] to, int max) {
int[][] g = new int[n][];
int[] p = new int[n];
for (int i = 0; i < max; i++) p[from[i]]++;
for (int i = 0; i < max; i++) p[to[i]]++;
for (int i = 0; i < n; i++) g[i] = new int[p[i]];
for (int i = 0; i < max; i++) {
g[from[i]][--p[from[i]]] = to[i];
g[to[i]][--p[to[i]]] = from[i];
}
return g;
}
public static int[][] parents3(int[][] g, int root) {
int n = g.length;
int[] par = new int[n];
Arrays.fill(par, -1);
int[] depth = new int[n];
depth[0] = 0;
int[] q = new int[n];
q[0] = root;
for (int p = 0, r = 1; p < r; p++) {
int cur = q[p];
for (int nex : g[cur]) {
if (par[cur] != nex) {
q[r++] = nex;
par[nex] = cur;
depth[nex] = depth[cur] + 1;
}
}
}
return new int[][]{par, q, depth};
}
public static int lower_bound(int[] nums, int target) {
int low = 0, high = nums.length - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (nums[mid] < target)
low = mid + 1;
else
high = mid;
}
return nums[low] == target ? low : -1;
}
public static int upper_bound(int[] nums, int target) {
int low = 0, high = nums.length - 1;
while (low < high) {
int mid = low + (high + 1 - low) / 2;
if (nums[mid] > target)
high = mid - 1;
else
low = mid;
}
return nums[low] == target ? low : -1;
}
public static boolean palin(String s)
{
int i=0;
int j=s.length()-1;
while(i<j)
{
if(s.charAt(i)==s.charAt(j))
{
i++;
j--;
}
else return false;
}
return true;
}
public static int gcd(int a,int b)
{
int res=1;
while(a>0)
{
res=a;
a=b%a;
b=res;
}
return res;
}
static int lcm(int a,int b)
{
return (a*b)/(gcd(a,b));
}
public static long pow(long n,long p,long m)
{
long result = 1;
if(p==0)
return 1;
while(p!=0)
{
if(p%2==1)
result *= n;
if(result>=m)
result%=m;
p >>=1;
n*=n;
if(n>=m)
n%=m;
}
return result;
}
/**
*
* @param n
* @param p
* @return
*/
public static long pow(long n,long p)
{
long result = 1;
if(p==0)
return 1;
if (p==1)
return n;
while(p!=0)
{
if(p%2==1)
result *= n;
p >>=1;
n*=n;
}
return result;
}
static class Edge implements Comparator<Edge> {
private int u;
private int v;
private long w;
public Edge() {
}
public Edge(int u, int v, long w) {
this.u=u;
this.v=v;
this.w=w;
}
public int getU() {
return u;
}
public void setU(int u) {
this.u = u;
}
public int getV() {
return v;
}
public void setV(int v) {
this.v = v;
}
public long getW() {
return w;
}
public void setW(int w) {
this.w = w;
}
public long compareTo(Edge e)
{
return (this.getW() - e.getW());
}
@Override
public int compare(Edge o1, Edge o2) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
static class Pair implements Comparable<Pair>
{
int x,y;
Pair (int a,int b)
{
this.x=a;
this.y=b;
}
public int compareTo(Pair o) {
// TODO Auto-generated method stub
if(this.x!=o.x)
return -Integer.compare(this.x,o.x);
else
return -Integer.compare(this.y, o.y);
//return 0;
}
public boolean equals(Object o) {
if (o instanceof Pair) {
Pair p = (Pair)o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Integer(x).hashCode() * 31 + new Integer(y).hashCode();
}
}
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private SpaceCharFilter filter;
byte inbuffer[] = new byte[1024];
int lenbuffer = 0, ptrbuffer = 0;
final int M = (int) 1e9 + 7;
int md=(int)(1e7+1);
int[] SMP=new int[md];
final double eps = 1e-6;
final double pi = Math.PI;
PrintWriter out;
String check = "";
InputStream obj = check.isEmpty() ? System.in : new ByteArrayInputStream(check.getBytes());
public InputReader(InputStream stream)
{
this.stream = stream;
}
int readByte() {
if (lenbuffer == -1) throw new InputMismatchException();
if (ptrbuffer >= lenbuffer) {
ptrbuffer = 0;
try {
lenbuffer = obj.read(inbuffer);
} catch (IOException e) {
throw new InputMismatchException();
}
}
if (lenbuffer <= 0) return -1;
return inbuffer[ptrbuffer++];
}
public int read()
{
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;
}
String is() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) // when nextLine, (isSpaceChar(b) && b!=' ')
{
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int ii() {
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();
}
}
public long il() {
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();
}
}
boolean isSpaceChar(int c) {
return (!(c >= 33 && c <= 126));
}
int skip()
{
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
float nf() {
return Float.parseFloat(is());
}
double id() {
return Double.parseDouble(is());
}
char ic() {
return (char) skip();
}
int[] iia(int n) {
int a[] = new int[n];
for (int i = 0; i<n; i++) a[i] = ii();
return a;
}
long[] ila(int n) {
long a[] = new long[n];
for (int i = 0; i <n; i++) a[i] = il();
return a;
}
String[] isa(int n) {
String a[] = new String[n];
for (int i = 0; i < n; i++) a[i] = is();
return a;
}
long mul(long a, long b) { return a * b % M; }
long div(long a, long b)
{
return mul(a, pow(b, M - 2));
}
double[][] idm(int n, int m) {
double a[][] = new double[n][m];
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = id();
return a;
}
int[][] iim(int n, int m) {
int a[][] = new int[n][m];
for (int i = 0; i < n; i++) for (int j = 0; j <m; j++) a[i][j] = ii();
return a;
}
public String readLine() {
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 interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
| Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | 4deb998dab3b2aee9bc424b315813219 | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count=0;
for(int i=0;i<n;i++)
System.out.print((i&2)==0?"b":"a");
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | 27e0913f8360219e1cfb293f3d5f9f99 | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;
public class start {
static int x[];
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
// System.out.println((int)('a'));
int n=Integer.parseInt(bf.readLine());
StringBuilder s = new StringBuilder("");
int f=1;
for(int i=0;i<n;i++){
if(f==1){
s.append("b");
f++;
}else if(f==2){
s.append("b");
f++;
}else if(f==3){
s.append("a");
f++;
}else if(f==4){
s.append("a");
f=1;
}
}
System.out.println(s.toString());
}
}
| Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | 409f10dc92c6c8a8c93b02171b2ac237 | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.io.*;
public class $805B
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder("");
int k = n/4;
char[] a = {'a', 'a', 'b'};
int x = 2*(int)Math.pow(10,5);
for(int i=1;i<=k;i++)
sb = sb.append("aabb");
k = n%4;
n = 0;
while(k>0) {
sb = sb.append(a[n++]);
k--;
}
System.out.print(sb);
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | 25e68d5a8a1ffe4b0a61f44ddd53be59 | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.Scanner;
public class main{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.close();
String s[]={"a","a","b","b"};
for(int i=0; i<n; i++)
System.out.print(s[i%4]);
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | 986bd489361c10157f5dff7e0fe5f6ea | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.Scanner;
public class main{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String s="aabb";
for(int i=0; i<n; i++)
System.out.print(s.charAt(i%4));
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | a8773d7c6ab54f72a2ce96482de36a5e | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.Scanner;
/**
* Created by rene on 29/09/17.
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
int count = 1;
for(int i = 1; i <= length; i++) {
if(count % 4 == 0 || count % 3 == 0) {
System.out.print("b");
} else {
System.out.print("a");
}
// if(count % 4 == 0) {
// System.out.print("c");
// } else if(count % 3 == 0) {
// System.out.print("b");
// } else {
// System.out.print("a");
// }
count++;
if(count == 5) {
count = 1;
}
}
}
}
| Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | 4596671b84bfe2fff6c7ba5bdff52756 | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.Scanner;
/**
* Created by rene on 29/09/17.
*/
// http://codeforces.com/problemset/problem/805/B
// https://www.codepit.io/#/problems/590c1627d876872abd96cc62/view?index=3
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
int count = 1;
for(int i = 1; i <= length; i++) {
if(count % 4 == 0 || count % 3 == 0) {
System.out.print("b");
} else {
System.out.print("a");
}
count++;
if(count == 5) {
count = 1;
}
}
}
}
| Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | afba3479cb2e3027e5840f96dd2d2fa3 | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class GFGss
{
static int mod1 = (int) (1e9 + 7);
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
Scanner scan = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String nextString() throws IOException
{
String str00=scan.next();
return str00;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
public int[] nextArray(int n) throws IOException
{
int[] a = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
}
static boolean primeCheck(long num0)
{
boolean b1 = true;
if(num0==1)
{
b1=false;
}
else
{
int num01 = (int) (Math.sqrt(num0)) + 1;
me1: for (int i = 2; i < num01; i++)
{
if (num0 % i == 0)
{
b1 = false;
break me1;
}
}
}
return b1;
}
static HashSet<Integer> primeDivisor(HashSet<Integer> hSet0,int num0)
{
boolean b1=primeCheck(num0);
if(b1)
{
hSet0.add(num0);
}
else
{
for(int i=2;i*i<=num0;i++)
{
if(num0%i==0)
{
hSet0.add(i);
while(num0%i==0)
{
num0 /= i;
}
}
}
boolean b2=primeCheck(num0);
if(b2)
{
hSet0.add(num0);
}
}
return hSet0;
}
static long GCD (long num0,long num00)
{
BigInteger big1=BigInteger.valueOf(num0);
BigInteger big2=BigInteger.valueOf(num00);
big1=big1.gcd(big2);
long num000=Long.parseLong(big1.toString());
return num000;
}
static long power1 (long num0,long num00)
{
long res1 = 1;
while (num00 > 0)
{
if (num00 % 2 != 0)
{
res1 = (res1 * (num0 % 100006)) % 1000016;
}
num0 *= num0;
num0 %= 1000016;
num00 /= 2;
}
return res1;
}
static HashSet<Integer> primeDivi(int num0)
{
HashSet<Integer> hSet1=new HashSet<>();
int num00=(int)Math.sqrt(num0);
hSet1.add(1);
for(int i=2;i<num00+1;i++)
{
if(num0%i==0)
{
hSet1.add(i);
hSet1.add(num0/i);
}
}
return hSet1;
}
static long gcd(long num0,long num00)
{
if(num00==0)
{
return num0;
}
return gcd(num00,num0%num00);
}
static boolean findChar(char chr0,char[] chrArr0)
{
int len0=chrArr0.length;
boolean bool0=false;
me0: for(int i=0;i<len0;i++)
{
if(chrArr0[i]==chr0)
{
bool0=true;
break me0;
}
}
return bool0;
}
public static void main(String[] args) throws IOException {
//Reader r = new Reader();
//PrintWriter writer=new PrintWriter(System.out);
Scanner r = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
OutputWriter33 out33=new OutputWriter33(System.out);
int num1=r.nextInt();
String ans1="";
int num2=num1/2;
int num3=num1%2;
for(int i=0;i<num2;i++)
{
if(i%2==0)
{
out33.print("bb");
}
else
{
out33.print("aa");
}
}
if(num3!=0)
{
if(num1>1) {
if(num2%2==0)
{
out33.print("b");
}
else
{
out33.print("a");
}
}
else
{
out33.print("a");
}
}
out33.print(ans1+"");
out33.close();
r.close();
}
}
class OutputWriter33
{
BufferedWriter writer;
public OutputWriter33(OutputStream stream)
{
writer = new BufferedWriter(new OutputStreamWriter(stream));
}
public void print(int i) throws IOException
{
writer.write(i + "");
}
public void println(int i) throws IOException
{
writer.write(i + "\n");
}
public void print(String s) throws IOException
{
writer.write(s + "");
}
public void println(String s) throws IOException
{
writer.write(s + "\n");
}
public void print(char[] c) throws IOException
{
writer.write(c);
}
public void close() throws IOException
{
writer.flush();
writer.close();
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | ae7ac472bb41f8730ff598fdddf2fd4c | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.*;
import java.io.*;
public class ProbB implements Runnable{
private static InputStream stream;
private static byte[] buf = new byte[1024];
private static int curChar;
private static int numChars;
private static SpaceCharFilter filter;
private static PrintWriter pw;
private static void soln(){
int n = nextInt();
int i=0;
if (n>4) {
for ( i=0; i<(n-4); i+=4) {
pw.print("aabb");
}
}
if (i<n) {
pw.print("a");
i++;
}
if (i<n) {
pw.print("a");
i++;
}
if (i<n) {
pw.print("b");
i++;
}
if (i<n) {
pw.print("b");
i++;
}
pw.println();
}
public void run (){
soln();
}
public static void main(String[] args) throws InterruptedException{
InputReader(System.in);
pw = new PrintWriter(System.out);
Thread t = new Thread(null, new ProbB(), "ProbB", 1<<28);
t.start();
t.join();
pw.close();
}
// To Get Input
// Some Buffer Methods
public static void InputReader(InputStream stream1) {
stream = stream1;
}
private static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private static boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
private static 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++];
}
private static 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;
}
private static 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;
}
private static String nextToken() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private static 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();
}
private static int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
private static long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
private static void pArray(int[] arr){
for (int i=0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
return;
}
private static void pArray(long[] arr){
for (int i=0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
return;
}
private static boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
private interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | d342522a4990d591bd1ec81a735f8d7b | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int i,j=0,k=0,l,m,n,f=0;
n=sc.nextInt();
for(i=0;i<n;i++)
{
if(i%4==0)
{
System.out.print("a");
}
else if(i%4==1)
{
System.out.print("a");
}
else if(i%4==2)
{
System.out.print("b");
}
else if(i%4==3)
{
System.out.print("b");
}
else
{
}
}
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | aa35d2f49f1cf8668e08c5c7efab82eb | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.*;
import java.io.*;
public class palindrome
{
public static void main(String[] args)
{
Scanner dank = new Scanner(System.in);
int x = dank.nextInt();
char[] a = new char[x];
for(int i= 0; i<a.length; i=i+4)
{
a[i]='a';
}
for(int i= 1; i<a.length; i=i+4)
{
a[i]='a';
}
for(int i = 2; i<a.length; i = i+4)
{
a[i]='b';
}
for(int i = 3; i<a.length; i = i+4)
{
a[i]='b';
}
for(int s = 0; s<a.length; s++)
{
System.out.print(a[s]);
}
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | d1ea53dad4a80d8427fc79d00dae55bf | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
import static java.lang.System.*;
public class A{
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(in);
int n=sc.nextInt();
int c=0;
//out.println(n);
int flag=0;
int i=0;
char arr[]={'a','a','b','b'};
String ans="";
String sss="aabb";
for(i=0;i<n/4;i++)
out.print(sss);
for(i=0;i<n%4;i++)
out.print(arr[i]);
//out.println(0%3);
//out.println(1%3);
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | 866927661d280c4a8d88b156e5ea789b | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.Scanner;
/**
* Created by admin on 04/05/2017.
*/
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String toPrint = "aabb";
int n = in.nextInt();
int times = n / 4;
int left = n % 4;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < times; i++)
sb.append(toPrint);
sb.append(toPrint.substring(0, left));
System.out.print(sb.toString());
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | 5622ffc5221c6b3dc11798f7e024ecd4 | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.*;
public class helloWorld
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String str = "aabb";
StringBuilder ans = new StringBuilder();
for(int i = 1; i <= n/4; i++)
ans.append(str);
ans.append( str.substring(0, n%4) );
System.out.println(ans);
in.close();
}
}
| Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | a8787b1a53cef13bb6c5eb774579a788 | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.Scanner;
public class practcas {
public static void main(String[] args) {
Scanner ingreso = new Scanner (System.in);
int n = ingreso.nextInt();
int cont=0;
for (int i = 0; i <=n;) {
System.out.print("a");
cont++;
if(cont==n)
break;
System.out.print("a");
cont++;
if(cont==n)
break;
System.out.print("b");
cont++;
if(cont==n)
break;
System.out.print("b");
cont++;
if(cont==n)
break;
}
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | f94b184027956f987de2a11785c92311 | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.Scanner;
public class practcas {
public static void main(String[] args) {
Scanner ingreso = new Scanner (System.in);
int n = ingreso.nextInt();
int cont=0;
for (int i = 0; i <=n;) {
System.out.print("a");
cont++;
if(cont==n)
break;
System.out.print("a");
cont++;
if(cont==n)
break;
System.out.print("b");
cont++;
if(cont==n)
break;
System.out.print("b");
cont++;
if(cont==n)
break;
}
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | c2d53f4de8a9442389b660291be16e6c | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.ArrayList;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main{
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
StringBuilder s=new StringBuilder("");
String a = "bbaabb";
String b = "aabbaa";
for(int i=1;i<=n/6+1;i++){
s.append(a);
String temp;
temp = a;
a = b;
b = temp;
}
// for(int i=0;i<n;i++) System.out.print(s.charAt(i));
System.out.println(s.substring(0,n));
}
}
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());
}
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | 58346aa1efecbeda7dffb35fb6352330 | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
/**
* Created by Augustinas on 20 09 2016!
*/
public class Solver {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
Task task = new Task();
task.setScanner(scanner);
task.solve();
}
public static class Task {
private Scanner scanner;
private int n;
private final String A = "a";
private final String B = "b";
private String rez = "";
public void solve() {
n = scanner.nextInt();
StringBuilder sb = new StringBuilder();
if (n == 1) {
System.out.println(A);
} else if (n == 2) {
System.out.println(A + B);
} else if (n == 3) {
System.out.println(A + B + B);
} else {
sb.append(A).append(A).append(B).append(B);
while (sb.length() < n) {
sb.append(A).append(A).append(B).append(B);
}
sb.setLength(n);
sb.trimToSize();
rez = sb.toString();
System.out.println(rez);
}
}
public void setScanner(Scanner scanner) {
this.scanner = scanner;
}
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | ccfcb85088484a1ca3ea8ffee3823421 | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Qu1{
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader() {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String s = null;
try {
s = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return s;
}
public String nextParagraph() {
String line = null;
String ans = "";
try {
while ((line = reader.readLine()) != null) {
ans += line;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return ans;
}
}
public static void main(String args[]){
InputReader sc=new InputReader();
PrintWriter pw=new PrintWriter(System.out);
int n=sc.nextInt();
String append = "aabb";
int i;
int stop = n/4;
int last = n%4;
for(i=1;i<=stop;i++){
pw.print(append);
}
for(i=1;i<=last;i++){
if(i==1 || i==2)
pw.print("a");
else pw.print("b");
}
pw.close();
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | c5c8faeed22fccdd137841ec3ca553d3 | train_000.jsonl | 1493909400 | In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible. | 256 megabytes | import java.util.*;
import java.lang.String;
public class P
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
String s="aabb";
String str=String.join("", Collections.nCopies(n, s)).substring(0,n);
System.out.print(str);
}
} | Java | ["2", "3"] | 1 second | ["aa", "bba"] | NoteA palindrome is a sequence of characters which reads the same backward and forward. | Java 8 | standard input | [
"constructive algorithms"
] | fbde86a29f416b3d83bcc63fb3776776 | The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string. | 1,000 | Print the string that satisfies all the constraints. If there are multiple answers, print any of them. | standard output | |
PASSED | 4e440082eb5c903643ff9a36aa31a73e | train_000.jsonl | 1336145400 | Imagine the Cartesian coordinate system. There are k different points containing subway stations. One can get from any subway station to any one instantly. That is, the duration of the transfer between any two subway stations can be considered equal to zero. You are allowed to travel only between subway stations, that is, you are not allowed to leave the subway somewhere in the middle of your path, in-between the stations. There are n dwarves, they are represented by their coordinates on the plane. The dwarves want to come together and watch a soap opera at some integer point on the plane. For that, they choose the gathering point and start moving towards it simultaneously. In one second a dwarf can move from point (x, y) to one of the following points: (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1). Besides, the dwarves can use the subway as many times as they want (the subway transfers the dwarves instantly). The dwarves do not interfere with each other as they move (that is, the dwarves move simultaneously and independently from each other). Help the dwarves and find the minimum time they need to gather at one point. | 256 megabytes | import static java.lang.Math.abs;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import static java.util.Arrays.sort;
import java.io.*;
import java.util.*;
public class Subway {
private static void solve() throws IOException {
int menCount = nextInt(), subwayCount = nextInt();
int[] manX = new int[menCount], manY = new int[menCount];
for (int i = 0; i < menCount; i++) {
manX[i] = nextInt() * 2;
manY[i] = nextInt() * 2;
}
int[] subwayX = new int[subwayCount], subwayY = new int[subwayCount];
for (int i = 0; i < subwayCount; i++) {
subwayX[i] = nextInt() * 2;
subwayY[i] = nextInt() * 2;
}
int answer = solve(manX, manY, subwayX, subwayY);
out.println(answer + 1 >> 1);
}
static int solve(int[] manX, int[] manY, int[] subwayX, int[] subwayY) {
int menCount = manX.length, subwayCount = subwayX.length;
Point[] manPoints = new Point[menCount];
Point[] subways = new Point[subwayCount];
for (int i = 0; i < menCount; i++) {
manPoints[i] = new Point(manX[i] + manY[i], manX[i] - manY[i]);
}
for (int i = 0; i < subwayCount; i++) {
subways[i] = new Point(subwayX[i] + subwayY[i], subwayX[i] - subwayY[i]);
}
int[] distToSubway = DistToSubwaySolver.distToSubwayForPoints(manPoints, subways);
Man[] men = new Man[menCount];
for (int i = 0; i < menCount; i++) {
men[i] = new Man(manPoints[i], distToSubway[i]);
}
sort(men, new Comparator<Man>() {
@Override
public int compare(Man o1, Man o2) {
return o2.distToSubway - o1.distToSubway;
}
});
int[] minRadius = new int[menCount];
Segment[] segments = new Segment[menCount];
int x1 = Integer.MAX_VALUE, y1 = Integer.MAX_VALUE;
int x2 = Integer.MIN_VALUE, y2 = Integer.MIN_VALUE;
for (int i = 0; i < menCount; i++) {
x1 = min(x1, men[i].where.x);
x2 = max(x2, men[i].where.x);
y1 = min(y1, men[i].where.y);
y2 = max(y2, men[i].where.y);
int d = max(x2 - x1, y2 - y1) >> 1;
minRadius[i] = d;
segments[i] = new Segment(min(x1 + d, x2 - d), min(y1 + d, y2 - d),
max(x1 + d, x2 - d), max(y1 + d, y2 - d));
}
int[] segmentToSubway = DistToSubwaySolver.distToSubwayForSegments(segments, subways);
int bestAnswer = Integer.MAX_VALUE;
for (int menWalk = 0; menWalk <= menCount; menWalk++) {
int r;
if (menWalk == 0) {
r = men[0].distToSubway;
} else {
if (menWalk == menCount) {
r = minRadius[menCount - 1];
} else {
int d = segmentToSubway[menWalk - 1] + minRadius[menWalk - 1]
+ men[menWalk].distToSubway;
r = d / 2;
r = max(r, minRadius[menWalk - 1]);
r = max(r, men[menWalk].distToSubway);
}
}
bestAnswer = min(bestAnswer, r);
}
return bestAnswer;
}
static class Segment {
int x1, y1, x2, y2;
private Segment(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
}
static class Man {
final Point where;
final int distToSubway;
private Man(Point where, int distToSubway) {
this.where = where;
this.distToSubway = distToSubway;
}
}
static class Point {
final int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
int dist(Point p) {
return max(abs(x - p.x), abs(y - p.y));
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}
static class DistToSubwaySolver {
static int[] distToSubwayForPoints(Point[] men, Point[] subways) {
int menCount = men.length, subwayCount = subways.length;
Event[] allEvents = new Event[subwayCount + menCount];
Set<Integer> allXplusY = new HashSet<Integer>();
for (int i = 0; i < subwayCount; i++) {
Event event = new SubwayEvent(subways[i]);
allEvents[i] = event;
allXplusY.add(subways[i].x + subways[i].y);
}
QueryEvent[] queries = new QueryEvent[menCount];
for (int i = 0; i < menCount; i++) {
QueryEvent event = new QueryEvent(men[i], i);
allEvents[i + subwayCount] = event;
allXplusY.add(men[i].x + men[i].y);
queries[i] = event;
}
int[] xplusyArray = toSortedArray(allXplusY);
for (final int aMul : new int[] { 1, -1 }) {
for (final int bMul : new int[] { 1, -1 }) {
Comparator<Event> comparator = new Comparator<Event>() {
@Override
public int compare(Event o1, Event o2) {
int cmp = (o1.p.x - o1.p.y) - (o2.p.x - o2.p.y);
if (cmp != 0) {
return cmp * bMul;
}
return o1.getType() - o2.getType();
}
};
sort(allEvents, comparator);
SmartStructureForPoints smart = new SmartStructureForPoints(new Fenwick(
xplusyArray.length), xplusyArray, aMul, bMul);
for (Event e : allEvents) {
e.processMe(smart);
}
}
}
int[] ret = new int[menCount];
for (int i = 0; i < menCount; i++) {
ret[i] = queries[i].answer >> 1;
}
return ret;
}
static int[] toSortedArray(Collection<Integer> c) {
int[] res = new int[c.size()];
int cnt = 0;
for (int i : c) {
res[cnt++] = i;
}
sort(res);
return res;
}
static abstract class Event {
Point p;
abstract int getType();
abstract void processMe(SmartStructureForPoints smart);
abstract void processMe(SmartStructureForSegments smart);
}
static class SubwayEvent extends Event {
private SubwayEvent(Point p) {
this.p = p;
}
@Override
public int getType() {
return 0;
}
@Override
public void processMe(SmartStructureForPoints smart) {
int index = binarySearch(smart.allXplusY, p.x + p.y);
if (smart.aMul < 0) {
index = smart.allXplusY.length - index - 1;
}
int value = (p.x + p.y) * smart.aMul + (p.x - p.y) * smart.bMul;
smart.f.relax(index, value);
}
@Override
void processMe(SmartStructureForSegments smart) {
int index = binarySearch(smart.all, p.x * (1 - smart.direction) + p.y
* smart.direction);
smart.tree.set(index, (p.x * smart.direction + p.y * (1 - smart.direction))
* smart.order);
}
}
static class QueryEvent extends Event {
final int id;
int answer;
private QueryEvent(Point p, int id) {
this.p = p;
this.id = id;
this.answer = Integer.MAX_VALUE;
}
@Override
public int getType() {
return 1;
}
@Override
public void processMe(SmartStructureForPoints smart) {
int index = binarySearch(smart.allXplusY, p.x + p.y);
if (smart.aMul < 0) {
index = smart.allXplusY.length - index - 1;
}
int value = (p.x + p.y) * smart.aMul + (p.x - p.y) * smart.bMul;
int okValue = smart.f.getMax(index);
if (okValue != Integer.MIN_VALUE) {
this.answer = min(this.answer, value - okValue);
}
}
@Override
void processMe(SmartStructureForSegments smart) {
throw new UnsupportedOperationException();
}
}
static class SegmentEvent extends Event {
final Segment segment;
final int id;
int answer;
private SegmentEvent(Segment segment, Point oneEnd, int id) {
this.p = oneEnd;
this.segment = segment;
this.id = id;
this.answer = Integer.MAX_VALUE;
}
@Override
int getType() {
return 1;
}
@Override
void processMe(SmartStructureForPoints smart) {
throw new UnsupportedOperationException();
}
@Override
void processMe(SmartStructureForSegments smart) {
if (segment.x1 == segment.x2 && smart.direction == 0) {
return;
}
if (segment.y1 == segment.y2 && smart.direction == 1) {
return;
}
int left = binarySearch(smart.all, segment.x1 * (1 - smart.direction) + segment.y1
* smart.direction);
int right = binarySearch(smart.all, segment.x2 * (1 - smart.direction) + segment.y2
* smart.direction);
int value = (p.x * smart.direction + p.y * (1 - smart.direction)) * smart.order;
int okValue = smart.tree.get(left, right + 1);
if (okValue != Integer.MIN_VALUE) {
answer = min(answer, value - okValue);
}
}
}
static class SmartStructureForPoints {
Fenwick f;
int[] allXplusY;
int aMul, bMul;
private SmartStructureForPoints(Fenwick f, int[] allX, int aMul, int bMul) {
this.f = f;
this.allXplusY = allX;
this.aMul = aMul;
this.bMul = bMul;
}
}
static class Fenwick {
int[] a;
Fenwick(int n) {
a = new int[n];
fill(a, Integer.MIN_VALUE);
}
void relax(int i, int val) {
for (; i < a.length; i |= i + 1) {
a[i] = max(a[i], val);
}
}
int getMax(int i) {
int ans = Integer.MIN_VALUE;
for (; i >= 0; i = (i & i + 1) - 1) {
ans = max(ans, a[i]);
}
return ans;
}
}
static int[] distToSubwayForSegments(Segment[] segments, Point[] subways) {
int segmentCount = segments.length, subwayCount = subways.length;
Point[] ends = new Point[2 * segmentCount];
for (int i = 0; i < segmentCount; i++) {
ends[2 * i] = new Point(segments[i].x1, segments[i].y1);
ends[2 * i + 1] = new Point(segments[i].x2, segments[i].y2);
}
int[] distForEnds = distToSubwayForPoints(ends, subways);
int[] answer = new int[segmentCount];
for (int i = 0; i < segmentCount; i++) {
answer[i] = min(distForEnds[2 * i], distForEnds[2 * i + 1]);
}
Set<Integer> all = new HashSet<Integer>();
for (Point p : ends) {
all.add(p.x);
all.add(p.y);
}
for (Point p : subways) {
all.add(p.x);
all.add(p.y);
}
int[] xArray = toSortedArray(all);
Event[] allEvents = new Event[segmentCount + subwayCount];
SegmentEvent[] segmentEvents = new SegmentEvent[segmentCount];
for (int i = 0; i < segmentCount; i++) {
segmentEvents[i] = new SegmentEvent(segments[i], ends[2 * i], i);
allEvents[i] = segmentEvents[i];
}
for (int i = 0; i < subwayCount; i++) {
allEvents[i + segmentCount] = new SubwayEvent(subways[i]);
}
for (final int direction : new int[] { 0, 1 }) {
for (final int order : new int[] { 1, -1 }) {
final int xMul = direction;
final int yMul = 1 - direction;
Comparator<Event> comparator = new Comparator<Event>() {
@Override
public int compare(Event o1, Event o2) {
int cmp = (o1.p.x * xMul + o1.p.y * yMul)
- (o2.p.x * xMul + o2.p.y * yMul);
if (cmp != 0) {
return cmp * order;
}
return o1.getType() - o2.getType();
}
};
sort(allEvents, comparator);
SmartStructureForSegments smart = new SmartStructureForSegments(
new SegmentTreeMax(xArray.length), xArray, direction, order);
for (Event e : allEvents) {
e.processMe(smart);
}
}
}
for (SegmentEvent e : segmentEvents) {
answer[e.id] = min(answer[e.id], e.answer);
}
return answer;
}
static class SmartStructureForSegments {
SegmentTreeMax tree;
int[] all;
int direction, order;
private SmartStructureForSegments(SegmentTreeMax tree, int[] all, int direction,
int order) {
this.tree = tree;
this.all = all;
this.direction = direction;
this.order = order;
}
}
static class SegmentTreeMax {
int[] max;
int n;
public SegmentTreeMax(int n) {
this.n = Integer.highestOneBit(n) << 1;
max = new int[this.n << 1];
Arrays.fill(max, Integer.MIN_VALUE);
}
void set(int x, int y) {
x += n;
max[x] = y;
while (x > 1) {
x >>= 1;
max[x] = Math.max(max[x << 1], max[(x << 1) | 1]);
}
}
int get(int l, int r) {
--r;
int ret = Integer.MIN_VALUE;
l += n;
r += n;
while (l <= r) {
if ((l & 1) == 1) {
ret = Math.max(ret, max[l++]);
}
if ((r & 1) == 0) {
ret = Math.max(ret, max[r--]);
}
l >>= 1;
r >>= 1;
}
return ret;
}
}
}
public static void main(String[] args) {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (Throwable e) {
e.printStackTrace();
System.exit(239);
}
}
static BufferedReader br;
static StringTokenizer st;
static PrintWriter out;
static String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String line = br.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | Java | ["1 0\n2 -2", "2 2\n5 -3\n-4 -5\n-4 0\n-3 -2"] | 6 seconds | ["0", "6"] | null | Java 6 | standard input | [
"data structures",
"binary search"
] | 90fd8635d89e248fa874245a45c1baaa | The first line contains two integers n and k (1 ≤ n ≤ 105; 0 ≤ k ≤ 105) — the number of dwarves and the number of subway stations, correspondingly. The next n lines contain the coordinates of the dwarves. The i-th line contains two space-separated integers xi and yi (|xi|, |yi| ≤ 108) — the coordinates of the i-th dwarf. It is guaranteed that all dwarves are located at different points. The next k lines contain the coordinates of the subway stations. The t-th line contains two space-separated integers xt and yt (|xt|, |yt| ≤ 108) — the coordinates of the t-th subway station. It is guaranteed that all subway stations are located at different points. | 3,000 | Print a single number — the minimum time, in which all dwarves can gather together at one point to watch the soap. | standard output | |
PASSED | 19e05551a1b0955283f76af3b1582968 | train_000.jsonl | 1424190900 | Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever.Drazil wants to know whether he can use this plan to make all his friends become happy at some moment. | 256 megabytes | //package pkg515b;
import java.util.*;
public class Main
{
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
public static void main(String[] args)
{
Main obj=new Main();
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int b=sc.nextInt();
int a=obj.gcd(n,m);
int arr[]=new int[a];
for(int i=0;i<b;i++)
{
arr[(sc.nextInt()%a)]=1;
}
int g=sc.nextInt();
for(int i=0;i<g;i++)
{
arr[(sc.nextInt()%a)]=1;
}
int k=0;
for( k=0;k<a;k++)
{
if(arr[k]==0)
{
System.out.println("No");
break;
}
}
if(k==a)
{
System.out.println("Yes");
}
}
}
| Java | ["2 3\n0\n1 0", "2 4\n1 0\n1 2", "2 3\n1 0\n1 1"] | 2 seconds | ["Yes", "No", "Yes"] | NoteBy we define the remainder of integer division of i by k.In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment. | Java 11 | standard input | [
"dsu",
"meet-in-the-middle",
"number theory",
"brute force"
] | 65efbc0a1ad82436100eea7a2378d4c2 | The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends. | 1,300 | If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No". | standard output | |
PASSED | 5dd753c41d5019ca68efa49bb28c12bc | train_000.jsonl | 1424190900 | Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever.Drazil wants to know whether he can use this plan to make all his friends become happy at some moment. | 256 megabytes | import java.util.*;
public class Solution {
static int gcd(int a, int b) {
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);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
boolean[] boys = new boolean[n];
int m = scanner.nextInt();
boolean[] girls = new boolean[m];
int nBoyHappy = scanner.nextInt();
for (int i = 0; i < nBoyHappy; i++) {
int idx = scanner.nextInt();
boys[idx] = true;
}
int nGirlHappy = scanner.nextInt();
for (int i = 0; i < nGirlHappy; i++) {
int idx = scanner.nextInt();
girls[idx] = true;
}
int nDays = n * m / gcd(n, m);
for (int i = 0; i < nDays * 2; i++) {
int boyIdx = i % n;
int girlIdx = i % m;
if (boys[boyIdx] || girls[girlIdx]) {
boys[boyIdx] = true;
girls[girlIdx] = true;
}
}
for (int i = 0; i < n; i++) {
if (!boys[i]) {
System.out.println("No");
return;
}
}
for (int i = 0; i < m; i++) {
if (!girls[i]) {
System.out.println("No");
return;
}
}
System.out.println("Yes");
}
} | Java | ["2 3\n0\n1 0", "2 4\n1 0\n1 2", "2 3\n1 0\n1 1"] | 2 seconds | ["Yes", "No", "Yes"] | NoteBy we define the remainder of integer division of i by k.In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment. | Java 11 | standard input | [
"dsu",
"meet-in-the-middle",
"number theory",
"brute force"
] | 65efbc0a1ad82436100eea7a2378d4c2 | The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends. | 1,300 | If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No". | standard output | |
PASSED | 7efcdd1895916a6411e58080892b05cd | train_000.jsonl | 1424190900 | Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever.Drazil wants to know whether he can use this plan to make all his friends become happy at some moment. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cf515b {
static BufferedReader __in;
static PrintWriter __out;
static StringTokenizer input;
public static void main(String[] args) throws IOException {
__in = new BufferedReader(new InputStreamReader(System.in));
__out = new PrintWriter(new OutputStreamWriter(System.out));
int n = rni(), m = ni();
DSU dsu = new DSU(n + m);
int b = rni();
for(int i = 0; i < b; ++i) {
dsu.hap[ni()] = true;
}
int g = rni();
for(int i = 0; i < g; ++i) {
dsu.hap[n + ni()] = true;
}
for(int i = 0; i < n * m; ++i) {
dsu.union(i % n, n + i % m);
}
for(int i = 0; i < n + m; ++i) {
if(!dsu.hap[dsu.find(i)]) {
prn();
close();
return;
}
}
pry();
close();
}
static class DSU {
int[] par, sz;
boolean[] hap;
DSU(int n) {
par = new int[n];
sz = new int[n];
hap = new boolean[n];
for(int i = 0; i < n; ++i) {
make(i);
}
}
void make(int v) {
par[v] = v;
sz[v] = 1;
}
int find(int v) {
if(v == par[v]) {
return v;
} else {
par[v] = find(par[v]);
return par[v];
}
}
void union(int u, int v) {
int a = find(u), b = find(v);
if(a != b) {
if(sz[a] < sz[b]) {
int swap = a;
a = b;
b = swap;
}
par[b] = a;
sz[a] += sz[b];
hap[a] = hap[a] || hap[b];
}
}
}
// references
// IBIG = 1e9 + 7
// IRAND ~= 3e8
// IMAX ~= 2e10
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IRAND = 327859546;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));}
static int minstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? min(x[0], x[1]) : min(x[0], minstarting(1, x));}
static long minstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? min(x[offset], x[offset + 1]) : min(x[offset], minstarting(offset + 1, x));}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));}
static int maxstarting(int offset, int... x) {assert x.length > 2; return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {return x.length == 1 ? x[0] : x.length == 2 ? max(x[0], x[1]) : max(x[0], maxstarting(1, x));}
static long maxstarting(int offset, long... x) {assert x.length > 2; return offset == x.length - 2 ? max(x[offset], x[offset + 1]) : max(x[offset], maxstarting(offset + 1, x));}
static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int floori(double d) {return (int)d;}
static int ceili(double d) {return (int)ceil(d);}
static long floorl(double d) {return (long)d;}
static long ceill(double d) {return (long)ceil(d);}
// input
static void r() throws IOException {input = new StringTokenizer(__in.readLine());}
static int ri() throws IOException {return Integer.parseInt(__in.readLine());}
static long rl() throws IOException {return Long.parseLong(__in.readLine());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Integer.parseInt(input.nextToken()); return a;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; input = new StringTokenizer(__in.readLine()); for(int i = 0; i < n; ++i) a[i] = Long.parseLong(input.nextToken()); return a;}
static char[] rcha() throws IOException {return __in.readLine().toCharArray();}
static String rline() throws IOException {return __in.readLine();}
static int rni() throws IOException {input = new StringTokenizer(__in.readLine()); return Integer.parseInt(input.nextToken());}
static int ni() {return Integer.parseInt(input.nextToken());}
static long rnl() throws IOException {input = new StringTokenizer(__in.readLine()); return Long.parseLong(input.nextToken());}
static long nl() {return Long.parseLong(input.nextToken());}
// output
static void pr(int i) {__out.print(i);}
static void prln(int i) {__out.println(i);}
static void pr(long l) {__out.print(l);}
static void prln(long l) {__out.println(l);}
static void pr(double d) {__out.print(d);}
static void prln(double d) {__out.println(d);}
static void pr(char c) {__out.print(c);}
static void prln(char c) {__out.println(c);}
static void pr(char[] s) {__out.print(new String(s));}
static void prln(char[] s) {__out.println(new String(s));}
static void pr(String s) {__out.print(s);}
static void prln(String s) {__out.println(s);}
static void pr(Object o) {__out.print(o);}
static void prln(Object o) {__out.println(o);}
static void prln() {__out.println();}
static void pryes() {__out.println("yes");}
static void pry() {__out.println("Yes");}
static void prY() {__out.println("YES");}
static void prno() {__out.println("no");}
static void prn() {__out.println("No");}
static void prN() {__out.println("NO");}
static void pryesno(boolean b) {__out.println(b ? "yes" : "no");};
static void pryn(boolean b) {__out.println(b ? "Yes" : "No");}
static void prYN(boolean b) {__out.println(b ? "YES" : "NO");}
static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); __out.println(iter.next());}
static void flush() {__out.flush();}
static void close() {__out.close();}
} | Java | ["2 3\n0\n1 0", "2 4\n1 0\n1 2", "2 3\n1 0\n1 1"] | 2 seconds | ["Yes", "No", "Yes"] | NoteBy we define the remainder of integer division of i by k.In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment. | Java 11 | standard input | [
"dsu",
"meet-in-the-middle",
"number theory",
"brute force"
] | 65efbc0a1ad82436100eea7a2378d4c2 | The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends. | 1,300 | If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No". | standard output | |
PASSED | 8156060c36575a67fcceb1710672d374 | train_000.jsonl | 1424190900 | Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever.Drazil wants to know whether he can use this plan to make all his friends become happy at some moment. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main
{
private void solve()throws IOException
{
int n=nextInt();
int m=nextInt();
int b=nextInt();
boolean happyboys[]=new boolean[n];
while(b-->0)
happyboys[nextInt()]=true;
boolean happygirls[]=new boolean[m];
int g=nextInt();
while(g-->0)
happygirls[nextInt()]=true;
for(int i=0;i<Math.max(n,m)*Math.max(n,m);i++)
{
int p=i%n;
int q=i%m;
if(happyboys[p])
happygirls[q]=true;
if(happygirls[q])
happyboys[p]=true;
}
for(int i=0;i<n;i++)
if(happyboys[i]==false)
{
out.println("No");
return;
}
for(int i=0;i<m;i++)
if(happygirls[i]==false)
{
out.println("No");
return;
}
out.println("Yes");
}
///////////////////////////////////////////////////////////
public void run()throws IOException
{
br=new BufferedReader(new InputStreamReader(System.in));
st=null;
out=new PrintWriter(System.out);
solve();
br.close();
out.close();
}
public static void main(String args[])throws IOException{
new Main().run();
}
BufferedReader br;
StringTokenizer st;
PrintWriter out;
String nextToken()throws IOException{
while(st==null || !st.hasMoreTokens())
st=new StringTokenizer(br.readLine());
return st.nextToken();
}
String nextLine()throws IOException{
return br.readLine();
}
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 | ["2 3\n0\n1 0", "2 4\n1 0\n1 2", "2 3\n1 0\n1 1"] | 2 seconds | ["Yes", "No", "Yes"] | NoteBy we define the remainder of integer division of i by k.In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment. | Java 11 | standard input | [
"dsu",
"meet-in-the-middle",
"number theory",
"brute force"
] | 65efbc0a1ad82436100eea7a2378d4c2 | The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends. | 1,300 | If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No". | standard output | |
PASSED | b7be42edf605a9b82cc19654e9d8bdc5 | train_000.jsonl | 1424190900 | Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever.Drazil wants to know whether he can use this plan to make all his friends become happy at some moment. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class MeetInTheMiddle_515B {
public static void main(String[] args) throws IOException {
new MeetInTheMiddle_515B().run();
}
private void run() throws IOException {
int n = nextInt();
int m = nextInt();
int gcd = gcd(n,m);
int happy_b = nextInt();
int[] h = new int[101];
for(int i=0;i<happy_b;i++) {
int x = nextInt();
h[x%gcd]=1;
}
int happy_g = nextInt();
for(int i=0;i<happy_g;i++) {
int x = nextInt();
h[x%gcd]=1;
}
for(int i=0;i<gcd;i++) {
if(h[i]!=1) {
System.out.println("No");
return;
}
}
System.out.println("Yes");
}
// ------------------
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
PrintWriter out = new PrintWriter(System.out);
String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
int gcd(int a, int b) {
int temp=0;
while(b!=0) {
temp = a%b;
a = b;
b = temp;
}
return a;
}
}
| Java | ["2 3\n0\n1 0", "2 4\n1 0\n1 2", "2 3\n1 0\n1 1"] | 2 seconds | ["Yes", "No", "Yes"] | NoteBy we define the remainder of integer division of i by k.In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment. | Java 11 | standard input | [
"dsu",
"meet-in-the-middle",
"number theory",
"brute force"
] | 65efbc0a1ad82436100eea7a2378d4c2 | The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends. | 1,300 | If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No". | standard output | |
PASSED | f4ad21e0ddb660ded91db82c36871f74 | train_000.jsonl | 1424190900 | Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever.Drazil wants to know whether he can use this plan to make all his friends become happy at some moment. | 256 megabytes | import java.util.Scanner;
public class D630 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
boolean boys[] = new boolean[n];
boolean girls[] = new boolean[m];
int b =in.nextInt();
for (int i = 0; i < b; i++) {
boys[in.nextInt()] = true;
}
int g =in.nextInt();
for (int i = 0; i < g; i++) {
girls[in.nextInt()] = true;
}
for (int i = 0; i<n*n*m*m; i++) {
if(boys[i%n]==true||girls[i%m]==true){
boys[i%n]=true;
girls[i%m]=true;
}
}
for (boolean c : boys) {
if(!c){ System.out.println("No"); return; }
}
for (boolean c : girls) {
if(!c){ System.out.println("No"); return; }
}
System.out.println("Yes");
}
} | Java | ["2 3\n0\n1 0", "2 4\n1 0\n1 2", "2 3\n1 0\n1 1"] | 2 seconds | ["Yes", "No", "Yes"] | NoteBy we define the remainder of integer division of i by k.In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment. | Java 11 | standard input | [
"dsu",
"meet-in-the-middle",
"number theory",
"brute force"
] | 65efbc0a1ad82436100eea7a2378d4c2 | The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends. | 1,300 | If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No". | standard output | |
PASSED | a05c2537007d5000ef8500de3029363e | train_000.jsonl | 1424190900 | Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever.Drazil wants to know whether he can use this plan to make all his friends become happy at some moment. | 256 megabytes | import java.io.*;
import java.util.*;
public class DrazilAndHisFriends
{
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 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();
}
}
public static void main(String args[])
{
FastReader sc=new FastReader();
OutputWriter out=new OutputWriter(System.out);
int n=sc.nextInt();
int m=sc.nextInt();
int a[]=new int[n];
int l[]=new int[m];
Arrays.fill(a,0);
Arrays.fill(l,0);
int b=sc.nextInt();
for(int i=0;i<b;i++)
{
int num=sc.nextInt();
a[num]=1;
}
int g=sc.nextInt();
for(int i=0;i<g;i++)
{
int num=sc.nextInt();
l[num]=1;
}
int f=0;
for(int i=0;i<=10000;i++)
{
int ai=i%n;
int bi=i%m;
if(a[ai]==0 && l[bi]==1)
{
f++;
a[ai]=1;
}
else if(a[ai]==1 && l[bi]==0)
{
f++;
l[bi]=1;
}
if(f==((n+m)-(b+g)))
break;
}
if(f==(n+m-(b+g)))
out.printLine("Yes");
else
out.printLine("No");
out.flush();
}
} | Java | ["2 3\n0\n1 0", "2 4\n1 0\n1 2", "2 3\n1 0\n1 1"] | 2 seconds | ["Yes", "No", "Yes"] | NoteBy we define the remainder of integer division of i by k.In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment. | Java 11 | standard input | [
"dsu",
"meet-in-the-middle",
"number theory",
"brute force"
] | 65efbc0a1ad82436100eea7a2378d4c2 | The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends. | 1,300 | If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No". | standard output | |
PASSED | 7190363a450f5b3fb6e6ef58c64fa273 | train_000.jsonl | 1424190900 | Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever.Drazil wants to know whether he can use this plan to make all his friends become happy at some moment. | 256 megabytes | // #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Main {
static int infi = Integer.MAX_VALUE / 2, mod = (int) (1e9 + 7), maxn = (int) 1e5;
static long infl = Long.MAX_VALUE / 2;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int n = nextInt();
int m = nextInt();
boolean[] a = new boolean[n];
boolean[] b = new boolean[m];
int t = nextInt();
for (int i = 0; i < t; i++) {
a[nextInt()] = true;
}
t = nextInt();
for (int i = 0; i < t; i++) {
b[nextInt()] = true;
}
for (int i = 0; i <= n * m * 1000; i++) {
if (a[i % n] || b[i % m]) {
a[i % n] = true;
b[i % m] = true;
}
}
for (int i = 0; i < n; i++) {
if (!a[i]) {
out.println("No");
out.close();
return;
}
}
for (int i = 0; i < m; i++) {
if (!b[i]) {
out.println("No");
out.close();
return;
}
}
out.println("Yes");
out.close();
}
static BufferedReader br;
static StringTokenizer st = new StringTokenizer("");
static PrintWriter out;
static String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
class f {
int x, y;
public f(int x, int y) {
this.x = x;
this.y = y;
}
} | Java | ["2 3\n0\n1 0", "2 4\n1 0\n1 2", "2 3\n1 0\n1 1"] | 2 seconds | ["Yes", "No", "Yes"] | NoteBy we define the remainder of integer division of i by k.In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment. | Java 11 | standard input | [
"dsu",
"meet-in-the-middle",
"number theory",
"brute force"
] | 65efbc0a1ad82436100eea7a2378d4c2 | The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends. | 1,300 | If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No". | standard output | |
PASSED | fff01426234419a824b60803971ca435 | train_000.jsonl | 1424190900 | Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever.Drazil wants to know whether he can use this plan to make all his friends become happy at some moment. | 256 megabytes |
import java.io.*;
import java.math.*;
import java.util.*;
public class practice {
// Faster Input...
static class Reader{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[200001]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void debug(int[] ...var) {
for(int[] row : var) {
debug(row);
}
}
public static void debug(long[] ...var) {
for(long[] row : var) {
debug(row);
}
}
public static void debug(String[] ...var) {
for(String[] row : var) {
debug(row);
}
}
public static void debug(double[] ...var) {
for(double[] row : var) {
debug(row);
}
}
public static void debug(int ...var) {
for(int i:var) System.err.print(i+" ");
System.err.println();
}
public static void debug(String ...var) {
for(String i:var) System.err.print(i+" ");
System.err.println();
}
public static void debug(double ...var) {
for(double i:var) System.err.print(i+" ");
System.err.println();
}
public static void debug(long ...var) {
for(long i:var) System.err.print(i+" ");
System.err.println();
}
/*
public static <T> void debug(T ...varargs) {
// Warning
// Heap Pollution might occur
// this overrides even 1d and 2d array methods as it is an object...
// + i am not using object based array like Integer[]
// I am using int[] so that is a problem as i need Wrapper class as an argument
for(T val:varargs) System.err.printf("%s ",val);
System.err.println();
}
*/
private static Reader scan = new Reader();
private static int gcd(int a,int b) {
if(b==0) {
return a;
}
return gcd(b,a%b);
}
public static void main(String args[]) throws Exception{
//Faster Output...
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//StringBuilder sb = new StringBuilder();
//be careful of readLine() -- length of string;
// Scanner scan = new Scanner(System.in);
//scan.useDelimiter(""); // for reading character by character
int boys = scan.nextInt();
int girls = scan.nextInt();
int lcm = boys*girls/gcd(boys,girls);
BitSet happyBoys = new BitSet(boys);
BitSet happyGirls = new BitSet(girls);
int b = scan.nextInt();
for(int i=0;i<b;++i) {
int idx = scan.nextInt();
happyBoys.set(idx);
}
int g = scan.nextInt();
for(int i=0;i<g;++i) {
int idx = scan.nextInt();
happyGirls.set(idx);
}
int sads = boys + girls - b - g;
int rep = 0;
b = 0;
g = 0;
while(rep<lcm && sads>0) {
if(b==boys) {
b=0;
}
if(g==girls) {
g=0;
}
if(happyBoys.get(b) || happyGirls.get(g)) {
if(happyBoys.get(b) && happyGirls.get(g)) {
// do nothing
} else {
happyBoys.set(b);
happyGirls.set(g);
rep = -1;
--sads;
}
}
++b;
++g;
++rep;
}
if(sads==0) {
out.println("Yes");
} else {
out.println("No");
}
scan.close();
out.close();
}
}
| Java | ["2 3\n0\n1 0", "2 4\n1 0\n1 2", "2 3\n1 0\n1 1"] | 2 seconds | ["Yes", "No", "Yes"] | NoteBy we define the remainder of integer division of i by k.In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment. | Java 11 | standard input | [
"dsu",
"meet-in-the-middle",
"number theory",
"brute force"
] | 65efbc0a1ad82436100eea7a2378d4c2 | The first line contains two integer n and m (1 ≤ n, m ≤ 100). The second line contains integer b (0 ≤ b ≤ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 ≤ xi < n), denoting the list of indices of happy boys. The third line conatins integer g (0 ≤ g ≤ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 ≤ yj < m), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends. | 1,300 | If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No". | standard output | |
PASSED | eaa5b95eb377dacfedf3b3576c054ff5 | train_000.jsonl | 1539880500 | There are $$$n$$$ children numbered from $$$1$$$ to $$$n$$$ in a kindergarten. Kindergarten teacher gave $$$a_i$$$ ($$$1 \leq a_i \leq n$$$) candies to the $$$i$$$-th child. Children were seated in a row in order from $$$1$$$ to $$$n$$$ from left to right and started eating candies. While the $$$i$$$-th child was eating candies, he calculated two numbers $$$l_i$$$ and $$$r_i$$$ — the number of children seating to the left of him that got more candies than he and the number of children seating to the right of him that got more candies than he, respectively.Formally, $$$l_i$$$ is the number of indices $$$j$$$ ($$$1 \leq j < i$$$), such that $$$a_i < a_j$$$ and $$$r_i$$$ is the number of indices $$$j$$$ ($$$i < j \leq n$$$), such that $$$a_i < a_j$$$.Each child told to the kindergarten teacher the numbers $$$l_i$$$ and $$$r_i$$$ that he calculated. Unfortunately, she forgot how many candies she has given to each child. So, she asks you for help: given the arrays $$$l$$$ and $$$r$$$ determine whether she could have given the candies to the children such that all children correctly calculated their values $$$l_i$$$ and $$$r_i$$$, or some of them have definitely made a mistake. If it was possible, find any way how she could have done it. | 256 megabytes | import java.util.*;
import java.io.*;
public class C
{
public static void main(String args[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
int[] arr = new int[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
int[] arr2 = new int[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr2[i] = Integer.parseInt(st.nextToken());
if(arr[0]+arr2[N-1] > 0)
{
System.out.println("NO");
return;
}
//find max
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int i=0; i < N; i++)
if(arr[i]+arr2[i] == 0)
ls.add(i);
if(ls.size() == 0)
{
System.out.println("NO");
return;
}
//
int[] res = new int[N];
for(int i=0; i < N; i++)
res[i] = N-arr[i]-arr2[i];
for(int i=0; i < N; i++)
{
//right
int rcount = 0;
for(int j=i+1; j < N; j++)
if(res[i] < res[j])
rcount++;
int lcount = 0;
for(int j=i-1; j >= 0; j--)
if(res[i] < res[j])
lcount++;
if(arr[i] != lcount || arr2[i] != rcount)
{
System.out.println("NO");
return;
}
}
System.out.println("YES");
for(int a: res)
System.out.print(a+" ");
}
} | Java | ["5\n0 0 1 1 2\n2 0 1 0 0", "4\n0 0 2 0\n1 1 1 1", "3\n0 0 0\n0 0 0"] | 1 second | ["YES\n1 3 1 2 1", "NO", "YES\n1 1 1"] | NoteIn the first example, if the teacher distributed $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$1$$$ candies to $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd, $$$4$$$-th, $$$5$$$-th child, respectively, then all the values calculated by the children are correct. For example, the $$$5$$$-th child was given $$$1$$$ candy, to the left of him $$$2$$$ children were given $$$1$$$ candy, $$$1$$$ child was given $$$2$$$ candies and $$$1$$$ child — $$$3$$$ candies, so there are $$$2$$$ children to the left of him that were given more candies than him.In the second example it is impossible to distribute the candies, because the $$$4$$$-th child made a mistake in calculating the value of $$$r_4$$$, because there are no children to the right of him, so $$$r_4$$$ should be equal to $$$0$$$.In the last example all children may have got the same number of candies, that's why all the numbers are $$$0$$$. Note that each child should receive at least one candy. | Java 11 | standard input | [
"constructive algorithms",
"implementation"
] | fa531c38833907d619f1102505ddbb6a | On the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the number of children in the kindergarten. On the next line there are $$$n$$$ integers $$$l_1, l_2, \ldots, l_n$$$ ($$$0 \leq l_i \leq n$$$), separated by spaces. On the next line, there are $$$n$$$ integer numbers $$$r_1, r_2, \ldots, r_n$$$ ($$$0 \leq r_i \leq n$$$), separated by spaces. | 1,500 | If there is no way to distribute the candies to the children so that all of them calculated their numbers correctly, print «NO» (without quotes). Otherwise, print «YES» (without quotes) on the first line. On the next line, print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces — the numbers of candies the children $$$1, 2, \ldots, n$$$ received, respectively. Note that some of these numbers can be equal, but all numbers should satisfy the condition $$$1 \leq a_i \leq n$$$. The number of children seating to the left of the $$$i$$$-th child that got more candies than he should be equal to $$$l_i$$$ and the number of children seating to the right of the $$$i$$$-th child that got more candies than he should be equal to $$$r_i$$$. If there is more than one solution, find any of them. | standard output | |
PASSED | 5357caaa825ac24347f5053aff4ea55e | train_000.jsonl | 1464798900 | Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.Vanya has n pieces of potato, the height of the i-th piece is equal to ai. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens: If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. Processor smashes k centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.stream.*;
//Arrays.stream(br.readLine().split("\\s")).mapToInt(Integer::parseInt).toArray();
public class solution{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
int[] arr1 = Arrays.stream(br.readLine().split("\\s")).mapToInt(Integer::parseInt).toArray();
int n = arr1[0];
int h = arr1[1];
int k = arr1[2];
int[] arr2 = Arrays.stream(br.readLine().split("\\s")).mapToInt(Integer::parseInt).toArray();
long s = 0;
int c = 0;
int i = 0;
while (true) {
while (i < n && c + arr2[i] <= h) {
c += arr2[i++];
}
if (i == n) {
s += Math.ceil((double)c / (double)k);
break;
} else if (c < k) {
s++;
c = 0;
} else {
s += (long) (c / k);
c %= k;
}
}
pw.println(s);
pw.close();
}
} | Java | ["5 6 3\n5 4 3 2 1", "5 6 3\n5 5 5 5 5", "5 6 3\n1 2 1 1 1"] | 1 second | ["5", "10", "2"] | NoteConsider the first sample. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds.In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds. | Java 11 | standard input | [
"implementation",
"math"
] | 5099a9ae62e82441c496ac37d92e99e3 | The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 109) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains n integers ai (1 ≤ ai ≤ h) — the heights of the pieces. | 1,400 | Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement. | standard output | |
PASSED | b64e7f7ec2828f84405552fad638881a | train_000.jsonl | 1464798900 | Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.Vanya has n pieces of potato, the height of the i-th piece is equal to ai. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens: If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. Processor smashes k centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Kraken
*/
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 n = in.nextInt();
long h = in.nextLong(), k = in.nextLong();
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = in.nextLong();
long sec = 0;
long cap = 0;
for (int i = 0; i < n; i++) {
if (cap + a[i] <= h)
cap += a[i];
else {
sec++;
cap = a[i];
}
sec += cap / k;
cap %= k;
}
sec += cap / k;
cap %= k;
if (cap > 0)
sec++;
out.println(sec);
}
}
}
| Java | ["5 6 3\n5 4 3 2 1", "5 6 3\n5 5 5 5 5", "5 6 3\n1 2 1 1 1"] | 1 second | ["5", "10", "2"] | NoteConsider the first sample. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds.In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds. | Java 11 | standard input | [
"implementation",
"math"
] | 5099a9ae62e82441c496ac37d92e99e3 | The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 109) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains n integers ai (1 ≤ ai ≤ h) — the heights of the pieces. | 1,400 | Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement. | standard output | |
PASSED | 570ee17e31871f3ecd4ac79bcb52c162 | train_000.jsonl | 1464798900 | Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.Vanya has n pieces of potato, the height of the i-th piece is equal to ai. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens: If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. Processor smashes k centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed. | 256 megabytes | import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
Reader in = new Reader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n = in.nextInt();
int h = in.nextInt();
int k = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
int p = 0;
long ans = 0;
int now = 0;
while (p != n || now != 0) {
while (p != n && now <= h - a[p]) {
now += a[p];
p++;
}
int nextNeed = p != n ? now - h + a[p] : now;
int time = nextNeed / k;
if (nextNeed % k != 0) time++;
ans += time;
now = Math.max(now - k * time, 0);
}
out.println(ans);
out.flush();
in.close();
out.close();
}
static class Reader {
BufferedReader reader;
StringTokenizer st;
Reader(InputStreamReader stream) {
reader = new BufferedReader(stream);
st = null;
}
void close() throws IOException {
reader.close();
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
String nextLine() throws IOException {
return reader.readLine();
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["5 6 3\n5 4 3 2 1", "5 6 3\n5 5 5 5 5", "5 6 3\n1 2 1 1 1"] | 1 second | ["5", "10", "2"] | NoteConsider the first sample. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds.In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds. | Java 11 | standard input | [
"implementation",
"math"
] | 5099a9ae62e82441c496ac37d92e99e3 | The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 109) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains n integers ai (1 ≤ ai ≤ h) — the heights of the pieces. | 1,400 | Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement. | standard output | |
PASSED | bf027fa3dab58f80720bb9b22f760d5b | train_000.jsonl | 1464798900 | Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.Vanya has n pieces of potato, the height of the i-th piece is equal to ai. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens: If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. Processor smashes k centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed. | 256 megabytes |
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class VanyaAndFood {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
int a[]=new int[n];
long ans=0;
long total=0;
int h=sc.nextInt();
int k=sc.nextInt();
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
int i=0;
while(i<n)
{
if(a[i]+total<=h)
total+=a[i++];
else {
ans+=total/k;
if(total%k+a[i]<=h)
{
total=total%k;
}else {
ans++;
total=0;
}
}
}
ans+=total/k;
if(total%k!=0)
ans++;
pw.println(ans);
pw.flush();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
}
| Java | ["5 6 3\n5 4 3 2 1", "5 6 3\n5 5 5 5 5", "5 6 3\n1 2 1 1 1"] | 1 second | ["5", "10", "2"] | NoteConsider the first sample. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds.In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds. | Java 11 | standard input | [
"implementation",
"math"
] | 5099a9ae62e82441c496ac37d92e99e3 | The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 109) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains n integers ai (1 ≤ ai ≤ h) — the heights of the pieces. | 1,400 | Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement. | standard output | |
PASSED | d38f47cec5f7168e1388e5ce13a3e87d | train_000.jsonl | 1464798900 | Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.Vanya has n pieces of potato, the height of the i-th piece is equal to ai. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens: If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. Processor smashes k centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
public class xl3ster
{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
int[] l1 = readArray();
int n = l1[0];
int h = l1[1];
int k = l1[2];
int[] pieces = readArray();
long seconds = 0;
int content = 0;
int i = 0;
while (true) {
while (i < n && content + pieces[i] <= h) {
content += pieces[i++];
}
if (i == n) {
seconds += Math.ceil((double)content / (double)k);
break;
} else if (content < k) {
seconds++;
content = 0;
} else {
seconds += (long) (content / k);
content %= k;
}
}
out.println(seconds);
out.close();
}
private static String read() throws IOException {
return in.readLine();
}
private static int readInt() throws IOException {
return Integer.parseInt(in.readLine());
}
private static int[] readArray() throws IOException {
String[] line = in.readLine().split("\\s");
int[] a = Arrays.stream(line).mapToInt(Integer::parseInt).toArray();
return a;
}
private static int[] readSortedArray() throws IOException {
String[] line = in.readLine().split("\\s");
int[] a = Arrays.stream(line).mapToInt(Integer::parseInt).sorted().toArray();
return a;
}
}
| Java | ["5 6 3\n5 4 3 2 1", "5 6 3\n5 5 5 5 5", "5 6 3\n1 2 1 1 1"] | 1 second | ["5", "10", "2"] | NoteConsider the first sample. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds.In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds. | Java 11 | standard input | [
"implementation",
"math"
] | 5099a9ae62e82441c496ac37d92e99e3 | The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 109) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains n integers ai (1 ≤ ai ≤ h) — the heights of the pieces. | 1,400 | Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement. | standard output | |
PASSED | 1b22278c7d3f926200342ce2d8627232 | train_000.jsonl | 1464798900 | Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.Vanya has n pieces of potato, the height of the i-th piece is equal to ai. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens: If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. Processor smashes k centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed. | 256 megabytes |
/**
* @author egaeus
* @mail [email protected]
* @veredict Not sended
* @url <https://codeforces.com/problemset/problem/677/B>
* @category ?
* @date 14/06/2020
**/
import java.io.*;
import java.util.*;
import static java.lang.Integer.*;
import static java.lang.Math.*;
public class CF677B {
public static void main(String args[]) throws Throwable {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (String ln; (ln = in.readLine()) != null && !ln.equals(""); ) {
StringTokenizer st = new StringTokenizer(ln);
int N = parseInt(st.nextToken());
long H = parseInt(st.nextToken()), K = parseInt(st.nextToken());
long[] arr = new long[N];
st = new StringTokenizer(in.readLine());
for (int i = 0; i < N; i++)
arr[i] = parseInt(st.nextToken());
long s = 0;
long result = 0;
for (int i = 0; i < N; i++) {
if (s + arr[i] <= H) {
s += arr[i];
} else {
s = arr[i];
result++;
}
result += s / K;
s = s % K;
}
if (s > 0)
result++;
System.out.println(result);
}
}
}
| Java | ["5 6 3\n5 4 3 2 1", "5 6 3\n5 5 5 5 5", "5 6 3\n1 2 1 1 1"] | 1 second | ["5", "10", "2"] | NoteConsider the first sample. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds.In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds. | Java 11 | standard input | [
"implementation",
"math"
] | 5099a9ae62e82441c496ac37d92e99e3 | The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 109) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains n integers ai (1 ≤ ai ≤ h) — the heights of the pieces. | 1,400 | Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement. | standard output | |
PASSED | e78ce85ed3f087e20c53522cc2704486 | train_000.jsonl | 1464798900 | Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.Vanya has n pieces of potato, the height of the i-th piece is equal to ai. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens: If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. Processor smashes k centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed. | 256 megabytes | import java.util.*;
import java.math.*;
import java.io.*;
public class solution {
public static void merge(int arr[], int l, int m, int r) {
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int[n1];
int R[] = new int[n2];
/*Copy data to temp arrays*/
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
// /* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
public static void sort(int arr[], int l, int r) {
if (l < r) {
// Find the middle point
int m = (l + r) / 2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int h = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
int a[] = new int[n];
st = new StringTokenizer(br.readLine());
for(int i=0;i<n;i++){
a[i] = Integer.parseInt(st.nextToken());
}
long ret = 0;
long he = 0;
for(int i = 0;i < n;i++){
if(he + a[i] > h){
long x = (he + a[i] - h + K-1) / K;
ret += x;
he -= x * K;
if(he < 0)he = 0;
}
he += a[i];
}
ret += (he+K-1)/K;
System.out.println(ret);
}
} | Java | ["5 6 3\n5 4 3 2 1", "5 6 3\n5 5 5 5 5", "5 6 3\n1 2 1 1 1"] | 1 second | ["5", "10", "2"] | NoteConsider the first sample. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds.In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds. | Java 11 | standard input | [
"implementation",
"math"
] | 5099a9ae62e82441c496ac37d92e99e3 | The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 109) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains n integers ai (1 ≤ ai ≤ h) — the heights of the pieces. | 1,400 | Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement. | standard output | |
PASSED | 031955aaab673ecf5b129f4fff3f6f36 | train_000.jsonl | 1464798900 | Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.Vanya has n pieces of potato, the height of the i-th piece is equal to ai. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens: If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. Processor smashes k centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed. | 256 megabytes | import java.util.*;
import java.math.*;
import java.io.*;
public class A{
static FastReader scan=new FastReader();
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
static int n;
static char orig[][];
static int check(char arr[][]){
int c=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(arr[j][i]!='1')
continue;
if(j==n-1&&arr[j][i]=='1')
c++;
}
}
return c;
}
static int tmp=0;
static int solve(int op){
char arr[][]=new char[n][n];
for(int l = 0; l <n; l++)
{
for (int m = 0; m < n; m++)
{
arr[l][m] = orig[l][m];
}
}
for(int i=0;i<n;i++){
if(arr[op][i]=='1')
arr[op][i]='0';
else arr[op][i]='1';
}
tmp=Math.max(tmp,check(arr));
return tmp;
}
public static void main(String[] args) throws IOException
{
int n=scan.nextInt();
int h=scan.nextInt();
int k=scan.nextInt();
long arr[]=new long[n];
long sum=0;
long cur=0,ans=0;
for(int i=0;i<n;i++){
arr[i]=scan.nextLong();
if(cur+arr[i]<=h)
cur+=arr[i];
else {
ans++;
cur=arr[i];
}
ans+=cur/k;
cur%=k;
}
if(cur==0)
out.println(ans);
else out.println(ans+1);
out.close();
}
public static long pow(long b, long e) {
long r = 1;
while (e > 0) {
if (e % 2 == 1) r = r * b ;
b = b * b;
e >>= 1;
}
return r;
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long object : arr) list.add(object);
Collections.sort(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
public static class FastReader {
BufferedReader br;
StringTokenizer root;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (root == null || !root.hasMoreTokens()) {
try {
root = new StringTokenizer(br.readLine());
} catch (Exception addd) {
addd.printStackTrace();
}
}
return root.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception addd) {
addd.printStackTrace();
}
return str;
}
public int[] nextIntArray(int arraySize) {
int array[] = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = nextInt();
}
return array;
}
}
static class pair{
int i;
int j;
pair(int i,int j){
this.i=i;
this.j=j;
}
}}
| Java | ["5 6 3\n5 4 3 2 1", "5 6 3\n5 5 5 5 5", "5 6 3\n1 2 1 1 1"] | 1 second | ["5", "10", "2"] | NoteConsider the first sample. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds.In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds. | Java 11 | standard input | [
"implementation",
"math"
] | 5099a9ae62e82441c496ac37d92e99e3 | The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 109) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains n integers ai (1 ≤ ai ≤ h) — the heights of the pieces. | 1,400 | Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement. | standard output | |
PASSED | 36f027b99c498c1fc57a86ae882024dd | train_000.jsonl | 1464798900 | Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.Vanya has n pieces of potato, the height of the i-th piece is equal to ai. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens: If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. Processor smashes k centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
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;
}
long power(int n) {
long x=1;
while(n--!=0) {
x*=2;
}
return x;
}
int binary_search(int ar[],int x) {
int low=0,high=ar.length;
while(low<=high) {
int mid=(low+high)/2;
if(x>ar[mid]) {
low=mid+1;
}
else if(x<ar[mid]) {
high=mid-1;
}
else {
return mid;
}
}
return -1;
}
}
public static void main(String[] args) {
FastReader s= new FastReader();
int n=s.nextInt();
long size=s.nextLong(); long persec=s.nextLong();
int [] pot= new int [n];
long sum=0,time=0;
for(int i=0;i<n;i++) {
pot[i]=s.nextInt();
if(sum+pot[i]>size) {
sum=0;time++;
}
time+=(sum+pot[i])/persec;
sum=(sum+pot[i])%persec;
}
if(sum==0)
System.out.println(time);
else
System.out.println(time+1);
}
} | Java | ["5 6 3\n5 4 3 2 1", "5 6 3\n5 5 5 5 5", "5 6 3\n1 2 1 1 1"] | 1 second | ["5", "10", "2"] | NoteConsider the first sample. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds.In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds. | Java 11 | standard input | [
"implementation",
"math"
] | 5099a9ae62e82441c496ac37d92e99e3 | The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 109) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains n integers ai (1 ≤ ai ≤ h) — the heights of the pieces. | 1,400 | Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement. | standard output | |
PASSED | c835886e3dd0731bd21a925a967bd4f1 | train_000.jsonl | 1464798900 | Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.Vanya has n pieces of potato, the height of the i-th piece is equal to ai. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens: If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. Processor smashes k centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed. | 256 megabytes | import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // 5
int h = sc.nextInt(); // 6
int k = sc.nextInt(); // 3
int a[] = new int[n];
for(int i = 0;i < n;i++) {
a[i] = sc.nextInt();
}
long seconds = 0;
int currentAmount = 0;
// 5 4 3 2 1
for(int i = 0;i < n;i++) {
if(currentAmount + a[i] <= h) { // 6 + 3
currentAmount += a[i]; // current = 5
} else {
// while( currentAmount + a[i] > h) {// 4 + 5 > 6
// currentAmount = Math.max(currentAmount - k, 0); // current = 2
// seconds++; // seconds = 1
// }
// currentAmount += a[i];
// 3
int temp1 = h - a[i]; // 6 - 3 = 3
int temp2 = currentAmount - temp1;// 6 - 3 = 3
temp2 =(int) Math.ceil(temp2 * 1.0/k * 1.0); // 1
seconds += (long) temp2; //
currentAmount = Math.max(currentAmount - temp2 * k, 0); //
currentAmount += a[i]; //
}
}
seconds += (long) Math.ceil(currentAmount * 1.0 / k * 1.0);
System.out.println(seconds);
}
}
| Java | ["5 6 3\n5 4 3 2 1", "5 6 3\n5 5 5 5 5", "5 6 3\n1 2 1 1 1"] | 1 second | ["5", "10", "2"] | NoteConsider the first sample. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds.In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds. | Java 11 | standard input | [
"implementation",
"math"
] | 5099a9ae62e82441c496ac37d92e99e3 | The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 109) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains n integers ai (1 ≤ ai ≤ h) — the heights of the pieces. | 1,400 | Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement. | standard output | |
PASSED | 0607e9a3b03707acbd5994620b5d1966 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int numeros[],anterior,l,actual;
int casos = Integer.parseInt(bf.readLine());
StringTokenizer tk;
boolean funciona;
for (int i = 0; i < casos; i++) {
anterior=0;
funciona=false;
l=Integer.parseInt(bf.readLine());
numeros=new int[l+1];
tk=new StringTokenizer(bf.readLine());
for (int j = 0; j < l; j++) {
actual=Integer.parseInt(tk.nextToken());
if(j<2) {
numeros[actual]++;
anterior=actual;
}else {
numeros[actual]++;
if(numeros[actual]==2) {
if(anterior!=actual) {
funciona=true;
break;
}
}else if(numeros[actual]>2) {
funciona=true;
break;
}
anterior=actual;
}
}
System.out.println(funciona?"YES":"NO");
}
}
}
| Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | bde1442a7812ce7b6f02d30abfd6eb5e | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.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();
while(t-->0){
int n=sc.nextInt();
int arr[]=new int [n+1];
int buf[]=new int[5005];
boolean flag=false;
for(int i=1;i<=n;i++){
arr[i]=sc.nextInt();
if(buf[arr[i]]!=0){
if(i-buf[arr[i]]>1){
flag=true;
}
}else{
buf[arr[i]]=i;
}
}
System.out.println(flag?"YES":"NO");
}
}
}
| Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | a4370665e173b561baeffeca0b3edf74 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
static FastScanner in;
static PrintWriter out;
public static void main(String[] args){
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
int T = in.nextInt();
for (int t = 0; t < T; ++t){
int n = in.nextInt();
int[] word = new int[n];
for (int i = 0; i < n; ++i){
word[i] = in.nextInt();
}
boolean found = false;
for (int i = 0; i < n - 2; ++i){
for (int j = i + 2; j < n; ++j)
if (word[i] == word[j]){
found = true;
break;
}
}
String ans = "NO";
if (found){
ans = "YES";
}
out.println(ans);
}
out.close();
}
public static class FastScanner {
private BufferedReader br;
private StringTokenizer st;
public FastScanner(InputStream inputStream){
br = new BufferedReader(new InputStreamReader(inputStream));
}
public String nextLine() throws IOException {
return br.readLine();
}
public String next() {
while (st == null || !st.hasMoreTokens()){
try {
st = new StringTokenizer(br.readLine());
} catch (IOException ignored){}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public boolean nextBoolean() {
return Boolean.parseBoolean(next());
}
public void close() throws IOException {
br.close();
}
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 0bba50781aa79893347e0b371961e8a8 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.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 Main 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);
}
}
// 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;
}
public static void main(String args[]) throws Exception
{
new Thread(null, new Main(),"Main",1<<27).start();
}
public void run()
{
InputReader sc = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
Integer a[] = new Integer[n];
for (int i=0; i<n; i++) {
a[i] = sc.nextInt();
}
if(n<3)
System.out.println("NO");
else{
boolean f = false;
for (int i=0; i<n-1; i++) {
for (int j=i+1; j<n; j++) {
if(a[i]-a[j]==0 && (j-i)>1){
f = true;
break;
}
}
if(f)
break;
}
if(f)
System.out.println("YES");
else
System.out.println("NO");
}
}
System.out.flush();
w.close();
}
}
| Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 3298d2ce159c5d4aec53a3b37f4c070e | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* Use pen and paper. Solve on paper then code.
* If there is some reasoning e.g. sequence/paths, try printing first 100 elements or 100 answers using brute and observe.
* Read question with extreme caution. Sometimes we make question complex due to misunderstanding.
* Prefix sum and suffix sum is highly usable concept, look for it.
* Think of cleanest approach. If too many if else are coming then its indication of WA.
* https://codeforces.com/contest/1354/submission/80639237
*/
public class Codeforces {
public static void main(String args[]) throws IOException {
Reader in = new Reader();
// Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-->0) {
b(in);
}
in.close();
}
private static void b(Reader in) throws IOException {
int n = in.nextInt();
int a[] = new int[n];
for (int i=0;i<n;i++) a[i] = in.nextInt();
int l = 0;
boolean found = false;
for (int i=0;i<n && !found;i++) {
for (int j=i+2;j<n;j++) {
if (a[i] == a[j]) {
found = true;
break;
}
}
}
if (found) System.out.println("YES");
else System.out.println("NO");
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
StringTokenizer st;
BufferedReader br;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
br = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | b4272fa2b0dc67ca068e2f0e065338e8 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.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 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();
int p=0;
for(int k=0;k<n;k++){
for(int l=k+2;l<n;l=l+1){
if(a[k]==a[l]){
p=1;
}
}
}
if(p==1)
System.out.println("YES");
else
System.out.println("NO");
}
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | fb571e22d55294450f6e35f757ceeffc | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class G_YetAnotherPalindrome {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int cont;
int pal[];
int casos = Integer.parseInt(bf.readLine());
for (int i = 0; i < casos; i++) {
cont = 0;
int tam = Integer.parseInt(bf.readLine());
st = new StringTokenizer(bf.readLine());
pal = new int[tam+1];
for (int j = 0; j < tam; j++) {
pal[j] = Integer.parseInt(st.nextToken());
}
for(int l=0; l<tam-2; l++) {
for(int j=l+2; j<tam; j++) {
if(pal[l] == pal[j]) {
cont++;
break;
}
}
}
if(cont==0)
System.out.println("NO");
else
System.out.println("YES");
}
}
}
| Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | ae701041d860f96d9735be80892b9069 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.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.Scanner;
import java.util.StringTokenizer;
import java.util.*;
public class yetAnotherPalindromeProblem {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
int flag=0;
for(int i=0;i<n;i++)
{
for(int j=i+2;j<n;j++)
{
if(a[i]==a[j])
{
flag=1;
break;
}
}
}
if(flag==1)
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
}
}
| Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | ecadd35107d4e96ba352c85ac60d8ba2 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
static FastReader sc=new FastReader();
public static void main(String[] args)
{
//StringBuffer sb=new StringBuffer("");
int t = i();
outer :while (t-- > 0)
{
int n=i();
int A[]=input(n);
HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else
map.put(i, 1);
}
boolean res=false;
for(int i=0;i<n;i++) {
if(map.get(A[i])>=3) {
res=true;
break;
}
if(map.get(A[i])==2) {
int k=A[i];
int l=map.get(A[i]);
boolean r=false;
for(int j=i;j<n;j++) {
if(A[j]!=k&&l>0)
r=true;
else if(A[j]==k)
l--;
}
if(r&&l==0)
res=true;
l=0;
}
map.put(A[i],map.get(A[i])-1);
}
if(res)
System.out.println("YES");
else
System.out.println("NO");
}
//System.out.println(sb.toString());
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static void input(int A[],int B[]) {
for(int i=0;i<A.length;i++) {
A[i]=sc.nextInt();
B[i]=sc.nextInt();
}
}
static long mod(long x) {
int mod=1000000007;
return ((x%mod + mod)%mod);
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static void sort(int[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n-i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 1904173bf05406e0aabc1ac3efc74e32 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.math.*;
public class PalindromProblem
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int runs = sc.nextInt();
while(runs-->0)
{
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0;i<n;i++)
{
arr[i] = sc.nextInt();
}
boolean works = false;
for(int i = 0;i<n-2;i++)
{
for(int j = i+2;j<n;j++)
{
if(arr[i] == arr[j])
{
works = true;
break;
}
}
}
System.out.println(works?"YES":"NO");
}
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 515ac2e80e700642ee8c7a69fe285bb0 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class cppalin {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t = 0; t < T; t++) {
int n = in.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
boolean d=true;
for(int i=0;i<n-2;i++){
for(int j=i+2;j<n;j++){
if(a[i]==a[j]){
d=false;
break;
}
}
}
if(!d){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
}
}
| Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | e6993895480870879d6cf0cc458b9413 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class YAPP {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int[] a = new int[n];
boolean b = false;
for(int i=0;i<n;i++)
a[i] = sc.nextInt();
for(int i=0;i<=n-3;i++)
for(int j=i+2;j<=n-1;j++) {
if(a[i] == a[j]) {
b = true;
break;
}
}
if(b == true)
System.out.println("YES");
else
System.out.println("NO");
}
}
}
| Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 289be99202abb593064ea20b45b889b9 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class YetAnotherPalindromeProblem {
public static void main(String [] args)
{
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for(int i=0;i<t;i++) {
int n = scanner.nextInt();
int[] arr = new int[n];
for(int j=0;j<n;j++)
{
arr[j] = scanner.nextInt();
}
int flag = 0;
for(int j=0;j<n-2;j++)
{
int[] newArray = Arrays.copyOfRange(arr, j+2,n);
List<Integer> arrlist = IntStream.of(newArray).boxed().collect(Collectors.toList());
boolean test
= arrlist.contains(arr[j]);
if(test)
{
flag = 1;
System.out.println("YES");
break;
}
}
if(flag==0)
{
System.out.println("NO");
}
}
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 9f7a35d1b78b38221942a6bf2b50c87f | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class Palindromesubssoln {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
HashMap<Integer,Integer>map=new HashMap<>();
boolean flag=false;
for(int i=0;i<arr.length;i++) {
if(!map.containsKey(arr[i])) {
map.put(arr[i], i);
}else {
int val=map.get(arr[i]);
if(val+1!=i) {
flag=true;
}
}
if(flag) {
break;
}
}
if(flag) {
System.out.println("YES");
}else {
System.out.println("NO");
}
}
}
}
| Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | ddf00a011dcccc20bfe42003264ed34c | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class graph1
{
public static int fun(int [] seq)
{
int n = seq.length;
int i, j, cl;
int L[][] = new int[n][n];
for (i = 0; i < n; i++)
L[i][i] = 1;
for (cl=2; cl<=n; cl++)
{
for (i=0; i<n-cl+1; i++)
{
j = i+cl-1;
if (seq[i] == seq[j] && cl == 2)
L[i][j] = 2;
else if (seq[i] == seq[j] )
L[i][j] = L[i+1][j-1] + 2;
else
L[i][j] = Math.max(L[i][j-1], L[i+1][j]);
}
}
return L[0][n-1];
}
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0) {
int n=s.nextInt();
int []a=new int[n];
for (int i = 0; i < a.length; i++)
a[i]=s.nextInt();
int tt=fun(a);
if(tt>=3) System.out.println("YES");
else System.out.println("NO");
}
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 0268f7cd4a087d303372ac540e77935a | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes |
import java.util.*;
import java.io.*;
import java.math.*;
public class graph1
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0) {
int n=s.nextInt();
int []a=new int[n];
for (int i = 0; i < a.length; i++)
a[i]=s.nextInt();
int fl=0;
for (int i = 0; i <= a.length-3; i++) {
for(int j=i+2;j<n;j++) {
if(a[i]==a[j]) {
fl=1;
System.out.println("YES"); break;
}
}if(fl==1) break;
}
if(fl==0) System.out.println("NO");
}
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 84d2fcb2511db7e5472f086bb90ba5c9 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class Main{
public static void main(String ... string ){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
int a[] = new int[n] ;
for(int i = 0; i<n ;i++)
a[i] = sc.nextInt();
HashMap<Integer, Integer> map = new HashMap<>();
boolean ok = false;
for(int i = 0;i<n; i++){
if(!map.containsKey(a[i]))
map.put(a[i],i);
else {
int index = map.get(a[i]);
if(i-index > 1){
ok = true;
break;
}
}
}
if(ok) System.out.println("YES");
else System.out.println("NO");
}
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 052b5da9db694b38ed0d35eb33b81c9e | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Integer t = Integer.parseInt(sc.nextLine());
int elements[] = new int[5002];
for (int i = 0; i < t; i++) {
boolean found = false;
Integer n = sc.nextInt();
for (int j = 1; j <= n; j++) {
elements[j] = sc.nextInt();
for (int k = 1; k < j - 1; k++) {
if(elements[k] == elements[j])
found = true;
}
}
System.out.println((found)? "YES": "NO");
}
}
}
| Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 73f20734e3865442bfb7ea857a21722c | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Integer t = Integer.parseInt(sc.nextLine());
int elements[] = new int[5002];
for (int i = 0; i < t; i++) {
boolean found = false;
Integer n = sc.nextInt();
for (int j = 1; j <= n; j++) {
elements[j] = sc.nextInt();
for (int k = 1; k < j - 1; k++) {
if(elements[k] == elements[j])
found = true;
}
}
System.out.println((found == true)? "YES": "NO");
}
}
}
| Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | b7524cf3f2b50514520b05b655287eb3 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public int[] nextSortedIntArray(int n) {
int array[] = nextIntArray(n);
Arrays.sort(array);
return array;
}
public int[] nextSumIntArray(int n) {
int[] array = new int[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; ++i) array[i] = nextLong();
return array;
}
public long[] nextSumLongArray(int n) {
long[] array = new long[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextSortedLongArray(int n) {
long array[] = nextLongArray(n);
Arrays.sort(array);
return array;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader in = new FastReader();
solveB(in);
}
static void solveA(FastReader in) {
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
int oddCount = 0;
for (int j = 0; j < n; j++) {
if (in.nextInt() % 2 == 1) oddCount++;
}
if (oddCount == 0 || oddCount == n) System.out.println("YES");
else System.out.println("NO");
}
}
static void solveB(FastReader in) {
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
HashMap<Integer, Integer> index = new HashMap<>();
int[] arr = in.nextIntArray(n);
boolean flag = false;
for (int j = 0; j < n; j++) {
if (j - index.getOrDefault(arr[j], j) > 1) {
System.out.println("YES");
flag = true;
break;
}
if (!index.containsKey(arr[j])) index.put(arr[j], j);
}
if (!flag) System.out.println("NO");
}
}
static void solveC(FastReader in) {
int t = in.nextInt();
for (int i = 0; i < t; i++) {
String s = in.next();
int lastR = -1;
int maxJump = 0;
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == 'R') {
if (maxJump < j - lastR) maxJump = j - lastR;
lastR = j;
}
}
if (maxJump < s.length() - lastR) maxJump = s.length() - lastR;
System.out.println(maxJump);
}
}
static void solveD(FastReader in) {
int n = in.nextInt();
int[] arr = in.nextIntArray(n);
TreeSet<Integer> negSet = new TreeSet<>();
TreeSet<Integer> posSet = new TreeSet<>();
int zeroCount = 0;
int count = 0;
for (int i = 0; i < n; i++) {
arr[i] -= in.nextInt();
if (arr[i] > 0) {
count += posSet.size();
count += zeroCount;
count += negSet.headSet(arr[i] - 1).size();
posSet.add(arr[i]);
} else if (arr[i] < 0) {
count += posSet.tailSet(-arr[i] + 1).size();
negSet.add(-arr[i]);
} else {
zeroCount++;
count += posSet.size();
}
}
System.out.println(count);
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 001c8d61e3c07e4ef903710399a47e70 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.*;
import java.util.*;
public class main {
public static void main(String[] args) throws NumberFormatException, IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw= new PrintWriter(System.out);
int t = Integer.parseInt(br.readLine());
for (int i = 0; i < t; i++) {
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] a= new int[n];
for (int j = 0; j < a.length; j++) {
a[j]=Integer.parseInt(st.nextToken());
}
boolean possible=false;
for (int j = 0; j < n; j++) {
for (int k = j+2; k < n; k++) {
if(a[j]==a[k])
{
possible=true;break;
}
}
if(possible)break;
}
pw.println(possible?"YES":"NO");
}
pw.close();
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 319d56ac12525a46f8bf385e2408aede | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
boolean flag = false;
for (int j = 0; j < n; j++) {
int temp = in.nextInt();
if(map.containsKey(temp)){
//System.out.println(map.get(temp) + " " + j);
if(j - map.get(temp) >= 2){
flag = true;
}
}
else{
map.put(temp, j);
}
}
if(flag)
System.out.println("YES");
else
System.out.println("NO");
}
}
}
| Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | e34afd9fa9104552326c73db746ea2e1 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.io.*;
public class Main{
public static void main (String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
boolean flag = false;
while(n-->0) {
flag = false;
int l = sc.nextInt();
int[]a = new int[l];
for(int i = 0 ; i < l ; i++)
a[i] = sc.nextInt();
for(int i = 0 ; i < l-2 ; i++) {
for(int j = i+2 ; j<l ; j++) {
if(a[i] == a[j]) {
flag = true;
break;
}
}
}
if(flag)
pw.println("YES");
else
pw.println("NO");
}
pw.close();
}
}
class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws FileNotFoundException {
br = new BufferedReader(new FileReader(file));
}
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 boolean ready() throws IOException {
return br.ready();
}
}
| Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 2bafefee70ddbed93c5d8ac32171cda2 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.io.*;
public class Main{
public static void main (String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
boolean flag = false;
while(n-->0) {
flag = false;
int l = sc.nextInt();
int[]a = new int[l];
for(int i = 0 ; i < l ; i++)
a[i] = sc.nextInt();
for(int i = 0 ; i < l-2 ; i++) {
for(int j = i+2 ; j<l ; j++) {
if(a[i] == a[j])
flag = true;
}
}
if(flag)
pw.println("YES");
else
pw.println("NO");
}
pw.close();
}
}
class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws FileNotFoundException {
br = new BufferedReader(new FileReader(file));
}
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 boolean ready() throws IOException {
return br.ready();
}
}
| Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 8f8c010d3c58736703795ba8c42e8ba4 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class yetAnotherPalindromeProblem {
static FS sc = new FS();
static PrintWriter pw = new PrintWriter(System.out);
static int n;
static int[] arr;
public static void main(String[] args) {
int t = sc.nextInt();
scan : for(int tt = 1; tt <= t; ++tt) {
n = sc.nextInt();
arr = new int[n];
for(int i = 0; i < n; ++i) arr[i] = sc.nextInt();
for(int i = 0; i < n; ++i) {
// check starting from i + 2
for(int j = i + 2; j < n; ++j) {
if(arr[i] == arr[j]) {
pw.println("YES");
continue scan;
}
}
}
pw.println("NO");
}
pw.flush();
}
static class FS {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while(!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch(Exception e) {}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | b2e1db4d97e7d723133c1a28770c6c45 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
import java.io.*;
public class yetAnotherPalindromeProblem {
static FS sc = new FS();
static PrintWriter pw = new PrintWriter(System.out);
static int n;
static int[] arr;
public static void main(String[] args) {
int t = sc.nextInt();
scan : for(int tt = 1; tt <= t; ++tt) {
n = sc.nextInt();
arr = new int[n];
for(int i = 0; i < n; ++i) arr[i] = sc.nextInt();
boolean[] seen = new boolean[n + 1];
for(int i = 0; i < n; ++i) {
if(seen[arr[i]]) {
pw.println("YES");
continue scan;
}
// update seen table
if(i > 0) seen[arr[i - 1]] = true;
}
pw.println("NO");
}
pw.flush();
}
static class FS {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while(!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch(Exception e) {}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 60328f0ef4654327f3674e48a54495b2 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.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 nn=Integer.parseInt(sc.nextLine());
while(nn-->0)
{
// hash=new Hashtable<>();
int a=Integer.parseInt(sc.nextLine());
String hh[]=sc.nextLine().split(" ");
Hashtable<Integer,Integer>hash=new Hashtable<>();
int flag=0;
for(int i=0;i<a;i++)
{
int aa=Integer.parseInt(hh[i]);
if(!hash.containsKey(aa)){hash.put(aa,i);}
else if(i-hash.get(aa)>1){flag=1;break;}
}
if(flag==1){System.out.println("YES");}
else{System.out.println("NO");}
}
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 57e7a94baa2a16adcbf3090e4000dda5 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | import java.util.*;
public class susbus{
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int j=0;j<t;j++){
int n = sc.nextInt();
int [] vals = new int[n];
int [] first= new int[n];
int [] last = new int[n];
Arrays.fill(first,-1); Arrays.fill(last,-1);
for(int i=0;i<n;i++){
vals[i] = sc.nextInt()-1;
}
for(int i=0;i<n;i++){
if(first[vals[i]] == -1){
first[vals[i]] = i;
}
}
for(int i=n-1;i>=0;i--){
if(last[vals[i]] == -1){
last[vals[i]] = i;
}
}
boolean x = false;
for(int i=0;i<n;i++){
if(last[i]-first[i]>=2){
x = true;
break;
}
}
if(x){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 5924c8650eb61c2b8eb08321463b44b3 | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes | /*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
public class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0)
{
int n = Integer.parseInt(br.readLine());
int[] a = new int[n];
String line = br.readLine();
String[] str = line.trim().split("\\s+");
for(int i=0;i<n;i++)
a[i] = Integer.parseInt(str[i]);
boolean flag = false;
for(int i=0;i<n;i++)
for(int j=i+2;j<n;j++)
if(a[i]==a[j])
flag = true;
if(flag)
System.out.println("YES");
else
System.out.println("NO");
}
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 233d96c45d98097d84d3fd46c5d316ef | train_000.jsonl | 1584018300 | You are given an array $$$a$$$ consisting of $$$n$$$ integers.Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome.Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not.You have to answer $$$t$$$ independent test cases. | 256 megabytes |
import java.util.ArrayList;
import java.util.Scanner;
public class Main{
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();
ArrayList<Integer> list = new ArrayList<Integer>();
for(int j=0 ; j<n ; j++) {
list.add(sc.nextInt());
}
boolean ok = false;
for(int k=0; k<n;k++) {
if(ok == false) {
if(list.lastIndexOf(list.get(k)) > k+1 ) {
ok = true;
break;
}
}
}
if(ok == true) System.out.println("YES");
else System.out.println("NO");
}
sc.close();
}
} | Java | ["5\n3\n1 2 1\n5\n1 2 2 3 2\n3\n1 1 2\n4\n1 2 2 1\n10\n1 1 2 2 3 3 4 4 5 5"] | 2 seconds | ["YES\nYES\nNO\nYES\nNO"] | NoteIn the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome.In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$.In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes.In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$).In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | Java 8 | standard input | [
"brute force",
"strings"
] | 5139d222fbbfa70d50e990f5d6c92726 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 5000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$). | 1,100 | For each test case, print the answer — "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. | standard output | |
PASSED | 06b9e13698ac70a65216d86916476989 | train_000.jsonl | 1596119700 | Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.In the beginning, uncle Bogdan wrote on a board a positive integer $$$x$$$ consisting of $$$n$$$ digits. After that, he wiped out $$$x$$$ and wrote integer $$$k$$$ instead, which was the concatenation of binary representations of digits $$$x$$$ consists of (without leading zeroes). For example, let $$$x = 729$$$, then $$$k = 111101001$$$ (since $$$7 = 111$$$, $$$2 = 10$$$, $$$9 = 1001$$$).After some time, uncle Bogdan understood that he doesn't know what to do with $$$k$$$ and asked Denis to help. Denis decided to wipe last $$$n$$$ digits of $$$k$$$ and named the new number as $$$r$$$.As a result, Denis proposed to find such integer $$$x$$$ of length $$$n$$$ that $$$r$$$ (as number) is maximum possible. If there are multiple valid $$$x$$$ then Denis is interested in the minimum one.All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?Note: in this task, we compare integers ($$$x$$$ or $$$k$$$) as numbers (despite what representations they are written in), so $$$729 < 1999$$$ or $$$111 < 1000$$$. | 256 megabytes | import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int a = 0; a < t; a++) {
int n = sc.nextInt();
int numOf9 = n - (((n - 1) / 4) + 1);
char [] arr = new char[n];
for (int i = 0; i < n; i++) {
arr[i] = i >= numOf9 ? '8' : '9';
}
System.out.println(arr);
}
}
}
| Java | ["2\n1\n3"] | 2 seconds | ["8\n998"] | NoteIn the second test case (with $$$n = 3$$$), if uncle Bogdan had $$$x = 998$$$ then $$$k = 100110011000$$$. Denis (by wiping last $$$n = 3$$$ digits) will obtain $$$r = 100110011$$$.It can be proved that the $$$100110011$$$ is the maximum possible $$$r$$$ Denis can obtain and $$$998$$$ is the minimum $$$x$$$ to obtain it. | Java 8 | standard input | [
"greedy",
"math"
] | 80c75a4c163c6b63a614075e094ad489 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain test cases — one per test case. The one and only line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the integer $$$x$$$ you need to find. It's guaranteed that the sum of $$$n$$$ from all test cases doesn't exceed $$$2 \cdot 10^5$$$. | 1,000 | For each test case, print the minimum integer $$$x$$$ of length $$$n$$$ such that obtained by Denis number $$$r$$$ is maximum possible. | standard output | |
PASSED | e01acfa2cf6825c4e8df2372b79aab8e | train_000.jsonl | 1596119700 | Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.In the beginning, uncle Bogdan wrote on a board a positive integer $$$x$$$ consisting of $$$n$$$ digits. After that, he wiped out $$$x$$$ and wrote integer $$$k$$$ instead, which was the concatenation of binary representations of digits $$$x$$$ consists of (without leading zeroes). For example, let $$$x = 729$$$, then $$$k = 111101001$$$ (since $$$7 = 111$$$, $$$2 = 10$$$, $$$9 = 1001$$$).After some time, uncle Bogdan understood that he doesn't know what to do with $$$k$$$ and asked Denis to help. Denis decided to wipe last $$$n$$$ digits of $$$k$$$ and named the new number as $$$r$$$.As a result, Denis proposed to find such integer $$$x$$$ of length $$$n$$$ that $$$r$$$ (as number) is maximum possible. If there are multiple valid $$$x$$$ then Denis is interested in the minimum one.All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?Note: in this task, we compare integers ($$$x$$$ or $$$k$$$) as numbers (despite what representations they are written in), so $$$729 < 1999$$$ or $$$111 < 1000$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(buf.readLine());
for (int i = 0; i< T; i++) {
int n = Integer.parseInt(buf.readLine());
ArrayList<Integer> list = new ArrayList<Integer>();
int e8s = (n%4 == 0) ? n/4 : (n/4)+1;
for(int j = 0; j< (n-e8s); j++) {
list.add(9);
}
for (int j=0; j<e8s; j++) {
list.add(8);
}
for (int j = 0; j<list.size(); j++)
System.out.print(list.get(j));
System.out.println();
}
}
} | Java | ["2\n1\n3"] | 2 seconds | ["8\n998"] | NoteIn the second test case (with $$$n = 3$$$), if uncle Bogdan had $$$x = 998$$$ then $$$k = 100110011000$$$. Denis (by wiping last $$$n = 3$$$ digits) will obtain $$$r = 100110011$$$.It can be proved that the $$$100110011$$$ is the maximum possible $$$r$$$ Denis can obtain and $$$998$$$ is the minimum $$$x$$$ to obtain it. | Java 8 | standard input | [
"greedy",
"math"
] | 80c75a4c163c6b63a614075e094ad489 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain test cases — one per test case. The one and only line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the integer $$$x$$$ you need to find. It's guaranteed that the sum of $$$n$$$ from all test cases doesn't exceed $$$2 \cdot 10^5$$$. | 1,000 | For each test case, print the minimum integer $$$x$$$ of length $$$n$$$ such that obtained by Denis number $$$r$$$ is maximum possible. | standard output | |
PASSED | 4c81ef8dab44d5cd43c1b727c62ce985 | train_000.jsonl | 1596119700 | Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.In the beginning, uncle Bogdan wrote on a board a positive integer $$$x$$$ consisting of $$$n$$$ digits. After that, he wiped out $$$x$$$ and wrote integer $$$k$$$ instead, which was the concatenation of binary representations of digits $$$x$$$ consists of (without leading zeroes). For example, let $$$x = 729$$$, then $$$k = 111101001$$$ (since $$$7 = 111$$$, $$$2 = 10$$$, $$$9 = 1001$$$).After some time, uncle Bogdan understood that he doesn't know what to do with $$$k$$$ and asked Denis to help. Denis decided to wipe last $$$n$$$ digits of $$$k$$$ and named the new number as $$$r$$$.As a result, Denis proposed to find such integer $$$x$$$ of length $$$n$$$ that $$$r$$$ (as number) is maximum possible. If there are multiple valid $$$x$$$ then Denis is interested in the minimum one.All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?Note: in this task, we compare integers ($$$x$$$ or $$$k$$$) as numbers (despite what representations they are written in), so $$$729 < 1999$$$ or $$$111 < 1000$$$. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
public class Solution
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for(int i = 0; i < t; i++)
{
int n = input.nextInt();
int a = 0;
if(n%4 == 0)
{
a = n/4;
}
else
{
a=1 + n/4;
}
int res = 0;
for(int j = n; j > a; j--)
{
System.out.print(9);
}
for(int j = a; j >=1; j--)
{
System.out.print(8);
}
System.out.println();
}
}
} | Java | ["2\n1\n3"] | 2 seconds | ["8\n998"] | NoteIn the second test case (with $$$n = 3$$$), if uncle Bogdan had $$$x = 998$$$ then $$$k = 100110011000$$$. Denis (by wiping last $$$n = 3$$$ digits) will obtain $$$r = 100110011$$$.It can be proved that the $$$100110011$$$ is the maximum possible $$$r$$$ Denis can obtain and $$$998$$$ is the minimum $$$x$$$ to obtain it. | Java 8 | standard input | [
"greedy",
"math"
] | 80c75a4c163c6b63a614075e094ad489 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain test cases — one per test case. The one and only line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the integer $$$x$$$ you need to find. It's guaranteed that the sum of $$$n$$$ from all test cases doesn't exceed $$$2 \cdot 10^5$$$. | 1,000 | For each test case, print the minimum integer $$$x$$$ of length $$$n$$$ such that obtained by Denis number $$$r$$$ is maximum possible. | standard output | |
PASSED | e2186359e7d7c6fc747a6e6890aaf678 | train_000.jsonl | 1596119700 | Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.In the beginning, uncle Bogdan wrote on a board a positive integer $$$x$$$ consisting of $$$n$$$ digits. After that, he wiped out $$$x$$$ and wrote integer $$$k$$$ instead, which was the concatenation of binary representations of digits $$$x$$$ consists of (without leading zeroes). For example, let $$$x = 729$$$, then $$$k = 111101001$$$ (since $$$7 = 111$$$, $$$2 = 10$$$, $$$9 = 1001$$$).After some time, uncle Bogdan understood that he doesn't know what to do with $$$k$$$ and asked Denis to help. Denis decided to wipe last $$$n$$$ digits of $$$k$$$ and named the new number as $$$r$$$.As a result, Denis proposed to find such integer $$$x$$$ of length $$$n$$$ that $$$r$$$ (as number) is maximum possible. If there are multiple valid $$$x$$$ then Denis is interested in the minimum one.All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?Note: in this task, we compare integers ($$$x$$$ or $$$k$$$) as numbers (despite what representations they are written in), so $$$729 < 1999$$$ or $$$111 < 1000$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class B {
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
int c = in.nextInt();
for (int cases = 0; cases < c; cases++) {
int n = in.nextInt();
for (int i = 0; i < n - ((n - 1) / 4 + 1); i++) {
System.out.print(9);
}
for (int i = 0; i < (n - 1) / 4 + 1; i++) {
System.out.print(8);
}
System.out.println();
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream i) {
br = new BufferedReader(new InputStreamReader(i));
st = new StringTokenizer("");
}
public String next() throws IOException {
if (st.hasMoreTokens()) {
return st.nextToken();
}
else
st = new StringTokenizer(br.readLine());
return next();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
return nextLine();
}
String ret = "";
while (st.hasMoreTokens()) {
ret += st.nextToken();
}
return ret;
}
public int[] nextIntArr(int size) throws IOException {
int[] arr = new int[size];
for (int i = 0; i < arr.length; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArr(int size) throws IOException {
long[] arr = new long[size];
for (int i = 0; i < arr.length; i++) {
arr[i] = nextLong();
}
return arr;
}
public double[] nextDoubleArr(int size) throws IOException {
double[] arr = new double[size];
for (int i = 0; i < arr.length; i++) {
arr[i] = nextDouble();
}
return arr;
}
}
} | Java | ["2\n1\n3"] | 2 seconds | ["8\n998"] | NoteIn the second test case (with $$$n = 3$$$), if uncle Bogdan had $$$x = 998$$$ then $$$k = 100110011000$$$. Denis (by wiping last $$$n = 3$$$ digits) will obtain $$$r = 100110011$$$.It can be proved that the $$$100110011$$$ is the maximum possible $$$r$$$ Denis can obtain and $$$998$$$ is the minimum $$$x$$$ to obtain it. | Java 8 | standard input | [
"greedy",
"math"
] | 80c75a4c163c6b63a614075e094ad489 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain test cases — one per test case. The one and only line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the integer $$$x$$$ you need to find. It's guaranteed that the sum of $$$n$$$ from all test cases doesn't exceed $$$2 \cdot 10^5$$$. | 1,000 | For each test case, print the minimum integer $$$x$$$ of length $$$n$$$ such that obtained by Denis number $$$r$$$ is maximum possible. | standard output | |
PASSED | a402b3ed153f5e605adb9aee2ebbdf27 | train_000.jsonl | 1596119700 | Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.In the beginning, uncle Bogdan wrote on a board a positive integer $$$x$$$ consisting of $$$n$$$ digits. After that, he wiped out $$$x$$$ and wrote integer $$$k$$$ instead, which was the concatenation of binary representations of digits $$$x$$$ consists of (without leading zeroes). For example, let $$$x = 729$$$, then $$$k = 111101001$$$ (since $$$7 = 111$$$, $$$2 = 10$$$, $$$9 = 1001$$$).After some time, uncle Bogdan understood that he doesn't know what to do with $$$k$$$ and asked Denis to help. Denis decided to wipe last $$$n$$$ digits of $$$k$$$ and named the new number as $$$r$$$.As a result, Denis proposed to find such integer $$$x$$$ of length $$$n$$$ that $$$r$$$ (as number) is maximum possible. If there are multiple valid $$$x$$$ then Denis is interested in the minimum one.All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?Note: in this task, we compare integers ($$$x$$$ or $$$k$$$) as numbers (despite what representations they are written in), so $$$729 < 1999$$$ or $$$111 < 1000$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class B {
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
int c = in.nextInt();
for (int cases = 0; cases < c; cases++) {
int n = in.nextInt();
for (int i = 0; i < n - ((n - 1) / 4 + 1); i++) {
System.out.print(9);
}
for (int i = 0; i < (n - 1) / 4 + 1; i++) {
System.out.print(8);
}
System.out.println();
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream i) {
br = new BufferedReader(new InputStreamReader(i));
st = new StringTokenizer("");
}
public String next() throws IOException {
if (st.hasMoreTokens()) {
return st.nextToken();
}
else
st = new StringTokenizer(br.readLine());
return next();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
return nextLine();
}
String ret = "";
while (st.hasMoreTokens()) {
ret += st.nextToken();
}
return ret;
}
public int[] nextIntArr(int size) throws IOException {
int[] arr = new int[size];
for (int i = 0; i < arr.length; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArr(int size) throws IOException {
long[] arr = new long[size];
for (int i = 0; i < arr.length; i++) {
arr[i] = nextLong();
}
return arr;
}
public double[] nextDoubleArr(int size) throws IOException {
double[] arr = new double[size];
for (int i = 0; i < arr.length; i++) {
arr[i] = nextDouble();
}
return arr;
}
}
} | Java | ["2\n1\n3"] | 2 seconds | ["8\n998"] | NoteIn the second test case (with $$$n = 3$$$), if uncle Bogdan had $$$x = 998$$$ then $$$k = 100110011000$$$. Denis (by wiping last $$$n = 3$$$ digits) will obtain $$$r = 100110011$$$.It can be proved that the $$$100110011$$$ is the maximum possible $$$r$$$ Denis can obtain and $$$998$$$ is the minimum $$$x$$$ to obtain it. | Java 8 | standard input | [
"greedy",
"math"
] | 80c75a4c163c6b63a614075e094ad489 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain test cases — one per test case. The one and only line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the integer $$$x$$$ you need to find. It's guaranteed that the sum of $$$n$$$ from all test cases doesn't exceed $$$2 \cdot 10^5$$$. | 1,000 | For each test case, print the minimum integer $$$x$$$ of length $$$n$$$ such that obtained by Denis number $$$r$$$ is maximum possible. | standard output | |
PASSED | 55529bcbc2d0f93257a606fed254b9c1 | train_000.jsonl | 1596119700 | Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.In the beginning, uncle Bogdan wrote on a board a positive integer $$$x$$$ consisting of $$$n$$$ digits. After that, he wiped out $$$x$$$ and wrote integer $$$k$$$ instead, which was the concatenation of binary representations of digits $$$x$$$ consists of (without leading zeroes). For example, let $$$x = 729$$$, then $$$k = 111101001$$$ (since $$$7 = 111$$$, $$$2 = 10$$$, $$$9 = 1001$$$).After some time, uncle Bogdan understood that he doesn't know what to do with $$$k$$$ and asked Denis to help. Denis decided to wipe last $$$n$$$ digits of $$$k$$$ and named the new number as $$$r$$$.As a result, Denis proposed to find such integer $$$x$$$ of length $$$n$$$ that $$$r$$$ (as number) is maximum possible. If there are multiple valid $$$x$$$ then Denis is interested in the minimum one.All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?Note: in this task, we compare integers ($$$x$$$ or $$$k$$$) as numbers (despite what representations they are written in), so $$$729 < 1999$$$ or $$$111 < 1000$$$. | 256 megabytes | import java.lang.*;
import java.util.*;
public class ct1 {
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
left=null;
right=null;
}
}
static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
int n = inp.nextInt();
while (n-->0){
int a = inp.nextInt();
if(a == 1)
System.out.println(8);
else if(a==2)
System.out.println(98);
else if (a==3)
System.out.println(998);
else {
StringBuilder x = new StringBuilder();
for (int i=0; i<a; i++){
x.append("1001");
}
int j = x.length()-1, k=1;
while(k<=a){
if(!(k%4==0))
x.setCharAt(j, '0');
k++;
j--;
}
StringBuilder b = new StringBuilder();
for(int i=0; i<x.length(); i+=4){
b.append(binaryToDecimal(x.substring(i, i+4)));
}
System.out.println(b);
}
}
}
static long binaryToDecimal(String n)
{
String num = n;
long dec_value = 0;
// Initializing base value to 1,
// i.e 2^0
long base = 1;
int len = num.length();
for (int i = len - 1; i >= 0; i--) {
if (num.charAt(i) == '1')
dec_value += base;
base = base * 2;
}
return dec_value;
}
static HashSet<String> a = new HashSet<>();
static HashSet<Integer> b = new HashSet<>();
public static boolean wordBreak(String s, List<String> w) {
for(int i=0; i<w.size(); i++){
a.add(w.get(i));
}
return res(s, 0, s.length());
}
static boolean res(String s, int i, int j){
if(a.contains(s.substring(i, j)))
return true;
if(b.contains(i))
return true;
for(int k=i+1; k<j; k++){
if(a.contains(s.substring(0, k)))
if(res(s, k, j)){
b.add(i);
return true;
}
}
return false;
}
} | Java | ["2\n1\n3"] | 2 seconds | ["8\n998"] | NoteIn the second test case (with $$$n = 3$$$), if uncle Bogdan had $$$x = 998$$$ then $$$k = 100110011000$$$. Denis (by wiping last $$$n = 3$$$ digits) will obtain $$$r = 100110011$$$.It can be proved that the $$$100110011$$$ is the maximum possible $$$r$$$ Denis can obtain and $$$998$$$ is the minimum $$$x$$$ to obtain it. | Java 8 | standard input | [
"greedy",
"math"
] | 80c75a4c163c6b63a614075e094ad489 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain test cases — one per test case. The one and only line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the integer $$$x$$$ you need to find. It's guaranteed that the sum of $$$n$$$ from all test cases doesn't exceed $$$2 \cdot 10^5$$$. | 1,000 | For each test case, print the minimum integer $$$x$$$ of length $$$n$$$ such that obtained by Denis number $$$r$$$ is maximum possible. | standard output | |
PASSED | f381239710ad1b1ab7825093e15fb727 | train_000.jsonl | 1596119700 | Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.In the beginning, uncle Bogdan wrote on a board a positive integer $$$x$$$ consisting of $$$n$$$ digits. After that, he wiped out $$$x$$$ and wrote integer $$$k$$$ instead, which was the concatenation of binary representations of digits $$$x$$$ consists of (without leading zeroes). For example, let $$$x = 729$$$, then $$$k = 111101001$$$ (since $$$7 = 111$$$, $$$2 = 10$$$, $$$9 = 1001$$$).After some time, uncle Bogdan understood that he doesn't know what to do with $$$k$$$ and asked Denis to help. Denis decided to wipe last $$$n$$$ digits of $$$k$$$ and named the new number as $$$r$$$.As a result, Denis proposed to find such integer $$$x$$$ of length $$$n$$$ that $$$r$$$ (as number) is maximum possible. If there are multiple valid $$$x$$$ then Denis is interested in the minimum one.All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?Note: in this task, we compare integers ($$$x$$$ or $$$k$$$) as numbers (despite what representations they are written in), so $$$729 < 1999$$$ or $$$111 < 1000$$$. | 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.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Gaurav
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Reader in = new Reader(inputStream);
Output out = new Output(outputStream);
TaskB solver = new TaskB();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, Reader in, Output out) {
int n = in.readInt();
StringBuilder s = new StringBuilder();
int k = (n - 1) / 4 + 1;
for (int i = 0; i < n - k; i++) s.append('9');
for (int i = 0; i < k; i++)
s.append('8');
out.printLine(s.toString());
}
}
static class Output {
private final PrintWriter wtr;
public Output(OutputStream outputStream) {
wtr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public Output(Writer wtr) {
this.wtr = new PrintWriter(wtr);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
wtr.print(' ');
}
wtr.print(objects[i]);
}
wtr.flush();
}
public void printLine(Object... objects) {
print(objects);
wtr.println();
wtr.flush();
}
public void close() {
wtr.close();
}
}
static class Reader {
private InputStream str;
private byte[] buf = new byte[1024];
private int curCh;
private int noCh;
private Reader.SpaceCharFilter fltr;
public Reader(InputStream strim) {
this.str = strim;
}
public int read() {
if (noCh == -1) {
throw new InputMismatchException();
}
if (curCh >= noCh) {
curCh = 0;
try {
noCh = str.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (noCh <= 0) {
return -1;
}
}
return buf[curCh++];
}
public int readInt() {
int c = read();
while (isspsch(c)) {
c = read();
}
int sin = 1;
if (c == '-') {
sin = -1;
c = read();
}
int result = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
result *= 10;
result += c - '0';
c = read();
} while (!isspsch(c));
return result * sin;
}
public String readString() {
int c = read();
while (isspsch(c)) {
c = read();
}
StringBuilder result = new StringBuilder();
do {
result.appendCodePoint(c);
c = read();
} while (!isspsch(c));
return result.toString();
}
public boolean isspsch(int c) {
if (fltr != null) {
return fltr.isspsch(c);
}
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isspsch(int ch);
}
}
}
| Java | ["2\n1\n3"] | 2 seconds | ["8\n998"] | NoteIn the second test case (with $$$n = 3$$$), if uncle Bogdan had $$$x = 998$$$ then $$$k = 100110011000$$$. Denis (by wiping last $$$n = 3$$$ digits) will obtain $$$r = 100110011$$$.It can be proved that the $$$100110011$$$ is the maximum possible $$$r$$$ Denis can obtain and $$$998$$$ is the minimum $$$x$$$ to obtain it. | Java 8 | standard input | [
"greedy",
"math"
] | 80c75a4c163c6b63a614075e094ad489 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain test cases — one per test case. The one and only line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the integer $$$x$$$ you need to find. It's guaranteed that the sum of $$$n$$$ from all test cases doesn't exceed $$$2 \cdot 10^5$$$. | 1,000 | For each test case, print the minimum integer $$$x$$$ of length $$$n$$$ such that obtained by Denis number $$$r$$$ is maximum possible. | standard output | |
PASSED | 2986b3d9ec2073bbf061c1688b6cdda0 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.ObjectInputStream.GetField;
import java.math.BigInteger;
import java.security.KeyStore.Entry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
public class Main {
static FastReader in = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
static boolean file = false;
static final int maxn = (int)5e5 + 111;
static int inf = (int)1e9;
static long n,m;
static int glob_pos;
static long a[] = new long[maxn];
static long T[] = new long[4*maxn];
private static void solve() throws Exception {
String s1 = in.nextToken();
String s2 = in.nextToken();
int pos = 0;
int lpos=-1,rpos=-1;
for (int i=0; i<s2.length(); i++) {
if (s1.charAt(pos)==s2.charAt(i)) {
pos++;
}
if (pos>s1.length()-1) {
lpos = i;
break;
}
}
pos = s1.length()-1;
for (int i=s2.length()-1; i>lpos; i--) {
if (s1.charAt(pos)==s2.charAt(i)) {
pos--;
}
if (pos<0) {
rpos = i;
break;
}
}
if (lpos==-1 || rpos==-1) out.println(0);
else {
out.println(rpos-lpos);
}
}
public static void main (String [] args) throws Exception {
if (file) {
in = new FastReader(new BufferedReader(new FileReader("input.txt")));
out = new PrintWriter ("output.txt");
}
solve();
out.close();
}
}
class Pair implements Comparable<Pair> {
int x,y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
Pair p = (Pair)obj;
if (p.x == x && p.y==y) return true;
return false;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return x;
}
@Override
public int compareTo(Pair p) {
if (x<p.x)return 1;
else if (x==p.x) return 0;
else return -1;
}
}
class FastReader {
BufferedReader bf;
StringTokenizer tk = null;
public FastReader(BufferedReader bf) {
this.bf = bf;
}
public String nextToken () throws Exception {
if (tk==null || !tk.hasMoreTokens()) {
tk = new StringTokenizer(bf.readLine());
}
return tk.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
} | Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | b79652b5560d5cc5d119b62e3fb2b9fd | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.*;
import java.util.*;
public class DoubleName {
public static final String taskname = "DoubleName";
public static BufferedReader inr;
public static PrintWriter out;
public static void main(String[] args) {
Locale.setDefault(Locale.US);
boolean online = System.getProperty("ONLINE_JUDGE") != null;
try {
inr = new BufferedReader(online ? new InputStreamReader(System.in, "ISO-8859-1") : new FileReader(taskname + ".in"));
out = new PrintWriter(online ? new OutputStreamWriter(System.out, "ISO-8859-1") : new FileWriter(taskname + ".out"));
solve();
} catch (Exception e) {
if (!online)
e.printStackTrace();
System.exit(9000);
} finally {
out.flush();
out.close();
}
}
// Solution
public static void solve() throws NumberFormatException, IOException {
String s = inr.readLine();
String t = inr.readLine();
int ti_left = 0;
for(int si = 0; si < s.length(); si++) {
while(ti_left < t.length() && s.charAt(si) != t.charAt(ti_left)) {
ti_left++;
}
if (ti_left >= t.length()) {
out.print(0);
return;
}
ti_left++;
}
int ti_right = t.length() - 1;
for(int si = s.length() - 1; si >= 0; si--) {
while(ti_right >= 0 && s.charAt(si) != t.charAt(ti_right)) {
ti_right--;
}
if (ti_right < 0) {
// well, this shouldn't happen, but
out.print(0);
return;
}
ti_right--;
}
out.print(Math.max(0, ti_right - ti_left + 2));
}
} | Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 31cd7a80fd583017929f743171901d0a | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class A {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String key = bf.readLine();
String word = bf.readLine();
int index1 = indexOfFirstMatch(key, word);
int index2 = indexOfFirstMatch(new StringBuilder(key).reverse().toString(), new StringBuilder(word).reverse().toString());
index2 = word.length() - index2 - 1;
// System.out.println(index1 + " " + index2);
if(index1 == -1) System.out.println("0");
else if(index2 <= index1) System.out.println("0");
else System.out.println((index2 - index1));
// Scanner scan = new Scanner(System.in);
// PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// int n = Integer.parseInt(bf.readLine());
// StringTokenizer st = new StringTokenizer(bf.readLine());
// int[] a = new int[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken());
// int n = Integer.parseInt(st.nextToken());
// int n = scan.nextInt();
// out.close(); System.exit(0);
}
public static int indexOfFirstMatch(String key, String word) {
char[] keyArr = key.toCharArray();
//System.out.println(Arrays.toString(keyArr));
char[] wordArr = word.toCharArray();
int index = 0;
int index2 = 0;
while(index < keyArr.length && index2 < wordArr.length) {
if(wordArr[index2] == keyArr[index])
index++;
index2++;
//System.out.println(index + " " + index2);
}
if(index == keyArr.length) return index2-1;
else return -1;
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 77e9902ca6533b3f227f5df585f5af6a | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
*
* @author pter9_000
*/
public class Ctask {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
String t = br.readLine();
int c = 0;
// for(int i=s.length(); i<t.length()-s.length()+1; i++){
// String p1 = t.substring(0,i);
// String p2 = t.substring(i, t.length());
//
// //System.out.println(p1+"+"+p2);
// if(goodString(p1,s)){
// if(goodString(p2,s)){
// c++;
// }
// }
//
// }
int start = 0;
int end = 0;
int sp = 0;
boolean first = false;
int fstart = 0;
int fend = 0;
int lstart = 0;
int lend = 0;
for(int i=0;i<t.length();i++){
if(t.charAt(i)==s.charAt(sp) ){
if(sp==0){start=i;}
sp++;
}
if(sp==s.length()){end=i;sp=0;
//System.out.println("st="+start+";end="+end);
if (first==false){fstart = start; fend = end; first=true; break;}
}
}
sp=s.length()-1;
first=false;
for(int i=t.length()-1;i>0;i--){
if(t.charAt(i)==s.charAt(sp) ){
if(sp==s.length()-1){start=i;}
sp--;
}
if(sp==-1){end=i;sp=0;
//System.out.println("st="+end+";end="+start);
if (first==false){lstart = end; lend = start; first=true; break;}
}
}
c = lstart-fend;
if(c<0){c=0;}
System.out.println(c);
}
public static boolean goodString(String stack,String needle){
int np=0;
for(int i =0;i<stack.length();i++){
if(stack.charAt(i)==needle.charAt(np)){
np++;
if(np==needle.length()){ return true;}
}
}
return false;
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 3aca7ae9ad4280c66edd0c3309068e44 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.util.Scanner;
public class TaskC
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
String text = scanner.nextLine();
// ищем позицию на которой заканчивается первое совпадение
int textLen = text.length();
int nameLen = name.length();
int namePos = 0;
int firstEndPos = 0;
for(int i = 0;i < textLen;i++) {
if(text.charAt(i) == name.charAt(namePos)) {
namePos++;
if(namePos == nameLen) {
firstEndPos = i;
break;
}
}
}
// ищем позицию на которой начинается последнее совпадение (идем с конца, до последнего символа первого совпадения)
int lastStartPos = firstEndPos;
namePos = nameLen - 1;
for(int i = textLen - 1;i >= firstEndPos;i--) {
if(text.charAt(i) == name.charAt(namePos)) {
namePos--;
if(namePos == -1) {
lastStartPos = i;
break;
}
}
}
System.out.println(lastStartPos - firstEndPos);
scanner.close();
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | f8b4ed5f52342bcbdc1ef7082399a383 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.util.Scanner ;
public class Main {
public static void main(String[] args){
Scanner cin = new Scanner(System.in) ;
System.out.println(new Solve(cin.nextLine() , cin.nextLine() ).run()) ;
}
}
class Solve{
private String word ;
private String text ;
public Solve(String word , String text){
this.word = word ;
this.text = text ;
}
public int run(){
int i = 0 , j = 0 , idfirst = -1 , idsecond = -1 ;
while(i < word.length() && j < text.length()){
if(word.charAt(i) == text.charAt(j)){
i++ ;
if(i == word.length()) idfirst = j ;
}
j++ ;
}
i = word.length() - 1 ;
j = text.length() - 1 ;
while(i >= 0 && j >= 0){
if(word.charAt(i) == text.charAt(j)){
i-- ;
if(i == -1) idsecond = j ;
}
j-- ;
}
if(idfirst == -1) return 0 ;
if(idfirst >= idsecond) return 0 ;
else return idsecond - idfirst ;
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | a0e452d5a93e126ecabb74958ff57f6a | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes |
import java.io.*;
import java.util.StringTokenizer;
public final class ProblemC {
private static Scanner in;
private static Output out;
public static void solve() throws Exception {
String n = in.nextLine();
String t = in.nextLine();
int end = -1;
int l = 0;
for (int i = 0; i < n.length(); i++) {
char c = n.charAt(i);
boolean found = false;
for (int j = l; j < t.length(); j++) {
if (t.charAt(j) == c) {
l = j;
if (i != n.length() - 1) {
l++;
}
found = true;
break;
}
}
if (!found) {
out.println(0);
return;
}
}
int r = t.length() - 1;
for (int i = n.length() - 1; i > -1; i--) {
char c = n.charAt(i);
boolean found = false;
for (int j = r; j > -1; j--) {
if (t.charAt(j) == c) {
r = j;
if (i != 0) {
r--;
}
found = true;
break;
}
}
if (!found) {
out.println(0);
return;
}
}
if (r - l <= 0) {
out.println(0);
} else {
out.println(r - l);
}
}
static Exception exception;
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new Runnable() {
@Override
public void run() {
try {
initReaderWriter();
solve();
out.close();
} catch (Exception ex) {
exception = ex;
}
}
}, "", 1 << 26);
thread.start();
thread.join();
if (exception != null) {
throw exception;
}
}
private static void initReaderWriter() throws Exception {
boolean isFile = false;
if (isFile) {
in = new Scanner("input.txt");
out = new Output(new File("output.txt"));
} else {
in = new Scanner();
out = new Output(System.out);
}
}
private static boolean log = false;
public static void log(String msg) {
if (log) {
out.println(msg);
out.flush();
}
}
private static class Scanner {
StringTokenizer st = null;
BufferedReader bf;
public Scanner() {
bf = new BufferedReader(new InputStreamReader(System.in));
}
public Scanner(String fileName) throws FileNotFoundException {
bf = new BufferedReader(new FileReader(fileName));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public String nextLine() throws IOException {
return bf.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
private static class Output extends PrintStream {
public Output(OutputStream out) {
super(new BufferedOutputStream(out));
}
public Output(File file) throws FileNotFoundException {
super(new BufferedOutputStream(new FileOutputStream(file)));
}
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 2b7234674e102b7322a03e5cbff999a1 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.*;
import java.util.Scanner;
public class Solution{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
String t = scanner.next();
char[] c = s.toCharArray();
char[] d = t.toCharArray();
int i=0;
int j=0;
int cl = c.length;
int dl = d.length;
while(j<dl){
if(d[j] == c[i]){
i++;
if(i >= cl){
break;
}
}
j++;
}
if(i<cl || j >= dl-cl){
System.out.println("0");
return;
}
int k=dl-1;
i=cl-1;
while(k>j){
if(d[k] == c[i]){
i--;
if(i<0){
break;
}
}
k--;
}
if(k>j){
System.out.println(k-j);
}else{
System.out.println(0);
}
}
} | Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 7f9a570ef12af8629747388c18fb1326 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.IOException;
import java.util.Scanner;
public class Kval2C {
public static void main(String[] args) throws IOException{
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
String t = scanner.next();
char[] sMass = s.toCharArray();
char[] tMass = t.toCharArray();
int j = 0;
int first = 0;
int second = 0;
for (int i = 0; i < tMass.length - sMass.length; i++) {
if (tMass[i] == sMass[j]){
j++;
if (j == sMass.length){
first = i;
break;
}
}
}
if (j == sMass.length){
j--;
for (int i = tMass.length - 1; i > first ; i--) {
if (tMass[i] == sMass[j]){
j--;
if (j == -1){
second = i;
break;
}
}
}
}
if (j == -1)
System.out.println(second - first);
else
System.out.println(0);
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | f24b13631d1f567fe3b7955d6bb51462 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.util.Scanner;
/**
* Created by Hedin on 14-Mar-15.
*/
public class SolveQual3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[] s = sc.next().toCharArray();
char[] t = sc.next().toCharArray();
sc.close();
int count = 0;
int firstEntry = -1;
for (int i = 0; i < t.length; i++) {
if (t[i] == s[count]) {
count++;
}
if (count == s.length) {
firstEntry = i;
break;
}
}
count = s.length - 1;
int secondEntry = -1;
for (int i = t.length - 1; i > firstEntry; i--) {
if (t[i] == s[count]) {
count--;
}
if (count == -1) {
secondEntry = i;
break;
}
}
if (secondEntry != -1 && firstEntry != -1) {
System.out.println(secondEntry - firstEntry);
} else {
System.out.println(0);
}
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | 1fdda9277c6f9d148951fadea66125de | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class VK_C_Q2 {
private static StreamTokenizer in = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
private static final PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
try {
solve();
out.flush();
}
catch (Exception e) {
e.printStackTrace();
}
}
private static void solve() throws Exception {
char[] s = readWord().toCharArray();
char[] t = readWord().toCharArray();
int l = t.length+1;
int current = 0;
for (int i = 0; i < t.length; i++) {
if (t[i] == s[current]) {
current++;
if (current == s.length) {
l = i;
break;
}
}
}
int r = -1;
current = s.length-1;
for (int i = t.length-1; i >=0; i--) {
if (t[i] == s[current]) {
current--;
if (current < 0) {
r = i;
break;
}
}
}
out.print(Math.max(r-l, 0));
}
private static int readInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
private static char readChar() throws IOException {
in.nextToken();
return in.sval.charAt(0);
}
private static String readWord() throws IOException {
in.nextToken();
return in.sval;
}
}
| Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output | |
PASSED | f119f7f06d94f363cfe87aaa76c64fb9 | train_000.jsonl | 1426345200 | A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if s=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.However rather than being happy once, he loves twice as much being happy twice! So, when he got string t as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.Help s determine the number of distinct ways to cut the given string t into two parts in the required manner. | 256 megabytes | import java.io.*;
import java.util.StringTokenizer;
public class Main {
static MyScanner in;
static PrintWriter out;
//static Timer t = new Timer();
public static void main(String[] args) throws IOException {
in = new MyScanner();
out = new PrintWriter(System.out, true);
char[] pat = in.next().toCharArray();
char[] str = in.next().toCharArray();
int i1 = -1, i2 = -1;
//count front
int index = 0;
for(int i = 0; i < str.length; i++) {
if(str[i] == pat[index]) {
index++;
if(index == pat.length) {
i1 = i;
break;
}
}
}
//count back
index = pat.length - 1;
for(int i = str.length - 1; i >= 0; i--) {
if(str[i] == pat[index]) {
index--;
if(index == -1) {
i2 = i;
break;
}
}
}
if(i1 == -1 || i2 == -1 || i2 < i1)
out.println(0);
else
out.println(i2 - i1);
}
}
//<editor-fold defaultstate="collapsed" desc="MyScanner">
class MyScanner {
private final BufferedReader br;
private StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public MyScanner(File r) throws FileNotFoundException {
br = new BufferedReader(new FileReader(r));
}
public MyScanner(String path) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(path)));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
boolean hasNext() {
if(st != null && st.hasMoreElements())
return true;
try {
while (st == null || !st.hasMoreElements())
st = new StringTokenizer(br.readLine());
}
catch(Exception e) {
return false;
}
return true;
}
String nextLine() throws IOException {
return br.readLine();
}
String[] nextStrings(int n) throws IOException {
String[] arr = new String[n];
for(int i = 0; i < n; i++)
arr[i] = next();
return arr;
}
String[] nextLines(int n) throws IOException {
String[] arr = new String[n];
for(int i = 0; i < n; i++)
arr[i] = nextLine();
return arr;
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
int[] nextInts(int n) throws IOException {
int[] arr = new int[n];
for(int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
int[][] next2Ints(int n, int m) throws IOException {
int[][] arr = new int[n][m];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
arr[i][j] = nextInt();
return arr;
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
long[] nextLongs(int n) throws IOException {
long[] arr = new long[n];
for(int i = 0; i < n; i++)
arr[i] = nextLong();
return arr;
}
long[][] next2Longs(int n, int m) throws IOException {
long[][] arr = new long[n][m];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
arr[i][j] = nextLong();
return arr;
}
double nextDouble(boolean replace) throws IOException {
if(replace)
return Double.parseDouble(next().replace(',', '.'));
else
return Double.parseDouble(next());
}
boolean nextBool() throws IOException {
String l = next();
if(l.equalsIgnoreCase("true") || l.equals("1"))
return true;
if(l.equalsIgnoreCase("false") || l.equals("0"))
return false;
throw new IOException("Boolean expected, String found!");
}
}
//</editor-fold> | Java | ["aba\nbaobababbah", "mars\nsunvenusearthmarsjupitersaturnuranusneptune"] | 2 seconds | ["2", "0"] | null | Java 7 | standard input | [
"*special",
"greedy"
] | 724fa4b1d35b8765048857fa8f2f6802 | The first line contains string s, consisting of lowercase English letters. The length of string s is from 1 to 1000 letters. The second line contains string t, that also consists of lowercase English letters. The length of string t is from 1 to 106 letters. | 1,400 | Print the sought number of ways to cut string t in two so that each part made s happy. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.