exec_outcome stringclasses 1 value | code_uid stringlengths 32 32 | file_name stringclasses 111 values | prob_desc_created_at stringlengths 10 10 | prob_desc_description stringlengths 63 3.8k | prob_desc_memory_limit stringclasses 18 values | source_code stringlengths 117 65.5k | lang_cluster stringclasses 1 value | prob_desc_sample_inputs stringlengths 2 802 | prob_desc_time_limit stringclasses 27 values | prob_desc_sample_outputs stringlengths 2 796 | prob_desc_notes stringlengths 4 3k ⌀ | lang stringclasses 5 values | prob_desc_input_from stringclasses 3 values | tags listlengths 0 11 | src_uid stringlengths 32 32 | prob_desc_input_spec stringlengths 28 2.37k ⌀ | difficulty int64 -1 3.5k ⌀ | prob_desc_output_spec stringlengths 17 1.47k ⌀ | prob_desc_output_to stringclasses 3 values | hidden_unit_tests stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | 44227250fdfe2328df8811a3f7090989 | train_002.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 megabytes | import java.util.*;
import java.io.*;
/*
4 3
1 3 2 4
4 1 4 4
1 1 2 3
1 4 1 4
*/
public class c implements Runnable {
PrintWriter out;
int n, q;
static long N;
int[] rows;
int[][] rects;
public void main() throws Throwable {
FastScanner in = new FastScanner(System.in);
out = new PrintWriter(System.out);
n = in.nextInt();
q = in.nextInt();
N = n;
rows = new int[n];
rects = new int[4][q];
for (int i = 0; i < n; i++) {
rows[i] = in.nextInt() - 1;
}
Query[] qs = new Query[q * 3];
Rect[] mrs = new Rect[q];
for (int i = 0; i < q; i++) {
for (int j = 0; j < 4; j++) {
rects[j][i] = in.nextInt() - 1;
}
Rect r = new Rect(rects[0][i], rects[1][i], rects[2][i], rects[3][i]);
mrs[i] = r;
qs[i] = new Query(r, r.l - 1, 0);
qs[i+q] = new Query(r, r.r, 1);
qs[i+q+q] = new Query(r, n - 1, 2);
}
BIT bit = new BIT(n + 3);
Arrays.sort(qs);
int curx = -1;
for (Query q : qs) {
while (curx < q.x) {
curx++;
bit.update(rows[curx], 1);
}
Rect r = q.rect;
if (q.type == 0) {
r.sw += bit.query(0, r.d - 1);
r.w += bit.query(r.d, r.u);
r.nw += bit.query(r.u + 1, n);
r.s -= r.sw;
r.c -= r.w;
r.n -= r.nw;
} else if (q.type == 1) {
r.s += bit.query(0, r.d - 1);
r.c += bit.query(r.d, r.u);
r.n += bit.query(r.u + 1, n);
r.se -= r.sw + r.s;
r.e -= r.w + r.c;
r.ne -= r.nw + r.n;
} else {
r.se += bit.query(0, r.d - 1);
r.e += bit.query(r.d, r.u);
r.ne += bit.query(r.u + 1, n);
}
}
for (Rect r : mrs) {
out.println(r.getAns());
}
out.close();
}
static class Rect {
long e, ne, n, nw, w, sw, s, se, c;
int l, d, r, u;
public Rect(int ll, int dd, int rr, int uu) {
l = ll; d = dd; r = rr; u = uu;
}
public long getAns() {
long ans = N * (N - 1) / 2;
ans -= ne * e;
ans -= ne * n;
ans -= ne * nw;
ans -= nw * n;
ans -= nw * w;
ans -= nw * sw;
ans -= sw * w;
ans -= sw * s;
ans -= sw * se;
ans -= se * s;
ans -= se * e;
ans -= se * ne;
ans -= e * (e - 1) / 2;
ans -= ne * (ne - 1) / 2;
ans -= n * (n - 1) / 2;
ans -= nw * (nw - 1) / 2;
ans -= w * (w - 1) / 2;
ans -= sw * (sw - 1) / 2;
ans -= s * (s - 1) / 2;
ans -= se * (se - 1) / 2;
return ans;
}
}
static class Query implements Comparable<Query> {
Rect rect;
int x;
int type;
public Query(Rect rectt, int xx, int typee) {
rect = rectt; x = xx; type = typee;
}
public int compareTo(Query q) {
if (x == q.x) return Integer.compare(type, q.type);
return Integer.compare(x, q.x);
}
}
static class BIT {
int n;
int[] tree;
public BIT(int n) {
this.n = n;
tree = new int[n + 3];
}
int query(int a, int b) {
a++;
b++;
if (a > b) return 0;
return read(b) - read(a - 1);
}
int read(int i) {
int sum = 0;
while (i > 0) {
sum += tree[i];
i -= i & -i;
}
return sum;
}
void update(int i, int val) {
i++;
while (i <= n) {
tree[i] += val;
i += i & -i;
}
}
}
public static void main(String[] args) throws Exception {
new Thread(null, new c(), "c", 1L << 28).start();
}
public void run() {
try {
main();
} catch (Throwable t) {
t.printStackTrace();
System.exit(-1);
}
}
static void sort(int[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 1; i < n; i++) {
int j = rand.nextInt(i);
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
Arrays.sort(arr);
}
static void sort(long[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 1; i < n; i++) {
int j = rand.nextInt(i);
long tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
Arrays.sort(arr);
}
static void sort(double[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 1; i < n; i++) {
int j = rand.nextInt(i);
double tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
Arrays.sort(arr);
}
static class FastScanner {
BufferedReader br; StringTokenizer st;
public FastScanner(InputStream i) {
br = new BufferedReader(new InputStreamReader(i));
st = new StringTokenizer("");
}
public String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public int[] nextIntArr(int n) throws Exception {
int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = nextInt();
return arr;
}
public double[] nextDoubleArr(int n) throws Exception {
double[] arr = new double[n];
for (int i = 0; i < n; i++) arr[i] = nextDouble();
return arr;
}
public long[] nextLongArr(int n) throws Exception {
long[] arr = new long[n];
for (int i = 0; i < n; i++) arr[i] = nextLong();
return arr;
}
public int[] nextOffsetIntArr(int n) throws Exception {
int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = nextInt() - 1;
return arr;
}
public int[][] nextIntArr(int n, int m) throws Exception {
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;
}
public double[][] nextDoubleArr(int n, int m) throws Exception {
double[][] arr = new double[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
arr[i][j] = nextDouble();
return arr;
}
public long[][] nextLongArr(int n, int m) throws Exception {
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;
}
}
} | Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles. The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ u ≤ n), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | ac6394a916d311cccff5ad09b1908409 | train_002.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class Boredom
{
InputStream in;
PrintWriter out;
long tree[];
int left[],right[];
int root[];
int MAX=(int)2e5+7;
int MAXN=25*((int)1e6);
int nnode=1;
void solve()
{
int n=ni();
int q=ni();
tree = new long[MAXN];
left = new int[MAXN];
right = new int[MAXN];
root = new int[MAX];
int p[]=new int[n+1];
for (int i=1;i<=n;i++)
p[i]=ni();
root[0]=0;
for (int i=1;i<=n;i++)
root[i]=build(root[i-1],1, n, p[i], 1);
while (q-->0){
int l,r,d,u;
l=ni();d=ni();r=ni();u=ni();
long r1,r2,r3,r4,r5,r6,r7,r8,r9;
//left side
r1=Query(l-1,0, 0, d-1, n);
r2=Query(l-1,0, d, u, n);
r3=l-1-r1-r2;
//centre side
r4=Query(r, l-1, 0, d-1, n);
r5=Query(r, l-1, d, u, n);
r6=r-l+1-r4-r5;
//right side
r7=Query(n, r, 0, d-1, n);
r8=Query(n, r, d, u, n);
r9=n-r-r7-r8;
//tr(r1+" "+r2+" "+r3+" "+r4+" "+r5+" "+r6+" "+r7+" "+r8+" "+r9);
long val1,val2,val3,val4,val5,val6,val7,val8,val9;
val1=r1*(r5+r8+r6+r9);
val2=r2*(n-r1-r3-r2);
val3=r3*(r4+r5+r7+r8);
val7=r7*(r5+r6+r2+r3);
val8=r8*(n-r7-r9-r8);
val9=r9*(r4+r5+r1+r2);
val4=r4*(n-r1-r7-r4);
val5=r5*(n-r5)+(r5*(r5-1));
val6=r6*(n-r3-r9-r6);
//tr(val1+" "+val2+" "+val3+" "+val4+" "+val5+" "+val6+" "+val7+" "+val8+" "+val9);
long ans=(val1+val2+val3+val4+val5+val6+val7+val8+val9)/2L;
out.println(ans);
}
}
class Node {
int left,right;
long value;
Node ()
{
left=right=-1;
value=0;
}
}
long query(int s1,int l,int r,int start,int end)
{
if (s1==-1||start > r || end < l || start > end)
return 0;
if (start >= l && end <= r)
return tree[s1];
int mid = ((start + end) >> 1);
return query(left[s1],l,r ,start, mid)+query(right[s1],l,r ,mid + 1, end);
}
long Query(int r,int l,int d,int u,int n)
{
return query(root[r],d,u,1,n)-query(root[l],d,u,1,n);
}
int build(int v,int l,int r,int pos,int val) {
int newid=nnode++;
if (v != -1) {
tree[newid] = tree[v];
left[newid] = left[v];
right[newid] = right[v];
}
tree[newid] += val;
if (l == r)
return newid;
int x = ((l + r)>>1);
if (pos <= x)
left[newid] = build(left[newid], l, x, pos, val);
else
right[newid] = build(right[newid], x + 1, r, pos, val);
return newid;
}
void run() throws Exception
{
String INPUT = "C:/Users/ayubs/Desktop/input.txt";
in = oj ? System.in : new FileInputStream(INPUT);
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception
{
new Boredom().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf)
{
ptrbuf = 0;
try
{
lenbuf = in.read(inbuf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean inSpaceChar(int c)
{
return !(c >= 33 && c <= 126);
}
private int skip()
{
int b;
while ((b = readByte()) != -1 && inSpaceChar(b))
;
return b;
}
private double nd()
{
return Double.parseDouble(ns());
}
private char nc()
{
return (char) skip();
}
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(inSpaceChar(b)))
{ // when nextLine, (inSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(inSpaceChar(b)))
{
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-')
{
minus = true;
b = readByte();
}
while (true)
{
if (b >= '0' && b <= '9')
{
num = num * 10 + (b - '0');
}
else
{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-')
{
minus = true;
b = readByte();
}
while (true)
{
if (b >= '0' && b <= '9')
{
num = num * 10 + (b - '0');
}
else
{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o)
{
if (!oj)
System.out.println(Arrays.deepToString(o));
}
} | Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles. The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ u ≤ n), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | c7ea0f6537bd9422384aa25943677731 | train_002.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, FastScanner in, PrintWriter out) {
int n = in.nextInt();
int numQueries = in.nextInt();
int[] p = new int[n + 2];
p[0] = -1;
p[n + 1] = -1;
for (int i = 1; i <= n; i++) {
p[i] = in.nextInt();
}
Rectangle[] rectanglesMinus = new Rectangle[4 * numQueries];
Rectangle[] rectanglesPlus = new Rectangle[4 * numQueries];
for (int queryId = 0; queryId < numQueries; queryId++) {
int c1 = in.nextInt();
int r1 = in.nextInt();
int c2 = in.nextInt();
int r2 = in.nextInt();
rectanglesMinus[4 * queryId + 0] = new Rectangle(0, 0, r1 - 1, n + 1);
rectanglesMinus[4 * queryId + 1] = new Rectangle(0, 0, n + 1, c1 - 1);
rectanglesMinus[4 * queryId + 2] = new Rectangle(r2 + 1, 0, n + 1, n + 1);
rectanglesMinus[4 * queryId + 3] = new Rectangle(0, c2 + 1, n + 1, n + 1);
rectanglesPlus[4 * queryId + 0] = new Rectangle(0, 0, r1 - 1, c1 - 1);
rectanglesPlus[4 * queryId + 1] = new Rectangle(0, c2 + 1, r1 - 1, n + 1);
rectanglesPlus[4 * queryId + 2] = new Rectangle(r2 + 1, 0, n + 1, c1 - 1);
rectanglesPlus[4 * queryId + 3] = new Rectangle(r2 + 1, c2 + 1, n + 1, n + 1);
}
int[] insideMinus = new int[rectanglesMinus.length];
calcPointsInside(n + 2, p, rectanglesMinus, insideMinus);
int[] insidePlus = new int[rectanglesMinus.length];
calcPointsInside(n + 2, p, rectanglesPlus, insidePlus);
long[] ans = new long[numQueries];
Arrays.fill(ans, (long) n * (n - 1) / 2);
for (int i = 0; i < insideMinus.length; i++) {
long k = insideMinus[i];
ans[i / 4] -= k * (k - 1) / 2;
}
for (int i = 0; i < insidePlus.length; i++) {
long k = insidePlus[i];
ans[i / 4] += k * (k - 1) / 2;
}
for (long x : ans) {
out.println(x);
}
}
private void calcPointsInside(int n, int[] p, Rectangle[] rs, int[] inside) {
int[] tree = new int[n];
Segment[] segments = new Segment[2 * rs.length];
for (int i = 0; i < rs.length; i++) {
segments[2 * i + 0] = new Segment(rs[i].r1, rs[i].r2, rs[i].c1, true, i);
segments[2 * i + 1] = new Segment(rs[i].r1, rs[i].r2, rs[i].c2 + 1, false, i);
}
Segment[] first = new Segment[n + 1];
for (Segment s : segments) {
s.next = first[s.x];
first[s.x] = s;
}
int ptr = 0;
for (int x = 0; x <= n; x++) {
while (ptr <= x - 1 && ptr < p.length) {
int r = p[ptr++];
if (r >= 0) {
add(tree, r);
}
}
for (Segment s = first[x]; s != null; s = s.next) {
if (s.opens) {
inside[s.id] -= sum(tree, s.l, s.r);
} else {
inside[s.id] += sum(tree, s.l, s.r);
}
}
}
}
private void add(int[] tree, int pos) {
while (pos < tree.length) {
++tree[pos];
pos |= pos + 1;
}
}
private int sum(int[] tree, int l, int r) {
return sum(tree, r) - sum(tree, l - 1);
}
private int sum(int[] tree, int r) {
int res = 0;
while (r >= 0) {
res += tree[r];
r = (r & (r + 1)) - 1;
}
return res;
}
class Segment implements Comparable<Segment> {
int l;
int r;
int x;
boolean opens;
int id;
Segment next;
Segment(int l, int r, int x, boolean opens, int id) {
this.l = l;
this.r = r;
this.x = x;
this.opens = opens;
this.id = id;
}
public int compareTo(Segment o) {
return x - o.x;
}
}
class Rectangle {
int r1;
int r2;
int c1;
int c2;
Rectangle(int r1, int c1, int r2, int c2) {
this.r1 = r1;
this.c1 = c1;
this.r2 = r2;
this.c2 = c2;
}
}
}
static class FastScanner {
private BufferedReader in;
private StringTokenizer st;
public FastScanner(InputStream stream) {
in = new BufferedReader(new InputStreamReader(stream));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles. The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ u ≤ n), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | ab031beb2b54e422c767c42c8fc26597 | train_002.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, FastScanner in, PrintWriter out) {
int n = in.nextInt();
int numQueries = in.nextInt();
int[] p = new int[n + 2];
p[0] = -1;
p[n + 1] = -1;
for (int i = 1; i <= n; i++) {
p[i] = in.nextInt();
}
Rectangle[] rectanglesMinus = new Rectangle[4 * numQueries];
Rectangle[] rectanglesPlus = new Rectangle[4 * numQueries];
for (int queryId = 0; queryId < numQueries; queryId++) {
int c1 = in.nextInt();
int r1 = in.nextInt();
int c2 = in.nextInt();
int r2 = in.nextInt();
rectanglesMinus[4 * queryId + 0] = new Rectangle(0, 0, r1 - 1, n + 1);
rectanglesMinus[4 * queryId + 1] = new Rectangle(0, 0, n + 1, c1 - 1);
rectanglesMinus[4 * queryId + 2] = new Rectangle(r2 + 1, 0, n + 1, n + 1);
rectanglesMinus[4 * queryId + 3] = new Rectangle(0, c2 + 1, n + 1, n + 1);
rectanglesPlus[4 * queryId + 0] = new Rectangle(0, 0, r1 - 1, c1 - 1);
rectanglesPlus[4 * queryId + 1] = new Rectangle(0, c2 + 1, r1 - 1, n + 1);
rectanglesPlus[4 * queryId + 2] = new Rectangle(r2 + 1, 0, n + 1, c1 - 1);
rectanglesPlus[4 * queryId + 3] = new Rectangle(r2 + 1, c2 + 1, n + 1, n + 1);
}
int[] insideMinus = new int[rectanglesMinus.length];
calcPointsInside(n + 2, p, rectanglesMinus, insideMinus);
int[] insidePlus = new int[rectanglesMinus.length];
calcPointsInside(n + 2, p, rectanglesPlus, insidePlus);
long[] ans = new long[numQueries];
Arrays.fill(ans, (long) n * (n - 1) / 2);
for (int i = 0; i < insideMinus.length; i++) {
long k = insideMinus[i];
ans[i / 4] -= k * (k - 1) / 2;
}
for (int i = 0; i < insidePlus.length; i++) {
long k = insidePlus[i];
ans[i / 4] += k * (k - 1) / 2;
}
for (long x : ans) {
out.println(x);
}
}
private void calcPointsInside(int n, int[] p, Rectangle[] rs, int[] inside) {
int[] tree = new int[n];
Segment[] segments = new Segment[2 * rs.length];
for (int i = 0; i < rs.length; i++) {
segments[2 * i + 0] = new Segment(rs[i].r1, rs[i].r2, rs[i].c1, true, i);
segments[2 * i + 1] = new Segment(rs[i].r1, rs[i].r2, rs[i].c2 + 1, false, i);
}
Arrays.sort(segments);
int ptr = 0;
for (Segment s : segments) {
while (ptr <= s.x - 1 && ptr < p.length) {
int r = p[ptr++];
if (r >= 0) {
add(tree, r);
}
}
if (s.opens) {
inside[s.id] -= sum(tree, s.l, s.r);
} else {
inside[s.id] += sum(tree, s.l, s.r);
}
}
}
private void add(int[] tree, int pos) {
while (pos < tree.length) {
++tree[pos];
pos |= pos + 1;
}
}
private int sum(int[] tree, int l, int r) {
return sum(tree, r) - sum(tree, l - 1);
}
private int sum(int[] tree, int r) {
int res = 0;
while (r >= 0) {
res += tree[r];
r = (r & (r + 1)) - 1;
}
return res;
}
class Segment implements Comparable<Segment> {
int l;
int r;
int x;
boolean opens;
int id;
Segment(int l, int r, int x, boolean opens, int id) {
this.l = l;
this.r = r;
this.x = x;
this.opens = opens;
this.id = id;
}
public int compareTo(Segment o) {
return x - o.x;
}
}
class Rectangle {
int r1;
int r2;
int c1;
int c2;
Rectangle(int r1, int c1, int r2, int c2) {
this.r1 = r1;
this.c1 = c1;
this.r2 = r2;
this.c2 = c2;
}
}
}
static class FastScanner {
private BufferedReader in;
private StringTokenizer st;
public FastScanner(InputStream stream) {
in = new BufferedReader(new InputStreamReader(stream));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles. The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ u ≤ n), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | db10a816aaad60c17b651c7860ca2c1e | train_002.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Comparator;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public long c2(int x) {
return 1L * x * (x - 1) / 2;
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt(), q = in.nextInt();
int[] arr = in.readIntArray(n);
for (int i = 0; i < n; i++) arr[i]--;
TaskC.Query[] qs = new TaskC.Query[q];
for (int i = 0; i < q; i++) {
qs[i] = new TaskC.Query(in.nextInt() - 1, in.nextInt() - 1, in.nextInt() - 1, in.nextInt() - 1, i);
}
long[] res = new long[q];
for (int i = 0; i < q; i++) {
res[i] = c2(n) - c2(qs[i].l) - c2(qs[i].d) - c2(n - 1 - qs[i].r) - c2(n - 1 - qs[i].u);
}
TaskC.Q2[] ff = new TaskC.Q2[2 * q];
TaskC.Q2[] bb = new TaskC.Q2[2 * q];
for (int i = 0; i < q; i++) {
ff[2 * i + 0] = new TaskC.Q2(qs[i].l - 1, qs[i].d - 1, true, i);
ff[2 * i + 1] = new TaskC.Q2(qs[i].l - 1, qs[i].u, false, i);
bb[2 * i + 0] = new TaskC.Q2(qs[i].r + 1, qs[i].d - 1, true, i);
bb[2 * i + 1] = new TaskC.Q2(qs[i].r + 1, qs[i].u, false, i);
}
Arrays.sort(ff, Comparator.comparingInt(x -> x.pos));
Arrays.sort(bb, Comparator.comparingInt(x -> -x.pos));
BIT b = new BIT(n + 10);
int ptr = 0;
for (int i = 0; i < 2 * q; i++) {
while (ptr <= ff[i].pos) {
b.update(arr[ptr], +1);
ptr++;
}
int x = b.query(ff[i].thresh);
if (!ff[i].less) x = ptr - x;
res[ff[i].idx] += c2(x);
}
b = new BIT(n + 10);
ptr = n - 1;
for (int i = 0; i < 2 * q; i++) {
while (ptr >= bb[i].pos) {
b.update(arr[ptr], +1);
ptr--;
}
int x = b.query(bb[i].thresh);
if (!bb[i].less) x = (n - 1 - ptr) - x;
res[bb[i].idx] += c2(x);
}
out.println(res);
}
static class Query {
public int l;
public int d;
public int r;
public int u;
public int idx;
public Query(int l, int d, int r, int u, int idx) {
this.l = l;
this.d = d;
this.r = r;
this.u = u;
this.idx = idx;
}
}
static class Q2 {
public int pos;
public int thresh;
public boolean less;
public int idx;
public Q2(int pos, int thresh, boolean less, int idx) {
this.pos = pos;
this.thresh = thresh;
this.less = less;
this.idx = idx;
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int[] readIntArray(int tokens) {
int[] ret = new int[tokens];
for (int i = 0; i < tokens; i++) {
ret[i] = nextInt();
}
return ret;
}
public int read() {
if (this.numChars == -1) {
throw new InputMismatchException();
} else {
if (this.curChar >= this.numChars) {
this.curChar = 0;
try {
this.numChars = this.stream.read(this.buf);
} catch (IOException var2) {
throw new InputMismatchException();
}
if (this.numChars <= 0) {
return -1;
}
}
return this.buf[this.curChar++];
}
}
public int nextInt() {
int c;
for (c = this.read(); isSpaceChar(c); c = this.read()) {
;
}
byte sgn = 1;
if (c == 45) {
sgn = -1;
c = this.read();
}
int res = 0;
while (c >= 48 && c <= 57) {
res *= 10;
res += c - 48;
c = this.read();
if (isSpaceChar(c)) {
return res * sgn;
}
}
throw new InputMismatchException();
}
public static boolean isSpaceChar(int c) {
return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(long[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void println(long[] array) {
print(array);
writer.println();
}
public void close() {
writer.close();
}
}
static class BIT {
private int[] tree;
private int N;
public BIT(int N) {
this.N = N;
this.tree = new int[N + 3];
}
public int query(int K) {
K += 2;
int sum = 0;
for (int i = K; i > 0; i -= (i & -i))
sum += tree[i];
return sum;
}
public void update(int K, int val) {
K += 2;
for (int i = K; i < tree.length; i += (i & -i))
tree[i] += val;
}
}
}
| Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles. The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ u ≤ n), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | 15df0b523953576ae6ce2d206c766d67 | train_002.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 megabytes | import java.io.*;
import java.util.*;
public class C {
long[] ans;
long[] c2;
static class Query implements Comparable<Query> {
int x, y, id;
public Query(int x, int y, int id) {
this.x = x;
this.y = y;
this.id = id;
}
@Override
public int compareTo(Query o) {
return Integer.compare(x, o.x);
}
}
void submit() {
int n = nextInt();
int q = nextInt();
c2 = new long[n + 1];
for (int i = 0; i <= n; i++) {
c2[i] = (long) i * (i - 1) / 2;
}
int[] p = new int[n];
for (int i = 0; i < n; i++) {
p[i] = nextInt() - 1;
}
ans = new long[q];
Query[][] qs = new Query[4][q];
for (int i = 0; i < q; i++) {
int x1 = nextInt() - 1;
int y1 = nextInt() - 1;
int x2 = nextInt();
int y2 = nextInt();
ans[i] = c2[x1] + c2[y1] + c2[n - x2] + c2[n - y2];
qs[0][i] = new Query(x1, y1, i);
qs[1][i] = new Query(n - x2, y1, i);
qs[2][i] = new Query(x1, n - y2, i);
qs[3][i] = new Query(n - x2, n - y2, i);
}
process(qs[0], p);
reverse(p);
process(qs[1], p);
flip(p);
process(qs[3], p);
reverse(p);
process(qs[2], p);
for (long x : ans) {
out.println(c2[n] - x);
}
}
void reverse(int[] a) {
for (int i = 0, j = a.length - 1; i < j; i++, j--) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
void flip(int[] a) {
int n = a.length;
for (int i = 0; i < a.length; i++) {
a[i] = n - 1 - a[i];
}
}
void process(Query[] qs, int[] p) {
Arrays.sort(qs);
int n = p.length;
int[] fen = new int[n];
int ptr = 0;
for (Query q : qs) {
while (ptr < q.x) {
add(fen, p[ptr]);
ptr++;
}
int cnt = get(fen, q.y - 1);
ans[q.id] -= c2[cnt];
}
}
void add(int[] fen, int pos) {
for (int i = pos; i < fen.length; i |= i + 1) {
fen[i]++;
}
}
int get(int[] fen, int pos) {
int ret = 0;
for (int i = pos; i >= 0; i = (i & (i + 1)) - 1) {
ret += fen[i];
}
return ret;
}
void preCalc() {
}
void stress() {
}
void test() {
}
C() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
preCalc();
submit();
// stress();
// test();
out.close();
}
static final Random rng = new Random();
static int rand(int l, int r) {
return l + rng.nextInt(r - l + 1);
}
public static void main(String[] args) throws IOException {
new C();
}
BufferedReader br;
PrintWriter out;
StringTokenizer st;
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
String nextString() {
try {
return br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
| Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles. The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ u ≤ n), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | 2f56e9957581218adf87a7dc3ac58a2a | train_002.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 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 Rustam Musin (PloadyFree@gmail.com)
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
int N = 1 << 18;
int[] sum = new int[20 * N];
int[] left = new int[20 * N];
int[] right = new int[20 * N];
int[] roots = new int[N];
int rootCount = 0;
int nodeCount = 0;
static long upTo(long x) {
return x * (x - 1) / 2;
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int q = in.readInt();
roots[rootCount++] = nodeCount++;
for (int col = 1; col <= n; col++) {
add(in.readInt());
}
for (int i = 0; i < q; i++) {
int l = in.readInt();
int u = in.readInt();
int r = in.readInt();
int d = in.readInt();
long sum = 0;
sum += upTo(u - 1);
sum += upTo(n - d);
sum += upTo(l - 1);
sum += upTo(n - r);
sum -= upTo(get(1, u - 1, l - 1));
sum -= upTo(get(1, u - 1, n) - get(1, u - 1, r));
sum -= upTo(get(d + 1, n, l - 1));
sum -= upTo(get(d + 1, n, n) - get(d + 1, n, r));
out.printLine(upTo(n) - sum);
}
}
void add(int at) {
int lastRoot = roots[rootCount - 1];
roots[rootCount++] = add(lastRoot, at, 0, N - 1);
}
int add(int root, int at, int treeL, int treeR) {
int next = nodeCount++;
left[next] = root == -1 ? -1 : left[root];
right[next] = root == -1 ? -1 : right[root];
if (treeL == treeR) {
sum[next] = 1;
return next;
}
int middle = (treeL + treeR) >> 1;
if (at <= middle) {
left[next] = add(left[next], at, treeL, middle);
} else {
right[next] = add(right[next], at, middle + 1, treeR);
}
int nodeL = left[next];
int nodeR = right[next];
sum[next] = (nodeL == -1 ? 0 : sum[nodeL]) + (nodeR == -1 ? 0 : sum[nodeR]);
return next;
}
int get(int u, int d, int lastCol) {
return get(roots[lastCol], u, d, 0, N - 1);
}
int get(int root, int l, int r, int treeL, int treeR) {
if (root == -1 || l > r || l > treeR || r < treeL) {
return 0;
}
if (l <= treeL && treeR <= r) {
return sum[root];
}
int middle = (treeL + treeR) >> 1;
return get(left[root], l, r, treeL, middle) + get(right[root], l, r, middle + 1, treeR);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void printLine(long i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles. The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ u ≤ n), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | b3484d9d4639df70c20a24c2344bbb7a | train_002.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 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 Rustam Musin (PloadyFree@gmail.com)
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
int N = 1 << 18;
int[] sum = new int[20 * N];
int[] left = new int[20 * N];
int[] right = new int[20 * N];
int[] roots = new int[N];
int rootCount = 0;
int nodeCount = 0;
static long upTo(long x) {
return x * (x - 1) / 2;
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int q = in.readInt();
roots[rootCount++] = nodeCount++;
for (int col = 1; col <= n; col++) {
add(in.readInt());
}
for (int i = 0; i < q; i++) {
int l = in.readInt();
int u = in.readInt();
int r = in.readInt();
int d = in.readInt();
long sum = 0;
sum += upTo(u - 1);
sum += upTo(n - d);
sum += upTo(l - 1);
sum += upTo(n - r);
sum -= upTo(get(1, u - 1, l - 1));
sum -= upTo(get(1, u - 1, n) - get(1, u - 1, r));
sum -= upTo(get(d + 1, n, l - 1));
sum -= upTo(get(d + 1, n, n) - get(d + 1, n, r));
out.printLine(upTo(n) - sum);
}
}
void add(int at) {
int lastRoot = roots[rootCount - 1];
roots[rootCount++] = add(lastRoot, at, 0, N - 1);
}
int add(int root, int at, int treeL, int treeR) {
int next = nodeCount++;
left[next] = left(root);
right[next] = right(root);
if (treeL == treeR) {
sum[next] = 1;
return next;
}
int middle = (treeL + treeR) >> 1;
if (at <= middle) {
left[next] = add(left(next), at, treeL, middle);
} else {
right[next] = add(right[next], at, middle + 1, treeR);
}
sum[next] = sum(left(next)) + sum(right(next));
return next;
}
int get(int u, int d, int lastCol) {
return get(roots[lastCol], u, d, 0, N - 1);
}
int get(int root, int l, int r, int treeL, int treeR) {
if (root == -1 || l > r || l > treeR || r < treeL) {
return 0;
}
if (l <= treeL && treeR <= r) {
return sum(root);
}
int middle = (treeL + treeR) >> 1;
return get(left(root), l, r, treeL, middle) + get(right(root), l, r, middle + 1, treeR);
}
int left(int node) {
if (node == -1) {
return -1;
}
return left[node];
}
int right(int node) {
if (node == -1) {
return -1;
}
return right[node];
}
int sum(int node) {
if (node == -1) {
return 0;
}
return sum[node];
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void printLine(long i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles. The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ u ≤ n), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | c6d87e8af8a888513a57fcf941d802e7 | train_002.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Div1_433C {
public static void main(String[] args) throws IOException {
new Div1_433C().execute();
}
int n;
int[] trees;
int[] lC = new int[10_000_000];
int[] rC = new int[10_000_000];
int[] val = new int[10_000_000];
int nN;
void execute() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
StringTokenizer inputData = new StringTokenizer(reader.readLine());
n = Integer.parseInt(inputData.nextToken());
int q = Integer.parseInt(inputData.nextToken());
trees = new int[n + 1];
trees[0] = init(1, n);
inputData = new StringTokenizer(reader.readLine());
for (int i = 1; i <= n; i++) {
trees[i] = update(trees[i - 1], 1, n, Integer.parseInt(inputData.nextToken()));
}
for (int i = 1; i <= q; i++) {
inputData = new StringTokenizer(reader.readLine());
int l = Integer.parseInt(inputData.nextToken());
int b = Integer.parseInt(inputData.nextToken());
int r = Integer.parseInt(inputData.nextToken());
int t = Integer.parseInt(inputData.nextToken());
long s1 = query(1, l - 1, 1, n);
long s2 = query(r + 1, n, 1, n);
long s3 = query(1, n, 1, b - 1);
long s4 = query(1, n, t + 1, n);
long a1 = query(1, l - 1, 1, b - 1);
long a2 = query(r + 1, n, 1, b - 1);
long a3 = query(r + 1, n, t + 1, n);
long a4 = query(1, l - 1, t + 1, n);
long sum = (long) n * (n - 1) - s1 * (s1 - 1) - s2 * (s2 - 1) - s3 * (s3 - 1) - s4 * (s4 - 1)
+ a1 * (a1 - 1) + a2 * (a2 - 1) + a3 * (a3 - 1) + a4 * (a4 - 1);
sum /= 2;
printer.println(sum);
}
printer.close();
}
int init(int cL, int cR) {
int ret = nN++;
if (cL != cR) {
int mid = (cL + cR) >> 1;
lC[ret] = init(cL, mid);
rC[ret] = init(mid + 1, cR);
}
return ret;
}
int update(int cNode, int cL, int cR, int uI) {
int ret = nN++;
if (cL == cR) {
val[ret] = 1;
} else {
int mid = (cL + cR) >> 1;
if (uI <= mid) {
lC[ret] = update(lC[cNode], cL, mid, uI);
rC[ret] = rC[cNode];
} else {
lC[ret] = lC[cNode];
rC[ret] = update(rC[cNode], mid + 1, cR, uI);
}
val[ret] = val[lC[ret]] + val[rC[ret]];
}
return ret;
}
int query(int l, int r, int b, int t) {
return query(trees[r], 1, n, b, t) - query(trees[l - 1], 1, n, b, t);
}
int query(int cNode, int cL, int cR, int qL, int qR) {
if (qR < cL || cR < qL) {
return 0;
}
if (qL <= cL && cR <= qR) {
return val[cNode];
}
int mid = (cL + cR) >> 1;
return query(lC[cNode], cL, mid, qL, qR) + query(rC[cNode], mid + 1, cR, qL, qR);
}
}
| Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles. The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ u ≤ n), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | 788bfa3eb832fff1176c774b89d0ac9f | train_002.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 megabytes | import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.BiFunction;
public class Main{
static Scanner scn = new Scanner(System.in);
static FastScanner sc = new FastScanner();
static Mathplus mp = new Mathplus();
static PrintWriter ot = new PrintWriter(System.out);
static Random rand = new Random();
static int mod = 1000000007;
static long modmod = (long)mod * mod;
static long inf = (long)1e17;
static int[] dx = {0,1,0,-1};
static int[] dy = {1,0,-1,0};
static int[] dx8 = {-1,-1,-1,0,0,1,1,1};
static int[] dy8 = {-1,0,1,-1,1,-1,0,1};
static char[] dc = {'R','D','L','U'};
static BiFunction<Integer,Integer,Integer> fmax = (a,b)-> {return Math.max(a,b);};
static BiFunction<Integer,Integer,Integer> fmin = (a,b)-> {return Math.min(a,b);};
static BiFunction<Integer,Integer,Integer> fsum = (a,b)-> {return a+b;};
static BiFunction<Long,Long,Long> fmaxl = (a,b)-> {return Math.max(a,b);};
static BiFunction<Long,Long,Long> fminl = (a,b)-> {return Math.min(a,b);};
static BiFunction<Long,Long,Long> fsuml = (a,b)-> {return a+b;};
static BiFunction<Integer,Integer,Integer> fadd = fsum;
static BiFunction<Integer,Integer,Integer> fupd = (a,b)-> {return b;};
static BiFunction<Long,Long,Long> faddl = fsuml;
static BiFunction<Long,Long,Long> fupdl = (a,b)-> {return b;};
static String sp = " ";
public static void main(String[] args) {
int n = sc.nextInt();
int q = sc.nextInt();
int[] p = sc.nextInts(n,1);
int[] l = new int[q];
int[] r = new int[q];
int[] u = new int[q];
int[] d = new int[q];
int[] lu = new int[q];
int[] ld = new int[q];
int[] ru = new int[q];
int[] rd = new int[q];
ArrayList<IntIntPair>[] qlu = new ArrayList[n+1];
ArrayList<IntIntPair>[] qld = new ArrayList[n+1];
ArrayList<IntIntPair>[] qru = new ArrayList[n+1];
ArrayList<IntIntPair>[] qrd = new ArrayList[n+1];
for(int i=1;i<=n;i++) {
qlu[i] = new ArrayList<IntIntPair>();
qld[i] = new ArrayList<IntIntPair>();
qru[i] = new ArrayList<IntIntPair>();
qrd[i] = new ArrayList<IntIntPair>();
}
for(int i=0;i<q;i++) {
l[i] = sc.nextInt();
d[i] = sc.nextInt();
r[i] = sc.nextInt();
u[i] = sc.nextInt();
qld[l[i]].add(new IntIntPair(d[i],i));
qlu[l[i]].add(new IntIntPair(u[i]+1,i));
qrd[r[i]].add(new IntIntPair(d[i],i));
qru[r[i]].add(new IntIntPair(u[i]+1,i));
}
AddSumSegmentTree st = new AddSumSegmentTree(new int[n+2]);
for(int i=1;i<=n;i++) {
for(IntIntPair P:qld[i]) {
ld[P.b] = st.query(0,P.a);
}
for(IntIntPair P:qlu[i]) {
lu[P.b] = st.query(P.a,n+2);
}
st.update(p[i],1);
}
st = new AddSumSegmentTree(new int[n+2]);
for(int i=n;i>=1;i--) {
for(IntIntPair P:qrd[i]) {
rd[P.b] = st.query(0,P.a);
}
for(IntIntPair P:qru[i]) {
ru[P.b] = st.query(P.a,n+2);
}
st.update(p[i],1);
}
for(int i=0;i<q;i++) {
long ans = mp.sigma(n-1);
ans -= mp.sigma(l[i]-2);
ans -= mp.sigma(n-r[i]-1);
ans -= mp.sigma(d[i]-2);
ans -= mp.sigma(n-u[i]-1);
ans += mp.sigma(ld[i]-1);
ans += mp.sigma(lu[i]-1);
ans += mp.sigma(rd[i]-1);
ans += mp.sigma(ru[i]-1);
ot.println(ans);
}
ot.flush();
}
}
class Slidemax{
int[] dat;
ArrayDeque<LongIntPair> q = new ArrayDeque<LongIntPair>();
long get() {
if(q.isEmpty()) return (long) -1e17;
return q.peek().a;
}
void remove() {
q.getFirst().b--;
if(q.getFirst().b==0)q.pollFirst();
}
void add(long x) {
int num = 1;
while(!q.isEmpty()&&q.peekLast().a<=x) {
num += q.peekLast().b;
q.pollLast();
}
q.addLast(new LongIntPair(x,num));
}
}
class Slidemin{
int[] dat;
int l = 0;
int r = -1;
ArrayDeque<LongIntPair> q = new ArrayDeque<LongIntPair>();
long get() {
if(q.isEmpty()) return (long)1e17;
return q.peek().a;
}
void remove() {
q.getFirst().b--;
if(q.getFirst().b==0)q.pollFirst();
}
void add(long x) {
int num = 1;
while(!q.isEmpty()&&q.peekLast().a>=x) {
num += q.peekLast().b;
q.pollLast();
}
q.addLast(new LongIntPair(x,num));
}
}
class Counter{
int[] cnt;
Counter(int M){
cnt = new int[M+1];
}
Counter(int M,int[] A){
cnt = new int[M+1];
for(int i=0;i<A.length;i++)add(A[i]);
}
void add(int e) {
cnt[e]++;
}
void remove(int e) {
cnt[e]--;
}
int count(int e) {
return cnt[e];
}
}
class MultiHashSet{
HashMap<Integer,Integer> set;
int size;
long sum;
MultiHashSet(){
set = new HashMap<Integer,Integer>();
size = 0;
sum = 0;
}
void add(int e){
if(set.containsKey(e))set.put(e,set.get(e)+1);
else set.put(e,1);
size++;
sum += e;
}
void remove(int e) {
set.put(e,set.get(e)-1);
if(set.get(e)==0)set.remove(e);
size--;
sum -= e;
}
boolean contains(int e) {
return set.containsKey(e);
}
boolean isEmpty() {
return set.isEmpty();
}
int count(int e) {
if(contains(e))return set.get(e);
else return 0;
}
Set<Integer> keyset(){
return set.keySet();
}
}
class MultiSet{
TreeMap<Integer,Integer> set;
long size;
long sum;
MultiSet(){
set = new TreeMap<Integer,Integer>();
size = 0;
sum = 0;
}
void add(int e){
if(set.containsKey(e))set.put(e,set.get(e)+1);
else set.put(e,1);
size++;
sum += e;
}
void addn(int e,int n){
if(set.containsKey(e))set.put(e,set.get(e)+n);
else set.put(e,n);
size += n;
sum += e*(long)n;
}
void remove(int e) {
set.put(e,set.get(e)-1);
if(set.get(e)==0)set.remove(e);
size--;
sum -= e;
}
int first() {return set.firstKey();}
int last() {return set.lastKey();}
int lower(int e) {return set.lowerKey(e);}
int higher(int e) {return set.higherKey(e);}
int floor(int e) {return set.floorKey(e);}
int ceil(int e) {return set.ceilingKey(e);}
boolean contains(int e) {return set.containsKey(e);}
boolean isEmpty() {return set.isEmpty();}
int count(int e) {
if(contains(e))return set.get(e);
else return 0;
}
MultiSet marge(MultiSet T) {
if(size>T.size) {
while(!T.isEmpty()) {
add(T.first());
T.remove(T.first());
}
return this;
}else {
while(!isEmpty()) {
T.add(first());
remove(first());
}
return T;
}
}
Set<Integer> keyset(){
return set.keySet();
}
}
class MultiSetL{
TreeMap<Long,Integer> set;
int size;
long sum;
MultiSetL(){
set = new TreeMap<Long,Integer>();
size = 0;
sum = 0;
}
void add(long e){
if(set.containsKey(e))set.put(e,set.get(e)+1);
else set.put(e,1);
size++;
sum += e;
}
void remove(long e) {
set.put(e,set.get(e)-1);
if(set.get(e)==0)set.remove(e);
size--;
sum -= e;
}
long first() {return set.firstKey();}
long last() {return set.lastKey();}
long lower(long e) {return set.lowerKey(e);}
long higher(long e) {return set.higherKey(e);}
long floor(long e) {return set.floorKey(e);}
long ceil(long e) {return set.ceilingKey(e);}
boolean contains(long e) {return set.containsKey(e);}
boolean isEmpty() {return set.isEmpty();}
int count(long e) {
if(contains(e))return set.get(e);
else return 0;
}
MultiSetL marge(MultiSetL T) {
if(size>T.size) {
while(!T.isEmpty()) {
add(T.first());
T.remove(T.first());
}
return this;
}else {
while(!isEmpty()) {
T.add(first());
remove(first());
}
return T;
}
}
Set<Long> keyset(){
return set.keySet();
}
}
class BetterGridGraph{
int N;
int M;
char[][] S;
HashMap<Character,ArrayList<Integer>> map;
int[] dx = {0,1,0,-1};
int[] dy = {1,0,-1,0};
char w;
char b = '#';
BetterGridGraph(int n,int m,String[] s,char[] c){
N = n;
M = m;
for(int i=0;i<s.length;i++) {
S[i] = s[i].toCharArray();
}
map = new HashMap<Character,ArrayList<Integer>>();
for(int i=0;i<c.length;i++) {
map.put(c[i],new ArrayList<Integer>());
}
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
for(int k=0;k<c.length;k++) {
if(S[i][j]==c[k])map.get(c[k]).add(toint(i,j));
}
}
}
}
BetterGridGraph(int n,int m,char[][] s,char[] c){
N = n;
M = m;
S = s;
map = new HashMap<Character,ArrayList<Integer>>();
for(int i=0;i<c.length;i++) {
map.put(c[i],new ArrayList<Integer>());
}
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
for(int k=0;k<c.length;k++) {
if(S[i][j]==c[k])map.get(c[k]).add(toint(i,j));
}
}
}
}
BetterGridGraph(int n,int m,String[] s,char[] c,char W,char B){
N = n;
M = m;
for(int i=0;i<s.length;i++) {
S[i] = s[i].toCharArray();
}
w = W;
b = B;
map = new HashMap<Character,ArrayList<Integer>>();
for(int i=0;i<c.length;i++) {
map.put(c[i],new ArrayList<Integer>());
}
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
for(int k=0;k<c.length;k++) {
if(S[i][j]==c[k])map.get(c[k]).add(toint(i,j));
}
}
}
}
BetterGridGraph(int n,int m,char[][] s,char[] c,char W,char B){
N = n;
M = m;
S = s;
w = W;
b = B;
map = new HashMap<Character,ArrayList<Integer>>();
for(int i=0;i<c.length;i++) {
map.put(c[i],new ArrayList<Integer>());
}
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
for(int k=0;k<c.length;k++) {
if(S[i][j]==c[k])map.get(c[k]).add(toint(i,j));
}
}
}
}
int toint(int i,int j) {
return i*M+j;
}
ArrayList<Integer> getposlist(char c) {
return map.get(c);
}
int getpos(char c) {
return map.get(c).get(0);
}
int[] bfs(char C) {
int[] L = new int[N*M];
ArrayDeque<Integer> Q = new ArrayDeque<Integer>();
for(int i=0;i<N*M;i++){
L[i] = -1;
}
for(int s:map.get(C)) {
L[s] = 0;
Q.add(s);
}
Range X = new Range(0,N-1);
Range Y = new Range(0,M-1);
while(!Q.isEmpty()){
int v = Q.poll();
for(int i=0;i<4;i++){
int x = v/M;
int y = v%M;
int nx = x+dx[i];
int ny = y+dy[i];
if(X.isIn(nx)&&Y.isIn(ny)&&S[nx][ny]!=b) {
int w = toint(nx,ny);
if(L[w]==-1){
L[w] = L[v] + 1;
Q.add(w);
}
}
}
}
return L;
}
int[] bfsb(int s) {
int[] L = new int[N*M];
ArrayDeque<Integer> Q = new ArrayDeque<Integer>();
for(int i=0;i<N*M;i++){
L[i] = -1;
}
Q.add(s);
L[s] = 0;
Range X = new Range(0,N-1);
Range Y = new Range(0,M-1);
while(!Q.isEmpty()){
int v = Q.poll();
for(int i=0;i<4;i++){
int x = v/M;
int y = v%M;
int nx = x+dx[i];
int ny = y+dy[i];
if(X.isIn(nx)&&Y.isIn(ny)) {
int w = toint(nx,ny);
if(L[w]==-1){
if(S[x][y]==S[nx][ny]) {
L[w] = L[v];
Q.addFirst(w);
}else {
L[w] = L[v] + 1;
Q.addLast(w);
}
}
}
}
}
return L;
}
int[][] bfs2(char C,int K){
int[][] L = new int[N*M][K+1];
ArrayDeque<IntIntPair> Q = new ArrayDeque<IntIntPair>();
for(int i=0;i<N*M;i++){
for(int j=0;j<=K;j++) {
L[i][j] = 1000000007;
}
}
for(int s:map.get(C)) {
L[s][0] = 0;
Q.add(new IntIntPair(0,s));
}
Range X = new Range(0,N-1);
Range Y = new Range(0,M-1);
while(!Q.isEmpty()){
IntIntPair v = Q.poll();
for(int i=0;i<4;i++){
int x = v.b/M;
int y = v.b%M;
int h = v.a;
int nx = x+dx[i];
int ny = y+dy[i];
if(X.isIn(nx)&&Y.isIn(ny)&&S[nx][ny]!=b) {
int ni = toint(nx,ny);
int nh = S[nx][ny]==w?h+1:h;
if(nh>K) continue;
if(L[ni][nh]==1000000007){
L[ni][nh] = L[v.b][h]+1;
Q.add(new IntIntPair(nh,ni));
}
}
}
}
for(int i=0;i<N*M;i++) {
for(int j=1;j<=K;j++) {
L[i][j] = Math.min(L[i][j],L[i][j-1]);
}
}
return L;
}
}
class IntGridGraph{
int N;
int M;
int[][] B;
int[] dx = {0,1,0,-1};
int[] dy = {1,0,-1,0};
BiFunction<Integer,Integer,Boolean> F;
IntGridGraph(int n,int m,int[][] b){
N = n;
M = m;
B = b;
}
IntGridGraph(int n,int m,int[][] b,BiFunction<Integer,Integer,Boolean> f){
N = n;
M = m;
B = b;
F = f;
}
int toint(int i,int j) {
return i*M+j;
}
int[] bfs(int s) {
int[] L = new int[N*M];
for(int i=0;i<N*M;i++){
L[i] = -1;
}
L[s] = 0;
ArrayDeque<Integer> Q = new ArrayDeque<Integer>();
Q.add(s);
Range X = new Range(0,N-1);
Range Y = new Range(0,M-1);
while(!Q.isEmpty()){
int v = Q.poll();
for(int i=0;i<4;i++){
int x = v/M;
int y = v%M;
int nx = x+dx[i];
int ny = y+dy[i];
if(X.isIn(nx)&&Y.isIn(ny)&&F.apply(B[x][y],B[nx][ny])) {
int w = toint(nx,ny);
if(L[w]==-1){
L[w] = L[v] + 1;
Q.add(w);
}
}
}
}
return L;
}
void bfs(int s,int[] L) {
if(L[s]!=-1) return;
L[s] = 0;
ArrayDeque<Integer> Q = new ArrayDeque<Integer>();
Q.add(s);
Range X = new Range(0,N-1);
Range Y = new Range(0,M-1);
while(!Q.isEmpty()){
int v = Q.poll();
for(int i=0;i<4;i++){
int x = v/M;
int y = v%M;
int nx = x+dx[i];
int ny = y+dy[i];
if(X.isIn(nx)&&Y.isIn(ny)&&F.apply(B[x][y],B[nx][ny])) {
int w = toint(nx,ny);
if(L[w]==-1){
L[w] = L[v] + 1;
Q.add(w);
}
}
}
}
return;
}
int[][] bfs2(int s,int K){
int[][] L = new int[N*M][K+1];
for(int i=0;i<N*M;i++){
for(int j=0;j<=K;j++)
L[i][j] = 1000000007;
}
L[s][0] = 0;
PriorityQueue<IntIntPair> Q = new PriorityQueue<IntIntPair>(new IntIntComparator());
Q.add(new IntIntPair(0,s));
Range X = new Range(0,N-1);
Range Y = new Range(0,M-1);
while(!Q.isEmpty()){
IntIntPair v = Q.poll();
for(int i=0;i<4;i++){
int x = v.b/M;
int y = v.b%M;
int h = v.a;
int nx = x+dx[i];
int ny = y+dy[i];
if(X.isIn(nx)&&Y.isIn(ny)&&F.apply(B[x][y],B[nx][ny])) {
int ni = toint(nx,ny);
int nh = h + B[nx][ny];
if(nh>K) continue;
if(L[ni][nh]==1000000007){
L[ni][nh] = L[v.b][h] + 1;
Q.add(new IntIntPair(nh,ni));
}
}
}
}
for(int i=0;i<N*M;i++) {
for(int j=1;j<=K;j++) {
L[i][j] = Math.min(L[i][j],L[i][j-1]);
}
}
return L;
}
}
class Trie{
int nodenumber = 1;
ArrayList<TrieNode> l;
Trie(){
l = new ArrayList<TrieNode>();
l.add(new TrieNode());
}
void add(String S,int W){
int now = 0;
for(int i=0;i<S.length();i++) {
TrieNode n = l.get(now);
char c = S.charAt(i);
if(n.Exist[c-'a']!=-1) {
now = n.Exist[c-'a'];
}else {
l.add(new TrieNode());
n.Exist[c-'a'] = nodenumber;
now = nodenumber;
nodenumber++;
}
}
l.get(now).weight = W;
}
void find(String S,int i,int[] dp) {
int now = 0;
dp[i+1] = Math.max(dp[i],dp[i+1]);
for(int j=0;;j++) {
TrieNode n = l.get(now);
dp[i+j] = Math.max(dp[i+j],dp[i]+n.weight);
int slook = i+j;
if(slook>=S.length())return;
char c = S.charAt(slook);
if(n.Exist[c-'a']==-1)return;
now = n.Exist[c-'a'];
}
}
}
class TrieNode{
int[] Exist = new int[26];
int weight = 0;
TrieNode(){
for(int i=0;i<26;i++) {
Exist[i] = -1;
}
}
}
class SizeComparator implements Comparator<Edge>{
int[] size;
SizeComparator(int[] s) {
size = s;
}
public int compare(Edge o1, Edge o2) {
return size[o1.to]-size[o2.to];
}
}
class ConvexHullTrick {
long[] A, B;
int len;
public ConvexHullTrick(int n) {
A = new long[n];
B = new long[n];
}
private boolean check(long a, long b) {
return (B[len - 2] - B[len - 1]) * (a - A[len - 1]) >= (B[len - 1] - b) * (A[len - 1] - A[len - 2]);
}
public void add(long a, long b) {
while (len >= 2 && check(a, b)) {
len--;
}
A[len] = a;
B[len] = b;
len++;
}
public long query(long x) {
int l = -1, r = len - 1;
while (r - l > 1) {
int mid = (r + l) / 2;
if (get(mid,x)>=get(mid+1,x)) {
l = mid;
} else {
r = mid;
}
}
return get(r,x);
}
private long get(int k, long x) {
return A[k] * x + B[k];
}
}
class Range{
long l;
long r;
long length;
Range(int L,int R){
l = L;
r = R;
length = R-L+1;
}
public Range(long L, long R) {
l = L;
r = R;
length = R-L+1;
}
boolean isIn(int x) {
return (l<=x&&x<=r);
}
long kasanari(Range S) {
if(this.r<S.l||S.r<this.l) return 0;
else return Math.min(this.r,S.r) - Math.max(this.l,S.l)+1;
}
}
class LeftComparator implements Comparator<Range>{
public int compare(Range P, Range Q) {
return (int) Math.signum(P.l-Q.l);
}
}
class RightComparator implements Comparator<Range>{
public int compare(Range P, Range Q) {
return (int) Math.signum(P.r-Q.r);
}
}
class LengthComparator implements Comparator<Range>{
public int compare(Range P, Range Q) {
return (int) Math.signum(P.length-Q.length);
}
}
class SegmentTree<T,E>{
int N;
BiFunction<T,T,T> f;
BiFunction<T,E,T> g;
T d1;
ArrayList<T> dat;
SegmentTree(BiFunction<T,T,T> F,BiFunction<T,E,T> G,T D1,T[] v){
int n = v.length;
f = F;
g = G;
d1 = D1;
init(n);
build(v);
}
void init(int n) {
N = 1;
while(N<n)N*=2;
dat = new ArrayList<T>();
}
void build(T[] v) {
for(int i=0;i<2*N;i++) {
dat.add(d1);
}
for(int i=0;i<v.length;i++) {
dat.set(N+i-1,v[i]);
}
for(int i=N-2;i>=0;i--) {
dat.set(i,f.apply(dat.get(i*2+1),dat.get(i*2+2)));
}
}
void update(int k,E a) {
k += N-1;
dat.set(k,g.apply(dat.get(k),a));
while(k>0){
k = (k-1)/2;
dat.set(k,f.apply(dat.get(k*2+1),dat.get(k*2+2)));
}
}
T query(int a,int b, int k, int l ,int r) {
if(r<=a||b<=l) return d1;
if(a<=l&&r<=b) return dat.get(k);
T vl = query(a,b,k*2+1,l,(l+r)/2);
T vr = query(a,b,k*2+2,(l+r)/2,r);
return f.apply(vl,vr);
}
T query(int a,int b){
return query(a,b,0,0,N);
}
}
class LazySegmentTree<T,E> extends SegmentTree<T,E>{
BiFunction<E,E,E> h;
BiFunction<E,Integer,E> p = (E a,Integer b) ->{return a;};
E d0;
ArrayList<E> laz;
LazySegmentTree(BiFunction<T,T,T> F,BiFunction<T,E,T> G,BiFunction<E,E,E> H,T D1,E D0,T[] v){
super(F,G,D1,v);
int n = v.length;
h = H;
d0 = D0;
Init(n);
}
void build() {
}
void Init(int n){
laz = new ArrayList<E>();
for(int i=0;i<2*N;i++) {
laz.add(d0);
}
}
void eval(int len,int k) {
if(laz.get(k).equals(d0)) return;
if(k*2+1<N*2-1) {
laz.set(k*2+1,h.apply(laz.get(k*2+1),laz.get(k)));
laz.set(k*2+2,h.apply(laz.get(k*2+2),laz.get(k)));
}
dat.set(k,g.apply(dat.get(k), p.apply(laz.get(k), len)));
laz.set(k,d0);
}
T update(int a,int b,E x,int k,int l,int r) {
eval(r-l,k);
if(r<=a||b<=l) {
return dat.get(k);
}
if(a<=l&&r<=b) {
laz.set(k,h.apply(laz.get(k),x));
return g.apply(dat.get(k),p.apply(laz.get(k),r-l));
}
T vl = update(a,b,x,k*2+1,l,(l+r)/2);
T vr = update(a,b,x,k*2+2,(l+r)/2,r);
dat.set(k,f.apply(vl,vr));
return dat.get(k);
}
T update(int a,int b,E x) {
return update(a,b,x,0,0,N);
}
T query(int a,int b,int k,int l,int r) {
eval(r-l,k);
if(r<=a||b<=l) return d1;
if(a<=l&&r<=b) return dat.get(k);
T vl = query(a,b,k*2+1,l,(l+r)/2);
T vr = query(a,b,k*2+2,(l+r)/2,r);
return f.apply(vl, vr);
}
T query(int a,int b){
return query(a,b,0,0,N);
}
}
class AddSumSegmentTree{
int N;
int d1;
ArrayList<Integer> dat;
AddSumSegmentTree(int[] v){
int n = v.length;
init(n);
build(v);
}
void init(int n) {
N = 1;
while(N<n)N*=2;
dat = new ArrayList<Integer>();
}
void build(int[] v) {
for(int i=0;i<2*N;i++) {
dat.add(d1);
}
for(int i=0;i<v.length;i++) {
dat.set(N+i-1,v[i]);
}
for(int i=N-2;i>=0;i--) {
dat.set(i,dat.get(i*2+1)+dat.get(i*2+2));
}
}
void update(int k,int a) {
k += N-1;
dat.set(k,dat.get(k)+a);
while(k>0){
k = (k-1)/2;
dat.set(k,dat.get(k*2+1)+dat.get(k*2+2));
}
}
int query(int a,int b, int k, int l ,int r) {
if(r<=a||b<=l) return d1;
if(a<=l&&r<=b) return dat.get(k);
int vl = query(a,b,k*2+1,l,(l+r)/2);
int vr = query(a,b,k*2+2,(l+r)/2,r);
return vl+vr;
}
int query(int a,int b){
return query(a,b,0,0,N);
}
}
class AddSumLazySegmentTree {
int N;
long[] dat;
long[] laz;
AddSumLazySegmentTree(long[] v){
init(v.length);
for(int i=0;i<v.length;i++) {
dat[N+i-1]=v[i];
}
for(int i=N-2;i>=0;i--) {
dat[i]=dat[i*2+1]+dat[i*2+2];
}
}
void init(int n) {
N = 1;
while(N<n)N*=2;
dat = new long[2*N];
laz = new long[2*N];
}
void eval(int len,int k) {
if(laz[k]==0) return;
if(k*2+1<N*2-1) {
laz[k*2+1] += laz[k];
laz[k*2+2] += laz[k];
}
dat[k] += laz[k] * len;
laz[k] = 0;
}
long update(int a,int b,long x,int k,int l,int r) {
eval(r-l,k);
if(r<=a||b<=l) {
return dat[k];
}
if(a<=l&&r<=b) {
laz[k] += x;
return dat[k]+laz[k]*(r-l);
}
long vl = update(a,b,x,k*2+1,l,(l+r)/2);
long vr = update(a,b,x,k*2+2,(l+r)/2,r);
return dat[k] = vl+vr;
}
long update(int a,int b,long x) {
return update(a,b,x,0,0,N);
}
long query(int a,int b,int k,int l,int r) {
eval(r-l,k);
if(r<=a||b<=l) return 0;
if(a<=l&&r<=b) return dat[k];
long vl = query(a,b,k*2+1,l,(l+r)/2);
long vr = query(a,b,k*2+2,(l+r)/2,r);
return vl+vr;
}
long query(int a,int b){
return query(a,b,0,0,N);
}
}
class BinaryIndexedTree{
int[] val;
BinaryIndexedTree(int N){
val = new int[N+1];
}
long sum(int i) {
if(i==0)return 0;
long s = 0;
while(i>0) {
s += val[i];
i -= i & (-i);
}
return s;
}
void add(int i,int x) {
if(i==0)return;
while(i<val.length){
val[i] += x;
i += i & (-i);
}
}
}
class UnionFindTree {
int[] root;
int[] rank;
long[] size;
int[] edge;
int num;
UnionFindTree(int N){
root = new int[N];
rank = new int[N];
size = new long[N];
edge = new int[N];
num = N;
for(int i=0;i<N;i++){
root[i] = i;
size[i] = 1;
}
}
public long size(int x) {
return size[find(x)];
}
public boolean isRoot(int x) {
return x==find(x);
}
public long extraEdge(int x) {
int r = find(x);
return edge[r] - size[r] + 1;
}
public int find(int x){
if(root[x]==x){
return x;
}else{
return find(root[x]);
}
}
public boolean unite(int x,int y){
x = find(x);
y = find(y);
if(x==y){
edge[x]++;
return false;
}else{
num--;
if(rank[x]<rank[y]){
root[x] = y;
size[y] += size[x];
edge[y] += edge[x]+1;
}else{
root[y] = x;
size[x] += size[y];
edge[x] += edge[y]+1;
if(rank[x]==rank[y]){
rank[x]++;
}
}
return true;
}
}
public boolean same(int x,int y){
return find(x)==find(y);
}
}
class LightUnionFindTree {
int[] par;
int num;
LightUnionFindTree(int N){
par = new int[N];
num = N;
for(int i=0;i<N;i++){
par[i] = -1;
}
}
public boolean isRoot(int x) {
return x==find(x);
}
public int find(int x){
if(par[x]<0){
return x;
}else{
return find(par[x]);
}
}
public void unite(int x,int y){
x = find(x);
y = find(y);
if(x==y){
return;
}else{
num--;
if(par[x]<par[y]){
par[x] += par[y];
par[y] = x;
}else{
par[y] += par[x];
par[x] = y;
}
}
}
public boolean same(int x,int y){
return find(x)==find(y);
}
}
class ParticalEternalLastingUnionFindTree extends UnionFindTree{
int[] time;
int now;
ParticalEternalLastingUnionFindTree(int N){
super(N);
time = new int[N];
for(int i=0;i<N;i++) {
time[i] = 1000000007;
}
}
public int find(int t,int i) {
if(time[i]>t) {
return i;
}else {
return find(t,root[i]);
}
}
public void unite(int x,int y,int t) {
now = t;
x = find(t,x);
y = find(t,y);
if(x==y)return;
if(rank[x]<rank[y]){
root[x] = y;
size[y] += size[x];
time[x] = t;
}else{
root[y] = x;
size[x] += size[y];
if(rank[x]==rank[y]){
rank[x]++;
}
time[y] = t;
}
}
public int sametime(int x,int y) {
if(find(now,x)!=find(now,y)) return -1;
int ok = now;
int ng = 0;
while(ok-ng>1) {
int mid = (ok+ng)/2;
if(find(mid,x)==find(mid,y)) {
ok = mid;
}else {
ng = mid;
}
}
return ok;
}
}
class FlowEdge{
int to;
long cap;
int rev = 0;
FlowEdge(int To,long Cap,int Rev){
to = To;
cap = Cap;
rev = Rev;
}
}
class FlowGraph{
ArrayList<FlowEdge>[] list;
int[] level;
int[] iter;
ArrayDeque<Integer> q;
FlowGraph(int N){
list = new ArrayList[N];
for(int i=0;i<N;i++) {
list[i] = new ArrayList<FlowEdge>();
}
level = new int[N];
iter = new int[N];
q = new ArrayDeque<Integer>();
}
void addEdge(int i, int to, long cap) {
list[i].add(new FlowEdge(to,cap,list[to].size()));
list[to].add(new FlowEdge(i,0,list[i].size()-1));
}
void bfs(int s) {
Arrays.fill(level,-1);
level[s] = 0;
q.add(s);
while(!q.isEmpty()) {
int v = q.poll();
for(FlowEdge e:list[v]) {
if(e.cap>0&&level[e.to]<0) {
level[e.to] = level[v] + 1;
q.add(e.to);
}
}
}
}
long dfs(int v,int t,long f) {
if(v==t) return f;
for(int i = iter[v];i<list[v].size();i++) {
FlowEdge e = list[v].get(i);
if(e.cap>0&&level[v]<level[e.to]) {
long d = dfs(e.to,t,Math.min(f,e.cap));
if(d>0) {
e.cap -= d;
list[e.to].get(e.rev).cap += d;
return d;
}
}
iter[v]++;
}
return 0;
}
long flow(int s,int t,long lim) {
long flow = 0;
while(true) {
bfs(s);
if(level[t]<0||lim==0) return flow;
Arrays.fill(iter,0);
while(true) {
long f = dfs(s,t,lim);
if(f>0) {
flow += f;
lim -= f;
}
else break;
}
}
}
long flow(int s,int t) {
return flow(s,t,1000000007);
}
}
class Graph {
ArrayList<Edge>[] list;
int size;
TreeSet<LinkEdge> Edges = new TreeSet<LinkEdge>(new LinkEdgeComparator());
@SuppressWarnings("unchecked")
Graph(int N){
size = N;
list = new ArrayList[N];
for(int i=0;i<N;i++){
list[i] = new ArrayList<Edge>();
}
}
public long[] dicount(int s) {
long[] L = new long[size];
long[] c = new long[size];
int mod = 1000000007;
for(int i=0;i<size;i++){
L[i] = -1;
}
int[] v = new int[size];
L[s] = 0;
c[s] = 1;
PriorityQueue<LongIntPair> Q = new PriorityQueue<LongIntPair>(new LongIntComparator());
Q.add(new LongIntPair(0,s));
while(!Q.isEmpty()){
LongIntPair C = Q.poll();
if(v[C.b]==0){
L[C.b] = C.a;
v[C.b] = 1;
for(Edge D:list[C.b]) {
//System.out.println(C.b +" "+ D.to);
if(L[D.to]==-1||L[D.to]>L[C.b]+D.cost) {
L[D.to]=L[C.b]+D.cost;
c[D.to] = c[C.b];
Q.add(new LongIntPair(L[C.b]+D.cost,D.to));
}else if(L[D.to]==L[C.b]+D.cost) {
c[D.to] += c[C.b];
}
c[D.to] %= mod;
}
}
}
return c;
}
public long[] roots(int s) {
int[] in = new int[size];
ArrayDeque<Integer> q = new ArrayDeque<Integer>();
long[] N = new long[size];
long mod = 1000000007;
for(int i=0;i<size;i++) {
for(Edge e:list[i])in[e.to]++;
}
for(int i=0;i<size;i++) {
if(in[i]==0)q.add(i);
}
N[s] = 1;
while(!q.isEmpty()) {
int v = q.poll();
for(Edge e:list[v]) {
N[e.to] += N[v];
if(N[e.to]>=mod)N[e.to]-= mod;
in[e.to]--;
if(in[e.to]==0)q.add(e.to);
}
}
return N;
}
void addEdge(int a,int b){
list[a].add(new Edge(b,1));
}
void addWeightedEdge(int a,int b,long c){
list[a].add(new Edge(b,c));
}
void addEgdes(int[] a,int[] b){
for(int i=0;i<a.length;i++){
list[a[i]].add(new Edge(b[i],1));
}
}
void addWeightedEdges(int[] a ,int[] b ,int[] c){
for(int i=0;i<a.length;i++){
list[a[i]].add(new Edge(b[i],c[i]));
}
}
long[] bfs(int s){
long[] L = new long[size];
for(int i=0;i<size;i++){
L[i] = -1;
}
L[s] = 0;
ArrayDeque<Integer> Q = new ArrayDeque<Integer>();
Q.add(s);
while(!Q.isEmpty()){
int v = Q.poll();
for(Edge e:list[v]){
int w = e.to;
long c = e.cost;
if(L[w]==-1){
L[w] = L[v] + c;
Q.add(w);
}
}
}
return L;
}
long[][] bfswithrev(int s){
long[][] L = new long[2][size];
for(int i=0;i<size;i++){
L[0][i] = -1;
L[1][i] = -1;
}
L[0][s] = 0;
ArrayDeque<Integer> Q = new ArrayDeque<Integer>();
Q.add(s);
while(!Q.isEmpty()){
int v = Q.poll();
for(Edge e:list[v]){
int w = e.to;
long c = e.cost;
if(L[0][w]==-1){
L[0][w] = L[0][v] + c;
L[1][w] = v;
Q.add(w);
}
}
}
return L;
}
long[] bfs2(int[] d,int s){
long[] L = new long[size];
for(int i=0;i<size;i++){
L[i] = -1;
}
int p = 0;
L[s] = 0;
d[s] = p;
p++;
ArrayDeque<Integer> Q = new ArrayDeque<Integer>();
Q.add(s);
while(!Q.isEmpty()){
int v = Q.poll();
for(Edge e:list[v]){
int w = e.to;
long c = e.cost;
if(L[w]==-1){
d[w] = p;
p++;
L[w] = L[v] + c;
Q.add(w);
}
}
}
return L;
}
boolean bfs3(int s,long[] L, int[] vi){
if(vi[s]==1) return true;
vi[s] = 1;
ArrayDeque<Integer> Q = new ArrayDeque<Integer>();
Q.add(s);
while(!Q.isEmpty()){
int v = Q.poll();
for(Edge e:list[v]){
int w = e.to;
long c = e.cost;
if(vi[e.to]==0) {
L[e.to] = (int)c - L[v];
Q.add(w);
vi[e.to] = 1;
}else {
if(L[e.to]!=(int)c - L[v]) {
return false;
}
}
}
}
return true;
}
int[] isTwoColor(){
int[] L = new int[size];
for(int i=0;i<size;i++){
L[i] = -1;
}
L[0] = 0;
ArrayDeque<Integer> Q = new ArrayDeque<Integer>();
Q.add(0);
while(!Q.isEmpty()){
int v = Q.poll();
for(Edge e:list[v]){
int w = e.to;
if(L[w]==-1){
L[w] = 1-L[v];
Q.add(w);
}else{
if(L[v]+L[w]!=1){
L[0] = -2;
}
}
}
}
return L;
}
void isTwoColor2(int i,int[] L){
L[i] = 0;
ArrayDeque<Integer> Q = new ArrayDeque<Integer>();
Q.add(i);
while(!Q.isEmpty()){
int v = Q.poll();
for(Edge e:list[v]){
int w = e.to;
if(L[w]==-1){
L[w] = 1-L[v];
Q.add(w);
}else{
if(L[v]+L[w]!=1){
L[0] = -2;
}
}
}
}
}
long[] dijkstra(int s){
long[] L = new long[size];
for(int i=0;i<size;i++){
L[i] = -1;
}
int[] v = new int[size];
L[s] = 0;
PriorityQueue<LongIntPair> Q = new PriorityQueue<LongIntPair>(new LongIntComparator());
Q.add(new LongIntPair(0,s));
while(!Q.isEmpty()){
LongIntPair C = Q.poll();
if(v[C.b]==0){
L[C.b] = C.a;
v[C.b] = 1;
for(Edge D:list[C.b]) {
if(L[D.to]==-1||L[D.to]>L[C.b]+D.cost) {
L[D.to]=L[C.b]+D.cost;
Q.add(new LongIntPair(L[C.b]+D.cost,D.to));
}
}
}
}
return L;
}
ArrayList<Graph> makeapart(){
ArrayList<Graph> ans = new ArrayList<Graph>();
boolean[] b = new boolean[size];
int[] num = new int[size];
for(int i=0;i<size;i++){
if(b[i])continue;
int sz = 0;
ArrayList<Integer> l = new ArrayList<Integer>();
ArrayDeque<Integer> Q = new ArrayDeque<Integer>();
Q.add(i);
b[i] = true;
while(!Q.isEmpty()){
int v = Q.poll();
num[v] = sz;
sz++;
l.add(v);
for(Edge e:list[v]){
if(!b[e.to]){
Q.add(e.to);
b[e.to] = true;
}
}
}
Graph H = new Graph(sz);
for(int e:l){
for(Edge E:list[e]){
H.addWeightedEdge(num[e],num[E.to],E.cost);
}
}
ans.add(H);
}
return ans;
}
long[] bellmanFord(int s) {
long inf = 1000000000;
inf *= inf;
long[] d = new long[size];
boolean[] n = new boolean[size];
d[s] = 0;
for(int i=1;i<size;i++){
d[i] = inf;
d[i] *= d[i];
}
for(int i=0;i<size-1;i++){
for(int j=0;j<size;j++){
for(Edge E:list[j]){
if(d[j]!=inf&&d[E.to]>d[j]+E.cost){
d[E.to]=d[j]+E.cost;
}
}
}
}
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
for(Edge e:list[j]){
if(d[j]==inf) continue;
if(d[e.to]>d[j]+e.cost) {
d[e.to]=d[j]+e.cost;
n[e.to] = true;
}
if(n[j])n[e.to] = true;
}
}
}
for(int i=0;i<size;i++) {
if(n[i])d[i] = inf;
}
return d;
}
long[][] WarshallFloyd(long[][] a){
int n = a.length;
long[][] ans = new long[n][n];
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
ans[i][j] = a[i][j]==0?(long)1e16:a[i][j];
if(i==j)ans[i][j]=0;
}
}
for(int k=0;k<n;k++) {
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
ans[i][j] = Math.min(ans[i][j],ans[i][k]+ans[k][j]);
}
}
}
return ans;
}
long[] maxtra(int s,long l){
long[] L = new long[size];
for(int i=0;i<size;i++){
L[i] = -1;
}
int[] v = new int[size];
L[s] = -1;
PriorityQueue<Pair> Q = new PriorityQueue<Pair>(new SampleComparator());
Q.add(new Pair(l,s));
while(!Q.isEmpty()){
Pair C = Q.poll();
if(v[(int)C.b]==0){
L[(int)C.b] = C.a;
v[(int) C.b] = 1;
for(Edge D:list[(int) C.b])Q.add(new Pair(Math.max(L[(int)C.b],D.cost),D.to));
}
}
return L;
}
long[] mintra(int s){
long[] L = new long[size];
for(int i=0;i<size;i++){
L[i] = -1;
}
int[] v = new int[size];
L[s] = s;
PriorityQueue<Pair> Q = new PriorityQueue<Pair>(new SampleComparator().reversed());
Q.add(new Pair(s,s));
while(!Q.isEmpty()){
Pair C = Q.poll();
if(v[(int)C.b]==0){
L[(int)C.b] = C.a;
v[(int) C.b] = 1;
for(Edge D:list[(int) C.b])Q.add(new Pair(Math.min(L[(int)C.b],D.cost),D.to));
}
}
return L;
}
long Kruskal(){
long r = 0;
for(int i=0;i<size;i++) {
for(Edge e:list[i]) {
Edges.add(new LinkEdge(e.cost,i,e.to));
}
}
UnionFindTree UF = new UnionFindTree(size);
for(LinkEdge e:Edges){
if(e.a>=0&&e.b>=0) {
if(!UF.same(e.a,e.b)){
r += e.L;
UF.unite(e.a,e.b);
}
}
}
return r;
}
ArrayList<Integer> Kahntsort(){
ArrayList<Integer> ans = new ArrayList<Integer>();
PriorityQueue<Integer> q = new PriorityQueue<Integer>();
int[] in = new int[size];
for(int i=0;i<size;i++) {
for(Edge e:list[i])in[e.to]++;
}
for(int i=0;i<size;i++) {
if(in[i]==0)q.add(i);
}
while(!q.isEmpty()) {
int v = q.poll();
ans.add(v);
for(Edge e:list[v]) {
in[e.to]--;
if(in[e.to]==0)q.add(e.to);
}
}
for(int i=0;i<size;i++) {
if(in[i]>0)return new ArrayList<Integer>();
}
return ans;
}
public Stack<Integer> findCycle() {
Stack<Integer> ans = new Stack<Integer>();
boolean[] v = new boolean[size];
boolean[] f = new boolean[size];
for(int i=0;i<size;i++) {
if(findCycle(i,ans,v,f))break;
}
return ans;
}
private boolean findCycle(int i, Stack<Integer>ans, boolean[] v,boolean[] f) {
v[i] = true;
ans.push(i);
for(Edge e:list[i]) {
if(f[e.to]) continue;
if(v[e.to]&&!f[e.to]) {
return true;
}
if(findCycle(e.to,ans,v,f))return true;
}
ans.pop();
f[i] = true;
return false;
}
RootedTree dfsTree(int i) {
int[] u = new int[size];
RootedTree r = new RootedTree(size);
dfsTree(i,u,r);
return r;
}
private void dfsTree(int i, int[] u, RootedTree r) {
u[i] = 1;
r.trans[r.node] = i;
r.rev[i] = r.node;
r.node++;
for(Edge e:list[i]) {
if(u[e.to]==0) {
r.list[i].add(e);
u[e.to] = 1;
dfsTree(e.to,u,r);
}
}
}
}
class LightGraph {
ArrayList<Integer>[] list;
int size;
TreeSet<LinkEdge> Edges = new TreeSet<LinkEdge>(new LinkEdgeComparator());
@SuppressWarnings("unchecked")
LightGraph(int N){
size = N;
list = new ArrayList[N];
for(int i=0;i<N;i++){
list[i] = new ArrayList<Integer>();
}
}
void addEdge(int a,int b){
list[a].add(b);
}
public Stack<Integer> findCycle() {
Stack<Integer> ans = new Stack<Integer>();
boolean[] v = new boolean[size];
boolean[] f = new boolean[size];
for(int i=0;i<size;i++) {
if(findCycle(i,ans,v,f))break;
}
return ans;
}
private boolean findCycle(int i, Stack<Integer>ans, boolean[] v,boolean[] f) {
v[i] = true;
ans.push(i);
for(int e:list[i]) {
if(f[e]) continue;
if(v[e]&&!f[e]) {
return true;
}
if(findCycle(e,ans,v,f))return true;
}
ans.pop();
f[i] = true;
return false;
}
}
class Tree extends Graph{
public Tree(int N) {
super(N);
}
long[] tyokkei(){
long[] a = bfs(0);
int md = -1;
long m = 0;
for(int i=0;i<size;i++){
if(m<a[i]){
m = a[i];
md = i;
}
}
long[] b = bfs(md);
int md2 = -1;
long m2 = 0;
for(int i=0;i<size;i++){
if(m2<b[i]){
m2 = b[i];
md2 = i;
}
}
long[] r = {m2,md,md2};
return r;
}
int[] size(int r) {
int[] ret = new int[size];
dfssize(r,-1,ret);
return ret;
}
private int dfssize(int i, int rev, int[] ret) {
int sz = 1;
for(Edge e:list[i]) {
if(e.to!=rev) sz += dfssize(e.to,i,ret);
}
return ret[i] = sz;
}
}
class RootedTree extends Graph{
int[] trans;
int[] rev;
int node = 0;
RootedTree(int N){
super(N);
trans = new int[N];
rev = new int[N];
}
public int[] parents() {
int[] ret = new int[size];
for(int i=0;i<size;i++) {
for(Edge e:list[i]) {
ret[rev[e.to]] = rev[i];
}
}
ret[0] = -1;
return ret;
}
}
class LinkEdge{
long L;
int a ;
int b;
int id;
LinkEdge(long l,int A,int B){
L = l;
a = A;
b = B;
}
LinkEdge(long l,int A,int B,int i){
L = l;
a = A;
b = B;
id = i;
}
public boolean equals(Object o){
LinkEdge O = (LinkEdge) o;
return O.a==this.a&&O.b==this.b&&O.L==this.L;
}
public int hashCode(){
return Objects.hash(L,a,b);
}
}
class DoubleLinkEdge{
double D;
int a;
int b;
DoubleLinkEdge(double d,int A,int B){
D = d;
a = A;
b = B;
}
public boolean equals(Object o){
DoubleLinkEdge O = (DoubleLinkEdge) o;
return O.a==this.a&&O.b==this.b&&O.D==this.D;
}
public int hashCode(){
return Objects.hash(D,a,b);
}
}
class Edge{
int to;
long cost;
Edge(int a,long b){
to = a;
cost = b;
}
}
class indexedEdge extends Edge{
int id;
indexedEdge(int a, long b, int c) {
super(a,b);
id = c;
}
}
class DoubleLinkEdgeComparator implements Comparator<DoubleLinkEdge>{
public int compare(DoubleLinkEdge P, DoubleLinkEdge Q) {
return Double.compare(P.D,Q.D);
}
}
class LinkEdgeComparator implements Comparator<LinkEdge>{
public int compare(LinkEdge P, LinkEdge Q) {
return Long.compare(P.L,Q.L);
}
}
class Pair{
long a;
long b;
Pair(long p,long q){
this.a = p;
this.b = q;
}
public boolean equals(Object o){
Pair O = (Pair) o;
return O.a==this.a&&O.b==this.b;
}
public int hashCode(){
return Objects.hash(a,b);
}
}
class SampleComparator implements Comparator<Pair>{
public int compare(Pair P, Pair Q) {
long t = P.a-Q.a;
if(t==0){
if(P.b==Q.b)return 0;
return P.b>Q.b?1:-1;
}
return t>=0?1:-1;
}
}
class LongIntPair{
long a;
int b;
LongIntPair(long p,int q){
this.a = p;
this.b = q;
}
public boolean equals(Object o){
LongIntPair O = (LongIntPair) o;
return O.a==this.a&&O.b==this.b;
}
public int hashCode(){
return Objects.hash(a,b);
}
}
class LongIntComparator implements Comparator<LongIntPair>{
public int compare(LongIntPair P, LongIntPair Q) {
long t = P.a-Q.a;
if(t==0){
if(P.b>Q.b){
return 1;
}else{
return -1;
}
}
return t>=0?1:-1;
}
}
class IntIntPair{
int a;
int b;
IntIntPair(int p,int q){
this.a = p;
this.b = q;
}
IntIntPair(int p,int q,String s){
if(s.equals("sort")) {
this.a = Math.min(p,q);
this.b = Math.max(p,q);
}
}
public boolean equals(Object o){
IntIntPair O = (IntIntPair) o;
return O.a==this.a&&O.b==this.b;
}
public int hashCode(){
return Objects.hash(a,b);
}
}
class IntIntComparator implements Comparator<IntIntPair>{
public int compare(IntIntPair P, IntIntPair Q) {
int t = P.a-Q.a;
if(t==0){
return P.b-Q.b;
}
return t;
}
}
class CIPair{
char c;
int i;
CIPair(char C,int I){
c = C;
i = I;
}
public boolean equals(Object o){
CIPair O = (CIPair) o;
return O.c==this.c&&O.i==this.i;
}
public int hashCode(){
return Objects.hash(c,i);
}
}
class DoublePair{
double a;
double b;
DoublePair(double p,double q){
this.a = p;
this.b = q;
}
public boolean equals(Object o){
DoublePair O = (DoublePair) o;
return O.a==this.a&&O.b==this.b;
}
public int hashCode(){
return Objects.hash(a,b);
}
}
class Triplet{
long a;
long b;
long c;
Triplet(long p,long q,long r){
a = p;
b = q;
c = r;
}
public boolean equals(Object o){
Triplet O = (Triplet) o;
return O.a==this.a&&O.b==this.b&&O.c==this.c?true:false;
}
public int hashCode(){
return Objects.hash(a,b,c);
}
}
class TripletComparator implements Comparator<Triplet>{
public int compare(Triplet P, Triplet Q) {
long t = P.a-Q.a;
if(t==0){
long tt = P.b-Q.b;
if(tt==0) {
if(P.c>Q.c) {
return 1;
}else if(P.c<Q.c){
return -1;
}else {
return 0;
}
}
return tt>0?1:-1;
}
return t>=0?1:-1;
}
}
class DDComparator implements Comparator<DoublePair>{
public int compare(DoublePair P, DoublePair Q) {
return P.a-Q.a>=0?1:-1;
}
}
class DoubleTriplet{
double a;
double b;
double c;
DoubleTriplet(double p,double q,double r){
this.a = p;
this.b = q;
this.c = r;
}
public boolean equals(Object o){
DoubleTriplet O = (DoubleTriplet) o;
return O.a==this.a&&O.b==this.b&&O.c==this.c;
}
public int hashCode(){
return Objects.hash(a,b,c);
}
}
class DoubleTripletComparator implements Comparator<DoubleTriplet>{
public int compare(DoubleTriplet P, DoubleTriplet Q) {
if(P.a==Q.a) return 0;
return P.a-Q.a>0?1:-1;
}
}
class FastScanner {
private final java.io.InputStream in = System.in;
private final byte[] b = new byte[1024];
private int p = 0;
private int bl = 0;
private boolean hNB() {
if (p<bl) {
return true;
}else{
p = 0;
try {
bl = in.read(b);
} catch (IOException e) {
e.printStackTrace();
}
if (bl<=0) {
return false;
}
}
return true;
}
private int rB() { if (hNB()) return b[p++]; else return -1;}
private static boolean iPC(int c) { return 33 <= c && c <= 126;}
private void sU() { while(hNB() && !iPC(b[p])) p++;}
public boolean hN() { sU(); return hNB();}
public String next() {
if (!hN()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = rB();
while(iPC(b)) {
sb.appendCodePoint(b);
b = rB();
}
return sb.toString();
}
public char nextChar() {
return next().charAt(0);
}
public long nextLong() {
if (!hN()) throw new NoSuchElementException();
long n = 0;
boolean m = false;
int b = rB();
if (b=='-') {
m=true;
b=rB();
}
if (b<'0'||'9'<b) {
throw new NumberFormatException();
}
while(true){
if ('0'<=b&&b<='9') {
n *= 10;
n += b - '0';
}else if(b == -1||!iPC(b)){
return (m?-n:n);
}else{
throw new NumberFormatException();
}
b = rB();
}
}
public int nextInt() {
if (!hN()) throw new NoSuchElementException();
long n = 0;
boolean m = false;
int b = rB();
if (b == '-') {
m = true;
b = rB();
}
if (b<'0'||'9'<b) {
throw new NumberFormatException();
}
while(true){
if ('0'<=b&&b<='9') {
n *= 10;
n += b-'0';
}else if(b==-1||!iPC(b)){
return (int) (m?-n:n);
}else{
throw new NumberFormatException();
}
b = rB();
}
}
public int[] nextInts(int n) {
int[] a = new int[n];
for(int i=0;i<n;i++) {
a[i] = nextInt();
}
return a;
}
public int[] nextInts(int n,int s) {
int[] a = new int[n+s];
for(int i=s;i<n+s;i++) {
a[i] = nextInt();
}
return a;
}
public long[] nextLongs(int n, int s) {
long[] a = new long[n+s];
for(int i=s;i<n+s;i++) {
a[i] = nextLong();
}
return a;
}
public long[] nextLongs(int n) {
long[] a = new long[n];
for(int i=0;i<n;i++) {
a[i] = nextLong();
}
return a;
}
public int[][] nextIntses(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] = nextInt();
}
}
return a;
}
public String[] nexts(int n) {
String[] a = new String[n];
for(int i=0;i<n;i++) {
a[i] = next();
}
return a;
}
void nextIntses(int n,int[] ...m) {
int l = m[0].length;
for(int i=0;i<l;i++) {
for(int j=0;j<m.length;j++) {
m[j][i] = nextInt();
}
}
}
void nextLongses(int n,long[] ...m) {
int l = m[0].length;
for(int i=0;i<l;i++) {
for(int j=0;j<m.length;j++) {
m[j][i] = nextLong();
}
}
}
Graph nextyukoGraph(int n,int m) {
Graph G = new Graph(n);
for(int i=0;i<m;i++) {
int a = nextInt()-1;
int b = nextInt()-1;
G.addEdge(a,b);
}
return G;
}
Graph nextGraph(int n,int m) {
Graph G = new Graph(n);
for(int i=0;i<m;i++) {
int a = nextInt()-1;
int b = nextInt()-1;
G.addEdge(a,b);
G.addEdge(b,a);
}
return G;
}
Graph nextWeightedGraph(int n,int m) {
Graph G = new Graph(n);
for(int i=0;i<m;i++) {
int a = nextInt()-1;
int b = nextInt()-1;
long c = nextLong();
G.addWeightedEdge(a,b,c);
G.addWeightedEdge(b,a,c);
}
return G;
}
Tree nextTree(int n) {
Tree T = new Tree(n);
for(int i=0;i<n-1;i++) {
int a = nextInt()-1;
int b = nextInt()-1;
T.addEdge(a,b);
T.addEdge(b,a);
}
return T;
}
}
class Mathplus{
long mod = 1000000007;
long[] fac;
long[] revfac;
long[][] comb;
long[] pow;
long[] revpow;
boolean isBuild = false;
boolean isBuildc = false;
boolean isBuildp = false;
int mindex = -1;
int maxdex = -1;
int graydiff = 0;
int graymark = 0;
int LIS(int N, int[] a) {
int[] dp = new int[N+1];
Arrays.fill(dp,(int)mod);
for(int i=0;i<N;i++) {
int ok = 0;
int ng = N;
while(ng-ok>1) {
int mid = (ok+ng)/2;
if(dp[mid]<a[i])ok = mid;
else ng = mid;
}
dp[ok+1] = a[i];
}
int ok = 0;
for(int i=1;i<=N;i++) {
if(dp[i]<mod)ok=i;
}
return ok;
}
public Integer[] Ints(int n, int i) {
Integer[] ret = new Integer[n];
Arrays.fill(ret,i);
return ret;
}
public Long[] Longs(int n, long i) {
Long[] ret = new Long[n];
Arrays.fill(ret,i);
return ret;
}
public boolean nexperm(int[] p) {
int n = p.length;
for(int i=n-1;i>0;i--) {
if(p[i-1]<p[i]) {
int sw = n;
for(int j=n-1;j>=i;j--) {
if(p[i-1]<p[j]) {
sw = j;
break;
}
}
int tmp = p[i-1];
p[i-1] = p[sw];
p[sw] = tmp;
int[] r = new int[n];
for(int j=i;j<n;j++) {
r[j] = p[n-1-j+i];
}
for(int j=i;j<n;j++) {
p[j] = r[j];
}
return true;
}
}
return false;
}
public int[] makeperm(int n) {
int[] a = new int[n];
for(int i=0;i<n;i++) {
a[i] = i;
}
return a;
}
public void timeout() throws InterruptedException {
Thread.sleep(10000);
}
public int gray(int i) {
for(int j=0;j<20;j++) {
if(contains(i,j)) {
graydiff = j;
if(contains(i,j+1))graymark=-1;
else graymark = 1;
break;
}
}
return i ^ (i>>1);
}
public void printjudge(boolean b, String y, String n) {
System.out.println(b?y:n);
}
public void printYN(boolean b) {
printjudge(b,"Yes","No");
}
public void printyn(boolean b) {
printjudge(b,"yes","no");
}
public void reverse(int[] x) {
int[] r = new int[x.length];
for(int i=0;i<x.length;i++)r[i] = x[x.length-1-i];
for(int i=0;i<x.length;i++)x[i] = r[i];
}
public void reverse(long[] x) {
long[] r = new long[x.length];
for(int i=0;i<x.length;i++)r[i] = x[x.length-1-i];
for(int i=0;i<x.length;i++)x[i] = r[i];
}
public DoubleTriplet Line(double x1,double y1,double x2,double y2) {
double a = y1-y2;
double b = x2-x1;
double c = x1*y2-x2*y1;
return new DoubleTriplet(a,b,c);
}
public double putx(DoubleTriplet T,double x) {
return -(T.a*x+T.c)/T.b;
}
public double puty(DoubleTriplet T,double y) {
return -(T.b*y+T.c)/T.a;
}
public double Distance(DoublePair P,DoublePair Q) {
return Math.sqrt((P.a-Q.a) * (P.a-Q.a) + (P.b-Q.b) * (P.b-Q.b));
}
public double DistanceofPointandLine(DoublePair P,Triplet T) {
return Math.abs(P.a*T.a+P.b*T.b+T.c) / Math.sqrt(T.a*T.a+T.b*T.b);
}
public boolean cross(long ax, long ay, long bx, long by, long cx, long cy, long dx, long dy) {
if((ax-bx)*(cy-dy)==(ay-by)*(cx-dx)) {
if(ax-bx!=0) {
Range A = new Range(ax,bx);
Range B = new Range(cx,dx);
return A.kasanari(B)>0;
}else {
Range A = new Range(ay,by);
Range B = new Range(cy,dy);
return A.kasanari(B)>0;
}
}
long ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
long tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
long tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
long td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);
return((tc>=0&&td<=0)||(tc<=0&&td>=0))&&((ta>=0&&tb<=0)||(ta<=0&&tb>=0));
}
public boolean dcross(double ax, double ay, double bx, double by, double cx, double cy, double dx, double dy) {
double ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
double tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
double tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
double td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);
return((tc>=0&&td<=0)||(tc<=0&&td>=0))&&((ta>=0&&tb<=0)||(ta<=0&&tb>=0));
}
void buildFac(){
fac = new long[10000003];
revfac = new long[10000003];
fac[0] = 1;
for(int i=1;i<=10000002;i++){
fac[i] = (fac[i-1] * i)%mod;
}
revfac[10000002] = rev(fac[10000002])%mod;
for(int i=10000001;i>=0;i--) {
revfac[i] = (revfac[i+1] * (i+1))%mod;
}
isBuild = true;
}
void buildFacn(int n){
fac = new long[n+1];
revfac = new long[n+1];
fac[0] = 1;
for(int i=1;i<=n;i++){
fac[i] = (fac[i-1] * i)%mod;
}
revfac[n] = rev(fac[n])%mod;
for(int i=n-1;i>=0;i--) {
revfac[i] = (revfac[i+1] * (i+1))%mod;
}
isBuild = true;
}
public long[] buildrui(int[] a) {
int n = a.length;
long[] ans = new long[n];
ans[0] = a[0];
for(int i=1;i<n;i++) {
ans[i] = ans[i-1] + a[i];
}
return ans;
}
public int[][] ibuildrui(int[][] a) {
int n = a.length;
int m = a[0].length;
int[][] ans = new int[n][m];
for(int i=1;i<n;i++) {
for(int j=1;j<m;j++) {
ans[i][j] = a[i][j];
}
}
for(int i=1;i<n;i++) {
for(int j=1;j<m;j++) {
ans[i][j] += ans[i][j-1] + ans[i-1][j] - ans[i-1][j-1];
}
}
return ans;
}
public void buildruin(int[][] a) {
int n = a.length;
int m = a[0].length;
for(int i=1;i<n;i++) {
for(int j=1;j<m;j++) {
a[i][j] += a[i][j-1] + a[i-1][j] - a[i-1][j-1];
}
}
}
public long[][] buildrui(int[][] a) {
int n = a.length;
int m = a[0].length;
long[][] ans = new long[n][m];
for(int i=1;i<n;i++) {
for(int j=1;j<m;j++) {
ans[i][j] = a[i][j];
}
}
for(int i=1;i<n;i++) {
for(int j=1;j<m;j++) {
ans[i][j] += ans[i][j-1] + ans[i-1][j] - ans[i-1][j-1];
}
}
return ans;
}
public int getrui(int[][] r,int a,int b,int c,int d) {
return r[c][d] - r[a-1][d] - r[c][b-1] + r[a-1][b-1];
}
public long getrui(long[][] r,int a,int b,int c,int d) {
if(a<0||b<0||c>=r.length||d>=r[0].length) return mod;
return r[c][d] - r[a-1][d] - r[c][b-1] + r[a-1][b-1];
}
long divroundup(long n,long d) {
if(n==0)return 0;
return (n-1)/d+1;
}
public long sigma(long i) {
return i*(i+1)/2;
}
public int digit(long i) {
int ans = 1;
while(i>=10) {
i /= 10;
ans++;
}
return ans;
}
public int digitsum(long n) {
int ans = 0;
while(n>0) {
ans += n%10;
n /= 10;
}
return ans;
}
public int popcount(int i) {
int ans = 0;
while(i>0) {
ans += i%2;
i /= 2;
}
return ans;
}
public boolean contains(int S,int i) {return (S>>i&1)==1;}
public int bitremove(int S,int i) {return S&(~(1<<i));}
public int bitadd(int S,int i) {return S|(1<<i);}
public boolean isSubSet(int S,int T) {return (S-T)==(S^T);}
public boolean isDisjoint(int S,int T) {return (S+T)==(S^T);}
public boolean contains(long S,int i) {return (S>>i&1)==1;}
public long bitremove(long S,int i) {return S&(~(1<<i));}
public long bitadd(long S,int i) {return S|(1<<i);}
public boolean isSubSet(long S,long T) {return (S-T)==(S^T);}
public boolean isDisjoint(long S,long T) {return (S+T)==(S^T);}
public int isBigger(int[] d, int i) {
int ok = d.length;
int ng = -1;
while(Math.abs(ok-ng)>1) {
int mid = (ok+ng)/2;
if(d[mid]>i) {
ok = mid;
}else {
ng = mid;
}
}
return ok;
}
public int isSmaller(int[] d, int i) {
int ok = -1;
int ng = d.length;
while(Math.abs(ok-ng)>1) {
int mid = (ok+ng)/2;
if(d[mid]<i) {
ok = mid;
}else {
ng = mid;
}
}
return ok;
}
public int isBigger(long[] d, long i) {
int ok = d.length;
int ng = -1;
while(Math.abs(ok-ng)>1) {
int mid = (ok+ng)/2;
if(d[mid]>i) {
ok = mid;
}else {
ng = mid;
}
}
return ok;
}
public int isSmaller(long[] d, long i) {
int ok = -1;
int ng = d.length;
while(Math.abs(ok-ng)>1) {
int mid = (ok+ng)/2;
if(d[mid]<i) {
ok = mid;
}else {
ng = mid;
}
}
return ok;
}
public int isBigger(ArrayList<Integer> d, int i) {
int ok = d.size();
int ng = -1;
while(Math.abs(ok-ng)>1) {
int mid = (ok+ng)/2;
if(d.get(mid)>i) {
ok = mid;
}else {
ng = mid;
}
}
return ok;
}
public int isSmaller(ArrayList<Integer> d, int i) {
int ok = -1;
int ng = d.size();
while(Math.abs(ok-ng)>1) {
int mid = (ok+ng)/2;
if(d.get(mid)<i) {
ok = mid;
}else {
ng = mid;
}
}
return ok;
}
public int isBigger(ArrayList<Long> d, long i) {
int ok = d.size();
int ng = -1;
while(Math.abs(ok-ng)>1) {
int mid = (ok+ng)/2;
if(d.get(mid)>i) {
ok = mid;
}else {
ng = mid;
}
}
return ok;
}
public int isSmaller(ArrayList<Long> d, long i) {
int ok = -1;
int ng = d.size();
while(Math.abs(ok-ng)>1) {
int mid = (ok+ng)/2;
if(d.get(mid)<i) {
ok = mid;
}else {
ng = mid;
}
}
return ok;
}
public HashSet<Integer> primetable(int m) {
HashSet<Integer> pt = new HashSet<Integer>();
for(int i=2;i<=m;i++) {
boolean b = true;
for(int d:pt) {
if(i%d==0) {
b = false;
break;
}
}
if(b) {
pt.add(i);
}
}
return pt;
}
public ArrayList<Integer> primetablearray(int m) {
ArrayList<Integer> al = new ArrayList<Integer>();
Queue<Integer> q = new ArrayDeque<Integer>();
for(int i=2;i<=m;i++) {
q.add(i);
}
boolean[] b = new boolean[m+1];
while(!q.isEmpty()) {
int e = q.poll();
if(!b[e]) {
al.add(e);
for(int j=1;e*j<=1000000;j++) {
b[e*j] = true;
}
}
}
return al;
}
public boolean isprime(int e) {
if(e==1) return false;
for(int i=2;i*i<=e;i++) {
if(e%i==0) return false;
}
return true;
}
public MultiSet Factrization(int e) {
MultiSet ret = new MultiSet();
for(int i=2;i*i<=e;i++) {
while(e%i==0) {
ret.add(i);
e /= i;
}
}
if(e!=1)ret.add(e);
return ret;
}
public int[] hipPush(int[] a){
int[] r = new int[a.length];
int[] s = new int[a.length];
for(int i=0;i<a.length;i++) {
s[i] = a[i];
}
Arrays.sort(s);
HashMap<Integer,Integer> m = new HashMap<Integer,Integer>();
for(int i=0;i<a.length;i++) {
m.put(s[i],i);
}
for(int i=0;i<a.length;i++) {
r[i] = m.get(a[i]);
}
return r;
}
public HashMap<Integer,Integer> hipPush(ArrayList<Integer> l){
HashMap<Integer,Integer> r = new HashMap<Integer,Integer>();
TreeSet<Integer> s = new TreeSet<Integer>();
for(int e:l)s.add(e);
int p = 0;
for(int e:s) {
r.put(e,p);
p++;
}
return r;
}
public TreeMap<Integer,Integer> thipPush(ArrayList<Integer> l){
TreeMap<Integer,Integer> r = new TreeMap<Integer,Integer>();
Collections.sort(l);
int b = -(1000000007+9393);
int p = 0;
for(int e:l) {
if(b!=e) {
r.put(e,p);
p++;
}
b=e;
}
return r;
}
int[] count(int[] a) {
int[] c = new int[max(a)+1];
for(int i=0;i<a.length;i++) {
c[a[i]]++;
}
return c;
}
int[] count(int[] a, int m) {
int[] c = new int[m+1];
for(int i=0;i<a.length;i++) {
c[a[i]]++;
}
return c;
}
long max(long[] a){
long M = Long.MIN_VALUE;
for(int i=0;i<a.length;i++){
if(M<=a[i]){
M =a[i];
maxdex = i;
}
}
return M;
}
int max(int[] a){
int M = Integer.MIN_VALUE;
for(int i=0;i<a.length;i++){
if(M<=a[i]){
M =a[i];
maxdex = i;
}
}
return M;
}
long min(long[] a){
long m = Long.MAX_VALUE;
for(int i=0;i<a.length;i++){
if(m>a[i]){
m =a[i];
mindex = i;
}
}
return m;
}
int min(int[] a){
int m = Integer.MAX_VALUE;
for(int i=0;i<a.length;i++){
if(m>a[i]){
m =a[i];
mindex = i;
}
}
return m;
}
long sum(long[] a){
long s = 0;
for(int i=0;i<a.length;i++)s += a[i];
return s;
}
long sum(int[] a){
long s = 0;
for(int i=0;i<a.length;i++)s += a[i];
return s;
}
long sum(ArrayList<Integer> l) {
long s = 0;
for(int e:l)s += e;
return s;
}
long gcd(long a, long b){
a = Math.abs(a);
b = Math.abs(b);
if(a==0)return b;
if(b==0)return a;
if(a%b==0) return b;
else return gcd(b,a%b);
}
int igcd(int a, int b) {
if(a%b==0) return b;
else return igcd(b,a%b);
}
long lcm(long a, long b) {return a / gcd(a,b) * b;}
public long perm(int a,int num) {
if(!isBuild)buildFac();
return fac[a]*(rev(fac[a-num]))%mod;
}
void buildComb(int N) {
comb = new long[N+1][N+1];
comb[0][0] = 1;
for(int i=1;i<=N;i++) {
comb[i][0] = 1;
for(int j=1;j<N;j++) {
comb[i][j] = comb[i-1][j-1]+comb[i-1][j];
if(comb[i][j]>mod)comb[i][j]-=mod;
}
comb[i][i] = 1;
}
}
public long comb(int a,int num){
if(a-num<0)return 0;
if(num<0)return 0;
if(!isBuild)buildFac();
if(a>10000000) return combN(a,num);
return fac[a] * ((revfac[num]*revfac[a-num])%mod)%mod;
}
long combN(int a,int num) {
long ans = 1;
for(int i=0;i<num;i++) {
ans *= a-i;
ans %= mod;
}
return ans * revfac[num] % mod;
}
long mulchoose(int n,int k) {
if(k==0) return 1;
return comb(n+k-1,k);
}
long rev(long l) {return pow(l,mod-2);}
void buildpow(int l,int i) {
pow = new long[i+1];
pow[0] = 1;
for(int j=1;j<=i;j++) {
pow[j] = pow[j-1]*l;
if(pow[j]>mod)pow[j] %= mod;
}
}
void buildrevpow(int l,int i) {
revpow = new long[i+1];
revpow[0] = 1;
for(int j=1;j<=i;j++) {
revpow[j] = revpow[j-1]*l;
if(revpow[j]>mod) revpow[j] %= mod;
}
}
long pow(long l, long i) {
if(i==0)return 1;
else{
if(i%2==0){
long val = pow(l,i/2);
return val * val % mod;
}
else return pow(l,i-1) * l % mod;
}
}
long mon(int i) {
long ans = 0;
for(int k=2;k<=i;k++) {
ans += (k%2==0?1:-1) * revfac[k];
ans += mod;
}
ans %= mod;
ans *= fac[i];
return ans%mod;
}
long dictnum(int[] A) {
int N = A.length;
long ans = 0;
BinaryIndexedTree bit = new BinaryIndexedTree(N+1);
buildFacn(N);
for(int i=1;i<=N;i++) {
bit.add(i,1);
}
for(int i=1;i<=N;i++) {
int a = A[i-1];
ans += bit.sum(a-1) * fac[N-i] % mod;
bit.add(a,-1);
}
return (ans+1)%mod;
}
}
| Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles. The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ u ≤ n), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | 63b8b0d0698dfbc0f628863372311b58 | train_002.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 megabytes | import java.io.BufferedReader;
import java.io.FileNotFoundException;
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 C {
static int [] T, size, l, r;
static int cur;
static int build(int s,int e){
int ret = cur++;
if(s == e) return ret;
int m = (s + e) >> 1;
l[ret] = build(s,m);
r[ret] = build(m+1,e);
return ret;
}
static int update(int root,int s,int e,int p){
int ret = cur++;
size[ret] = size[root];
l[ret] = l[root];
r[ret] = r[root];
size[ret]++;
if(s == e) return ret;
int m = (s + e) >> 1;
if(p <= m) l[ret] = update(l[root],s,m,p);
else r[ret] = update(r[root],m+1,e,p);
return ret;
}
static int query(int u,int v,int s,int e,int p){
if(e <= p) return size[v] - size[u];
int m = (s + e) >> 1;
if(p <= m) return query(l[u],l[v],s,m,p);
else return query(l[u],l[v],s,m,p) + query(r[u],r[v],m+1,e,p);
}
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
n = sc.nextInt();
int q = sc.nextInt();
T = new int[n + 1];
size = new int [200005 * 30];
l = new int [200005 * 30];
r = new int [200005 * 30];
int prev = build(0, 200005);
T[0] = prev;
for (int i = 1; i <= n; i++) {
int x = sc.nextInt();
T[i] = update(T[i - 1], 0, 200005, x);
}
for (int i = 0; i < q; i++) {
int[] arr = find(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt());
long ans = 0;
for (int j = 0; j < 9; j++) {
int r1 = j / 3;
int c1 = j % 3;
for (int k = 0; k < 9; k++) {
int r2 = k / 3;
int c2 = k % 3;
if((r1 <= 1 && r2 >= 1 || r1 >= 1 && r2 <= 1) && (c1 <= 1 && c2 >= 1 || c1 >= 1 && c2 <= 1)) {
if(j == k)
ans += 1l * arr[j] * (arr[k] - 1);
else
ans += 1l * arr[j] * arr[k];
}
}
}
out.println(ans / 2);
}
out.flush();
out.close();
}
static int n;
static int[] find(int y1, int x1, int y2, int x2) {
int[] ans = new int[9];
ans[0] = count(1, 1, x1 - 1, y1 - 1);
ans[1] = count(1, y1, x1 - 1, y2);
ans[2] = count(1, y2 + 1, x1 - 1, n);
ans[3] = count(x1, 1, x2, y1 - 1);
ans[4] = count(x1, y1, x2, y2);
ans[5] = count(x1, y2 + 1, x2, n);
ans[6] = count(x2 + 1, 1, n, y1 - 1);
ans[7] = count(x2 + 1, y1, n, y2);
ans[8] = count(x2 + 1, y2 + 1, n, n);
return ans;
}
static int count(int x1, int y1, int x2, int y2) {
if(x1 > x2 || y1 > y2)
return 0;
int u = y2 - y1 + 1;
if(x2 < n)
u = query(T[y1 - 1], T[y2], 0, 200005, x2);
int d = 0;
if(x1 > 1)
d = query(T[y1 - 1], T[y2], 0, 200005, x1 - 1);
return u - d;
}
static 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 String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(next());
}
public long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
}
}
| Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles. The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ u ≤ n), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | 67ccf6e01c18a2ba27f7fb4774893b06 | train_002.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 megabytes | import java.io.BufferedReader;
import java.io.FileNotFoundException;
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 C {
static int [] T, size, l, r;
static int cur;
static int build(int s,int e){
int ret = cur++;
if(s == e) return ret;
int m = (s + e) >> 1;
l[ret] = build(s,m);
r[ret] = build(m+1,e);
return ret;
}
static int update(int root,int s,int e,int p){
int ret = cur++;
size[ret] = size[root];
l[ret] = l[root];
r[ret] = r[root];
size[ret]++;
if(s == e) return ret;
int m = (s + e) >> 1;
if(p <= m) l[ret] = update(l[root],s,m,p);
else r[ret] = update(r[root],m+1,e,p);
return ret;
}
static int query(int u,int v,int s,int e,int p){
if(e <= p) return size[v] - size[u];
int m = (s + e) >> 1;
if(p <= m) return query(l[u],l[v],s,m,p);
else return query(l[u],l[v],s,m,p) + query(r[u],r[v],m+1,e,p);
}
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
n = sc.nextInt();
int q = sc.nextInt();
T = new int[n + 1];
size = new int [200005 * 100];
l = new int [200005 * 100];
r = new int [200005 * 100];
int prev = build(0, 200005);
T[0] = prev;
for (int i = 1; i <= n; i++) {
int x = sc.nextInt();
T[i] = update(T[i - 1], 0, 200005, x);
}
for (int i = 0; i < q; i++) {
int[] arr = find(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt());
long ans = 0;
for (int j = 0; j < 9; j++) {
int r1 = j / 3;
int c1 = j % 3;
for (int k = 0; k < 9; k++) {
int r2 = k / 3;
int c2 = k % 3;
if((r1 <= 1 && r2 >= 1 || r1 >= 1 && r2 <= 1) && (c1 <= 1 && c2 >= 1 || c1 >= 1 && c2 <= 1)) {
if(j == k)
ans += 1l * arr[j] * (arr[k] - 1);
else
ans += 1l * arr[j] * arr[k];
}
}
}
out.println(ans / 2);
}
out.flush();
out.close();
}
static int n;
static int[] find(int y1, int x1, int y2, int x2) {
int[] ans = new int[9];
ans[0] = count(1, 1, x1 - 1, y1 - 1);
ans[1] = count(1, y1, x1 - 1, y2);
ans[2] = count(1, y2 + 1, x1 - 1, n);
ans[3] = count(x1, 1, x2, y1 - 1);
ans[4] = count(x1, y1, x2, y2);
ans[5] = count(x1, y2 + 1, x2, n);
ans[6] = count(x2 + 1, 1, n, y1 - 1);
ans[7] = count(x2 + 1, y1, n, y2);
ans[8] = count(x2 + 1, y2 + 1, n, n);
return ans;
}
static int count(int x1, int y1, int x2, int y2) {
if(x1 > x2 || y1 > y2)
return 0;
int u = y2 - y1 + 1;
if(x2 < n)
u = query(T[y1 - 1], T[y2], 0, 200005, x2);
int d = 0;
if(x1 > 1)
d = query(T[y1 - 1], T[y2], 0, 200005, x1 - 1);
return u - d;
}
static 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 String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(next());
}
public long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
}
}
| Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles. The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ u ≤ n), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | 1e8cad390b3ece96cc4123682cecb176 | train_002.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 megabytes | import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.util.Map.Entry;
import static java.lang.Math.*;
public class C extends PrintWriter {
int cnt1, cnt2;
class Node {
final int fx, tx;
final int[] y;
final Node l, r;
Node(int x, int y) {
this.fx = this.tx = x;
this.y = new int[] { y };
l = r = null;
}
Node(Node l, Node r) {
this.fx = l.fx;
this.tx = r.tx;
this.l = l;
this.r = r;
int[] ly = l.y;
int[] ry = r.y;
int lp = 0, rp = 0;
int len = ly.length + ry.length;
y = new int[len];
for (int i = 0; i < len; i++) {
if ((rp == ry.length) || (lp < ly.length && ly[lp] < ry[rp])) {
y[i] = ly[lp++];
} else {
y[i] = ry[rp++];
}
}
}
void cnt(int from, int to, int up1, int up2) {
if (to < from || tx < from || to < fx) {
return;
}
if (from <= fx && tx <= to) {
int i1 = Arrays.binarySearch(y, up1);
int i2 = Arrays.binarySearch(y, up2);
if (i1 < 0) {
cnt1 += ~i1;
} else {
cnt1 += i1 + 1;
}
if (i2 < 0) {
cnt2 += ~i2;
} else {
cnt2 += i2 + 1;
}
return;
}
l.cnt(from, to, up1, up2);
r.cnt(from, to, up1, up2);
}
}
void run() {
int n = nextInt(), q = nextInt();
Node[] tree = new Node[n];
for (int i = 0; i < n; i++) {
tree[i] = new Node(i + 1, nextInt());
}
int m = n;
while (m > 1) {
int k = 0;
for (int i = 1; i < m; i += 2) {
tree[k++] = new Node(tree[i - 1], tree[i]);
}
if (m % 2 == 1) {
tree[k++] = tree[m - 1];
}
m = k;
}
Node root = tree[0];
for (int i = 0; i < q; i++) {
int lx = nextInt(), dy = nextInt();
int rx = nextInt(), uy = nextInt();
cnt1 = cnt2 = 0;
root.cnt(1, lx - 1, dy - 1, uy);
long adg = lx - 1;
long dg = cnt2;
long a = adg - dg;
long g = cnt1;
long d = dg - g;
cnt1 = cnt2 = 0;
root.cnt(lx, rx, dy - 1, uy);
long beh = rx - lx + 1;
long eh = cnt2;
long b = beh - eh;
long h = cnt1;
long e = eh - h;
long abc = n - uy;
long c = abc - a - b;
long def = uy - dy + 1;
long f = def - d - e;
long ghk = dy - 1;
long k = ghk - g - h;
long ans = 0;
ans += e * (n - e);
ans += e * (e - 1) / 2;
ans += (a + d) * (f + h + k);
ans += b * (d + f + g + h + k);
ans += (c + f) * (g + h);
ans += c * d;
// println(a + " " + b + " " + c + " " + d + " " + e + " " + f + " " + g + " " + h + " " + k);
println(ans);
}
}
void safeSort(int[] a) {
int n = a.length;
for (int i = 23; i < n; i++) {
int j = rnd.nextInt(i);
a[i] ^= a[j];
a[j] ^= a[i];
a[i] ^= a[j];
}
Arrays.sort(a);
}
boolean skip() {
while (hasNext()) {
next();
}
return true;
}
int[][] nextMatrix(int n, int m) {
int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
matrix[i][j] = nextInt();
return matrix;
}
String next() {
while (!tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(nextLine());
return tokenizer.nextToken();
}
boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String line = nextLine();
if (line == null) {
return false;
}
tokenizer = new StringTokenizer(line);
}
return true;
}
int[] nextArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = nextInt();
}
return array;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
try {
return reader.readLine();
} catch (IOException err) {
return null;
}
}
public C(OutputStream outputStream) {
super(outputStream);
}
static BufferedReader reader;
static StringTokenizer tokenizer = new StringTokenizer("");
static Random rnd = new Random();
static boolean OJ;
public static void main(String[] args) throws IOException {
OJ = System.getProperty("ONLINE_JUDGE") != null;
C solution = new C(System.out);
if (OJ) {
reader = new BufferedReader(new InputStreamReader(System.in));
solution.run();
} else {
reader = new BufferedReader(new FileReader(new File(C.class.getName() + ".txt")));
long timeout = System.currentTimeMillis();
while (solution.hasNext()) {
solution.run();
solution.println();
solution.println("----------------------------------");
}
solution.println("time: " + (System.currentTimeMillis() - timeout));
}
solution.close();
reader.close();
}
} | Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles. The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ u ≤ n), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | 3d4392b8d07327ed0e07cb87f096225b | train_002.jsonl | 1569762300 | You have a simple undirected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected.Let's make a definition.Let $$$v_1$$$ and $$$v_2$$$ be two some nonempty subsets of vertices that do not intersect. Let $$$f(v_{1}, v_{2})$$$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $$$v_1$$$. There are no edges with both endpoints in vertex set $$$v_2$$$. For every two vertices $$$x$$$ and $$$y$$$ such that $$$x$$$ is in $$$v_1$$$ and $$$y$$$ is in $$$v_2$$$, there is an edge between $$$x$$$ and $$$y$$$. Create three vertex sets ($$$v_{1}$$$, $$$v_{2}$$$, $$$v_{3}$$$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $$$f(v_{1}, v_{2})$$$, $$$f(v_{2}, v_{3})$$$, $$$f(v_{3}, v_{1})$$$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. | 256 megabytes | import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;
public class D {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringTokenizer st = new StringTokenizer(sc.nextLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
ArrayList<Integer>[] adj = new ArrayList[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<Integer>();
}
for (int i = 0; i < m; i++) {
st = new StringTokenizer(sc.nextLine());
int a = Integer.parseInt(st.nextToken()) - 1;
int b = Integer.parseInt(st.nextToken()) - 1;
adj[a].add(b);
adj[b].add(a);
}
// System.out.println(Arrays.toString(adj));
boolean works = true;
int[] col = new int[n]; // 0 = indeterminate, 1 = red, 2 =blue, 3 = green
int[] depth = new int[n];
Arrays.fill(depth, -1);
ArrayList<Integer> bi = new ArrayList<Integer>();
for(int i =0; i < n; i++) {
if(depth[i] == -1) {
col[i] = 1;
depth[i] = 0;
Queue<Integer> q = new LinkedList<Integer>();
q.add(i);
W: while (!q.isEmpty()) {
int v = q.poll();
if (depth[v] > 2) {
works = false;
break;
}
if (depth[v] == 0) {
for (int w : adj[v]) {
depth[w] = depth[v] + 1;
q.add(w);
}
} else if (depth[v] == 1) {
bi.add(v);
for (int w : adj[v]) {
if (col[w] == 0 && depth[w] != 1) {
depth[w] = depth[v] + 1;
col[w] = col[i];
q.add(w);
}
}
} else {
for (int w : adj[v]) {
if (col[w] == 0 && depth[w] != 1) {
works = false;
break W;
}
}
}
}
}
}
//System.out.println(Arrays.toString(col));
if (!works) {
System.out.println(-1);
} else {
//System.out.println(Arrays.toString(col));
for(int i : bi) {
if(col[i] == 0) {
Queue<Integer> q = new LinkedList<Integer>();
q.add(i);
col[i] = 2;
W2: while (!q.isEmpty()) {
int v = q.poll();
for (int w : adj[v]) {
if (depth[w] == 1) {
if (col[w] == 0) {
col[w] = 5 - col[v];
q.add(w);
} else if (col[w] == col[v]) {
works = false;
break W2;
}
}
}
}
}
}
//System.out.println(Arrays.toString(col));
if(!works) {
System.out.println(-1);
} else {
int[] num = new int[4];
for(int i =0; i <n; i++) {
num[col[i]] ++;
}
if(num[0] > 0 || num[1] == 0 || num[2] == 0 || num[3] == 0) {
works = false;
}
for(int i =0; i <n; i++) {
if(col[i] == 1) {
int ad = num[2]+num[3];
if(adj[i].size() != ad) {
works = false;
break;
}
}
if(col[i] == 2) {
int ad = num[1]+num[3];
if(adj[i].size() != ad) {
works = false;
break;
}
}
if(col[i] == 3) {
int ad = num[2]+num[1];
if(adj[i].size() != ad) {
works = false;
break;
}
}
}
//System.out.println("still working " + works);
if(!works) {
System.out.println("-1");
} else {
for(int i =0; i < n; i++) {
System.out.print(col[i] + " ");
}
System.out.println();
}
}
}
sc.close();
}
}
| Java | ["6 11\n1 2\n1 3\n1 4\n1 5\n1 6\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6", "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["1 2 2 3 3 3", "-1"] | NoteIn the first example, if $$$v_{1} = \{ 1 \}$$$, $$$v_{2} = \{ 2, 3 \}$$$, and $$$v_{3} = \{ 4, 5, 6 \}$$$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. In the second example, it's impossible to make such vertex sets. | Java 8 | standard input | [
"hashing",
"graphs",
"constructive algorithms",
"implementation",
"brute force"
] | 7dea96a7599946a5b5d0b389c7e76651 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$3 \le n \le 10^{5}$$$, $$$0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$$$) — the number of vertices and edges in the graph. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$a_{i}$$$ and $$$b_{i}$$$ ($$$1 \le a_{i} \lt b_{i} \le n$$$) — it means there is an edge between $$$a_{i}$$$ and $$$b_{i}$$$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. | 1,900 | If the answer exists, print $$$n$$$ integers. $$$i$$$-th integer means the vertex set number (from $$$1$$$ to $$$3$$$) of $$$i$$$-th vertex. Otherwise, print $$$-1$$$. If there are multiple answers, print any. | standard output | |
PASSED | 4f54d72a4473c5be045a664d14e3d685 | train_002.jsonl | 1569762300 | You have a simple undirected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected.Let's make a definition.Let $$$v_1$$$ and $$$v_2$$$ be two some nonempty subsets of vertices that do not intersect. Let $$$f(v_{1}, v_{2})$$$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $$$v_1$$$. There are no edges with both endpoints in vertex set $$$v_2$$$. For every two vertices $$$x$$$ and $$$y$$$ such that $$$x$$$ is in $$$v_1$$$ and $$$y$$$ is in $$$v_2$$$, there is an edge between $$$x$$$ and $$$y$$$. Create three vertex sets ($$$v_{1}$$$, $$$v_{2}$$$, $$$v_{3}$$$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $$$f(v_{1}, v_{2})$$$, $$$f(v_{2}, v_{3})$$$, $$$f(v_{3}, v_{1})$$$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.math.*;
import java.util.*;
import java.util.Map.Entry;
public class Main {
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;
static int BIT[];
public final static int INF = (int) 1E9;
static ArrayList<Integer> adj[];
static boolean Visited[];
static HashSet<Integer> exc;
static long oddsum[] = new long[1000001];
static long co = 0, ans = 0;
static int parent[];
static int size[], color[], res[], k;
static ArrayList<Integer> al[];
static long MOD = (long) 1e9 + 7;
static int[] levl, dist;
static int[] eat;
static int nm = 1000007;
static boolean divisor[];
static int[] phi;
static int mod = (int) 1e9 + 7;
static HashSet<Integer> div;
static double maxvector = 1.5 * (1e6);
static HashSet<Integer> neigh[];
static int subset[];
public static void solve() {
int n = nextInt();
int m = nextInt();
neigh = new HashSet[n+1];
subset = new int[n+1];
Arrays.fill(subset, -1);
for (int i=0; i< n+1; i++) {
neigh[i] = new HashSet<Integer>();
}
for (int i=0; i<m; i++) {
int a = nextInt();
int b = nextInt();
neigh[a].add(b);
neigh[b].add(a);
}
for (int group=0; group < 3; group ++) {
int group_member = -1;
for(int i=1;i<=n; i++) {
if (subset[i] == -1){
group_member = i;
subset[group_member] = group;
break;
}
}
if (group_member == -1) {
pw.println("-1");
return;
}
for(int i=1;i<=n; i++) {
if (i!=group_member && !neigh[group_member].contains(i) && subset[i]== -1) {
subset[i] = group;
}
}
}
ArrayList<Integer> tukda[] = new ArrayList[3];
for (int i=0;i<3;i++) {
tukda[i] = new ArrayList<>();
}
for (int i=1; i<=n; i++) {
if (subset[i] == -1)
{pw.println("-1");
return;}
else
tukda[subset[i]].add(i);
}
int total_edges = 0;
for (int i=0;i<3;i++)
for(int j=i+1;j<3;j++)
{
for(int k: tukda[i])
for(int l: tukda[j])
{
if(!neigh[k].contains(l))
{pw.print("-1");return;}
else
total_edges++;
}
}
if ( m != total_edges)
{pw.print("-1");return;}
else {
for(int i=1;i<=n;i++)
pw.print(subset[i]+1 + " ");
}
}
public static HashSet<Integer> get_factors(int x) {
HashSet<Integer> a = new HashSet<>();
while( x%2== 0) {
a.add(2);
x = x/2;
}
for(int i=3; i*i<=x;i+=2) {
while( x % i == 0) {
x = x / i;
a.add(i);
}
}
if (x > 2) {
a.add(x);
}
return a;
}
public static void main(String args[]) {
InputReader(System.in);
pw = new PrintWriter(System.out);
new Thread(null, new Runnable() {
public void run() {
try {
solve();
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}, "1", 1 << 26).start();
}
static StringBuilder sb;
public static void test() {
sb = new StringBuilder();
int t = nextInt();
while (t-- > 0) {
solve();
}
pw.println(sb);
}
public static long pow(long n, long p, long mod) {
if (p == 0)
return 1;
if (p == 1)
return n % mod;
if (p % 2 == 0) {
long temp = pow(n, p / 2, mod);
return (temp * temp) % mod;
} else {
long temp = pow(n, p / 2, mod);
temp = (temp * temp) % mod;
return (temp * n) % mod;
}
}
public static long pow(long n, long p) {
if (p == 0)
return 1;
if (p == 1)
return n;
if (p % 2 == 0) {
long temp = pow(n, p / 2);
return (temp * temp);
} else {
long temp = pow(n, p / 2);
temp = (temp * temp);
return (temp * n);
}
}
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
public static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
private static void buildgraph(int n) {
adj = new ArrayList[n + 1];
Visited = new boolean[n + 1];
levl = new int[n + 1];
for (int i = 0; i <= n; i++) {
adj[i] = new ArrayList<Integer>();
}
}
static void sprime() {
spf = new int[nm + 1];
for (int i = 2; i < nm; i++) {
if (spf[i] != 0)
continue;
for (int j = i; j < nm; j += i) {
spf[j] = i;
}
}
}
static void toposort(int i) {
Visited[i] = true;
for (int j : adj[i]) {
if (!Visited[j])
toposort(j);
}
}
static void comp(int n) {
for (int i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
if (i == n / i)
div.add(i);
else {
div.add(i);
div.add(n / i);
}
}
}
}
static void calculatephi() {
int end = (int) 1e6 + 5;
for (int i = 1; i < end; i++)
phi[i] = i;
for (int i = 2; i < end; i++) {
if (phi[i] == i) {
phi[i] = i - 1;
for (int j = 2 * i; j < end; j = j + i) {
phi[j] = (phi[j] / i) * (i - 1);
}
}
}
}
static void randomize(long arr[], int n) {
Random r = new Random();
for (int i = n - 1; i > 0; i--) {
int j = r.nextInt(i);
long temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
static class pa implements Comparator<Integer> {
public int compare(Integer a, Integer b) {
return b - a;
}
}
static class node implements Comparable<node> {
long r;
long i;
node(long r, long i) {
this.r = r;
this.i = i;
}
public int compareTo(node other) {
return Long.compare(this.r, other.r);
}
}
/*
* static void BITupdate(int x,int val) { while(x<=n) { BIT[x]+=val; x+= x & -x;
* } }
*/
/*
* static void update(int x,long val) {
*
* val=val%MOD; while(x<=n) { // System.out.println(x); BIT[x]=(BIT[x]+val)%MOD;
* x+=(x & -x); } // System.out.println("dfd"); }
*/
static int BITsum(int x) {
int sum = 0;
while (x > 0) {
sum += BIT[x];
x -= (x & -x);
}
return sum;
}
/*
* static long sum(int x) { long sum=0; while(x>0) { sum=(sum+BIT[x])%MOD; x-=x
* & -x; } return sum; }
*/
static boolean union(int x, int y) {
int xr = find(x);
int yr = find(y);
if (xr == yr)
return false;
if (size[xr] < size[yr]) {
size[yr] += size[xr];
parent[xr] = yr;
} else {
size[xr] += size[yr];
parent[yr] = xr;
}
return true;
}
static int find(int x) {
if (parent[x] == x)
return x;
else {
parent[x] = find(parent[x]);
return parent[x];
}
}
public static class Edge implements Comparable<Edge> {
long x;
int i;
public Edge(long x, int i) {
this.x = x;
this.i = i;
// this.s = s;
}
public int hashCode() {
return Objects.hash();
}
public int compareTo(Edge other) {
return Long.compare(this.x, other.x);
}
}
static int col[];
static int no_vert = 0;
public static String reverseString(String s) {
StringBuilder sb = new StringBuilder(s);
sb.reverse();
return (sb.toString());
}
static int indeg[];
/*
* private static void kahn(int n){
*
* PriorityQueue<Integer> q=new PriorityQueue<Integer>(); for(int i=1;i<=n;i++){
* if(indeg[i]==0){ q.add(i); } } while(!q.isEmpty()){ int top=q.poll();
* st.push(top); for(Node i:adj[top]){ indeg[i.to]--; if(indeg[i.to]==0){
* q.add(i.to); } } } }
*
* static int state=1; static long no_exc=0,no_vert=0; static Stack<Integer> st;
* static HashSet<Integer> inset; private static void topo(int curr){
*
* Visited[curr]=true; inset.add(curr); for(int x:adj[curr]){
* if(adj[x].contains(curr) || inset.contains(x)){ state=0; return; }
* if(state==0) return;
*
* } st.push(curr);
*
* inset.remove(curr); }
*/
static HashSet<Integer> hs;
static boolean prime[];
static int spf[];
public static void sieve(int n) {
prime = new boolean[n + 1];
spf = new int[n + 1];
Arrays.fill(spf, 1);
Arrays.fill(prime, true);
prime[1] = false;
spf[2] = 2;
for (int i = 2; i * i <= n; i++) {
if (prime[i]) {
spf[i] = i;
for (int j = 2 * i; j <= n; j += i) {
prime[j] = false;
}
}
}
}
// 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[][] next2dArray(int n, int m) {
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;
}
private static char[][] nextCharArray(int n, int m) {
char[][] c = new char[n][m];
for (int i = 0; i < n; i++) {
String s = nextLine();
for (int j = 0; j < s.length(); j++) {
c[i][j] = s.charAt(j);
}
}
return c;
}
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++) {
pw.print(arr[i] + " ");
}
pw.println();
return;
}
private static void pArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
pw.print(arr[i] + " ");
}
pw.println();
return;
}
private static void pArray(boolean[] arr) {
for (int i = 0; i < arr.length; i++) {
pw.print(arr[i] + " ");
}
pw.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 | ["6 11\n1 2\n1 3\n1 4\n1 5\n1 6\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6", "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["1 2 2 3 3 3", "-1"] | NoteIn the first example, if $$$v_{1} = \{ 1 \}$$$, $$$v_{2} = \{ 2, 3 \}$$$, and $$$v_{3} = \{ 4, 5, 6 \}$$$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. In the second example, it's impossible to make such vertex sets. | Java 8 | standard input | [
"hashing",
"graphs",
"constructive algorithms",
"implementation",
"brute force"
] | 7dea96a7599946a5b5d0b389c7e76651 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$3 \le n \le 10^{5}$$$, $$$0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$$$) — the number of vertices and edges in the graph. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$a_{i}$$$ and $$$b_{i}$$$ ($$$1 \le a_{i} \lt b_{i} \le n$$$) — it means there is an edge between $$$a_{i}$$$ and $$$b_{i}$$$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. | 1,900 | If the answer exists, print $$$n$$$ integers. $$$i$$$-th integer means the vertex set number (from $$$1$$$ to $$$3$$$) of $$$i$$$-th vertex. Otherwise, print $$$-1$$$. If there are multiple answers, print any. | standard output | |
PASSED | 6d84f0013355dfadf18b69ac8e3b04e6 | train_002.jsonl | 1569762300 | You have a simple undirected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected.Let's make a definition.Let $$$v_1$$$ and $$$v_2$$$ be two some nonempty subsets of vertices that do not intersect. Let $$$f(v_{1}, v_{2})$$$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $$$v_1$$$. There are no edges with both endpoints in vertex set $$$v_2$$$. For every two vertices $$$x$$$ and $$$y$$$ such that $$$x$$$ is in $$$v_1$$$ and $$$y$$$ is in $$$v_2$$$, there is an edge between $$$x$$$ and $$$y$$$. Create three vertex sets ($$$v_{1}$$$, $$$v_{2}$$$, $$$v_{3}$$$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $$$f(v_{1}, v_{2})$$$, $$$f(v_{2}, v_{3})$$$, $$$f(v_{3}, v_{1})$$$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. | 256 megabytes |
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int m=s.nextInt();
HashSet<Integer> g[]=new HashSet[n+1];
pair arr[]=new pair[m];
for(int i=0;i<g.length;i++) {
g[i]=new HashSet<Integer>();
}
for(int i=0;i<m;i++) {
int a=s.nextInt();
int b=s.nextInt();
pair p=new pair(a,b);
arr[i]=p;
g[a].add(b);
g[b].add(a);
}
HashSet<Integer> f1=new HashSet<Integer>();
HashSet<Integer> f2=new HashSet<Integer>();
HashSet<Integer> f3=new HashSet<Integer>();
for(int i=1;i<=n;i++) {
if(!g[1].contains(i)) {
f1.add(i);
}
}
for(int i=1;i<=n;i++) {
if(!f1.contains(i)) {
for(int j=1;j<=n;j++) {
if(!g[i].contains(j)) {
f2.add(j);
}
}
break;
}
}
for(int i=1;i<=n;i++) {
if(!f1.contains(i)&&(!f2.contains(i))) {
for(int j=1;j<=n;j++) {
if(!g[i].contains(j)) {
f3.add(j);
}
}
break;
}
}
// System.out.println(f1);
// System.out.println(f2);
// System.out.println(f3);
if(f1.size()==0||f2.size()==0||f3.size()==0) {
System.out.print(-1);
return;
}
if(f1.size()+f2.size()+f3.size()!=n) {
System.out.print(-1);
return;
}
if(f1.size()*f2.size()+f2.size()*f3.size()+f3.size()*f1.size()!=m) {
System.out.print(-1);
return;
}
for(int i=0;i<m;i++) {
int a=arr[i].a;
int b=arr[i].b;
if(f1.contains(a)) {
if((!f2.contains(b)&&(!f3.contains(b)))||(f1.contains(b))) {
System.out.print(-1);
return;
}
}
else if(f2.contains(a)) {
if((!f1.contains(b)&&(!f3.contains(b)))||(f2.contains(b))) {
System.out.print(-1);
return;
}
}
else if(f3.contains(a)) {
if((!f1.contains(b)&&(!f2.contains(b)))||(f3.contains(b))) {
System.out.print(-1);
return;
}
}
}
for(int i=1;i<=n;i++) {
if(f1.contains(i)) {
System.out.print(1+" ");
}
else if (f2.contains(i)) {
System.out.print(2+" ");
}
else {
System.out.print(3+" ");
}
}
}
}
class pair{
public int a;
public int b;
public pair(int a,int b){
this.a=a;
this.b=b;
}
} | Java | ["6 11\n1 2\n1 3\n1 4\n1 5\n1 6\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6", "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["1 2 2 3 3 3", "-1"] | NoteIn the first example, if $$$v_{1} = \{ 1 \}$$$, $$$v_{2} = \{ 2, 3 \}$$$, and $$$v_{3} = \{ 4, 5, 6 \}$$$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. In the second example, it's impossible to make such vertex sets. | Java 8 | standard input | [
"hashing",
"graphs",
"constructive algorithms",
"implementation",
"brute force"
] | 7dea96a7599946a5b5d0b389c7e76651 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$3 \le n \le 10^{5}$$$, $$$0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$$$) — the number of vertices and edges in the graph. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$a_{i}$$$ and $$$b_{i}$$$ ($$$1 \le a_{i} \lt b_{i} \le n$$$) — it means there is an edge between $$$a_{i}$$$ and $$$b_{i}$$$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. | 1,900 | If the answer exists, print $$$n$$$ integers. $$$i$$$-th integer means the vertex set number (from $$$1$$$ to $$$3$$$) of $$$i$$$-th vertex. Otherwise, print $$$-1$$$. If there are multiple answers, print any. | standard output | |
PASSED | db2165de50f8203bfea36a1b80aad8fc | train_002.jsonl | 1569762300 | You have a simple undirected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected.Let's make a definition.Let $$$v_1$$$ and $$$v_2$$$ be two some nonempty subsets of vertices that do not intersect. Let $$$f(v_{1}, v_{2})$$$ be true if and only if all the conditions are satisfied: There are no edges with both endpoints in vertex set $$$v_1$$$. There are no edges with both endpoints in vertex set $$$v_2$$$. For every two vertices $$$x$$$ and $$$y$$$ such that $$$x$$$ is in $$$v_1$$$ and $$$y$$$ is in $$$v_2$$$, there is an edge between $$$x$$$ and $$$y$$$. Create three vertex sets ($$$v_{1}$$$, $$$v_{2}$$$, $$$v_{3}$$$) which satisfy the conditions below; All vertex sets should not be empty. Each vertex should be assigned to only one vertex set. $$$f(v_{1}, v_{2})$$$, $$$f(v_{2}, v_{3})$$$, $$$f(v_{3}, v_{1})$$$ are all true. Is it possible to create such three vertex sets? If it's possible, print matching vertex set for each vertex. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class D {
private static class Node {
ArrayList<Integer> e = new ArrayList<Integer>();
}
public static void main(String[] args) throws Exception {
// BufferedReader br = new BufferedReader(new FileReader("F:/books/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s1 = br.readLine().split(" ");
Integer n = Integer.parseInt(s1[0]);
Integer m = Integer.parseInt(s1[1]);
Node[] G = new Node[n+1];
for(int i=0;i<=n;i++) G[i] = new Node();
for(int i=0;i<m;i++) {
String[] s2 = br.readLine().split(" ");
Integer u = Integer.parseInt(s2[0]);
Integer v = Integer.parseInt(s2[1]);
G[u].e.add(v);
G[v].e.add(u);
}
int[] arr = new int[n+1];
int cnt = 1;
for(int i=1;i<=n && cnt<=3;i++)
if(arr[i]==0)
fill(arr,G,cnt++,i);
int a=0,b=0,c=0;
for(int i=1;i<=n;i++) {
if(arr[i]==0) exit();
if(arr[i]==1) a++;
if(arr[i]==2) b++;
if(arr[i]==3) c++;
}
if(a==0 || b==0 || c==0) exit();
int[] mp = {0,a,b,c};
for(int i=1;i<=n;i++) {
if(G[i].e.size()!=(n-mp[arr[i]]))
exit();
for(int node : G[i].e) {
if(arr[node]==arr[i]) exit();
}
}
StringBuilder sb = new StringBuilder();
for(int i=1;i<=n;i++) sb.append(arr[i]+" ");
System.out.println(sb);
}
private static void fill(int[] arr, Node[] G, int s,int n) {
boolean[] tmp = new boolean[arr.length];
for(int node : G[n].e) tmp[node]=true;
for(int i=1;i<arr.length;i++) if(!tmp[i]) if(arr[i]==0) arr[i]=s;else exit();
}
private static void exit() {
System.out.println("-1");
System.exit(0);
}
}
| Java | ["6 11\n1 2\n1 3\n1 4\n1 5\n1 6\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6", "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4"] | 2 seconds | ["1 2 2 3 3 3", "-1"] | NoteIn the first example, if $$$v_{1} = \{ 1 \}$$$, $$$v_{2} = \{ 2, 3 \}$$$, and $$$v_{3} = \{ 4, 5, 6 \}$$$ then vertex sets will satisfy all conditions. But you can assign vertices to vertex sets in a different way; Other answers like "2 3 3 1 1 1" will be accepted as well. In the second example, it's impossible to make such vertex sets. | Java 8 | standard input | [
"hashing",
"graphs",
"constructive algorithms",
"implementation",
"brute force"
] | 7dea96a7599946a5b5d0b389c7e76651 | The first line contains two integers $$$n$$$ and $$$m$$$ ($$$3 \le n \le 10^{5}$$$, $$$0 \le m \le \text{min}(3 \cdot 10^{5}, \frac{n(n-1)}{2})$$$) — the number of vertices and edges in the graph. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$a_{i}$$$ and $$$b_{i}$$$ ($$$1 \le a_{i} \lt b_{i} \le n$$$) — it means there is an edge between $$$a_{i}$$$ and $$$b_{i}$$$. The graph doesn't contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected. | 1,900 | If the answer exists, print $$$n$$$ integers. $$$i$$$-th integer means the vertex set number (from $$$1$$$ to $$$3$$$) of $$$i$$$-th vertex. Otherwise, print $$$-1$$$. If there are multiple answers, print any. | standard output | |
PASSED | f42d02f46fc22ad0c9d0f375a4eb4115 | train_002.jsonl | 1548516900 | You are playing a new famous fighting game: Kortal Mombat XII. You have to perform a brutality on your opponent's character.You are playing the game on the new generation console so your gamepad have $$$26$$$ buttons. Each button has a single lowercase Latin letter from 'a' to 'z' written on it. All the letters on buttons are pairwise distinct.You are given a sequence of hits, the $$$i$$$-th hit deals $$$a_i$$$ units of damage to the opponent's character. To perform the $$$i$$$-th hit you have to press the button $$$s_i$$$ on your gamepad. Hits are numbered from $$$1$$$ to $$$n$$$.You know that if you press some button more than $$$k$$$ times in a row then it'll break. You cherish your gamepad and don't want to break any of its buttons.To perform a brutality you have to land some of the hits of the given sequence. You are allowed to skip any of them, however changing the initial order of the sequence is prohibited. The total damage dealt is the sum of $$$a_i$$$ over all $$$i$$$ for the hits which weren't skipped.Note that if you skip the hit then the counter of consecutive presses the button won't reset.Your task is to skip some hits to deal the maximum possible total damage to the opponent's character and not break your gamepad buttons. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
FastReader scan = new FastReader();
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("taming.out")));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
Task solver = new Task();
//int t = scan.nextInt();
int t = 1;
for(int i = 1; i <= t; i++) solver.solve(i, scan, out);
out.close();
}
static class Task {
public void solve(int testNumber, FastReader sc, PrintWriter pw) {
int n = sc.nextInt();
int m = sc.nextInt();
Integer[] arr= new Integer[n];
long sum =0;
for(int i =0;i<n;i++){
arr[i]=sc.nextInt();
}
String str = sc.nextLine();
int ctr = 0;
int lch = '0';
for(int i =0;i<n;i++){
if(str.charAt(i)!=lch){
lch = str.charAt(i);
Arrays.sort(arr,i-ctr,i);
ctr=1;
}
else ctr++;
}
if(ctr!=1) {
Arrays.sort(arr, n - ctr, n);
}
int[] backsu = new int[n];
backsu[n-1]=1;
for (int i=n-2;i>=0;i--){
if(str.charAt(i)==str.charAt((i+1))){
backsu[i]=backsu[i+1]+1;
}
else{
backsu[i]=1;
}
}
for(int i=0;i<n;i++){
if(backsu[i]<=m){
sum+=arr[i];
}
}
//pw.println(Arrays.toString(arr));
pw.println(sum);
}
}
static class tup implements Comparable<tup> {
int a, b;
tup() {
}
;
tup(int a, int b) {
this.a=a;
this.b=b;
}
@Override
public int compareTo(tup o2) {
return Double.compare((double)o2.a/o2.b,(double)a/b);
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(int[] a,int l, int rb) {
Random get = new Random();
for (int i = l; i < rb; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["7 3\n1 5 16 18 7 2 10\nbaaaaca", "5 5\n2 4 1 3 1000\naaaaa", "5 4\n2 4 1 3 1000\naaaaa", "8 1\n10 15 2 1 4 8 15 16\nqqwweerr", "6 3\n14 18 9 19 2 15\ncccccc", "2 1\n10 10\nqq"] | 1 second | ["54", "1010", "1009", "41", "52", "10"] | NoteIn the first example you can choose hits with numbers $$$[1, 3, 4, 5, 6, 7]$$$ with the total damage $$$1 + 16 + 18 + 7 + 2 + 10 = 54$$$.In the second example you can choose all hits so the total damage is $$$2 + 4 + 1 + 3 + 1000 = 1010$$$.In the third example you can choose all hits expect the third one so the total damage is $$$2 + 4 + 3 + 1000 = 1009$$$.In the fourth example you can choose hits with numbers $$$[2, 3, 6, 8]$$$. Only this way you can reach the maximum total damage $$$15 + 2 + 8 + 16 = 41$$$.In the fifth example you can choose only hits with numbers $$$[2, 4, 6]$$$ with the total damage $$$18 + 19 + 15 = 52$$$.In the sixth example you can change either first hit or the second hit (it does not matter) with the total damage $$$10$$$. | Java 11 | standard input | [
"two pointers",
"sortings",
"greedy"
] | aa3b5895046ed34e89d5fcc3264b3944 | The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$) — the number of hits and the maximum number of times you can push the same button in a row. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the damage of the $$$i$$$-th hit. The third line of the input contains the string $$$s$$$ consisting of exactly $$$n$$$ lowercase Latin letters — the sequence of hits (each character is the letter on the button you need to press to perform the corresponding hit). | 1,300 | Print one integer $$$dmg$$$ — the maximum possible damage to the opponent's character you can deal without breaking your gamepad buttons. | standard output | |
PASSED | 2a95d5437d8c5987a1081437a7dd0855 | train_002.jsonl | 1548516900 | You are playing a new famous fighting game: Kortal Mombat XII. You have to perform a brutality on your opponent's character.You are playing the game on the new generation console so your gamepad have $$$26$$$ buttons. Each button has a single lowercase Latin letter from 'a' to 'z' written on it. All the letters on buttons are pairwise distinct.You are given a sequence of hits, the $$$i$$$-th hit deals $$$a_i$$$ units of damage to the opponent's character. To perform the $$$i$$$-th hit you have to press the button $$$s_i$$$ on your gamepad. Hits are numbered from $$$1$$$ to $$$n$$$.You know that if you press some button more than $$$k$$$ times in a row then it'll break. You cherish your gamepad and don't want to break any of its buttons.To perform a brutality you have to land some of the hits of the given sequence. You are allowed to skip any of them, however changing the initial order of the sequence is prohibited. The total damage dealt is the sum of $$$a_i$$$ over all $$$i$$$ for the hits which weren't skipped.Note that if you skip the hit then the counter of consecutive presses the button won't reset.Your task is to skip some hits to deal the maximum possible total damage to the opponent's character and not break your gamepad buttons. | 256 megabytes |
import java.nio.charset.IllegalCharsetNameException;
import java.rmi.MarshalException;
import java.util.*;
import java.io.*;
import java.math.*;
public class program {
public static FastIO file = new FastIO();
private static void solve() {
// int tt = nextInt();
int tt = 1;
long start = 0;
while (tt-- > 0) {
int n = nextInt(), k = nextInt();
ArrayList<Long> kk = new ArrayList<>();
for (int i = 0; i < n; i++) {
kk.add(nextLong());
}
String s = nextLine();
ArrayList<Long> a = new ArrayList<>();
BigInteger dmg = new BigInteger("0");
BigInteger b;
char c[] = s.toCharArray();
// if (n == 1) {
// System.out.println(s);
// } else {
// if (c[0] == c[1]) {
// a.add(kk.get(0));
// }else{
// dmg += kk.get(0);
// }
// }
for (int i = 0; i < n; i++) {
if (i + 1 < n && c[i] == c[i + 1]) {
a.add(kk.get(i));
continue;
}
a.add(kk.get(i));
Collections.sort(a);
for (int j = 0; j < k; j++) {
if (j == a.size()) break;
b = new BigInteger(String.valueOf(a.get(a.size()-1-j)));
dmg = dmg.add(b);
}
// System.out.println(a.toString());
a.clear();
}
System.out.println(dmg);
}
}
private static int highest(int n) {
for (int i = n - 1; i >= 2; i--) {
if (n % i == 0) return i;
}
return 1;
}
private static boolean func(int n) {
for (int i = 2; i < n; i++) {
if (n % 2 == 0) return true;
}
return false;
}
private static int[] nextArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
public static int[][] nextArray2D(int n, int m) {
int[][] result = new int[n][];
for (int i = 0; i < n; i++) {
result[i] = nextArray(m);
}
return result;
}
public static long pow(long n, long p) {
long ret = 1L;
while (p > 0) {
if (p % 2 != 0L)
ret *= n;
n *= n;
p >>= 1L;
}
return ret;
}
public static String next() {
return file.next();
}
public static int nextInt() {
return file.nextInt();
}
public static long nextLong() {
return file.nextLong();
}
public static double nextDouble() {
return file.nextDouble();
}
public static String nextLine() {
return file.nextLine();
}
public static class FastIO {
BufferedReader br;
StringTokenizer st;
public FastIO() {
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) {
solve();
}
} | Java | ["7 3\n1 5 16 18 7 2 10\nbaaaaca", "5 5\n2 4 1 3 1000\naaaaa", "5 4\n2 4 1 3 1000\naaaaa", "8 1\n10 15 2 1 4 8 15 16\nqqwweerr", "6 3\n14 18 9 19 2 15\ncccccc", "2 1\n10 10\nqq"] | 1 second | ["54", "1010", "1009", "41", "52", "10"] | NoteIn the first example you can choose hits with numbers $$$[1, 3, 4, 5, 6, 7]$$$ with the total damage $$$1 + 16 + 18 + 7 + 2 + 10 = 54$$$.In the second example you can choose all hits so the total damage is $$$2 + 4 + 1 + 3 + 1000 = 1010$$$.In the third example you can choose all hits expect the third one so the total damage is $$$2 + 4 + 3 + 1000 = 1009$$$.In the fourth example you can choose hits with numbers $$$[2, 3, 6, 8]$$$. Only this way you can reach the maximum total damage $$$15 + 2 + 8 + 16 = 41$$$.In the fifth example you can choose only hits with numbers $$$[2, 4, 6]$$$ with the total damage $$$18 + 19 + 15 = 52$$$.In the sixth example you can change either first hit or the second hit (it does not matter) with the total damage $$$10$$$. | Java 11 | standard input | [
"two pointers",
"sortings",
"greedy"
] | aa3b5895046ed34e89d5fcc3264b3944 | The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$) — the number of hits and the maximum number of times you can push the same button in a row. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the damage of the $$$i$$$-th hit. The third line of the input contains the string $$$s$$$ consisting of exactly $$$n$$$ lowercase Latin letters — the sequence of hits (each character is the letter on the button you need to press to perform the corresponding hit). | 1,300 | Print one integer $$$dmg$$$ — the maximum possible damage to the opponent's character you can deal without breaking your gamepad buttons. | standard output | |
PASSED | c07e05814516731563d645ac21c247b9 | train_002.jsonl | 1548516900 | You are playing a new famous fighting game: Kortal Mombat XII. You have to perform a brutality on your opponent's character.You are playing the game on the new generation console so your gamepad have $$$26$$$ buttons. Each button has a single lowercase Latin letter from 'a' to 'z' written on it. All the letters on buttons are pairwise distinct.You are given a sequence of hits, the $$$i$$$-th hit deals $$$a_i$$$ units of damage to the opponent's character. To perform the $$$i$$$-th hit you have to press the button $$$s_i$$$ on your gamepad. Hits are numbered from $$$1$$$ to $$$n$$$.You know that if you press some button more than $$$k$$$ times in a row then it'll break. You cherish your gamepad and don't want to break any of its buttons.To perform a brutality you have to land some of the hits of the given sequence. You are allowed to skip any of them, however changing the initial order of the sequence is prohibited. The total damage dealt is the sum of $$$a_i$$$ over all $$$i$$$ for the hits which weren't skipped.Note that if you skip the hit then the counter of consecutive presses the button won't reset.Your task is to skip some hits to deal the maximum possible total damage to the opponent's character and not break your gamepad buttons. | 256 megabytes | import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
public class Solution {
static class Pair implements Comparable<Pair>{
long first;
long second;
public Pair(long first,long second) {
this.first=first;
this.second=second;
}
public Pair() {}
@Override
public int compareTo(Solution.Pair o) {
if(first==o.first) {
return (int) (second-o.second);
}else {
return (int) (first-o.first);
}
}
}
public static void main(String[] args) {
try (Scanner in = new Scanner(System.in)) {
int n=in.nextInt();
int k=in.nextInt();
long [] a=new long[n];
for(int i=0;i<n;i++)
a[i]=in.nextLong();
String s=in.next();
int l=0;
int r=0;
long sum=0;
List<Long> list=new ArrayList<>();
while(r<n) {
r=l;
while(r<n && s.charAt(l)==s.charAt(r))
r++;
for(int i=l;i<r;i++) {
list.add(a[i]);
}
Collections.sort(list);
for(int i=list.size()-1;i>=Math.max(list.size()-k, 0);i--) {
sum+=list.get(i);
}
list.clear();
l=r;
}
System.out.println(sum);
}
}
}
| Java | ["7 3\n1 5 16 18 7 2 10\nbaaaaca", "5 5\n2 4 1 3 1000\naaaaa", "5 4\n2 4 1 3 1000\naaaaa", "8 1\n10 15 2 1 4 8 15 16\nqqwweerr", "6 3\n14 18 9 19 2 15\ncccccc", "2 1\n10 10\nqq"] | 1 second | ["54", "1010", "1009", "41", "52", "10"] | NoteIn the first example you can choose hits with numbers $$$[1, 3, 4, 5, 6, 7]$$$ with the total damage $$$1 + 16 + 18 + 7 + 2 + 10 = 54$$$.In the second example you can choose all hits so the total damage is $$$2 + 4 + 1 + 3 + 1000 = 1010$$$.In the third example you can choose all hits expect the third one so the total damage is $$$2 + 4 + 3 + 1000 = 1009$$$.In the fourth example you can choose hits with numbers $$$[2, 3, 6, 8]$$$. Only this way you can reach the maximum total damage $$$15 + 2 + 8 + 16 = 41$$$.In the fifth example you can choose only hits with numbers $$$[2, 4, 6]$$$ with the total damage $$$18 + 19 + 15 = 52$$$.In the sixth example you can change either first hit or the second hit (it does not matter) with the total damage $$$10$$$. | Java 11 | standard input | [
"two pointers",
"sortings",
"greedy"
] | aa3b5895046ed34e89d5fcc3264b3944 | The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$) — the number of hits and the maximum number of times you can push the same button in a row. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the damage of the $$$i$$$-th hit. The third line of the input contains the string $$$s$$$ consisting of exactly $$$n$$$ lowercase Latin letters — the sequence of hits (each character is the letter on the button you need to press to perform the corresponding hit). | 1,300 | Print one integer $$$dmg$$$ — the maximum possible damage to the opponent's character you can deal without breaking your gamepad buttons. | standard output | |
PASSED | f7fc4213a3021454000116e36eea7dec | train_002.jsonl | 1349105400 | Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet.Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0.It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet.Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. | 256 megabytes |
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.PriorityQueue;
import java.util.Scanner;
public class P229B {
private static class Node implements Comparable<Node> {
private int u;
private boolean readyToGo;
private long dist;
public Node(int a, boolean c, long b) {
u=a;
readyToGo = c;
dist=b;
}
public int compareTo(Node node) {
return Long.compare(dist, node.dist);
}
}
private static class Edge {
private int u, v, w;
public Edge(int a, int b, int c) {
u=a;
v=b;
w=c;
}
public int other(int x) {
return (u==x?v:u);
}
}
static InputReader in = new InputReader(System.in);
static PrintWriter w = new PrintWriter(System.out);
private static int n, m;
private static HashMap<Integer, ArrayList<Edge>> g;
private static HashMap<Integer, ArrayList<Long>> times;
public static void main(String[] args) {
// ArrayList<Integer> temp = new ArrayList<>();
// temp.add(1);
//
// System.out.println(Collections.binarySearch(temp, 0));
n = in.nextInt();
m = in.nextInt();
g = new HashMap<>();
for(int i=0; i<n; i++) g.put(i, new ArrayList<>());
times = new HashMap<>();
for(int i=0; i<n; i++) {
times.put(i, new ArrayList<>());
}
for(int i=0; i<m; i++) {
int p = in.nextInt()-1;
int q = in.nextInt()-1;
int w = in.nextInt();
g.get(p).add(new Edge(p, q, w));
g.get(q).add(new Edge(p, q, w));
}
for(int i=0; i<n; i++) {
int count = in.nextInt();
for(int j=0; j<count; j++) {
int time = in.nextInt();
times.get(i).add((long) time);
}
}
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(0, false, 0));
boolean[][] relaxed = new boolean[n][2];
while(!pq.isEmpty()) {
Node node = pq.poll();
int u = node.u;
// System.out.println(u);
if (relaxed[u][node.readyToGo?1:0])
continue;
relaxed[u][node.readyToGo?1:0] = true;
if (u==n-1) {
System.out.println(node.dist);
return;
}
if (node.readyToGo==false) {
if (times.get(u).contains(node.dist)) {
int k;
// System.out.println("HI");
for(k=0; k<times.get(u).size(); k++) {
if (times.get(u).get(k)==node.dist) break;
}
int z;
for(z=k+1; z<times.get(u).size(); z++) {
if (times.get(u).get(z)!=times.get(u).get(z-1)+1) {
break;
}
}
// System.out.println("HI");
if (z==times.get(u).size()) {
pq.add(new Node(u, true, times.get(u).get(times.get(u).size()-1)+1));
} else {
pq.add(new Node(u, true, times.get(u).get(z-1)+1));
}
} else {
pq.add(new Node(u, true, node.dist));
}
continue;
}
for(Edge e: g.get(u)) {
int v = e.other(u);
pq.add(new Node(v, false, node.dist+e.w));
}
}
System.out.println(-1);
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
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 String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
| Java | ["4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0", "3 1\n1 2 3\n0\n1 3\n0"] | 2 seconds | ["7", "-1"] | NoteIn the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds.In the second sample one can't get from planet 1 to planet 3 by moving through stargates. | Java 8 | standard input | [
"data structures",
"binary search",
"graphs",
"shortest paths"
] | d5fbb3033bd7508fd468edb9bb995d6c | The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. | 1,700 | Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. | standard output | |
PASSED | ee618639e128eb291e67941b8142839f | train_002.jsonl | 1349105400 | Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet.Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0.It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet.Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. | 256 megabytes |
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
public class P229B {
static class Edge {
int u, v, w;
public Edge(int a, int b, int c) {
u=a;
v=b;
w=c;
}
public int other(int a) {
if (u==a) {
return v;
} else {
return u;
}
}
}
static class Node implements Comparable<Node>{
int u;
long dist;
public Node(int a, long d) {
u=a;
dist=d;
}
public int compareTo(Node node) {
if (dist>node.dist) {
return 1;
} else {
return -1;
}
}
}
static class P229BTask {
private HashMap<Integer, ArrayList<Edge>> g;
private HashMap<Integer, List<Long>> times;
private HashMap<Integer, List<Long>> exitTimes;
private int n;
public P229BTask(HashMap<Integer, ArrayList<Edge>> graph, HashMap<Integer, List<Long>> times, HashMap<Integer, List<Long>> exitTimes) {
g = graph;
this.times = times;
this.exitTimes = exitTimes;
n = g.size();
}
public long dk() {
HashSet<Integer> relaxed =new HashSet<>();
PriorityQueue<Node> pq = new PriorityQueue<>();
long[] dist = new long[n];
Arrays.fill(dist, (long)1e15);
Integer I=0;
int distOfZeroIndex=Collections.binarySearch(times.get(0), 0L);
long distOfZero=0;
if (distOfZeroIndex>=0) {
distOfZero = exitTimes.get(0).get(distOfZeroIndex);
}
dist[0]=distOfZero;
pq.add(new Node(0, distOfZero));
while(!pq.isEmpty()) {
// System.out.println("SIZE "+pq.size());
Node node = pq.poll();
int u = node.u;
// System.out.println(u+" "+dist[u]);
if (u==n-1) {
return dist[u];
}
if (relaxed.contains(u)) continue;
relaxed.add(u);
for(Edge e: g.get(u)) {
int v = e.other(u);
long reachTime = dist[u]+e.w;
long getOutTime=reachTime;
if (v!=n-1) {
int index=Collections.binarySearch(times.get(v), getOutTime);
if (index>=0) {
getOutTime = exitTimes.get(v).get(index);
}
}
if (getOutTime<dist[v]) {
dist[v]=getOutTime;
pq.add(new Node(v, dist[v]));
}
}
}
return -1;
}
}
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int n = in.nextInt();
int m = in.nextInt();
HashMap<Integer, ArrayList<Edge>> g = new HashMap<>();
for(int i=0; i<n; i++) {
g.put(i, new ArrayList<>());
}
for(int i=0; i<m; i++) {
int p = in.nextInt()-1;
int q = in.nextInt()-1;
int w1 = in.nextInt();
Edge e = new Edge(p, q, w1);
g.get(p).add(e);
g.get(q).add(e);
}
HashMap<Integer, List<Long>> times = new HashMap<>();
HashMap<Integer, List<Long>> exitTimes = new HashMap<>();
for(int i=0; i<n; i++) {
times.put(i, new ArrayList<>());
exitTimes.put(i, new ArrayList<>());
}
for(int i=0; i<n; i++) {
int ts = in.nextInt();
for(int j=0; j<ts; j++) {
times.get(i).add(in.nextLong());
exitTimes.get(i).add(0L);
}
List<Long> et = exitTimes.get(i);
if (ts>=1)
et.set(ts-1, times.get(i).get(ts-1)+1);
for(int j=ts-2; j>=0; j--) {
if (times.get(i).get(j)==times.get(i).get(j+1)-1) {
et.set(j, et.get(j+1));
} else {
et.set(j, times.get(i).get(j)+1);
}
}
}
// for(int i=0; i<n; i++) {
// for(long j: exitTimes.get(i)) {
// System.out.print(j+" ");
// }
// if (exitTimes.get(i).size()!=0)System.out.println();
// }
P229BTask task = new P229BTask(g, times, exitTimes);
System.out.println(task.dk());
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
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 String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
| Java | ["4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0", "3 1\n1 2 3\n0\n1 3\n0"] | 2 seconds | ["7", "-1"] | NoteIn the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds.In the second sample one can't get from planet 1 to planet 3 by moving through stargates. | Java 8 | standard input | [
"data structures",
"binary search",
"graphs",
"shortest paths"
] | d5fbb3033bd7508fd468edb9bb995d6c | The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. | 1,700 | Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. | standard output | |
PASSED | 03e2830a4a514d6c5818f1744a73b3d5 | train_002.jsonl | 1349105400 | Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet.Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0.It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet.Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. | 256 megabytes | import java.util.*;
import java.io.*;
public class Planets {
static final long INF = Long.MAX_VALUE / 3;
static class Edge {
int to;
long w;
Edge(int to, long w) {
this.to = to;
this.w = w;
}
}
static class QueueItem implements Comparable<QueueItem> {
int u;
long t, dist;
public QueueItem(int u, long t, long dist) {
this.u = u; this.t = t; this.dist = dist;
}
public int compareTo(QueueItem q) {
if (this.dist < q.dist) return -1;
if (this.dist > q.dist) return 1;
return 0;
}
}
static long findNext(long t, List<Long> times) {
int posT = Collections.binarySearch(times, t);
if (posT < 0) return t;
int low = 1;
int high = times.size() - 1;
int ans = -1;
while (low <= high) {
int mid = (low + high) / 2;
if (posT + mid >= times.size()) {
high = mid - 1;
} else if (times.get(posT + mid) == t + mid) {
low = mid + 1;
} else {
ans = mid;
high = mid - 1;
}
}
return ans == -1 ? times.get(times.size() - 1) + 1 : t + ans;
}
static long dijkstra(List<List<Edge>> graph, Map<Integer, List<Long>> times) {
int N = graph.size();
long[] dist = new long[N];
PriorityQueue<QueueItem> Q = new PriorityQueue<QueueItem>();
Arrays.fill(dist, INF);
dist[0] = 0;
Q.offer(new QueueItem(0, 0, 0));
while (!Q.isEmpty()) {
QueueItem q = Q.poll();
int u = q.u;
long t = q.t;
long d = q.dist;
if (d == dist[u])
for (Edge e : graph.get(u)) {
int v = e.to;
long w = e.w;
long nt = findNext(t, times.get(u));
if (dist[u] + w + nt - t < dist[v]) {
dist[v] = dist[u] + w + nt - t;
//System.out.println("u = " + u + ", v = " + v + ", w = " + w + ", nt - t = " + (nt - t));
Q.offer(new QueueItem(v, nt + w, dist[v]));
}
}
}
return dist[N - 1] == INF ? -1 : dist[N - 1];
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stk = new StringTokenizer(in.readLine());
int n = Integer.parseInt(stk.nextToken());
int m = Integer.parseInt(stk.nextToken());
List<List<Edge>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) graph.add(new ArrayList<Edge>());
for (int i = 0; i < m; i++) {
stk = new StringTokenizer(in.readLine());
int a = Integer.parseInt(stk.nextToken()) - 1;
int b = Integer.parseInt(stk.nextToken()) - 1;
long c = Long.parseLong(stk.nextToken());
graph.get(a).add(new Edge(b, c));
graph.get(b).add(new Edge(a, c));
}
Map<Integer, List<Long>> times = new HashMap<>();
for (int i = 0; i < n; i++) {
stk = new StringTokenizer(in.readLine());
int k = Integer.parseInt(stk.nextToken());
List<Long> tmp = new ArrayList<>();
for (int j = 0; j < k; j++) {
long t = Long.parseLong(stk.nextToken());
tmp.add(t);
}
times.put(i, tmp);
}
System.out.println(dijkstra(graph, times));
in.close();
System.exit(0);
}
}
| Java | ["4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0", "3 1\n1 2 3\n0\n1 3\n0"] | 2 seconds | ["7", "-1"] | NoteIn the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds.In the second sample one can't get from planet 1 to planet 3 by moving through stargates. | Java 8 | standard input | [
"data structures",
"binary search",
"graphs",
"shortest paths"
] | d5fbb3033bd7508fd468edb9bb995d6c | The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. | 1,700 | Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. | standard output | |
PASSED | 451bdd1fc348eba8ce68c7b65bd767b9 | train_002.jsonl | 1349105400 | Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet.Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0.It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet.Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. | 256 megabytes | import java.io.BufferedReader;
import java.util.Comparator;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author AlexFetisov
*/
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
static Graph g = new Graph();
int[][] travellers;
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
g.initGraphWithCost(n, m);
for (int i = 0; i < m; ++i) {
g.addEdgeWithCost(in.nextInt()-1, in.nextInt()-1, in.nextInt());
}
travellers = new int[n][];
for (int i = 0; i < n; ++i) {
int am = in.nextInt();
travellers[i] = new int[am];
for (int j = 0; j < am; ++j) {
travellers[i][j] = in.nextInt();
}
}
if (n == 1) {
out.println(0);
return;
}
long[] dst = dijkstra();
if (dst[n-1] == Long.MAX_VALUE) {
out.println(-1);
} else {
out.println(dst[n-1]);
}
}
long[] dijkstra() {
int n = g.nVertex;
final long[] dst = new long[n];
Arrays.fill(dst, Long.MAX_VALUE);
dst[0] = getFirstMoreOrEqual(0, 0);
Heap heap = new Heap(n, new IntComparator() {
public int compare(int first, int second) {
return IntegerUtils.longCompare(dst[first], dst[second]);
}
}, n);
heap.add(0);
while (!heap.isEmpty()) {
int current = heap.poll();
for (int id = g.first[current]; id != -1; id = g.next[id]) {
int to = g.to[id];
long total = dst[current] + g.cost[id];
if (to != n-1) {
total = getFirstMoreOrEqual(total, to);
}
if (dst[to] > total) {
dst[to] = total;
if (heap.getIndex(to) == -1) {
heap.add(to);
} else {
heap.shiftUp(heap.getIndex(to));
}
}
}
}
return dst;
}
private long getFirstMoreOrEqual(long total, int to) {
int id = Arrays.binarySearch(travellers[to], (int)total);
if (id < 0) {
return total;
}
// ++id;
// while (id < travellers[to].length && travellers[to][id] == travellers[to][id-1] + 1) {
// ++id;
// }
// if (id >= travellers[to].length) {
// --id;
// }
// return travellers[to][id] + 1;
int left = id, right = travellers[to].length-1;
int middle, result = id;
while (left <= right) {
middle = (left + right) / 2;
long wouldBe = total + middle - id;
if (travellers[to][middle] == wouldBe) {
left = middle + 1;
result = middle;
} else {
right = middle - 1;
}
}
// int otherResult = id + 1;
// while (otherResult < travellers[to].length) {
// if (travellers[to][otherResult] == travellers[to][otherResult-1] + 1) {
// ++otherResult;
// }
// }
// if (otherResult >= travellers[to].length) {
// --otherResult;
// }
// if (otherResult != result) {
// throw new AssertionError("Not equal bin search!");
// }
return travellers[to][result] + 1;
}
}
class Graph {
public int[] from;
public int[] to;
public int[] first;
public int[] next;
public int[] cost;
public int nVertex;
public int nEdges;
public int curEdge;
public Graph() {}
public void initGraphWithCost(int n, int m) {
curEdge = 0;
nVertex = n;
nEdges = m;
from = new int[m * 2];
to = new int[m * 2];
first = new int[n];
next = new int[m * 2];
cost = new int[m * 2];
Arrays.fill(first, -1);
}
public void addEdgeWithCost(int a, int b, int c) {
next[curEdge] = first[a];
first[a] = curEdge;
to[curEdge] = b;
from[curEdge] = a;
cost[curEdge] = c;
++curEdge;
next[curEdge] = first[b];
first[b] = curEdge;
to[curEdge] = a;
from[curEdge] = b;
cost[curEdge] = c;
++curEdge;
}
}
class InputReader {
private BufferedReader reader;
private StringTokenizer stt;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
return null;
}
}
public String nextString() {
while (stt == null || !stt.hasMoreTokens()) {
stt = new StringTokenizer(nextLine());
}
return stt.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextString());
}
}
class Heap {
private IntComparator comparator;
private int size = 0;
private int[] elements;
private int[] at;
public Heap(int capacity, IntComparator comparator, int maxElement) {
this.comparator = comparator;
elements = new int[capacity];
at = new int[maxElement];
Arrays.fill(at, -1);
}
public boolean isEmpty() {
return size == 0;
}
public int add(int element) {
ensureCapacity(size + 1);
elements[size] = element;
at[element] = size;
shiftUp(size++);
return at[element];
}
public void shiftUp(int index) {
int value = elements[index];
while (index != 0) {
int parent = (index - 1) >>> 1;
int parentValue = elements[parent];
if (comparator.compare(parentValue, value) <= 0) {
elements[index] = value;
at[value] = index;
return;
}
elements[index] = parentValue;
at[parentValue] = index;
index = parent;
}
elements[0] = value;
at[value] = 0;
}
public void shiftDown(int index) {
while (true) {
int child = (index << 1) + 1;
if (child >= size) {
return;
}
if (child + 1 < size && comparator.compare(elements[child], elements[child + 1]) > 0) {
child++;
}
if (comparator.compare(elements[index], elements[child]) <= 0) {
return;
}
swap(index, child);
index = child;
}
}
public int getIndex(int element) {
return at[element];
}
private void swap(int first, int second) {
int temp = elements[first];
elements[first] = elements[second];
elements[second] = temp;
at[elements[first]] = first;
at[elements[second]] = second;
}
private void ensureCapacity(int size) {
if (elements.length < size) {
int[] oldElements = elements;
elements = new int[Math.max(2 * elements.length, size)];
System.arraycopy(oldElements, 0, elements, 0, this.size);
}
}
public int poll() {
if (isEmpty())
throw new IndexOutOfBoundsException();
int result = elements[0];
at[result] = -1;
if (size == 1) {
size = 0;
return result;
}
elements[0] = elements[--size];
at[elements[0]] = 0;
shiftDown(0);
return result;
}
}
interface IntComparator {
public int compare(int first, int second);
}
class IntegerUtils {
public static int longCompare(long a, long b) {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
}
| Java | ["4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0", "3 1\n1 2 3\n0\n1 3\n0"] | 2 seconds | ["7", "-1"] | NoteIn the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds.In the second sample one can't get from planet 1 to planet 3 by moving through stargates. | Java 8 | standard input | [
"data structures",
"binary search",
"graphs",
"shortest paths"
] | d5fbb3033bd7508fd468edb9bb995d6c | The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. | 1,700 | Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. | standard output | |
PASSED | 3d9897e263ee4748590d0853a669a757 | train_002.jsonl | 1349105400 | Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet.Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0.It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet.Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. | 256 megabytes | import java.util.List;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.util.TreeSet;
import java.io.Reader;
import java.io.Writer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author niyaznigmatul
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, FastScanner in, FastPrinter out) {
int n = in.nextInt();
int m = in.nextInt();
DijkstraGraph g = new DijkstraGraph(n);
int[][] z = new int[n][];
for (int i = 0; i < m; i++) {
int from = in.nextInt() - 1;
int to = in.nextInt() - 1;
int w = in.nextInt();
g.addEdge(from, to, w);
g.addEdge(to, from, w);
}
for (int i = 0; i < n; i++) {
z[i] = new int[in.nextInt()];
for (int j = 0; j < z[i].length; j++) {
z[i][j] = in.nextInt();
}
ArrayUtils.sort(z[i]);
}
g.z = z;
long[] answer = g.dijkstra(0);
out.println(answer[n - 1] == Long.MAX_VALUE ? -1 : answer[n - 1]);
}
static class DijkstraGraph {
public static class Edge {
public int from;
public int to;
public int w;
public Edge(int from, int to, int w) {
this.from = from;
this.to = to;
this.w = w;
}
}
static class Element implements Comparable<Element> {
int v;
long d;
public Element(int v, long d) {
this.v = v;
this.d = d;
}
public int compareTo(Element o) {
if (d != o.d) {
return d < o.d ? -1 : 1;
}
return v - o.v;
}
}
ArrayList<Edge>[] edges;
int n;
int[][] z;
public DijkstraGraph(int n) {
this.n = n;
@SuppressWarnings("unchecked")
ArrayList<Edge>[] edges = new ArrayList[n];
this.edges = edges;
for (int i = 0; i < n; i++) {
edges[i] = new ArrayList<Edge>();
}
}
public Edge addEdge(int from, int to, int w) {
Edge ret = new Edge(from, to, w);
edges[from].add(ret);
return ret;
}
public long[] dijkstra(int source) {
long[] d = new long[n];
Arrays.fill(d, Long.MAX_VALUE);
Element[] q = new Element[n];
for (int i = 0; i < q.length; i++) {
q[i] = new Element(i, Long.MAX_VALUE);
}
q[source].d = d[source] = 0;
TreeSet<Element> queue = new TreeSet<Element>();
queue.add(q[source]);
while (!queue.isEmpty()) {
Element el = queue.pollFirst();
long current = d[el.v];
for (int i = 0; i < z[el.v].length; i++) {
if (z[el.v][i] == current) {
++current;
}
}
for (int i = 0; i < edges[el.v].size(); i++) {
Edge e = edges[el.v].get(i);
if (d[e.to] > current + e.w) {
queue.remove(q[e.to]);
d[e.to] = q[e.to].d = current + e.w;
queue.add(q[e.to]);
}
}
}
return d;
}
}
}
class FastScanner extends BufferedReader {
boolean isEOF;
public FastScanner(InputStream is) {
super(new InputStreamReader(is));
}
public int read() {
try {
int ret = super.read();
if (isEOF && ret < 0) {
throw new InputMismatchException();
}
isEOF = ret == -1;
return ret;
} catch (IOException e) {
throw new InputMismatchException();
}
}
static boolean isWhiteSpace(int c) {
return c >= -1 && c <= 32;
}
public int nextInt() {
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int ret = 0;
while (!isWhiteSpace(c)) {
if (c < '0' || c > '9') {
throw new NumberFormatException("digit expected " + (char) c
+ " found");
}
ret = ret * 10 + c - '0';
c = read();
}
return ret * sgn;
}
}
class FastPrinter extends PrintWriter {
public FastPrinter(OutputStream out) {
super(out);
}
public FastPrinter(Writer out) {
super(out);
}
}
class ArrayUtils {
static Random rand;
static public void sort(int[] a) {
if (rand == null) {
rand = new Random(System.nanoTime());
}
for (int i = 1; i < a.length; i++) {
int j = rand.nextInt(i + 1);
int t = a[i];
a[i] = a[j];
a[j] = t;
}
Arrays.sort(a);
}
}
| Java | ["4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0", "3 1\n1 2 3\n0\n1 3\n0"] | 2 seconds | ["7", "-1"] | NoteIn the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds.In the second sample one can't get from planet 1 to planet 3 by moving through stargates. | Java 8 | standard input | [
"data structures",
"binary search",
"graphs",
"shortest paths"
] | d5fbb3033bd7508fd468edb9bb995d6c | The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. | 1,700 | Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. | standard output | |
PASSED | 988bba20d66e5466fbdbc8353f92a1f7 | train_002.jsonl | 1349105400 | Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet.Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0.It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet.Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.TreeSet;
/**
* Created by himanshubhardwaj on 15/06/19.
*/
public class Planets {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int m = Integer.parseInt(str[1]);
Graph graph = new Graph(n);
for (int i = 0; i < m; i++) {
str = br.readLine().split(" ");
graph.insert(Integer.parseInt(str[0]), Integer.parseInt(str[1]), Integer.parseInt(str[2]));
}
for (int i = 0; i < n; i++) {
str = br.readLine().split(" ");
graph.insertArrivalTime(i, str);
}
System.out.println(graph.shortestTime(0));
}
}
class Graph {
ArrayList<Edge>[] adjList;
int numNodes;
Vertex[] vertices;
long maxPossibleTime = Long.MAX_VALUE - (200000l * Integer.MAX_VALUE);
public Graph(int n) {
this.numNodes = n;
adjList = new ArrayList[n];
vertices = new Vertex[n];
for (int i = 0; i < n; i++) {
adjList[i] = new ArrayList<>();
vertices[i] = new Vertex(i, maxPossibleTime, maxPossibleTime, new TreeSet<>());
}
}
public void insert(int v1, int v2, int weight) {
v1--;
v2--;
Edge e = new Edge(vertices[v1], vertices[v2], weight);
adjList[v1].add(e);
adjList[v2].add(e);
}
long shortestTime(int source) {
vertices[source].timeOfArrival = 0;
vertices[source].computeDepartureTime();
TreeSet<Vertex> explorableVertices = new TreeSet<>();
explorableVertices.add(vertices[source]);
while (!explorableVertices.isEmpty()) {
Vertex nearestVertex = explorableVertices.first();
explorableVertices.remove(nearestVertex);
if (nearestVertex.timeOfArrival == maxPossibleTime) {
break;
}
for (Edge e : adjList[nearestVertex.index]) {
Vertex neighbour = (e.v1.index == nearestVertex.index) ? e.v2 : e.v1;
if (neighbour.timeOfArrival > nearestVertex.timeofdeparture + e.weight) {
explorableVertices.remove(neighbour);
neighbour.timeOfArrival = nearestVertex.timeofdeparture + e.weight;
neighbour.computeDepartureTime();
explorableVertices.add(neighbour);
}
}
}
return (vertices[numNodes - 1].timeOfArrival != maxPossibleTime) ? vertices[numNodes - 1].timeOfArrival : -1;
}
void insertArrivalTime(int vertexId, String[] str) {
for (int i = 1; i < str.length; i++) {
vertices[vertexId].inserstArrivalTime(Integer.parseInt(str[i]));
}
}
}
class Edge {
Vertex v1;
Vertex v2;
int weight;
@java.beans.ConstructorProperties({"v1", "v2", "weight"})
public Edge(Vertex v1, Vertex v2, int weight) {
this.v1 = v1;
this.v2 = v2;
this.weight = weight;
}
}
class Vertex implements Comparable<Vertex> {
int index;
long timeOfArrival;
long timeofdeparture;
TreeSet<Long> guestArrivalTime;
@java.beans.ConstructorProperties({"index", "timeOfArrival", "timeofdeparture", "guestArrivalTime"})
public Vertex(int index, long timeOfArrival, long timeofdeparture, TreeSet<Long> guestArrivalTime) {
this.index = index;
this.timeOfArrival = timeOfArrival;
this.timeofdeparture = timeofdeparture;
this.guestArrivalTime = guestArrivalTime;
}
@Override
public int compareTo(Vertex o) {
if (this.timeofdeparture != o.timeofdeparture) {
return (int) (this.timeofdeparture - o.timeofdeparture);
} else {
return this.index - o.index;
}
}
void inserstArrivalTime(int time) {
guestArrivalTime.add((long) time);
}
void computeDepartureTime() {
timeofdeparture = this.timeOfArrival;
while (guestArrivalTime.contains(timeofdeparture)) {
timeofdeparture++;
}
}
}
| Java | ["4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0", "3 1\n1 2 3\n0\n1 3\n0"] | 2 seconds | ["7", "-1"] | NoteIn the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds.In the second sample one can't get from planet 1 to planet 3 by moving through stargates. | Java 8 | standard input | [
"data structures",
"binary search",
"graphs",
"shortest paths"
] | d5fbb3033bd7508fd468edb9bb995d6c | The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. | 1,700 | Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. | standard output | |
PASSED | 6aa5a18372c293e3f3a748ba5724088f | train_002.jsonl | 1349105400 | Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet.Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0.It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet.Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.TreeSet;
/**
* Created by himanshubhardwaj on 15/06/19.
*/
public class Planets {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int m = Integer.parseInt(str[1]);
Graph graph = new Graph(n);
for (int i = 0; i < m; i++) {
str = br.readLine().split(" ");
graph.insert(Integer.parseInt(str[0]), Integer.parseInt(str[1]), Integer.parseInt(str[2]));
}
for (int i = 0; i < n; i++) {
str = br.readLine().split(" ");
graph.insertArrivalTime(i, str);
}
System.out.println(graph.shortestTime(0));
}
}
class Graph {
ArrayList<Edge>[] adjList;
int numNodes;
Vertex[] vertices;
long maxPossibleTime = Long.MAX_VALUE - (200000l * Integer.MAX_VALUE);
public Graph(int n) {
this.numNodes = n;
adjList = new ArrayList[n];
vertices = new Vertex[n];
for (int i = 0; i < n; i++) {
adjList[i] = new ArrayList<>();
vertices[i] = new Vertex(i, maxPossibleTime, maxPossibleTime, new TreeSet<>());
}
}
public void insert(int v1, int v2, int weight) {
v1--;
v2--;
Edge e = new Edge(vertices[v1], vertices[v2], weight);
adjList[v1].add(e);
adjList[v2].add(e);
}
long shortestTime(int source) {
vertices[source].timeOfArrival = 0;
vertices[source].computeDepartureTime();
TreeSet<Vertex> explorableVertices = new TreeSet<>();
explorableVertices.add(vertices[source]);
while (!explorableVertices.isEmpty()) {
Vertex nearestVertex = explorableVertices.first();
explorableVertices.remove(nearestVertex);
if (nearestVertex.timeOfArrival == maxPossibleTime) {
break;
}
for (Edge e : adjList[nearestVertex.index]) {
Vertex neighbour = (e.v1.index == nearestVertex.index) ? e.v2 : e.v1;
if (neighbour.timeOfArrival > nearestVertex.timeofdeparture + e.weight) {
explorableVertices.remove(neighbour);
neighbour.timeOfArrival = nearestVertex.timeofdeparture + e.weight;
neighbour.computeDepartureTime();
explorableVertices.add(neighbour);
}
}
// for (Vertex v : vertices) {
// System.out.print(v.timeOfArrival + " ");
// }
// System.out.println();
}
return (vertices[numNodes - 1].timeOfArrival != maxPossibleTime) ? vertices[numNodes - 1].timeOfArrival : -1;
}
void insertArrivalTime(int vertexId, String[] str) {
for (int i = 1; i < str.length; i++) {
vertices[vertexId].inserstArrivalTime(Integer.parseInt(str[i]));
}
}
}
class Edge {
Vertex v1;
Vertex v2;
int weight;
@java.beans.ConstructorProperties({"v1", "v2", "weight"})
public Edge(Vertex v1, Vertex v2, int weight) {
this.v1 = v1;
this.v2 = v2;
this.weight = weight;
}
}
class Vertex implements Comparable<Vertex> {
int index;
long timeOfArrival;
long timeofdeparture;
TreeSet<Long> guestArrivalTime;
@java.beans.ConstructorProperties({"index", "timeOfArrival", "timeofdeparture", "guestArrivalTime"})
public Vertex(int index, long timeOfArrival, long timeofdeparture, TreeSet<Long> guestArrivalTime) {
this.index = index;
this.timeOfArrival = timeOfArrival;
this.timeofdeparture = timeofdeparture;
this.guestArrivalTime = guestArrivalTime;
}
@Override
public int compareTo(Vertex o) {
if (this.timeofdeparture != o.timeofdeparture) {
return (int) (this.timeofdeparture - o.timeofdeparture);
} else {
return this.index - o.index;
}
}
void inserstArrivalTime(int time) {
guestArrivalTime.add((long) time);
}
void computeDepartureTime() {
timeofdeparture = this.timeOfArrival;
while (guestArrivalTime.contains(timeofdeparture)) {
timeofdeparture++;
}
}
}
| Java | ["4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0", "3 1\n1 2 3\n0\n1 3\n0"] | 2 seconds | ["7", "-1"] | NoteIn the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds.In the second sample one can't get from planet 1 to planet 3 by moving through stargates. | Java 8 | standard input | [
"data structures",
"binary search",
"graphs",
"shortest paths"
] | d5fbb3033bd7508fd468edb9bb995d6c | The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. | 1,700 | Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. | standard output | |
PASSED | d98b2e0914a8cbf2180e87e5217de482 | train_002.jsonl | 1349105400 | Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet.Overall the galaxy has n planets, indexed with numbers from 1 to n. Jack is on the planet with index 1, and Apophis will land on the planet with index n. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0.It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time t another traveller arrives to the planet, Jack can only pass through the stargate at time t + 1, unless there are more travellers arriving at time t + 1 to the same planet.Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index n. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static final int INF = (int)1e15; //don't increase, avoid overflow
static ArrayList<Edge>[] adjList;
static HashSet<Long> []h;
static int V;
static class Edge implements Comparable<Edge>
{
int node; long cost;
Edge(int a, long b) { node = a; cost = b; }
public int compareTo(Edge e){ return (int)(cost - e.cost); }
}
static long dijkstra(int S, int T) //O(E log E)
{
long[] dist = new long[V];
Arrays.fill(dist, INF);
PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
dist[S] = 0l;
pq.add(new Edge(S, 0l)); //may add more in case of MSSP (Mult-Source)
while(!pq.isEmpty())
{
Edge cur = pq.remove();
if(cur.node == T) //remove if all computations are needed
return dist[T];
if(cur.cost > dist[cur.node]) //lazy deletion
continue;
for(Edge nxt: adjList[cur.node]){
while(h[cur.node].contains((long)cur.cost)) cur.cost++;
if(1l*cur.cost+1l*nxt.cost < dist[nxt.node])
pq.add(new Edge(nxt.node, dist[nxt.node] = 1l*cur.cost+1l*nxt.cost ));}
}
return -1;
}
public static void main(String[] args) throws IOException,InterruptedException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
// String s = br.readLine();
// char[] arr=s.toCharArray();
// ArrayList<Integer> arrl = new ArrayList<Integer>();
// Queue<Integer> q = new LinkedList<>();
// TreeSet<Integer> ts1 = new TreeSet<Integer>();
// HashSet<Integer> h = new HashSet<Integer>();
// HashMap<Integer, Integer> map= new HashMap<>();
// PriorityQueue<String> pQueue = new PriorityQueue<String>();
// LinkedList<String> object = new LinkedList<String>();
// StringBuilder str = new StringBuilder();
//*******************************************************//
// StringTokenizer st = new StringTokenizer(br.readLine());
// int t = Integer.parseInt(st.nextToken());
// while(t-->0){
// st = new StringTokenizer(br.readLine());
// int n = Integer.parseInt(st.nextToken());
// int[] arr = new int[n];
// st = new StringTokenizer(br.readLine());
// for(int i=0; i<n; i++){
// arr[i] =Integer.parseInt(st.nextToken());
// }
// int ans =0;
// out.println(ans);
// }
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
V = n;
int m = Integer.parseInt(st.nextToken());
adjList = new ArrayList[n];
for(int i =0; i<n ; i++) adjList[i] = new ArrayList<Edge>();
for(int i =0; i<m ; i++){
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken())-1;
int y = Integer.parseInt(st.nextToken())-1;
int c = Integer.parseInt(st.nextToken());
adjList[x].add(new Edge(y,c));
adjList[y].add(new Edge(x,c));
}
h = new HashSet[n];
for(int i =0; i<n ; i++) h[i] = new HashSet<Long>();
for(int i =0; i<n; i++){
st = new StringTokenizer(br.readLine());
int num = Integer.parseInt(st.nextToken());
for(int j =0; j<num; j++) h[i].add(1l*Integer.parseInt(st.nextToken()));
}
long ans = dijkstra(0,n-1);
out.println(ans);
out.flush();
}
} | Java | ["4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0", "3 1\n1 2 3\n0\n1 3\n0"] | 2 seconds | ["7", "-1"] | NoteIn the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then — to planet 4, then he spends a total of only 2 + 5 = 7 seconds.In the second sample one can't get from planet 1 to planet 3 by moving through stargates. | Java 8 | standard input | [
"data structures",
"binary search",
"graphs",
"shortest paths"
] | d5fbb3033bd7508fd468edb9bb995d6c | The first line contains two space-separated integers: n (2 ≤ n ≤ 105), the number of planets in the galaxy, and m (0 ≤ m ≤ 105) — the number of pairs of planets between which Jack can travel using stargates. Then m lines follow, containing three integers each: the i-th line contains numbers of planets ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), which are connected through stargates, and the integer transfer time (in seconds) ci (1 ≤ ci ≤ 104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection. Then n lines follow: the i-th line contains an integer ki (0 ≤ ki ≤ 105) that denotes the number of moments of time when other travellers arrive to the planet with index i. Then ki distinct space-separated integers tij (0 ≤ tij < 109) follow, sorted in ascending order. An integer tij means that at time tij (in seconds) another traveller arrives to the planet i. It is guaranteed that the sum of all ki does not exceed 105. | 1,700 | Print a single number — the least amount of time Jack needs to get from planet 1 to planet n. If Jack can't get to planet n in any amount of time, print number -1. | standard output | |
PASSED | 211f02c4ea4f738594f1391382faefbc | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class test {
/*
* array list
*
* ArrayList<Integer> al=new ArrayList<>(); creating BigIntegers
*
* BigInteger a=new BigInteger(); BigInteger b=new BigInteger();
*
* hash map
*
* HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>(); for(int
* i=0;i<ar.length;i++) { Integer c=hm.get(ar[i]); if(hm.get(ar[i])==null) {
* hm.put(ar[i],1); } else { hm.put(ar[i],++c); } }
*
* while loop
*
* int t=sc.nextInt(); while(t>0) { t--; }
*
* array input
*
* int n = sc.nextInt(); int ar[] = new int[n]; for (int i = 0; i < ar.length;
* i++) { ar[i] = sc.nextInt(); }
*/
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Pair {
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
private static final Scanner sc = new Scanner(System.in);
private static final FastReader fs = new FastReader();
private static final OutputWriter op = new OutputWriter(System.out);
static int[] getintarray(int n) {
int ar[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = fs.nextInt();
}
return ar;
}
static long[] getlongarray(int n) {
long ar[] = new long[n];
for (int i = 0; i < n; i++) {
ar[i] = fs.nextLong();
}
return ar;
}
static int[][] get2darray(int n, int m) {
int ar[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ar[i][j] = fs.nextInt();
}
}
return ar;
}
static Pair[] getpairarray(int n) {
Pair ar[] = new Pair[n];
for (int i = 0; i < n; i++) {
ar[i] = new Pair(fs.nextInt(), fs.nextInt());
}
return ar;
}
static void printarray(int ar[]) {
System.out.println(Arrays.toString(ar));
}
static boolean checkmyArray(int[] ar)
{
int temp = ar[0];
for (int i = 1; i < ar.length; i++) {
if (temp != ar[i])
return false;
}
return true;
}
static int fact(int n) {
int fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
public static void main(String[] args) {
int t = fs.nextInt();
// int t = 1;
while (t > 0) {
int n = fs.nextInt();
int k1 = fs.nextInt();
int k2 = fs.nextInt();
TreeSet<Integer> ss1 = new TreeSet<Integer>();
TreeSet<Integer> ss2 = new TreeSet<Integer>();
for (int i = 0; i < k1; i++) {
ss1.add(fs.nextInt());
}
for (int i = 0; i < k2; i++) {
ss2.add(fs.nextInt());
}
while (!ss1.isEmpty() || !ss2.isEmpty()) {
int x = ss1.last();
int y = ss2.last();
if (x > y) {
ss1.add(y);
ss2.remove(new Integer(y));
} else {
ss2.add(x);
ss1.remove(new Integer(x));
}
if (ss1.isEmpty() || ss2.isEmpty()) {
break;
}
}
if (ss1.isEmpty()) {
System.out.println("NO");
} else {
System.out.println("YES");
}
t--;
}
}
}/*
* 1 5 -2 -3 -1 -4 -6till here
*/
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | e8cf94b3bb6bfa9ef25e695f227d30aa | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | //package c1270.a;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
private static class FastScanner {
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
public String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner(System.in);
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < t; i++) {
if (i > 0) sb.append("\n");
int n = sc.nextInt();
int k1 = sc.nextInt();
int k2 = sc.nextInt();
int[] a = sc.nextIntArray(k1);
int[] b = sc.nextIntArray(k2);
boolean isWin = false;
for (int val : a) {
if (val == n) {
isWin = true;
break;
}
}
sb.append(isWin ? "YES" : "NO");
}
System.out.println(sb.toString());
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 5bc0198342a15b9b8379ad70028ea48a | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int t = read.nextInt();
while(t-- > 0) {
int n = read.nextInt(), k1 = read.nextInt(), k2 = read.nextInt();
int first = 0, second = 0;
for(int i = 0; i < k1; i++)
first = Math.max(first, read.nextInt());
for(int i = 0; i < k2; i++)
second = Math.max(second, read.nextInt());
if(first > second)
System.out.println("yes");
else
System.out.println("no");
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 1b0c706d74fda9d7ff1e835b7c27f799 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.lang.*;
public class Main{
static long mod = 1000000007;
static InputReader in = new InputReader(System.in);
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
int t=in.readInt();
while(t-->0){
int n=in.readInt();
int k1=in.readInt();
int k2=in.readInt();
int[] a=nextIntArray(k1);
int[] b=nextIntArray(k2);
Arrays.sort(a);
Arrays.sort(b);
if(a[k1-1]>b[k2-1])
System.out.println("YES");
else
System.out.println("NO");
//long n=in.readLong();
// String a=in.readString();
}
}
static String removeChar(String s,int a,int b) {
return s.substring(0,a)+s.substring(b,s.length());
}
static int[] nextIntArray(int n){
int[] arr= new int[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static long[] nextLongArray(int n){
long[]arr= new long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static int[] nextIntArray1(int n) {
int[] arr= new int[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readInt();
}
return arr;
}
static long[] nextLongArray1(int n){
long[]arr= new long[n+1];
int i=1;
while(i<=n) {
arr[i++]=in.readLong();
}
return arr;
}
static long gcd(long x, long y) {
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}
static long pow(long n, long m) {
if(m==0)
return 1;
else if(m==1)
return n;
else {
long r=pow(n,m/2);
if(m%2==0)
return r*r;
else
return r*r*n;
}
}
static long max(long a,long b,long c) {
return Math.max(Math.max(a, b),c);
}
static long min(long a,long b,long c) {
return Math.min(Math.min(a, b), c);
}
static class Pair implements Comparable<Pair> {
int a, b;
Pair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(Pair o) {
if (this.a != o.a)
return Integer.compare(this.a, o.a);
else
return Integer.compare(this.b, o.b);
// return 0;
}
public boolean equals(Object o) {
if (o instanceof Pair) {
Pair p = (Pair) o;
return p.a == a && p.b == b;
}
return false;
}
public int hashCode() {
return new Integer(a).hashCode() * 31 + new Integer(b).hashCode();
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public double readDouble() {
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, readInt());
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, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 466b5c82d1697b8f2cbfa1647650b2d6 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while(t-- > 0){
int n = scan.nextInt(),k1 = scan.nextInt(),k2 = scan.nextInt(),flag = 0;
int arr1[] = new int[k1];
int arr2[] = new int[k2];
for(int i = 0; i < k1; i++){
arr1[i] = scan.nextInt();
if(arr1[i] == n)
flag += 1;
}
for(int i = 0; i < k2; i++)
arr2[i] = scan.nextInt();
if(flag == 1)
System.out.println("YES");
else
System.out.println("NO");
}
scan.close();
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 8302e529194dc1b142726a0358ddac29 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Hello {
public static void main(String[] args) {
//try {
// FileInputStream fis = new FileInputStream("data.txt");
// System.setIn(fis);
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0;i < t;++i) {
int n = in.nextInt();
int k1 = in.nextInt();
int k2 = in.nextInt();
int max1 = 0;
int max2 = 0;
for (int j = 0;j < k1;++j) {
int tmp = in.nextInt();
if (tmp > max1) max1 = tmp;
}
for (int j = 0;j < k2;++j) {
int tmp = in.nextInt();
if (tmp > max2) max2 = tmp;
}
if (max1 > max2) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
//} catch (FileNotFoundException e){
// System.out.println("error");
//}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 65c71bf648cf4b61cde7385d1904b5f1 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
public class D {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
while (t>0){
int n = input.nextInt();
int a = input.nextInt();
int b = input.nextInt();
boolean flag = true;
for (int i = 0; i <a ; i++) {
int x= input.nextInt();
}
for (int i = 0; i <b ; i++) {
int x= input.nextInt();
if (x==n){
flag = false;
}
}
if (flag){
System.out.println("YES");
}else {
System.out.println("NO");
}
t--;
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 563a0bcecadbe92be792507433922c88 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.Scanner;
public class CardGame {
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 k1 = sc.nextInt();
int k2 = sc.nextInt();
boolean yes = false;
for (int j = 0; j < k1; j++)
{
int a = sc.nextInt();
if (a==n)
yes = true;
}
for (int j = 0; j < k2; j++)
sc.nextInt();
if (yes)
System.out.println("YES");
else
System.out.println("NO");
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 0788efb35d3800840ee44346fa0fe134 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t > 0){
int n = sc.nextInt();int k1 = sc.nextInt(); int k2 = sc.nextInt();
int[] a1 = new int[k1];
int[] a2 = new int[k2];
for(int i=0;i<k1;i++){
a1[i] = sc.nextInt();
}
for(int i=0;i<k2;i++){
a2[i] = sc.nextInt();
}
Arrays.sort(a1);Arrays.sort(a2);
if(a1[a1.length-1] > a2[a2.length-1]){
System.out.println("YES");
}else{
System.out.println("NO");
}
t--;
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | e0fad7d51644198f02e6f7c32d8277b6 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.stream.IntStream;
import java.util.Arrays;
import java.util.Scanner;
import java.util.OptionalInt;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author ky112233
*/
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);
TaskA solver = new TaskA();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
int k1 = in.nextInt();
int k2 = in.nextInt();
int[] arr1 = new int[k1];
int[] arr2 = new int[k2];
for (int i = 0; i < k1; i++) arr1[i] = in.nextInt();
for (int i = 0; i < k2; i++) arr2[i] = in.nextInt();
int mx1 = Arrays.stream(arr1).max().getAsInt();
int mx2 = Arrays.stream(arr2).max().getAsInt();
out.println(mx1 > mx2 ? "YES" : "NO");
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | ce9487fa649bb70271a81db3ed531d3b | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 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();
while(t-->0) {
int n=sc.nextInt();
int a=sc.nextInt();
int b=sc.nextInt();
int ma=0;
int temp;
for(int i=1;i<=n;i++) {
temp=sc.nextInt();
if(temp==n)
ma=i;
}
if(ma<=a)
System.out.println("YES");
else
System.out.println("NO");
}
sc.close();
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | d112edf290a6e106c11df9e433de51ff | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cf1270a {
public static void main(String[] args) throws IOException {
int t = ri();
while (t --> 0) {
int n = rni(), k1 = ni(), k2 = ni(), a = maxof(ria(k1)), b = maxof(ria(k2));
prYN(a > b);
}
close();
}
static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random __rand = new Random();
// references
// IBIG = 1e9 + 7
// IMAX ~= 2e10
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// math util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;}
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 fli(double d) {return (int)d;}
static int cei(double d) {return (int)ceil(d);}
static long fll(double d) {return (long)d;}
static long cel(double d) {return (long)ceil(d);}
static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
static long hash(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
// array util
static void reverse(int[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(long[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(char[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void rsort(int[] a) {shuffle(a); sort(a);}
static void rsort(long[] a) {shuffle(a); sort(a);}
static int[] copy(int[] a) {int[] ans = new int[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static long[] copy(long[] a) {long[] ans = new long[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static char[] copy(char[] a) {char[] ans = new char[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static int[] sorted(int[] a) {int[] ans = copy(a); sort(ans); return ans;}
static long[] sorted(long[] a) {long[] ans = copy(a); sort(ans); return ans;}
static int[] rsorted(int[] a) {int[] ans = copy(a); rsort(ans); return ans;}
static long[] rsorted(long[] a) {long[] ans = copy(a); rsort(ans); return ans;}
// graph util
static List<List<Integer>> graph(int n) {List<List<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new ArrayList<>()); return g;}
static List<List<Integer>> graph(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connect(g, rni() - 1, ni() - 1); return g;}
static List<List<Integer>> graph(int n, int m) throws IOException {return graph(graph(n), m);}
static List<List<Integer>> dgraph(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connecto(g, rni() - 1, ni() - 1); return g;}
static List<List<Integer>> dgraph(List<List<Integer>> g, int n, int m) throws IOException {return dgraph(graph(n), m);}
static List<Set<Integer>> sgraph(int n) {List<Set<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new HashSet<>()); return g;}
static List<Set<Integer>> sgraph(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connect(g, rni() - 1, ni() - 1); return g;}
static List<Set<Integer>> sgraph(int n, int m) throws IOException {return sgraph(sgraph(n), m);}
static List<Set<Integer>> dsgraph(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) connecto(g, rni() - 1, ni() - 1); return g;}
static List<Set<Integer>> dsgraph(List<Set<Integer>> g, int n, int m) throws IOException {return dsgraph(sgraph(n), m);}
static void connect(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v); g.get(v).add(u);}
static void connecto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v);}
static void dconnect(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v); g.get(v).remove(u);}
static void dconnecto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v);}
// 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 int[] riam1(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()) - 1; 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); if(n >= 0) __out.println(iter.next()); else __out.println();}
static void h() {__out.println("hlfd");}
static void flush() {__out.flush();}
static void close() {__out.close();}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | f6554c5d21c065ff1fbb83cd7715c683 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
public class b
{
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 k1=sc.nextInt(); //no. of cards with the first player
int k2=sc.nextInt(); //no. of cards with the 2nd player
int a[]=new int[k1];
int b[]=new int[k2];
int j;
for(j=0;j<k1;j++ )
{
a[j]=sc.nextInt();
}
for(j=0;j<k2;j++ )
{
b[j]=sc.nextInt();
}
Arrays.sort(a);
Arrays.sort(b);
if(a[k1-1]>b[k2-1])
System.out.println("YES");
else
System.out.println("NO");
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | a22d423f4e5b4f122f1ccafdbc0ed0ae | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.*;
import java.util.*;
public class file
{
public static void main(String args[])throws IOException
{
BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(reader.readLine());
while(t>0)
{
StringTokenizer tk = new StringTokenizer(reader.readLine());
StringTokenizer tk1 = new StringTokenizer(reader.readLine());
StringTokenizer tk2 = new StringTokenizer(reader.readLine());
int n = Integer.parseInt(tk.nextToken());
int k1 = Integer.parseInt(tk.nextToken());
int k2 = Integer.parseInt(tk.nextToken());
int f[] = new int[k1];
int s[] = new int[k2];
int mf=0,ms=0;
for(int i=0;i<k1;i++)
{
f[i]=Integer.parseInt(tk1.nextToken());
mf=Math.max(mf,f[i]);
}
for(int i=0;i<k2;i++)
{
s[i]=Integer.parseInt(tk2.nextToken());
ms = Math.max(ms,s[i]);
}
if(mf>ms)
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
t--;
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | e87f40679c68107c2337a38882bf9f1d | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 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 Almas Turganbayev
*/
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);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int t = in.nextInt();
while ((t--) > 0) {
int n = in.nextInt();
int k1 = in.nextInt();
int k2 = in.nextInt();
int arr[] = new int[k1];
int arr1[] = new int[k2];
for (int i = 0; i < k1; i++) {
arr[i] = in.nextInt();
}
for (int i = 0; i < k2; i++) {
arr1[i] = in.nextInt();
}
int max = arr[0], max1 = arr1[0];
for (int i = 0; i < k1; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
for (int i = 0; i < k2; i++) {
if (arr1[i] > max1) {
max1 = arr1[i];
}
}
if (max > max1) {
out.print("YES\n");
} else {
out.print("NO\n");
}
}
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 4f20b159d652a86597b117fe4e546fe6 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
import java.io.*;
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();
int[] a = new int[in.nextInt()];
int[] b = new int[in.nextInt()];
for(int j = 0; j < a.length; j++){
a[j] = in.nextInt();
}
for(int j = 0; j < b.length; j++){
b[j] = in.nextInt();
}
Arrays.sort(a);
Arrays.sort(b);
System.out.println(a[a.length-1] > b[b.length-1]? "YES" : "NO");
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 943ca25432897c9567a7671efddfb6e8 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
public class JavaApplication14 {
public static void main(String[] args) {
Scanner io = new Scanner(System.in);
int t = io.nextInt();
while(t-- != 0) {
int n = io.nextInt(), a = io.nextInt(), b = io.nextInt();
int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE;
while(a-- != 0) max1 = Math.max(max1, io.nextInt());
while(b-- != 0) max2 = Math.max(max2, io.nextInt());
System.out.println(max1 > max2 ? "YES" : "NO");
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 3f36c1c7a76ef172b7c72f1061dc2c91 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.*;
import java.util.*;
public class card_game{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int []first_values = new int[99];
int []second_values = new int[99];
int t =sc.nextInt();
while(t-- >0){
int n=sc.nextInt();
int k1= sc.nextInt();
int k2= sc.nextInt();
int x=k2;
for(int i=0;i<k1;i++)
first_values[i]=sc.nextInt();
for(int i=0;i<k2;i++)
second_values[i]=sc.nextInt();
int max_k1=first_values[0];
int max_k2=second_values[0];
for(int i=1;i<k1;i++){
if(max_k1<first_values[i])
max_k1=first_values[i];
}
for(int i=1;i<k2;i++){
if(max_k2<second_values[i])
max_k2=second_values[i];
}
if(max_k1>max_k2)
System.out.println("YES");
else
System.out.println("NO");
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 2b6bdeba58572c92f70319056300753c | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
public class CardGame {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt();
int k1 = s.nextInt();
int k2 = s.nextInt();
int firstMax = 0;
for (int i = 0; i < k1; ++i) {
firstMax = Math.max(s.nextInt(), firstMax);
}
int secondMax = 0;
for (int i = 0; i < k2; ++i) {
secondMax = Math.max(s.nextInt(), secondMax);
}
System.out.println(firstMax > secondMax ? "YES" : "NO");
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 04fb0cc6238839fbc19082889d8c3b27 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class CodeForces {
public static void main(String[] args) throws IOException,NumberFormatException{
try {
FastScanner sc=new FastScanner();
int t =sc.nextInt();
while(t-->0) {
int n=sc.nextInt(),k1=sc.nextInt(),k2=sc.nextInt();
List<Integer> a=new ArrayList<>();
List<Integer> b=new ArrayList<>();
for(int i=0;i<k1;i++) {
a.add(sc.nextInt());
}
for(int i=0;i<k2;i++) {
b.add(sc.nextInt());
}
int maxa=Collections.max(a);
int maxb=Collections.max(b);
if(maxa>maxb) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
catch(Exception e) {
return ;
}
}
public static boolean isPrime(int n) {
if(n==1)
return false;
for(int i=2;i*i<=n;i++) {
if(n%i==0) {
return false;
}
}
return true;
}
public static int[] gcd(int a,int b) {
if(b==0) {
return new int[] {a,1,0};
}
int[] vals=gcd(b,a%b);
int d=vals[0];
int x=vals[2];
int y=vals[1]-(a/b)*vals[2];
return new int[] {d,x,y};
}
public static int prodDigit(int n) {
int sum=1;
while(n!=0) {
sum*=(n%10);
n/=10;
}
return sum;
}
public static long GCD(long a,long b) {
if(b==0)
return a;
return GCD(b,a%b);
}
public static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 77bf1f77e7d6ba6bec10dda09ee59c68 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
public class B implements Runnable {
public void run() {
final InputReader sc = new InputReader(System.in);
final PrintWriter out = new PrintWriter(System.out);
int t=sc.nextInt();
while(t--!=0)
{
int temp=0;
int n=sc.nextInt();
int k1=sc.nextInt();
int k2=sc.nextInt();
int a[]=new int[k1];
int b[]=new int[k2];
for(int i=0;i<k1;i++)
{
temp=sc.nextInt();
a[i]=temp;
}
for(int i=0;i<k2;i++)
{
temp=sc.nextInt();
b[i]=temp;
}
Arrays.sort(a);
Arrays.sort(b);
if(a[k1-1]>b[k2-1])
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
out.close();
}
//========================================================
static class Pair
{
int a,b;
Pair(final int aa,final int bb)
{
a=aa;
b=bb;
}
}
static void sa(final int a[],final InputReader sc)
{
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
Arrays.sort(a);
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(final 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 (final IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (final 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();
final StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(final int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(final String args[]) throws Exception {
new Thread(null, new B(),"Main",1<<27).start();
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 1ff86b550ce5d69c6f1c508443f1576d | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class forces{
public static void main(String args[])throws IOException{
DataInputStream ins=new DataInputStream(System.in);
int t=Integer.parseInt(ins.readLine());
for(int i=0;i<t;i++){
String nkk[]=ins.readLine().split(" ");
int n=Integer.parseInt(nkk[0]);
int k1=Integer.parseInt(nkk[1]);
int k2=Integer.parseInt(nkk[2]);
String s1=ins.readLine();
String s2=ins.readLine();
int ind1=s1.indexOf(String.valueOf(n));
int ind2=s2.indexOf(String.valueOf(n));
if(ind1>=0){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 3d6db0c0aa35595c62f3ab868a6586fc | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 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 x=0;x<t;x++)
{
int n=sc.nextInt();
int k1=sc.nextInt();
int k2=sc.nextInt();
int arr1[]=new int[k1];
int arr2[]=new int[k2];
for(int i=0;i<k1;i++)
arr1[i]=sc.nextInt();
for(int i=0;i<k2;i++)
arr2[i]=sc.nextInt();
boolean b=false;
for(int i=0;i<k1;i++)
if(arr1[i]==n)
{
b=true;
break;
}
if(b)
System.out.println("YES");
else
System.out.println("NO");
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 54823a17e686c6f8adf682b400fa2690 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes |
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int t,n,k1,k2;
t=in.nextInt();
for(int i=1;i<=t;i++)
{
ArrayList<Integer>a=new ArrayList<>();
ArrayList<Integer>b=new ArrayList<>();
n=in.nextInt();
k1=in.nextInt();
k2=in.nextInt();
while(k1>0)
{
a.add(in.nextInt());
k1--;
}
while(k2>0)
{
b.add(in.nextInt());
k2--;
}
Collections.sort(a, Collections.reverseOrder());
Collections.sort(b, Collections.reverseOrder());
if(a.get(0)>b.get(0))
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 012f3a9e8e415a9b2aee7a15e0f14f6c | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
import java.io.*;
public class GB2019A {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
while (t > 0) {
int n = sc.nextInt();
int k1 = sc.nextInt();
int k2 = sc.nextInt();
int max1 = 0;
int max2 = 0;
for (int i = 0; i < k1; i++) max1 = Math.max(max1, sc.nextInt());
for (int i = 0; i < k2; i++) max2 = Math.max(max2, sc.nextInt());
out.println(max1 > max2 ? "YES" : "NO");
t--;
}
out.close();
}
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 888bfe47cd79c6c35fd9ee81982fdadf | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
int testCases = in.nextInt();
for(; testCases > 0 ; testCases--) {
int noCards = in.nextInt();
int noFirstCards = in.nextInt();
int noSecondCards = in.nextInt();
boolean firstWinner = false;
for(int i = 0 ; i < noFirstCards ; i++) {
if (in.nextInt() == noCards) {
firstWinner = true;
}
}
for(int i = 0 ; i < noSecondCards ; i++)
in.nextInt();
if (firstWinner)
System.out.println("YES");
else
System.out.println("NO");
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nIArr(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public long[] nLArr(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(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;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 3594d6f6942e643b64e4ece7ed164ed9 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java. util. Arrays;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in = new Scanner(System.in);
int i,t,j,n,m,k,p;
t=in.nextInt();
String s;
for(i=0;i<t;i++)
{
n=in.nextInt();
k=in.nextInt();
p=in.nextInt();
int k1[]=new int[k];
int k2[]=new int[p];
for(j=0;j<k;j++)
k1[j]=in.nextInt();
for(j=0;j<p;j++)
k2[j]=in.nextInt();
Arrays.sort(k1);
if(k1[k-1]==n)
System.out.println("YES");
else
System.out.println("NO");
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 8f3df36c2f94cfcaa4952c03f60a6961 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the maximumSum function below
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
int test=scanner.nextInt();
for(int k=0;k<test;k++){
int t=scanner.nextInt();
int a=scanner.nextInt();
int b=scanner.nextInt();
int arr[]=new int[a];
for(int i=0;i<a;i++){
arr[i]=scanner.nextInt();
}
int brr[]=new int[b];
for(int i=0;i<b;i++){
brr[i]=scanner.nextInt();
}
Arrays.sort(arr);
Arrays.sort(brr);
if(arr[a-1]>brr[b-1]){
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 32e14b98ee61638be2862f0f57e82539 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
public class Solution{
public static void test_case(BufferedReader in) throws IOException{
String str[] = in.readLine().trim().split(" ");
int n = Integer.parseInt(str[0]);
int a = Integer.parseInt(str[1]);
int b = Integer.parseInt(str[2]);
str = in.readLine().trim().split(" ");
boolean flag = false;
for(int i=0;i<a;i++){
if(Integer.parseInt(str[i])==n){
flag = true;
}
}
str = in.readLine().trim().split(" ");
if(flag)
System.out.println("Yes");
else{
System.out.println("No");
}
}
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
while(t-->0){
test_case(in);
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | cd6c39935bab359213d5011601e4ce30 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Q1 {
public static void main(String[] args) {
InputReader in = new InputReader();
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
while (t-- > 0) {
int N = in.nextInt(), k1 = in.nextInt(), k2 = in.nextInt();
int arr1[] = new int[k1], arr2[] = new int[k2];
int max1 = 0, max2 = 0;
for (int i = 0; i < k1; i++) {
arr1[i] = in.nextInt();
max1 = Math.max(arr1[i], max1);
}
for (int i = 0; i < k2; i++) {
arr2[i] = in.nextInt();
max2 = Math.max(arr2[i], max2);
}
if (max1 > max2)
out.println("YES");
else out.println("NO");
}
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public int[] shuffle(int[] arr) {
Random r = new Random();
for (int i = 1, j; i < arr.length; i++) {
j = r.nextInt(i);
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
return arr;
}
public InputReader() {
reader = new BufferedReader(new InputStreamReader(System.in), 32768);
tokenizer = null;
}
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(System.in), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public char nextChar() {
return next().charAt(0);
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
// public static void main(String[] args) {
//
// InputReader in = new InputReader();
// PrintWriter out = new PrintWriter(System.out);
// int t =in.nextInt();
//
// while(t-->0){
// int N=in.nextInt();
// int arr[]=new int[N];
// for(int i=0 ;i<N;i++)
// arr[i]=in.nextInt();
//
// ArrayList<Integer> set1=new ArrayList<>(),set2=new ArrayList<>(),set3=new ArrayList<>();
// double xval=in.nextInt(),x=in.nextInt(),yval=in.nextInt(),y=in.nextInt();
// long k=in.nextLong();
// long ans =0;
//
// if(xval<yval){
// double temp=xval;
// xval=yval;
// yval=temp;
//
// temp=x;
// x=y;
// y=temp;
// }
// xval=xval/100; yval=yval/100;
//
// for(int i =0;i<N;i++){
// if((i+1)%x==0 && (i+1)%y==0)
// set1.add(i);
// else if ((i+1)%x==0 )
// set2.add(i);
// else if((i+1)%y==0)
// set3.add(i);
// }
// arr=in.shuffle(arr);
//
// int a =0,b=0,c=0,add=0;
//
// for(int i=N-1;i>=0;i--){
//
// int temp=0;
//
// if(a==set1.size() && b==set2.size() && c==set3.size())
// continue;
// else if (a==set1.size() && b==set2.size() ){
// ans += (yval) * arr[i];
// c++;
// temp=11;
// }else if(a==set1.size()){
// if (set2.get(b) <set3.get(c)) {
// ans += (xval) * arr[i];
// b++;
// } else {
// ans += (yval) * arr[i];
// c++;
// }
// }else{
// if (set1.get(a) < set2.get(b) && set1.get(a) < set3.get(c)) {
// ans += (xval + yval) * arr[i];
// a++;
// } else if (set2.get(b)<set3.get(c)) {
// ans += (xval) * arr[i];
// b++;
// } else {
// ans += (yval) * arr[i];
// c--;
// }
// }
//
//
//
// if(ans>=k){
// break;
// }
//
// }
//
// if(ans<k)
// out.println(-1);
// else
// out.println(add);
//
//
//
// }
//
// out.close();
// }
//
//import java.io.*;
//import java.util.*;
//
//
//public class Q3 {
//
//
// // tunnel wala Question tourist
//
//// public static void main(String[] args) {
////
//// InputReader in = new InputReader();
//// PrintWriter out = new PrintWriter(System.out);
////
//// int N=in.nextInt();
//// int arr1[]=new int [N],arr2[]=new int[N];
//// int ans =0;
////
//// for(int i =0;i<N;i++){
//// arr1[i]=in.nextInt();
//// }
////
//// HashMap<Integer,Integer>map=new HashMap<>();
////
//// for(int j=0;j<N;j++){
//// int num=in.nextInt();
//// arr2[j]=num;
//// map.put(num,N-j);
//// }
//// int a[]=new int [N+1];
////
//// boolean flag=false;
//// for(int i =0;i<N;i++) {
//// int num = arr1[i];
//// int val=map.get(num);
//// if(val>(N-i))
//// ans++;
//// else if(val==N-i && !flag){
//// ans++;
////
//// }
//// a[arr1[i]]++;
//// a[arr2[N-i-1]]++;
////
//// if(a[arr1[i]]!=2 || a[arr2[N-i-1]]!=2)
//// flag=false;
//// else
//// flag=true;
////
//// }
//// out.println(ans);
////
//// out.close();
////
////
//// }
//
// static class InputReader {
// public BufferedReader reader;
// public StringTokenizer tokenizer;
//
//
// public int[] shuffle(int[] arr) {
// Random r = new Random();
// for (int i = 1, j; i < arr.length; i++) {
// j = r.nextInt(i);
// arr[i] = arr[i] ^ arr[j];
// arr[j] = arr[i] ^ arr[j];
// arr[i] = arr[i] ^ arr[j];
// }
// return arr;
// }
//
// public InputReader() {
// reader = new BufferedReader(new InputStreamReader(System.in), 32768);
// tokenizer = null;
// }
//
// public InputReader(InputStream stream) {
// reader = new BufferedReader(new InputStreamReader(System.in), 32768);
// tokenizer = null;
// }
//
// public String next() {
// while (tokenizer == null || !tokenizer.hasMoreTokens()) {
// try {
// tokenizer = new StringTokenizer(reader.readLine());
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// }
// return tokenizer.nextToken();
// }
//
// public char nextChar() {
// return next().charAt(0);
// }
//
// public int nextInt() {
// return Integer.parseInt(next());
// }
//
// public long nextLong() {
// return Long.parseLong(next());
// }
//
// public double nextDouble() {
// return Double.parseDouble(next());
// }
//
//
// }
//
//}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 7f8c584976ec3b4f5e3dd8897b692196 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 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 k1=sc.nextInt();
int k2=sc.nextInt();
for(int i=0;i<k1;i++){
if(sc.nextInt()==n) System.out.println("Yes");
}
for(int i=0;i<k2;i++){
if(sc.nextInt()==n) System.out.println("No");
}
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 71d9e4398c31a2a59b0ac5ab455545b3 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class A1270_CardGame {
public static void main(String[] args) {
PrintWriter pw = new PrintWriter(System.out);
int t = nextInt();
while (t-- != 0) {
int n = nextInt();
nextInt();
nextInt();
int[] A = Arrays.stream(nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] B = Arrays.stream(nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
pw.println(Arrays.stream(A).anyMatch(x -> x == n) ? "YES" : "NO");
}
pw.close();
}
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer st;
private static String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
private static String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
private static int nextInt() {
return Integer.parseInt(next());
}
private static long nextLong() {
return Long.parseLong(next());
}
private static double nextDouble() {
return Double.parseDouble(next());
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 32b2672c0b5d6ac799d556026b5a4e6c | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 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 a=sc.nextInt();
int b=sc.nextInt();
int ma=0;
int temp;
for(int i = 1; i<=n; i++){
if(sc.nextInt()==n){
ma = i;
}
}
if(ma<=a){//value of cards<= number of cards
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 9b04a87725ea1b3b24cb2d63489db125 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
public class Solution{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
int tests=scan.nextInt();
for (int i=0;i<tests;i++){
scan.nextInt();
int p1=scan.nextInt();
int p2=scan.nextInt();
int max1=0;
int max2=0;
for (int j=0;j<p1;j++){
int entry=scan.nextInt();
if (entry>max1){
max1=entry;
}
}
for (int k=0;k<p2;k++){
int entry=scan.nextInt();
if (entry>max2){
max2=entry;
}
}
if (max1>max2){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | bd79eec8e431cd1c9849801fd510175a | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes |
import java.io.*;
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,k1,k2;
n= sc.nextInt();
k1= sc.nextInt();
k2= sc.nextInt();
int p1[] = new int[k1];
int p2[] = new int[k2];
for(int j=0;j<k1;j++){
p1[j] = sc.nextInt();
}
for(int j=0;j<k2;j++){
p2[j] = sc.nextInt();
}
Arrays.sort(p1);
Arrays.sort(p2);
if(p1[k1-1]>p2[k2-1]){
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 9d351ed231ae98dbb0ae50db99c18993 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes |
//package codeforces;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Findlo {
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();
int k1 = in.nextInt();
int k2 = in.nextInt();
int ar1[] = new int[k1];
int ar2[] = new int[k2];
for (int j = 0; j <k1; j++) {
ar1[j]=in.nextInt();
}
for (int j = 0; j <k2; j++) {
ar2[j]=in.nextInt();
}
Arrays.sort(ar1);
Arrays.sort(ar2);
if (ar1[k1 - 1] >ar2[k2 - 1]) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 6ff86485d5060da2e6aeb80096cd42ed | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | //package com.company;
//import com.sun.source.tree.Tree;
//import com.sun.source.tree.TreeVisitor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
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;
}
}
public static void main (String [] args) {
FastReader s = new FastReader();
int t = s.nextInt();
for (int i = 0; i < t; i++) {
int n = s.nextInt();
int k1 = s.nextInt();
int k2 = s.nextInt();
int j = 0;
boolean souted = false;
for (j = 0; j < k1; j++) {
int f = s.nextInt();
if (f == n){
souted = true;
}
}
for (int k = 0; k < k2; k++) {
int g = s.nextInt();
}
if (souted){
System.out.println("YES");
}
else
System.out.println("NO");
}
}
public static int gcd(int a, int b){
if (a<b){
b = a+b;
a = b-a;
b = b-a;
}
if (a == b || b == 0){
return a;
}
a = a%b;
return gcd(a,b);
}
public static double sqrt (double d){
double l = 0;
double r = d;
for (int i = 0; i < 64; i++) {
double mid = (l+r) / 2;
if (mid*mid == d){
return mid;
}
if (mid*mid < d){
l = mid;
}
else
{
r = mid;
}
}
return r;
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 99717b780807d0241c9efde132e9b245 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = Integer.parseInt(scan.nextLine());
for (int i = 0; i < t; i++) {
int n = scan.nextInt();
int k1 = scan.nextInt();
int k2 = scan.nextInt();
scan.nextLine();
boolean player1 = false;
for (int j = 0; j < k1; j++) {
if (scan.nextInt() == n) player1 = true;
}
scan.nextLine();
for (int j = 0; j < k2; j++) {
if (scan.nextInt() == n) player1 = false;
}
scan.nextLine();
if (player1) System.out.println("YES");
else System.out.println("NO");
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | c0d57ae4e6eb7035a6a79b5f491d9891 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
import java.io.*;
public class C1270 {
public static void main(String[] args) {
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int T = sc.nextInt();
while (T-- > 0) {
int N = sc.nextInt(), A = sc.nextInt(), B = sc.nextInt();
int maxA = 0;
while (A-- > 0) {
maxA = Math.max(sc.nextInt(), maxA);
}
int maxB = 0;
while (B-- > 0) {
maxB = Math.max(sc.nextInt(), maxB);
}
System.out.println(maxA > maxB ? "YES" : "NO");
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | a3f0174dd97790ef3d808142495097ca | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t > 0) {
--t;
in.nextInt();
int k1 = in.nextInt();
int k2 = in.nextInt();
int p1_max = 1;
for (int i=0;i<k1;++i) {
int val = in.nextInt();
if (val > p1_max) p1_max = val;
}
int p2_max = 1;
for (int i=0;i<k2;++i) {
int val = in.nextInt();
if (val > p2_max) p2_max = val;
}
if (p1_max > p2_max) System.out.println("YES");
else System.out.println("NO");
}
in.close();
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 1c1b8302edef65c5ed04cf9fd38b8f23 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | // package CodeForces;
import java.util.Scanner;
public class CardGame {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while (t > 0) {
int noc = scn.nextInt();
int fst = scn.nextInt();
int sec = scn.nextInt();
int[] a = new int[fst];
int[] b = new int[sec];
for (int i = 0; i < a.length; i++) {
a[i] = scn.nextInt();
}
for (int i = 0; i < b.length; i++) {
b[i] = scn.nextInt();
}
int max = 0;
for (int i = 0; i < a.length; i++) {
int flag = a[i];
max = Math.max(max, flag);
}
int p = 0;
for (int i = 0; i < b.length; i++) {
int q = b[i];
p = Math.max(p, q);
}
if (max > p) {
System.out.println("YES");
} else {
System.out.println("NO");
}
t--;
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 5946952c24b93ac36e822a920fec424e | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | //codeforces 700
import java.io.*;
import java.util.*;
import java.util.Arrays;
import static java.lang.System.out;
public class CardGame
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int k1=sc.nextInt();
int k2=sc.nextInt();
int max1=0,max2=0,i;
//Stack<Integer> kf=new Stack<>();
//Stack<Integer> ks=new Stack<>();
int a[]=new int[k1];
int b[]=new int[k2];
for(i=0;i<k1;i++)
{
a[i]=sc.nextInt();
if(a[i]>max1)
max1=a[i];
}//for ends
for(i=0;i<k2;i++)
{
b[i]=sc.nextInt();
if(b[i]>max2)
max2=b[i];
}//for ends
out.println(max2>max1?"NO":"YES");
}//while ends
}//main ends
}//class ends | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 45b53b224d712ed2eeb3b09a246fcabc | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.math.BigInteger;
import java.util.*;
public class div {
private static Scanner sc = new Scanner(System.in);
private static Map<Integer, Long> m = new TreeMap<>();
private static List<Integer> l = new ArrayList<>();
private static long sum = 0;
public static String sortString(String inputString) {
// convert input string to char array
char tempArray[] = inputString.toCharArray();
// sort tempArray
Arrays.sort(tempArray);
// return new sorted string
return new String(tempArray);
}
public static void main(String[] args) {
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt(), k1 = sc.nextInt(), k2 = sc.nextInt();
int[] a = new int[n];
for (int j = 0; j < n; j++) {
a[j] = sc.nextInt();
}
boolean f = true;
for (int j = 0; j < k1; j++) {
if (a[j] == n) {
f = false;
System.out.println("YES");
break;
}
}
if (f)
System.out.println("NO");
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | e30b13302a2d86c4c52a527b3954782b | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.math.BigInteger;
import java.util.*;
import java.io.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
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 Graph{
int V;
ArrayList[] adj;
Graph(int V){
this.V=V;
adj=new ArrayList[V+1];
for(int i=0;i<=V;i++){
adj[i]=new ArrayList<>();
}
}
}
static void addEdge(int a,int b,Graph g){
g.adj[a].add(b);
g.adj[b].add(a);
}
public static void main(String[] args) throws IOException {
FastReader ip = new FastReader();
OutputStream output = System.out;
PrintWriter out = new PrintWriter(output);
int t=ip.nextInt();
while(t-->0){
int n=ip.nextInt();
int k1=ip.nextInt();
int k2=ip.nextInt();
int arr1[]=new int[k1];
int arr2[]=new int[k2];
for(int i=0;i<k1;i++){
arr1[i]=ip.nextInt();
}
for(int i=0;i<k2;i++){
arr2[i]=ip.nextInt();
}
Arrays.sort(arr1);
Arrays.sort(arr2);
if(arr1[k1-1]>arr2[k2-1]){
out.println("YES");
}else{
out.println("NO");
}
}
out.close();
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 1388b81fb0b4accf59e0c48c93da42db | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
public class problem1372A25 {
private static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
int t=sc.nextInt();
while(t>0) {
int max1=Integer.MIN_VALUE;
int max2=Integer.MIN_VALUE;
int n=sc.nextInt();
int k1=sc.nextInt();
int k2=sc.nextInt();
int[] arr1=new int[k1];
int[] arr2=new int[k2];
for (int i = 0; i <k1 ; i++) {
arr1[i]=sc.nextInt();
if(arr1[i]>max1)
max1=arr1[i];
}
for (int i = 0; i <k2 ; i++) {
arr2[i]=sc.nextInt();
if(arr2[i]>max2)
max2=arr2[i];
}
if(max1>max2)
System.out.println("YES");
else
System.out.println("NO");
t--;
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 48c49330e0b34bfb66c8a74e208f8fae | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.*;
public class a {
static long mod = 1000000009L;
public static void main(String[] args) throws Exception {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
sc = new FastScanner(in);
int t = sc.nextInt();
while (t-- > 0) {
int n =sc.nextInt();
int k1 = sc.nextInt();
int k2 = sc.nextInt();
int max1 = Integer.MIN_VALUE;int max2 = Integer.MIN_VALUE;
for(int i = 0 ;i < k1; i++) {
max1 = Math.max(max1 , sc.nextInt());
}
for(int i = 0 ;i < k2; i++) {
max2 = Math.max(max2 , sc.nextInt());
}
if(max1>max2)
out.println("YES");
else
out.println("NO");
}
out.flush();
}
static BufferedReader in;
static FastScanner sc;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 01b0b7db5dac8fe14735317e5a03bad4 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.*;
import static java.lang.System.*;
/*
Shortcut-->
Arrays.stream(n).parallel().sum();
string builder fast_output -->
*/
public class cf_hai1 {
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
public static void main(String[] args) throws IOException {
FastScanner sc = new FastScanner();
int test_case=sc.nextInt();
// int test_case=1;
for(int j=0;j<test_case;j++) {
int a= sc.nextInt();
int k1= sc.nextInt();
int k2= sc.nextInt();
int arr[] = new int[k1];
int arr1[] = new int[k2];
for(int i=0;i<k1;i++){
arr[i]=sc.nextInt();
}
for (int i=0;i<k2;i++){
arr1[i]=sc.nextInt();
}
solve(a,arr,arr1);
}
}
private static void solve(int a, int[] arr, int[] arr1) {
Arrays.sort(arr);
Arrays.sort(arr1);
int l=arr.length-1;
int l1=arr1.length-1;
if(arr[l]>arr1[l1]){
out.println("YES");
}
else
out.println("NO");
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | e083636418d8761ec3ace6aaf117720a | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.Scanner;
public class card {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int tot = 0;
tot = in.nextInt();
for (int i = 0; i < tot; i++) {
int hai =0 ;
int a=0, b=0, c=0,v = 0;
int max = 0;
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
for (int j = 0; j < b; j++) {
v = in.nextInt();
if (v > max) {
max = v;
}
}
for (int k = 0; k < c; k++) {
v = in.nextInt();
if (v > max) {
hai=1;
}
}
if(hai==1){
System.out.println("NO");
}else{
System.out.println("YES");}
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 66bce1804c4c45d860fbce234519e8e1 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
public class Cf1270A {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t,n,k1,k2,m1,m2,i;
t=sc.nextInt();
while(t>0){
m1=0;
m2=0;
n=sc.nextInt();
k1=sc.nextInt();
k2=sc.nextInt();
int []arr=new int[k1];
int []brr=new int[k2];
for (i=0;i<k1;i++) {
arr[i]=sc.nextInt();
if(arr[i]>m1)
m1=arr[i];
}
for (i=0;i<k2;i++) {
brr[i]=sc.nextInt();
if(brr[i]>m2)
m2=brr[i];
}
if(m1>m2)
System.out.println("YES");
else
System.out.println("NO");
t--;
}
sc.close();
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | a423530f67b4d082ce0526cd3234afff | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.*;
import java.util.*;
public class CardGame
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int numoftc = Integer.parseInt(st.nextToken());
while(numoftc!=0)
{
st = new StringTokenizer(br.readLine());
int n,k1,k2;
n = Integer.parseInt(st.nextToken()); //number of cards
k1 = Integer.parseInt(st.nextToken());//number of cards owned by the first player
k2 = Integer.parseInt(st.nextToken());//number of cards owned by the second player
ArrayList<Integer> cardsofk1 = new ArrayList<Integer>();
ArrayList<Integer> cardsofk2 = new ArrayList<Integer>();
st = new StringTokenizer(br.readLine());
for(int i=0; i<k1; i++)
{
int card = Integer.parseInt(st.nextToken());
cardsofk1.add(card);
}
st = new StringTokenizer(br.readLine());
for(int i=0; i<k2; i++)
{
int card = Integer.parseInt(st.nextToken());
cardsofk2.add(card);
}
if(Collections.max(cardsofk1)>Collections.max(cardsofk2))
System.out.println("YES");
else
System.out.println("NO");
numoftc--;
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 52e39e1077132117f79d38c62c79ed16 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.*;
import java.util.*;
//System.out.println();
public class A
{
public static int[] a1;
public static int[] a2;
public static int n;
public static int k1;
public static int k2;
public static String s;
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine().trim());
for(int t = 1; t <= T; t++)
{
String[] in = br.readLine().trim().split("\\s");
n = Integer.parseInt(in[0]);
k1 = Integer.parseInt(in[1]);
k2 = Integer.parseInt(in[2]);
a1 = new int[k1]; int i = 0;
for(String w : br.readLine().trim().split("\\s"))
a1[i++] = Integer.parseInt(w);
a2 = new int[k2]; i = 0;
for(String w : br.readLine().trim().split("\\s"))
a2[i++] = Integer.parseInt(w);
//fn();
System.out.println(fn());
}
}
public static String fn()
{
//System.out.println();
//StringBuilder x = new StringBuilder(s);
int m1 = a1[0];
for(int i = 1; i < k1; i++) m1 = Math.max(m1, a1[i]);
int m2 = a2[0];
for(int i = 0; i < k2; i++) m2 = Math.max(m2, a2[i]);
return m1 > m2 ? "YES" : "NO";
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | c2f8357c10d13261a9c91661d7e9fc86 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.stream.*;
public class Solution
{
public static void main (String[] args) throws java.lang.Exception
{
int t;
int n, k1, k2, card;
Scanner scan = new Scanner(System.in);
t = scan.nextInt();
while(t-- > 0) {
n = scan.nextInt();
k1 = scan.nextInt();
k2 = scan.nextInt();
List <Integer> p1 = new ArrayList<>();
List <Integer> p2 = new ArrayList<>();
while(k1-- > 0) {
card = scan.nextInt();
p1.add(card);
}
while(k2-- > 0) {
card = scan.nextInt();
p2.add(card);
}
int p1max = p1.stream().reduce(0, (m, c)-> m > c ? m : c);
int p2max = p2.stream().reduce(0, (m, c)-> m > c ? m : c);
if(p1max > p2max) System.out.println("YES");
else System.out.println("NO");
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 1ae47a918d9452ef3330fd01e0cb81ad | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
public class Yash
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int k1 = sc.nextInt();
int k2 = sc.nextInt();
int a[] = new int[k1];
int b[] = new int[k2];
int max1 = 0;
int max2 = 0;
for(int i=0; i<k1; i++)
{
a[i] = sc.nextInt();
if(a[i]>max1)
max1=a[i];
}
for(int i=0; i<k2; i++)
{
b[i] = sc.nextInt();
if(b[i]>max2)
max2=b[i];
}
if(max1>max2)
System.out.println("YES");
else
System.out.println("NO");
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | f28726622f660805473aa051c2e89b65 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in=new Scanner(System.in);
int t=in.nextInt();
for(int z=0;z<t;z++){
int n=in.nextInt();
int k1=in.nextInt();
int k2=in.nextInt();
int a[]=new int[k1];
int b[]=new int[k2];
for(int i=0;i<k1;i++){
a[i]=in.nextInt();
}
for(int i=0;i<k2;i++){
b[i]=in.nextInt();
}
Arrays.sort(a);
Arrays.sort(b);
if(a[k1-1]>b[k2-1]){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
} | Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 74bfe661a7352197c6257aa62e833858 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes |
import java.util.*;import java.io.*;import java.math.*;
public class Main
{
public static void process()throws IOException
{
int n=ni(),k1=ni(),k2=ni();
int a1=0,a2=0;
for(int i=1;i<=k1;i++)
a1=Math.max(a1,ni());
for(int i=1;i<=k2;i++)
a2=Math.max(a2,ni());
if(a1>a2)
pn("YES");
else
pn("NO");
}
static AnotherReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
out = new PrintWriter(System.out);
sc=new AnotherReader();
boolean oj = true;
oj = System.getProperty("ONLINE_JUDGE") != null;
if(!oj) sc=new AnotherReader(100);
long s = System.currentTimeMillis();
int t=ni();
while(t-->0)
process();
out.flush();
if(!oj)
System.out.println(System.currentTimeMillis()-s+"ms");
System.out.close();
}
static void pn(Object o){out.println(o);}
static void p(Object o){out.print(o);}
static void pni(Object o){out.println(o);System.out.flush();}
static int ni()throws IOException{return sc.nextInt();}
static long nl()throws IOException{return sc.nextLong();}
static double nd()throws IOException{return sc.nextDouble();}
static String nln()throws IOException{return sc.nextLine();}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
static boolean multipleTC=false;
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static class AnotherReader{BufferedReader br; StringTokenizer st;
AnotherReader()throws FileNotFoundException{
br=new BufferedReader(new InputStreamReader(System.in));}
AnotherReader(int a)throws FileNotFoundException{
br = new BufferedReader(new FileReader("input.txt"));}
String next()throws IOException{
while (st == null || !st.hasMoreElements()) {try{
st = new StringTokenizer(br.readLine());}
catch (IOException e){ e.printStackTrace(); }}
return st.nextToken(); } int nextInt() throws IOException{
return Integer.parseInt(next());}
long nextLong() throws IOException
{return Long.parseLong(next());}
double nextDouble()throws IOException { return Double.parseDouble(next()); }
String nextLine() throws IOException{ String str = ""; try{
str = br.readLine();} catch (IOException e){
e.printStackTrace();} return str;}}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 92d5c5454d556f95d8c23aefca8509d4 | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class main {
public static int getMax(int[] inputArray){
int maxValue = inputArray[0];
int position=0;
for(int i=1;i < inputArray.length;i++){
if(inputArray[i] > maxValue){
maxValue = inputArray[i];
position=i;
}
}
return position;
}
public static void main(String[]arg){
Scanner sc=new Scanner(System.in);
int count =sc.nextInt();
for(int i=0;i<count;i++){
// System.out.println(i);
int n=sc.nextInt();
int k1=sc.nextInt();
int k2=sc.nextInt();
int []arr=new int[n];
for(int j=0;j<n;j++){
arr[j]=sc.nextInt();
}
int max=getMax(arr);
// int po=arr.
max++;
if(max<=k1){
System.out.println("YES");
}else{
System.out.println("NO");
}
//int index = Arrays.binarySearch(arr, max);
// System.out.println(++index);
// algorithm(/*countOfCCards,countOfCardForFirstPlayer,countOfCardForSecondPlayer*/);
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | 06b2d9a52ea6b0686cf4ff47a01efb8e | train_002.jsonl | 1577628300 | Two players decided to play one interesting card game.There is a deck of $$$n$$$ cards, with values from $$$1$$$ to $$$n$$$. The values of cards are pairwise different (this means that no two different cards have equal values). At the beginning of the game, the deck is completely distributed between players such that each player has at least one card. The game goes as follows: on each turn, each player chooses one of their cards (whichever they want) and puts on the table, so that the other player doesn't see which card they chose. After that, both cards are revealed, and the player, value of whose card was larger, takes both cards in his hand. Note that as all cards have different values, one of the cards will be strictly larger than the other one. Every card may be played any amount of times. The player loses if he doesn't have any cards.For example, suppose that $$$n = 5$$$, the first player has cards with values $$$2$$$ and $$$3$$$, and the second player has cards with values $$$1$$$, $$$4$$$, $$$5$$$. Then one possible flow of the game is:The first player chooses the card $$$3$$$. The second player chooses the card $$$1$$$. As $$$3>1$$$, the first player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$, $$$3$$$, the second player has cards $$$4$$$, $$$5$$$.The first player chooses the card $$$3$$$. The second player chooses the card $$$4$$$. As $$$3<4$$$, the second player gets both cards. Now the first player has cards $$$1$$$, $$$2$$$. The second player has cards $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$1$$$. The second player chooses the card $$$3$$$. As $$$1<3$$$, the second player gets both cards. Now the first player has only the card $$$2$$$. The second player has cards $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$.The first player chooses the card $$$2$$$. The second player chooses the card $$$4$$$. As $$$2<4$$$, the second player gets both cards. Now the first player is out of cards and loses. Therefore, the second player wins.Who will win if both players are playing optimally? It can be shown that one of the players has a winning strategy. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Ribhav
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
ACardGame solver = new ACardGame();
solver.solve(1, in, out);
out.close();
}
static class ACardGame {
public void solve(int testNumber, FastReader s, PrintWriter out) {
int t = s.nextInt();
while (t-- > 0000) {
int n = s.nextInt();
int k1 = s.nextInt();
int k2 = s.nextInt();
int[] arr1 = s.nextIntArray(k1);
int[] arr2 = s.nextIntArray(k2);
int max = Integer.MIN_VALUE;
for (int i = 0; i < k1; i++) {
max = Math.max(arr1[i], max);
}
boolean yes = true;
for (int i = 0; i < k2; i++) {
if (arr2[i] > max) {
yes = false;
}
}
out.println(yes ? "YES" : "NO");
}
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["2\n2 1 1\n2\n1\n5 2 3\n2 3\n1 4 5"] | 1 second | ["YES\nNO"] | NoteIn the first test case of the example, there is only one possible move for every player: the first player will put $$$2$$$, the second player will put $$$1$$$. $$$2>1$$$, so the first player will get both cards and will win.In the second test case of the example, it can be shown that it is the second player who has a winning strategy. One possible flow of the game is illustrated in the statement. | Java 11 | standard input | [
"greedy",
"games",
"math"
] | 3ef23f114be223255bd10131b2375b86 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$2 \le n \le 100, 1 \le k_1 \le n - 1, 1 \le k_2 \le n - 1, k_1 + k_2 = n$$$) — the number of cards, number of cards owned by the first player and second player correspondingly. The second line of each test case contains $$$k_1$$$ integers $$$a_1, \dots, a_{k_1}$$$ ($$$1 \le a_i \le n$$$) — the values of cards of the first player. The third line of each test case contains $$$k_2$$$ integers $$$b_1, \dots, b_{k_2}$$$ ($$$1 \le b_i \le n$$$) — the values of cards of the second player. It is guaranteed that the values of all cards are different. | 800 | For each test case, output "YES" in a separate line, if the first player wins. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). | standard output | |
PASSED | e9acf5924b933a5d00bd0d7a81231e97 | train_002.jsonl | 1369582200 | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is .The pseudo code of the unexpected code is as follows:input nfor i from 1 to n input the i-th point's coordinates into p[i]sort array p[] by increasing of x coordinate first and increasing of y coordinate secondd=INF //here INF is a number big enoughtot=0for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j]))output dHere, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? | 256 megabytes | import java.util.*;
import java.io.*;
public class third
{
public static void main(String args[])
{
int n,k;
Scanner sc = new Scanner(System.in);
n=sc.nextInt();
k=sc.nextInt();
if(k>=((n*(n-1))/2))
{
System.out.println("no solution");
}
else
{
int f=0,t=0;
while(t<n)
{
System.out.println(1+" "+t);
t++;
}
}
}
}
| Java | ["4 3", "2 100"] | 2 seconds | ["0 0\n0 1\n1 0\n1 1", "no solution"] | null | Java 6 | standard input | [
"constructive algorithms"
] | a7846e4ae1f3fa5051fab9139a25539c | A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). | 1,300 | If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: All the points must be distinct. |xi|, |yi| ≤ 109. After running the given code, the value of tot should be larger than k. | standard output | |
PASSED | 850ff54c173f395c85ab7fc2b3c54584 | train_002.jsonl | 1369582200 | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is .The pseudo code of the unexpected code is as follows:input nfor i from 1 to n input the i-th point's coordinates into p[i]sort array p[] by increasing of x coordinate first and increasing of y coordinate secondd=INF //here INF is a number big enoughtot=0for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j]))output dHere, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? | 256 megabytes | import java.io.IOException;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: Alexander Shchegolev
* Date: 26.05.13
* Time: 18:08
* To change this template use File | Settings | File Templates.
*/
public class Three {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();
if (n * (n - 1) / 2 <= k) {
System.out.println("no solution");
return;
}
for (int i = 0; i < n; i++) {
int x = i / 500000000;
int y = i % 500000000;
System.out.printf("%d %d\n", x, y);
}
}
}
| Java | ["4 3", "2 100"] | 2 seconds | ["0 0\n0 1\n1 0\n1 1", "no solution"] | null | Java 6 | standard input | [
"constructive algorithms"
] | a7846e4ae1f3fa5051fab9139a25539c | A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). | 1,300 | If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: All the points must be distinct. |xi|, |yi| ≤ 109. After running the given code, the value of tot should be larger than k. | standard output | |
PASSED | 50a5596d58e9ad9ab9f8b148aa5aa1a3 | train_002.jsonl | 1369582200 | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is .The pseudo code of the unexpected code is as follows:input nfor i from 1 to n input the i-th point's coordinates into p[i]sort array p[] by increasing of x coordinate first and increasing of y coordinate secondd=INF //here INF is a number big enoughtot=0for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j]))output dHere, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class CF {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int n = in.nextInt();
int k = in.nextInt();
if (n * (n - 1) / 2 <= k) {
out.println("no solution");
} else {
int x = 0;
int y = 0;
for (int i = 0; i < n; i++) {
out.println(x + " " + y);
y += (n - x);
x++;
}
}
out.close();
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
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 | ["4 3", "2 100"] | 2 seconds | ["0 0\n0 1\n1 0\n1 1", "no solution"] | null | Java 6 | standard input | [
"constructive algorithms"
] | a7846e4ae1f3fa5051fab9139a25539c | A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). | 1,300 | If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: All the points must be distinct. |xi|, |yi| ≤ 109. After running the given code, the value of tot should be larger than k. | standard output | |
PASSED | f30d359c26e47f7e8413e1313e4cf408 | train_002.jsonl | 1369582200 | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is .The pseudo code of the unexpected code is as follows:input nfor i from 1 to n input the i-th point's coordinates into p[i]sort array p[] by increasing of x coordinate first and increasing of y coordinate secondd=INF //here INF is a number big enoughtot=0for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j]))output dHere, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? | 256 megabytes | import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import java.awt.Point;
import java.math.BigInteger;
import static java.lang.Math.*;
public class Codeforces_Solution_C implements Runnable{
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws FileNotFoundException{
if (ONLINE_JUDGE){
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}else{
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String readString() throws IOException{
while(!tok.hasMoreTokens()){
try{
tok = new StringTokenizer(in.readLine());
}catch (Exception e){
return null;
}
}
return tok.nextToken();
}
int readInt() throws IOException{
return Integer.parseInt(readString());
}
long readLong() throws IOException{
return Long.parseLong(readString());
}
double readDouble() throws IOException{
return Double.parseDouble(readString());
}
public static void main(String[] args){
new Thread(null, new Codeforces_Solution_C(), "", 128 * (1L << 20)).start();
}
long timeBegin, timeEnd;
void time(){
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
long memoryTotal, memoryFree;
void memory(){
memoryFree = Runtime.getRuntime().freeMemory();
System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB");
}
void debug(Object... objects){
if (DEBUG){
for (Object o: objects){
System.err.println(o.toString());
}
}
}
public void run(){
try{
timeBegin = System.currentTimeMillis();
memoryTotal = Runtime.getRuntime().freeMemory();
init();
solve();
out.close();
time();
memory();
}catch (Exception e){
e.printStackTrace(System.err);
System.exit(-1);
}
}
boolean DEBUG = false;
void solve() throws IOException{
int n = readInt();
int k = readInt();
if (k>=(n*(n-1))/2) {
out.println("no solution");
return;
}
for (int i=0; i<n; i++) out.println(i+" "+i*n);
}
} | Java | ["4 3", "2 100"] | 2 seconds | ["0 0\n0 1\n1 0\n1 1", "no solution"] | null | Java 6 | standard input | [
"constructive algorithms"
] | a7846e4ae1f3fa5051fab9139a25539c | A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). | 1,300 | If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: All the points must be distinct. |xi|, |yi| ≤ 109. After running the given code, the value of tot should be larger than k. | standard output | |
PASSED | 782c071f2b598ac4f7da077226a5f96e | train_002.jsonl | 1369582200 | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is .The pseudo code of the unexpected code is as follows:input nfor i from 1 to n input the i-th point's coordinates into p[i]sort array p[] by increasing of x coordinate first and increasing of y coordinate secondd=INF //here INF is a number big enoughtot=0for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j]))output dHere, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? | 256 megabytes | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.util.*;
/**
*
* @author N-AssassiN
*/
public class Main {
private static BufferedReader reader;
private static BufferedWriter out;
private static StringTokenizer tokenizer;
//private final static String filename = "filename";
/**
* call this method to initialize reader for InputStream and OututStream
*/
private static void init(InputStream input, OutputStream output) {
reader = new BufferedReader(new InputStreamReader(input));
out = new BufferedWriter(new OutputStreamWriter(output));
//reader = new BufferedReader(new FileReader(filename + ".in"));
//out = new BufferedWriter(new FileWriter(filename + ".out"));
tokenizer = new StringTokenizer("");
}
/**
* get next word
*/
private static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
private static int nextInt() throws IOException {
return Integer.parseInt(next());
}
private static long nextLong() throws IOException {
return Long.parseLong(next());
}
private static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
init(System.in, System.out);
int n = nextInt();
//long startTime = System.currentTimeMillis();
int k = nextInt();
int maxTot = (n * (n - 1)) / 2;
if (maxTot <= k) {
out.write("no solution\n");
} else {
for (int i = 0; i < n; i++) {
out.write("0 " + i + "\n");
}
}
//long runTime = System.currentTimeMillis() - startTime;
//out.write(runTime + "\n");
out.flush();
}
} | Java | ["4 3", "2 100"] | 2 seconds | ["0 0\n0 1\n1 0\n1 1", "no solution"] | null | Java 6 | standard input | [
"constructive algorithms"
] | a7846e4ae1f3fa5051fab9139a25539c | A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). | 1,300 | If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: All the points must be distinct. |xi|, |yi| ≤ 109. After running the given code, the value of tot should be larger than k. | standard output | |
PASSED | 0e1a8de6c44415c8f79a05118800625b | train_002.jsonl | 1369582200 | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is .The pseudo code of the unexpected code is as follows:input nfor i from 1 to n input the i-th point's coordinates into p[i]sort array p[] by increasing of x coordinate first and increasing of y coordinate secondd=INF //here INF is a number big enoughtot=0for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j]))output dHere, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? | 256 megabytes | import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), k = in.nextInt();
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
sum++;
StringBuilder sb = new StringBuilder();
if (sum > k) {
int x = 0, y = 0;
int d = 10000;
for (int i = 0; i < n; i++) {
sb.append(x + " " + y).append('\n');
y += d;
d--;
}
} else {
sb.append("no solution").append('\n');
}
System.out.print(sb);
}
}
| Java | ["4 3", "2 100"] | 2 seconds | ["0 0\n0 1\n1 0\n1 1", "no solution"] | null | Java 6 | standard input | [
"constructive algorithms"
] | a7846e4ae1f3fa5051fab9139a25539c | A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109). | 1,300 | If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point. The conditions below must be held: All the points must be distinct. |xi|, |yi| ≤ 109. After running the given code, the value of tot should be larger than k. | standard output | |
PASSED | b038d87ef123741774d2c29c99e84cd9 | train_002.jsonl | 1591886100 | You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class Scan {
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
static int primes[];
public static void main(String args[]) throws IOException {
Scan input=new Scan();
int n=input.scanInt();
int arr[]=new int[n];
for(int i=0;i<n;i++) {
arr[i]=input.scanInt();
}
sieve();
StringBuilder ans1=new StringBuilder("");
StringBuilder ans2=new StringBuilder("");
for(int i=0;i<n;i++) {
ArrayList<Integer> div=new ArrayList<>();
int tmp=arr[i];
for(int j=0;j<primes.length;j++) {
int cnt=0;
while(tmp%primes[j]==0) {
tmp/=primes[j];
cnt++;
}
if(cnt>=1) {
div.add(primes[j]);
}
}
if(tmp!=1) {
div.add(tmp);
}
if(div.size()==1) {
ans1.append(-1+" ");
ans2.append(-1+" ");
}
else {
int mul=1;
while(arr[i]%div.get(0)==0) {
arr[i]/=div.get(0);
mul*=div.get(0);
}
ans1.append(mul+" ");
ans2.append(arr[i]+" ");
}
}
System.out.println(ans1);
System.out.println(ans2);
}
public static void sieve() {
int indx=0;
primes=new int[452];
boolean sieve[]=new boolean[3200];
for(int i=2;i<sieve.length;i++) {
if(!sieve[i]) {
primes[indx]=i;
indx++;
for(int j=2;i*j<sieve.length;j++) {
sieve[i*j]=true;
}
}
}
// System.out.println(indx);
}
}
| Java | ["10\n2 3 4 5 6 7 8 9 10 24"] | 2 seconds | ["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"] | NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them. | Java 11 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 8639d3ec61f25457c24c5e030b15516e | The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$. | 2,000 | To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them. | standard output | |
PASSED | ce0585b9c30ca5725292510a5cf834e2 | train_002.jsonl | 1591886100 | You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair. | 256 megabytes | import java.util.*;
public class Main {
static int prime[]=new int[10000000+1];
static int factor[]=new int[10000000+1];
static void prims()
{
Arrays.fill(prime,1);
for(int i=2;i<=Math.sqrt(10000000);i++)
{
if(prime[i]==1)
{
for(int j=i*i;j<=10000000;j+=i)
{
prime[j]=0;
if(factor[j]==0)
factor[j]=i;
}
}
}
}
// 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 {
// Your code here!
Scanner sc=new Scanner(System.in);
prims();
int n=sc.nextInt();
StringBuilder a=new StringBuilder();
StringBuilder b=new StringBuilder();
out:
for(int i=0;i<n;i++)
{
int num=sc.nextInt();
if(prime[num]==1)
{
a.append(-1+" ");
b.append(-1+" ");
continue;
}
int j=factor[num];
int temp=num;
// divide num to get other factor
while(temp%j==0)
{
temp/=j;
}
if(temp!=1)
{
a.append(j+" ");
b.append(temp+" ");
}
// for(int k=j+1;k<=num/2;k++)
// {
// if(num%k==0 && prime[k]==1 && j!=k)
// {
// if(gcd(j+k,num)==1)
// {
// a[i]=j;
// b[i]=k;
// continue out;
// }
// }
// }
else
{
a.append(-1+" ");
b.append(-1+" ");
}
}
System.out.println(a);
System.out.println(b);
// System.out.println("XXXXXXXX");
}
}
| Java | ["10\n2 3 4 5 6 7 8 9 10 24"] | 2 seconds | ["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"] | NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them. | Java 11 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 8639d3ec61f25457c24c5e030b15516e | The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$. | 2,000 | To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them. | standard output | |
PASSED | e5e107dde09222c8f6f1f7c5f3bc8249 | train_002.jsonl | 1591886100 | You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair. | 256 megabytes | import java.util.*;
public class Main {
static int prime[]=new int[10000000+1];
static int factor[]=new int[10000000+1];
static void prims()
{
Arrays.fill(prime,1);
for(int i=2;i<=Math.sqrt(10000000);i++)
{
if(prime[i]==1)
{
for(int j=i*i;j<=10000000;j+=i)
{
prime[j]=0;
if(factor[j]==0)
factor[j]=i;
}
}
}
}
// 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 {
// Your code here!
Scanner sc=new Scanner(System.in);
prims();
int n=sc.nextInt();
StringBuilder a=new StringBuilder();
StringBuilder b=new StringBuilder();
out:
for(int i=0;i<n;i++)
{
int num=sc.nextInt();
if(prime[num]==1)
{
a.append(-1+" ");
b.append(-1+" ");
continue;
}
int j=factor[num];
int temp=num;
while(temp%j==0)
{
temp/=j;
}
if(temp!=1)
{
a.append(j+" ");
b.append(temp+" ");
}
// for(int k=j+1;k<=num/2;k++)
// {
// if(num%k==0 && prime[k]==1 && j!=k)
// {
// if(gcd(j+k,num)==1)
// {
// a[i]=j;
// b[i]=k;
// continue out;
// }
// }
// }
else
{
a.append(-1+" ");
b.append(-1+" ");
}
}
System.out.println(a);
System.out.println(b);
// System.out.println("XXXXXXXX");
}
}
| Java | ["10\n2 3 4 5 6 7 8 9 10 24"] | 2 seconds | ["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"] | NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them. | Java 11 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 8639d3ec61f25457c24c5e030b15516e | The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$. | 2,000 | To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them. | standard output | |
PASSED | f7b876d4eeb6e098ff6bb5f906f4f69e | train_002.jsonl | 1591886100 | You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main{
static int mod = (int)(Math.pow(10, 9) + 7);
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
boolean[] sieve = new boolean[(int)Math.sqrt(pow(10,7))+1];
sieve[0] = true;
sieve[1] = true;
for (int i = 2; i * i <= sieve.length; i++){
if (sieve[i]) continue;
for (int j = i*2; j < sieve.length; j+=i){
sieve[j] = true;
}
}
List<Integer> primes = new ArrayList<Integer>();
for (int i = 0; i < sieve.length; i++){
if (!sieve[i]) primes.add(i);
}
int[] p = new int[primes.size()];
for (int i = 0; i < p.length; i++){
p[i] = primes.get(i);
}
int tq= sc.nextInt();
int[][] ans = new int[tq][2];
for( int t = 0; t < tq; t++) {
int a = sc.nextInt();
ans[t][0] = -1;
ans[t][1] = -1;
for (int i = 0; i < p.length; i++){
int pow = 0;
int temp = a;
while (temp % p[i] == 0) {
pow++;
temp /= p[i];
}
int e = (int)pow(p[i],pow);
if (e == 1) continue;
if (a % e == 0 && a/e != 1)
if (gcd(a, e + a/e) == 1){
ans[t][0] = e;
ans[t][1] = a/e;
}
}
}
for (int j = 0; j < 2; j++){
for (int i = 0; i < tq; i++){
out.print(ans[i][j] + " ");
}
out.println();
}
out.close();
}
static boolean isPow2(int a){
int pow = 1;
while (pow(2, pow) <= a){
if (a == pow(2, pow)) return true;
pow++;
}
return false;
}
static int gcd(int a, int b) {
while (b != 0) {
int t = a;
a = b;
b = t % b;
}
return a;
}
static long pow(long a, long N) {
if (N == 0) return 1;
else if (N == 1) return a;
else {
long R = pow(a,N/2);
if (N % 2 == 0) {
return R*R;
}
else {
return R*R*a;
}
}
}
static long powMod(long a, long N) {
if (N == 0) return 1;
else if (N == 1) return a % mod;
else {
long R = powMod(a,N/2) % mod;
R *= R % mod;
if (N % 2 == 1) {
R *= a % mod;
}
return R % mod;
}
}
static void mergeSort(int[] A){ // low to hi sort, single array only
int n = A.length;
if (n < 2) return;
int[] l = new int[n/2];
int[] r = new int[n - n/2];
for (int i = 0; i < n/2; i++){
l[i] = A[i];
}
for (int j = n/2; j < n; j++){
r[j-n/2] = A[j];
}
mergeSort(l);
mergeSort(r);
merge(l, r, A);
}
static void merge(int[] l, int[] r, int[] a){
int i = 0, j = 0, k = 0;
while (i < l.length && j < r.length && k < a.length){
if (l[i] < r[j]){
a[k] = l[i];
i++;
}
else{
a[k] = r[j];
j++;
}
k++;
}
while (i < l.length){
a[k] = l[i];
i++;
k++;
}
while (j < r.length){
a[k] = r[j];
j++;
k++;
}
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
}
| Java | ["10\n2 3 4 5 6 7 8 9 10 24"] | 2 seconds | ["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"] | NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them. | Java 11 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 8639d3ec61f25457c24c5e030b15516e | The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$. | 2,000 | To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them. | standard output | |
PASSED | 148e5f85967fab7b90ca6f18b60b5fea | train_002.jsonl | 1591886100 | You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;
public class ProblemD {
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 scanner = new FastReader();
int n = scanner.nextInt();
List<Integer> d1s = new ArrayList<>();
List<Integer> d2s = new ArrayList<>();
int[] memo = new int[10_000_001];
for (int i = 2; i <= Math.sqrt(memo.length); i++) {
if (i % 2 == 0 && i != 2 || i % 3 == 0 && i != 3) {
continue;
}
for (int j = i + i; j < memo.length; j += i) {
if (memo[j] == 0) {
memo[j] = i;
}
}
}
for (int i = 0; i < n; i++) {
int currentNumber = scanner.nextInt();
int smallestPrimeFactor = -1;
Set<Integer> otherPrimeFactors = new HashSet<>();
while (currentNumber != 1) {
if (memo[currentNumber] == 0) {
memo[currentNumber] = currentNumber;
}
if (smallestPrimeFactor == -1) {
smallestPrimeFactor = memo[currentNumber];
} else if (memo[currentNumber] != smallestPrimeFactor) {
otherPrimeFactors.add(memo[currentNumber]);
}
currentNumber /= memo[currentNumber];
}
if (smallestPrimeFactor != -1 && otherPrimeFactors.size() > 0) {
d1s.add(smallestPrimeFactor);
d2s.add(otherPrimeFactors.stream().reduce(1, (el, acc) -> el * acc));
} else {
d1s.add(-1);
d2s.add(-1);
}
}
System.out.println(d1s.stream().map(String::valueOf).collect(Collectors.joining(" ")));
System.out.println(d2s.stream().map(String::valueOf).collect(Collectors.joining(" ")));
}
}
| Java | ["10\n2 3 4 5 6 7 8 9 10 24"] | 2 seconds | ["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"] | NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them. | Java 11 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 8639d3ec61f25457c24c5e030b15516e | The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$. | 2,000 | To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them. | standard output | |
PASSED | b68ae01b31605c014b5e0e7daee37a92 | train_002.jsonl | 1591886100 | You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair. | 256 megabytes |
// Imports
import java.util.*;
import java.io.*;
public class D {
static int MAX_NUM = (int)1E7 + 10;
static int smallest[];
static void loadPrimes() {
smallest[1] = 1;
for(int i = 2; i < MAX_NUM; i++) {
smallest[i] = i;
}
for(int i = 4; i < MAX_NUM; i += 2) {
smallest[i] = 2;
}
for(int i = 3; i*i < MAX_NUM; i += 2) {
if(smallest[i] == i) {
for(int j = i*i; j < MAX_NUM; j += i) {
if(smallest[j] == j) {
smallest[j] = i;
}
}
}
}
}
/**
* @param args the command line arguments
* @throws IOException, FileNotFoundException
*/
public static void main(String[] args) throws IOException, FileNotFoundException {
// TODO UNCOMMENT WHEN ALGORITHM CORRECT
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
smallest = new int[MAX_NUM];
loadPrimes();
// TODO code application logic here
int N = Integer.parseInt(f.readLine());
int[][] ans = new int[N][2];
StringTokenizer st = new StringTokenizer(f.readLine());
for(int i = 0; i < N; i++) {
int current = Integer.parseInt(st.nextToken());
int first = 1;
int second = 1;
int spf = smallest[current];
while(current > 1) {
if(smallest[current] == spf) {
first *= spf;
}
else {
second *= smallest[current];
}
current = current / smallest[current];
}
if(second == 1) {
ans[i][0] = -1;
ans[i][1] = -1;
}
else {
ans[i][0] = first;
ans[i][1] = second;
}
}
StringBuilder sb = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
for(int i = 0; i < N; i++) {
sb.append(ans[i][0]);
sb.append(" ");
sb2.append(ans[i][1]);
sb2.append(" ");
}
System.out.println(sb.substring(0, sb.length() - 1));
System.out.println(sb2.substring(0, sb2.length() - 1));
}
}
| Java | ["10\n2 3 4 5 6 7 8 9 10 24"] | 2 seconds | ["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"] | NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them. | Java 11 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 8639d3ec61f25457c24c5e030b15516e | The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$. | 2,000 | To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them. | standard output | |
PASSED | 3ba29feb61fa1f7077b7452adc73ced4 | train_002.jsonl | 1591886100 | You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair. | 256 megabytes | import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
public class Main{
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
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 int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static void main(String[] args) throws IOException {
Reader scan = new Reader();
PrintWriter out = new PrintWriter(System.out, true);
int n =scan.nextInt();
int max = 2;
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=scan.nextInt();
max = Math.max(max, arr[i]);
}
int spf[] = new int[max+1];
boolean prime[] = new boolean[max+1];
for(int i=0;i<prime.length;i++)
{
prime[i] = true;
spf[i]=i;
}
for(int p = 2; p*p <=max; p++)
{
if(prime[p] == true)
for(int i = p*2; i <= max; i += p)
{
prime[i] = false;
spf[i]=p;
}
}
int d1[]=new int[n];
int d2[]=new int[n];
for(int i=0;i<n;i++)
{
int a = 1,b=0,x=arr[i],y=spf[arr[i]];
while(x%y == 0)
{
x = x/y;
a = a*y;
}
b = arr[i]/a;
if((gcd(a+b, arr[i])==1)&&(a>1)&&(b>1))
{
d1[i]=a;
d2[i]=b;
}
else
{
d1[i]=-1;
d2[i]=-1;
}
}
for(int i=0;i<n;i++)
out.print(d1[i]+" ");
out.println("");
for(int i=0;i<n;i++)
out.print(d2[i]+" ");
out.println();
}
}
| Java | ["10\n2 3 4 5 6 7 8 9 10 24"] | 2 seconds | ["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"] | NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them. | Java 11 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 8639d3ec61f25457c24c5e030b15516e | The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$. | 2,000 | To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them. | standard output | |
PASSED | 39d3b281225cb6a4bc3d4c7f881c19b1 | train_002.jsonl | 1591886100 | You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair. | 256 megabytes | import java.io.*;
import java.util.*;
public class D{
private void solve() {
int primes[]=new int[10000007];
for(int i=2;i<10000007;i++){
if(primes[i]==0){
for(int j=i;j<10000007;j+=i){
primes[j]=i;
}
}
}
int n=nextInt();
List<Pair>ans=new ArrayList<>();
for(int i=1;i<=n;i++){
int num=nextInt();
List<Integer>factors=new ArrayList<>();
while(num>1){
int divider=primes[num];
if(divider!=0){
while(num%divider==0){
num=num/divider;
}
factors.add(divider);
}
}
if(factors.size()<2){
ans.add(new Pair(-1,-1));
}
else{
long sum=1;
int sze=factors.size();
for(int j=1;j<sze;j++){
sum=sum*factors.get(j);
}
ans.add(new Pair(factors.get(0),sum));
}
}
int sze=ans.size();
List<Long>F=new ArrayList<>();
List<Long>S=new ArrayList<>();
for(int i=0;i<sze;i++){
F.add(ans.get(i).frst);
S.add(ans.get(i).scnd);
}
for(int i=0;i<(F.size());i++){
writer.print(F.get(i)+" ");
}
writer.println();
for(int i=0;i<(S.size());i++) {
writer.print(S.get(i) + " ");
}
}
class Pair{
long frst;
long scnd;
Pair(long f,long s){
this.frst=f;
this.scnd=s;
}
}
public static void main(String[] args) {
new D().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
private void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(System.out);
tokenizer = null;
solve();
reader.close();
writer.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private String nextToken() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
private int nextInt() {
return Integer.parseInt(nextToken());
}
private long nextLong() {
return Long.parseLong(nextToken());
}
private double nextDouble() {
return Double.parseDouble(nextToken());
}
} | Java | ["10\n2 3 4 5 6 7 8 9 10 24"] | 2 seconds | ["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"] | NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them. | Java 11 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 8639d3ec61f25457c24c5e030b15516e | The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$. | 2,000 | To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them. | standard output | |
PASSED | f7c2383cabd4b44e53c5c710e55ff671 | train_002.jsonl | 1591886100 | You are given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$.For each $$$a_i$$$ find its two divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ (where $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$) or say that there is no such pair. | 256 megabytes |
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class d
{
public static void print(String str,int val){
System.out.println(str+" "+val);
}
public static void debug(long[][] arr){
int len = arr.length;
for(int i=0;i<len;i++){
System.out.println(Arrays.toString(arr[i]));
}
}
public static void debug(int[][] arr){
int len = arr.length;
for(int i=0;i<len;i++){
System.out.println(Arrays.toString(arr[i]));
}
}
public static void debug(String[] arr){
int len = arr.length;
for(int i=0;i<len;i++){
System.out.println(arr[i]);
}
}
public static void print(int[] arr){
int len = arr.length;
for(int i=0;i<len;i++){
System.out.print(arr[i]+" ");
}
System.out.print('\n');
}
public static void print(String[] arr){
int len = arr.length;
for(int i=0;i<len;i++){
System.out.print(arr[i]+" ");
}
System.out.print('\n');
}
public static void print(long[] arr){
int len = arr.length;
for(int i=0;i<len;i++){
System.out.print(arr[i]+" ");
}
System.out.print('\n');
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static int[] spf ;
static int MAXN;
static void sieve(){
spf[1] =1;
for(int i=2;i<MAXN;i++){
spf[i] =i;
}
for(int i=4;i<MAXN;i=i+2){
spf[i] = 2;
}
for(int i=3;i*i<MAXN;i++){
if(spf[i]==i){
for(int j=i*i;j<MAXN;j+=i){
if(spf[j]==j){
spf[j] =i;
}
}
}
}
}
public static void main(String[] args) throws IOException {
FastReader s=new FastReader();
MAXN = 10000001;
spf = new int[MAXN];
sieve();
int n = s.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = s.nextInt();
}
int [] first = new int[n];
int[] last = new int[n];
for(int i=0;i<n;i++){
//first highest power of spf
int a= hpower(arr[i]);
int b = (arr[i]/a);
if(a==1 || b==1){
first[i] = -1;
last[i] =-1;
}
else {
first [i] =a ;
last[i] =b;
}
}
OutputStream out = new BufferedOutputStream( System.out );
for(int i=0;i<n;i++){
out.write((first[i]+" ").getBytes());
}
out.write('\n');
out.flush();
for(int i=0;i<n;i++){
out.write((last[i]+" ").getBytes());
}
out.write('\n');
out.flush();
}
public static int hpower(int a){
int sp = spf[a];
int ans =0;
while(spf[a] == sp){
a = a/spf[a];
ans++;
}
return a;
}
// OutputStream out = new BufferedOutputStream( System.out );
// for(int i=1;i<n;i++){
// out.write((max_divisor[i]+" ").getBytes());
//}
// out.flush();
// long start = System.currentTimeMillis();
// long end = System.currentTimeMillis();
// System.out.println((end - start) + "ms");
}
| Java | ["10\n2 3 4 5 6 7 8 9 10 24"] | 2 seconds | ["-1 -1 -1 -1 3 -1 -1 -1 2 2 \n-1 -1 -1 -1 2 -1 -1 -1 5 3"] | NoteLet's look at $$$a_7 = 8$$$. It has $$$3$$$ divisors greater than $$$1$$$: $$$2$$$, $$$4$$$, $$$8$$$. As you can see, the sum of any pair of divisors is divisible by $$$2$$$ as well as $$$a_7$$$.There are other valid pairs of $$$d_1$$$ and $$$d_2$$$ for $$$a_{10}=24$$$, like $$$(3, 4)$$$ or $$$(8, 3)$$$. You can print any of them. | Java 11 | standard input | [
"constructive algorithms",
"number theory",
"math"
] | 8639d3ec61f25457c24c5e030b15516e | The first line contains single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 10^7$$$) — the array $$$a$$$. | 2,000 | To speed up the output, print two lines with $$$n$$$ integers in each line. The $$$i$$$-th integers in the first and second lines should be corresponding divisors $$$d_1 > 1$$$ and $$$d_2 > 1$$$ such that $$$\gcd(d_1 + d_2, a_i) = 1$$$ or $$$-1$$$ and $$$-1$$$ if there is no such pair. If there are multiple answers, print any of them. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.