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
3687b03cda7491fdbf4e66d580397f94
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
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 */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DColoredRectangles solver = new DColoredRectangles(); solver.solve(1, in, out); out.close(); } } static class DColoredRectangles { public void solve(int testNumber, FastReader s, PrintWriter w) { int r = s.nextInt(), g = s.nextInt(), b = s.nextInt(); int[] ri = new int[r], gi = new int[g], bi = new int[b]; for (int i = 0; i < r; i++) ri[i] = s.nextInt(); for (int i = 0; i < g; i++) gi[i] = s.nextInt(); for (int i = 0; i < b; i++) bi[i] = s.nextInt(); func.sort(ri); func.sort(gi); func.sort(bi); int[][][] dp = new int[r + 1][g + 1][b + 1]; for (int i = 0; i <= r; i++) { for (int j = 0; j <= g; j++) { for (int k = 0; k <= b; k++) { if (i > 0 && j > 0) { dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j - 1][k] + ri[r - i] * gi[g - j]); //dp[i][j][j] = max(dp[i - 1][j][k], dp[i][j - 1][k], dp[i][j][k]); } if (i > 0 && k > 0) { dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j][k - 1] + ri[r - i] * bi[b - k]); //dp[i][j][j] = max(dp[i - 1][j][k], dp[i][j][k - 1], dp[i][j][k]); } if (k > 0 && j > 0) { dp[i][j][k] = Math.max(dp[i][j][k], dp[i][j - 1][k - 1] + bi[b - k] * gi[g - j]); //dp[i][j][j] = max(dp[i][j][k - 1], dp[i][j - 1][k], dp[i][j][k]); } } } } int mx = 0; //w.println(dp[1][0][2]); for (int i = 0; i <= r; i++) { for (int j = 0; j <= g; j++) { for (int k = 0; k <= b; k++) { mx = Math.max(mx, dp[i][j][k]); //w.print(dp[i][j][k] + " "); } //w.println(); } } w.println(mx); } } 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 c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class func { public static void sort(int[] arr) { int n = arr.length, mid, h, s, l, i, j, k; int[] res = new int[n]; n--; for (s = 1; s <= n; s <<= 1) { for (l = 0; l < n; l += (s << 1)) { h = Math.min(l + (s << 1) - 1, n); mid = Math.min(l + s - 1, n); i = l; j = mid + 1; k = l; while (i <= mid && j <= h) res[k++] = arr[i] <= arr[j] ? arr[i++] : arr[j++]; while (i <= mid) res[k++] = arr[i++]; while (j <= h) res[k++] = arr[j++]; for (k = l; k <= h; k++) arr[k] = res[k]; } } } } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
763af780c4d5239df9cbd6083c7b9f02
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.io.*; import java.math.*; import java.util.*; public class Main { public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int R = sc.ni(); int G = sc.ni(); int B = sc.ni(); int[] reds = new int[R]; for (int i = 0; i < R; i++) reds[i] = sc.ni(); int[] greens = new int[G]; for (int i = 0; i < G; i++) greens[i] = sc.ni(); int[] blues = new int[B]; for (int i = 0; i < B; i++) blues[i] = sc.ni(); Arrays.sort(reds); Arrays.sort(greens); Arrays.sort(blues); long[][][] dp = new long[R+1][G+1][B+1]; for (int i = 0; i <= R; i++) { for (int j = 0; j <= G; j++) { for (int k = 0; k <= B; k++) { if (i+j+k <= 1) continue; long v1 = 0; if (i>0&&j>0) v1 = dp[i-1][j-1][k]+reds[i-1]*greens[j-1]; long v2 = 0; if (i>0&&k>0) v2 = dp[i-1][j][k-1]+reds[i-1]*blues[k-1]; long v3 = 0; if (j>0&&k>0) v3 = dp[i][j-1][k-1]+greens[j-1]*blues[k-1]; if (i>0) dp[i][j][k] = Math.max(dp[i][j][k], dp[i-1][j][k]); if (j>0) dp[i][j][k] = Math.max(dp[i][j][k], dp[i][j-1][k]); if (k>0) dp[i][j][k] = Math.max(dp[i][j][k], dp[i][j][k-1]); dp[i][j][k] = Math.max(Math.max(dp[i][j][k], v1), Math.max(v2, v3)); } } } pw.println(dp[R][G][B]); pw.close(); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { 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 ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
3cd00b197c60ec582f902be968cd4e90
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class DEdu93{ public static void main(String args[]){ FastReader sc = new FastReader(); StringBuilder sb=new StringBuilder(); int t,i,n1,n2,n3,j,k; t=1; while(t-->0){ n1=sc.nextInt(); n2=sc.nextInt(); n3=sc.nextInt(); int r[]=new int[n1]; int g[]=new int[n2]; int b[]=new int[n3]; for(i=0;i<n1;i++) r[i]=sc.nextInt(); for(i=0;i<n2;i++) g[i]=sc.nextInt(); for(i=0;i<n3;i++) b[i]=sc.nextInt(); Arrays.sort(r); Arrays.sort(g); Arrays.sort(b); int dp[][][]=new int[n1+1][n2+1][n3+1]; for(i=0;i<=n1;i++){ for(j=0;j<=n2;j++){ for(k=0;k<=n3;k++){ if(i>0 && j>0) dp[i][j][k]=Math.max(dp[i][j][k],dp[i-1][j-1][k]+r[i-1]*g[j-1]); if(i>0 && k>0) dp[i][j][k]=Math.max(dp[i][j][k],dp[i-1][j][k-1]+r[i-1]*b[k-1]); if(j>0 && k>0) dp[i][j][k]=Math.max(dp[i][j][k],dp[i][j-1][k-1]+g[j-1]*b[k-1]); } } } sb.append(dp[n1][n2][n3]).append('\n'); } sb.deleteCharAt(sb.length()-1); out.println(sb); } static PrintWriter out; static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(System.out,true); } 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 boolean isPrime(int n) { if(n<2) return false; for(int i=2;i<=(int)Math.sqrt(n);i++) { if(n%i==0) return false; } return true; } public static void print(int a[],int l,int r){ int i; for(i=l;i<=r;i++) out.print(a[i]+" "); out.println(); } public static long fastexpo(long x, long y, long p){ long res=1; while(y > 0){ if((y & 1)==1) res= ((res%p)*(x%p))%p; y= y >> 1; x = ((x%p)*(x%p))%p; } return res; } public static boolean[] sieve (int n) { boolean primes[]=new boolean[n+1]; Arrays.fill(primes,true); primes[0]=primes[1]=false; for(int i=2;i*i<=n;i++){ if(primes[i]){ for(int j=i*i;j<=n;j+=i) primes[j]=false; } } return primes; } public static long gcd(long a,long b){ return (BigInteger.valueOf(a).gcd(BigInteger.valueOf(b))).longValue(); } public static void merge(long a[],int l,int m,int r){ int n1,n2,i,j,k; n1=m-l+1; n2=r-m; long L[]=new long[n1]; long R[]=new long[n2]; for(i=0;i<n1;i++) L[i]=a[l+i]; for(j=0;j<n2;j++) R[j]=a[m+1+j]; i=0;j=0; k=l; while(i<n1&&j<n2){ if(L[i]<=R[j]){ a[k]=L[i]; i++; } else{ a[k]=R[j]; j++; } k++; } while(i<n1){ a[k]=L[i]; i++; k++; } while(j<n2){ a[k]=R[j]; j++; k++; } } public static void sort(long a[],int l,int r){ int m; if(l<r){ m=(l+r)/2; sort(a,l,m); sort(a,m+1,r); merge(a,l,m,r); } } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
c41c98c097ba1a373319d75fc46fffb1
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.util.*; import java.io.*; public class Mamo { static long mod=1000000007; static Reader in=new Reader(); static List<Integer >G[]; static long a[],p[],xp[],xv[]; static StringBuilder Sd=new StringBuilder(),Sl=new StringBuilder(); public static void main(String [] args) { //Dir by MohammedElkady int t=1; while(t-->0) { int na=in.nextInt(),nb=in.nextInt(),nc=in.nextInt(); int []aa=new int[na+1],ab=new int[nb+1],ac=new int[nc+1]; for(int i=0;i<na;i++)aa[i]=in.nextInt(); for(int i=0;i<nb;i++)ab[i]=in.nextInt(); for(int i=0;i<nc;i++)ac[i]=in.nextInt(); Sorting.bucketSort(aa, na+1);Sorting.bucketSort(ab, nb+1);Sorting.bucketSort(ac, nc+1); ans=mola(na,nb,nc,aa,ab,ac); out.append(ans+" "); } out.close(); } static long dp [][][]=new long[201][201][201]; static long mola (int na,int nb, int nc, int aa[],int ab[],int ac[]) { if(na<0||nb<0||nc<0) return 0; if(dp[na][nb][nc]>0) {return dp[na][nb][nc];} long res=0; res=Math.max(res,((long)aa[na]*(long)ab[nb])+mola(na-1,nb-1,nc,aa,ab,ac)); res=Math.max(res,((long)ac[nc]*(long)ab[nb])+mola(na,nb-1,nc-1,aa,ab,ac)); res=Math.max(res,((long)aa[na]*(long)ac[nc])+mola(na-1,nb,nc-1,aa,ab,ac)); dp[na][nb][nc]=res; return res; } static long ans=0L; static boolean v[]; static ArrayList<Integer>res; static Queue <Integer> pop; static Stack <Integer>rem; public static void Dfs(int o) { v[o]=true; for(int i:G[o]) { Dfs(i); if(a[i]>0) { res.add(i+1); a[o]+=a[i];} else { rem.add(i+1); } } ans+=a[o]; } public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static long gcd(long g,long x){if(x<1)return g;else return gcd(x,g%x);} static class Reader { private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;} public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];} public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;} public String s(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();} public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;} public int nextInt(){int c = read() ;while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }int res = 0;do{if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read() ;}while(!isSpaceChar(c));return res * sgn;} public double d() throws IOException {return Double.parseDouble(s()) ;} public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public int[] arr(int n){int[] ret = new int[n];for (int i = 0; i < n; i++) {ret[i] = nextInt();}return ret;} } } class stree{ public stree(int n,long a[]) { this.a=a; seg=new long[n*4]; lazy=new long[n*4]; build(1,0,n-1); } long seg[], a[],lazy[]; void check(int p,int s,int e) { if(lazy[p]!=0) { seg[p] += lazy[p]; if(s!=e) { lazy[2*p] += lazy[p]; lazy[2*p+1] += lazy[p]; } lazy[p] = 0; } } void build(int p,int s,int e) { check(p,s,e); if(s==e) { seg[p] = a[s]; return; } build(2*p,s,(s+e)/2); build(2*p+1,(s+e)/2+1,e); seg[p] = Math.max(seg[2*p], seg[2*p+1]); } void update(int p,int s,int e,int i,int v) { check(p,s,e); if(s==e) { seg[p] = v; return; } if(i<=(s+e)/2) update(2*p,s,(s+e)/2,i,v); else update(2*p+1,(s+e)/2+1,e,i,v); seg[p] = Math.max(seg[2*p],seg[2*p+1]); } void update(int p,int s,int e,int a,int b,int v) { check(p,s,e); if(s>=a && e<=b) { seg[p] += v; if(s!=e) { lazy[2*p] += v; lazy[2*p+1] += v; } return; } if(s>b || e<a) return; update(2*p,s,(s+e)/2,a,b,v); update(2*p+1,(s+e)/2+1,e,a,b,v); seg[p] = Math.max(seg[2*p],seg[2*p+1]); } long get(int p,int s,int e,int a,int b) { if(s>=a && e<=b) return seg[p]; if(s>b || e<a) return Long.MIN_VALUE; return Math.max(get(2*p,s,(s+e)/2,a,b), get(2*p+1,(s+e)/2+1,e,a,b)); } } class node implements Comparable<node>{ int a, b; node(int tt,int ll){ a=tt;b=ll; } @Override public int compareTo(node o) { return b-o.b; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } class Sorting{ public static int[] bucketSort(int[] array, int bucketCount) { if (bucketCount <= 0) throw new IllegalArgumentException("Invalid bucket count"); if (array.length <= 1) return array; //trivially sorted int high = array[0]; int low = array[0]; for (int i = 1; i < array.length; i++) { //find the range of input elements if (array[i] > high) high = array[i]; if (array[i] < low) low = array[i]; } double interval = ((double)(high - low + 1))/bucketCount; //range of one bucket ArrayList<Integer> buckets[] = new ArrayList[bucketCount]; for (int i = 0; i < bucketCount; i++) { //initialize buckets buckets[i] = new ArrayList(); } for (int i = 0; i < array.length; i++) { //partition the input array buckets[(int)((array[i] - low)/interval)].add(array[i]); } int pointer = 0; for (int i = 0; i < buckets.length; i++) { Collections.sort(buckets[i]); //mergeSort for (int j = 0; j < buckets[i].size(); j++) { //merge the buckets array[pointer] = buckets[i].get(j); pointer++; } } return array; } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
62c68ee356af59a30ce4301768350409
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.util.*; import java.util.stream.*; import java.io.*; import java.math.*; public class Main { static boolean FROM_FILE = false; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { if (FROM_FILE) { try { br = new BufferedReader(new FileReader("input.txt")); } catch (IOException error) { } } else { 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 max(int... nums) { int res = Integer.MIN_VALUE; for (int num: nums) res = Math.max(res, num); return res; } static int min(int... nums) { int res = Integer.MAX_VALUE; for (int num: nums) res = Math.min(res, num); return res; } static long max(long... nums) { long res = Long.MIN_VALUE; for (long num: nums) res = Math.max(res, num); return res; } static long min(long... nums) { long res = Long.MAX_VALUE; for (long num: nums) res = Math.min(res, num); return res; } static FastReader fr = new FastReader(); static PrintWriter out; public static void main(String[] args) { if (FROM_FILE) { try { out = new PrintWriter(new FileWriter("output.txt")); } catch (IOException error) { } } else { out = new PrintWriter(new OutputStreamWriter(System.out)); } new Main().run(); out.flush(); out.close(); } void run() { int r = fr.nextInt(), g = fr.nextInt(), b = fr.nextInt(); int[] ar = new int[r], ag = new int[g], ab = new int[b]; for (int i = 0; i < r; i += 1) ar[i] = fr.nextInt(); for (int i = 0; i < g; i += 1) ag[i] = fr.nextInt(); for (int i = 0; i < b; i += 1) ab[i] = fr.nextInt(); Arrays.sort(ar); Arrays.sort(ag); Arrays.sort(ab); int[][][] dp = new int[r + 1][g + 1][b + 1]; for (int i = 0; i <= r; i += 1) { for (int j = 0; j <= g; j += 1) { for (int k = 0; k <= b; k += 1) { int t1 = i > 0 && j > 0 ? ar[r - i] * ag[g - j] + dp[i - 1][j - 1][k] : 0; int t2 = i > 0 && k > 0 ? ar[r - i] * ab[b - k] + dp[i - 1][j][k - 1] : 0; int t3 = j > 0 && k > 0 ? ag[g - j] * ab[b - k] + dp[i][j - 1][k - 1] : 0; int t4 = i > 0 ? dp[i - 1][j][k] : 0; int t5 = j > 0 ? dp[i][j - 1][k] : 0; int t6 = k > 0 ? dp[i][j][k - 1] : 0; dp[i][j][k] = max(t1, t2, t3, t4, t5, t6); // out.println(Arrays.asList(i, j, k, dp[i][j][k])); } } } out.println(dp[r][g][b]); } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
f829f35e2ac6ace0288219cf56f56e37
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.util.*; // scanner.nextInt(); public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int r = scanner.nextInt(); int g = scanner.nextInt(); int b = scanner.nextInt(); long[] rr = new long[r]; long[] gg = new long[g]; long[] bb = new long[b]; for(int i = 0; i < r; ++i) { rr[i] = scanner.nextLong(); } for(int i = 0; i < g; ++i) { gg[i] = scanner.nextLong(); } for(int i = 0; i < b; ++i) { bb[i] = scanner.nextLong(); } sort_l(rr); sort_l(gg); sort_l(bb); long[][][] dp = new long[r+1][g+1][b+1]; dp[0][0][0] = 0; for(int i = 0; i < r; ++i) dp[i][0][0] = 0; for(int j = 0; j < g; ++j) dp[0][j][0] = 0; for(int k = 0; k < b; ++k) dp[0][0][k] = 0; long max = -1; for(int i = 1; i <= r; ++i) { for(int j = 1; j <= g; ++j) { if(i == 1) dp[i][j][0] = (rr[r-1]*gg[g-1]); else if(i != j) { dp[i][j][0] = dp[Math.min(i, j)][Math.min(i, j)][0]; } else { dp[i][j][0] = dp[i-1][j-1][0] + (rr[r-i]*gg[g-j]); } max = Math.max(max, dp[i][j][0]); } } for(int i = 1; i <= r; ++i) { for(int k = 1; k <= b; ++k) { if(i == 1) dp[i][0][k] = (rr[r-1]*bb[b-1]); else if(i != k) { dp[i][0][k] = dp[Math.min(i, k)][0][Math.min(i, k)]; } else { dp[i][0][k] = dp[i-1][0][k-1] + (rr[r-i]*bb[b-k]); } max = Math.max(max, dp[i][0][k]); } } for(int j = 1; j <= g; ++j) { for(int k = 1; k <= b; ++k) { if(j == 1) dp[0][j][k] = (gg[g-1]*bb[b-1]); else if(j != k) { dp[0][j][k] = dp[0][Math.min(j, k)][Math.min(j, k)]; } else { dp[0][j][k] = dp[0][j-1][k-1] + (gg[g-j]*bb[b-k]); } max = Math.max(max, dp[0][j][k]); } } for(int i = 1; i <= r; ++i) { for(int j = 1; j <= g; ++j) { for(int k = 1; k <= b; ++k) { dp[i][j][k] = Math.max(dp[i-1][j-1][k] + (rr[r-i]*gg[g-j]), Math.max(dp[i-1][j][k-1] + (rr[r-i]*bb[b-k]),dp[i][j-1][k-1] + (gg[g-j]*bb[b-k]))); max = Math.max(max, dp[i][j][k]); } } } System.out.println(max); } public static void sort_l(long arr[]) { int n = arr.length; for (int i = n / 2 - 1; i >= 0; i--) heapify_l(arr, n, i); for (int i=n-1; i>0; i--) { long temp = arr[0]; arr[0] = arr[i]; arr[i] = temp; heapify_l(arr, i, 0); } } static void heapify_l(long arr[], int n, int i) { int largest = i; int l = 2*i + 1; int r = 2*i + 2; if (l < n && arr[l] > arr[largest]) largest = l; if (r < n && arr[r] > arr[largest]) largest = r; if (largest != i) { long swap = arr[i]; arr[i] = arr[largest]; arr[largest] = swap; heapify_l(arr, n, largest); } } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
b0809f965fae504af7c368f4fc7f71ad
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public class Prac{ static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nia(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public int[] nia1(int n) { int a[] = new int[n+1]; for (int i = 1; i <=n; i++) { a[i] = ni(); } return a; } public long[] nla(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public long[] nla1(int n) { long a[] = new long[n+1]; for (int i = 1; i <= n; i++) { a[i] = nl(); } return a; } public Long[] nLa(int n) { Long a[] = new Long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public Long[] nLa1(int n) { Long a[] = new Long[n+1]; for (int i = 1; i <= n; i++) { a[i] = nl(); } return a; } public Integer[] nIa(int n) { Integer a[] = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public Integer[] nIa1(int n) { Integer a[] = new Integer[n+1]; for (int i = 1; i <= n; i++) { a[i] = ni(); } return a; } public String rs() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static class Key { private final int x, y; public Key(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Key)) return false; Key key = (Key) o; return x == key.x && y == key.y; } @Override public int hashCode() { int result = x; result = 31 * result + y; return result; } } static class Pair implements Comparable<Pair>{ int x,y; Pair(int a,int b){ x=a;y=b; } @Override public int compareTo(Pair p) { if(x==p.x) return y-p.y; return x-p.x; } } static void shuffleArray(int temp[]){ int n = temp.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = temp[i]; int randomPos = i + rnd.nextInt(n-i); temp[i] = temp[randomPos]; temp[randomPos] = tmp; } } static long gcd(long a,long b){ return b==0?a:gcd(b,a%b);} static long lcm(long a,long b){return (a/gcd(a,b))*b;} static PrintWriter w = new PrintWriter(System.out); static long mod1=998244353L,mod=1000000007; //static int r[]={0,1,0,-1}, c[]={1,0,-1,0}; static long dp[][][]; static Long red[],green[],blue[]; static int n1,n2,n3; static long find(int i,int j,int k){ if((i>=n1&&j>=n2)||(i>=n1&&k>=n3)||(j>=n2&&k>=n3))return 0; if(i>=n1){ return green[j]*blue[k]+find(i,j+1,k+1); } if(j>=n2){ return red[i]*blue[k]+find(i+1,j,k+1); } if(k>=n3){ return red[i]*green[j]+find(i+1,j+1,k); } if(dp[i][j][k]!=-1)return dp[i][j][k]; long ans=0; //if(i<n1-1&&j<n2-1){ ans=Math.max(ans,red[i]*green[j]+find(i+1,j+1,k)); //} //else{ //ans=Math.max(ans,red[i]*green[j]); //if(i<n1-1){ // ans=Math.max(ans,red[i]*green[j]); //} //} //long ans1=red[i]*blue[k]+find(i+1,j,k+1); //if(i<n1-1&&k<n3-1){ ans=Math.max(red[i]*blue[k]+find(i+1,j,k+1),ans); //} //else{ //ans=Math.max(ans,red[i]*green[j]); //} //if(j<n2-1&&k<n3-1){ ans=Math.max(ans,green[j]*blue[k]+find(i,j+1,k+1)); //} // else{ //ans=Math.max(ans,green[j]*blue[k]); //} return dp[i][j][k]=ans; } public static void main(String [] args){ InputReader sc=new InputReader(System.in); n1=sc.ni();n2=sc.ni();n3=sc.ni(); dp=new long[n1+5][n2+5][n3+5]; for(int i=0;i<n1+5;i++){ for(int j=0;j<n2+5;j++){ Arrays.fill(dp[i][j],-1); } } red=sc.nLa(n1); green=sc.nLa(n2); blue=sc.nLa(n3); Arrays.sort(red,Collections.reverseOrder()); Arrays.sort(green,Collections.reverseOrder()); Arrays.sort(blue,Collections.reverseOrder()); long ans=find(0,0,0); w.println(ans); w.close(); } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
a5864d2aba3fe56bc1e4df5103f1a472
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.io.IOException; import java.util.Random; import java.io.UncheckedIOException; import java.io.Closeable; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "khokharnikunj8", 1 << 29); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); DColoredRectangles solver = new DColoredRectangles(); solver.solve(1, in, out); out.close(); } } static class DColoredRectangles { Random rd = new Random(); public void shuffle(int[] ar) { for (int i = 0; i < ar.length; i++) { int id = rd.nextInt(ar.length); ar[i] = ar[i] + ar[id] - (ar[id] = ar[i]); } } public void solve(int testNumber, FastInput in, FastOutput out) { int r = in.readInt(); int g = in.readInt(); int b = in.readInt(); int[] R = new int[r]; int[] G = new int[g]; int[] B = new int[b]; in.populate(R); in.populate(G); in.populate(B); shuffle(R); shuffle(G); shuffle(B); Arrays.sort(R); Arrays.sort(G); Arrays.sort(B); long[][][] dp = new long[r + 1][g + 1][b + 1]; for (int i = 0; i <= r; i++) for (int j = 0; j <= g; j++) Arrays.fill(dp[i][j], Long.MIN_VALUE / 3); long ans = Long.MIN_VALUE; for (int i = r; i >= 0; i--) { for (int j = g; j >= 0; j--) { for (int k = b; k >= 0; k--) { if (i == r && j == g && k == b) dp[i][j][k] = 0; else { if (i + 1 <= r && j + 1 <= g) { dp[i][j][k] = Math.max(dp[i][j][k], dp[i + 1][j + 1][k] + R[i] * G[j]); } if (j + 1 <= g && k + 1 <= b) { dp[i][j][k] = Math.max(dp[i][j][k], dp[i][j + 1][k + 1] + G[j] * B[k]); } if (i + 1 <= r && k + 1 <= b) { dp[i][j][k] = Math.max(dp[i][j][k], dp[i + 1][j][k + 1] + R[i] * B[k]); } if (i + 1 <= r) dp[i][j][k] = Math.max(dp[i][j][k], dp[i + 1][j][k]); if (j + 1 <= g) dp[i][j][k] = Math.max(dp[i][j][k], dp[i][j + 1][k]); if (k + 1 <= b) dp[i][j][k] = Math.max(dp[i][j][k], dp[i][j][k + 1]); } ans = Math.max(ans, dp[i][j][k]); } } } out.println(ans); } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private final Writer os; private final StringBuilder cache = new StringBuilder(5 << 20); public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } public FastOutput append(char c) { cache.append(c); return this; } public FastOutput append(long c) { cache.append(c); return this; } public FastOutput println(long c) { return append(c).println(); } public FastOutput println() { cache.append(System.lineSeparator()); return this; } public FastOutput flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class FastInput { private final InputStream is; private final byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } public void populate(int[] data) { for (int i = 0; i < data.length; i++) { data[i] = readInt(); } } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
5a33a2c606529f03c85dc1924c7b6aeb
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.io.*; import java.util.*; public class Main { private static long testCase(int R, int G, int B, int[] r, int[] g, int[] b) { int[][][] dp = new int[R + 1][G + 1][B + 1]; Arrays.sort(r); Arrays.sort(g); Arrays.sort(b); reverse(r); reverse(g); reverse(b); for (int i = 0; i <= R; i++) dp[i][G][B] = 0; for (int i = 0; i <= G; i++) dp[R][i][B] = 0; for (int i = 0; i <= B; i++) dp[R][G][i] = 0; for (int i = R - 1; i >= 0; i--) { for (int j = G - 1; j >= 0; j--) { dp[i][j][B] = dp[i + 1][j + 1][B] + r[i] * g[j]; } } for (int j = G - 1; j >= 0; j--) { for (int k = B - 1; k >= 0; k--) { dp[R][j][k] = dp[R][j + 1][k + 1] + g[j] * b[k]; } } for (int i = R - 1; i >= 0; i--) { for (int k = B - 1; k >= 0; k--) { dp[i][G][k] = dp[i + 1][G][k + 1] + r[i] * b[k]; } } for (int i = R - 1; i >= 0; i--) { for (int j = G - 1; j >= 0; j--) { for (int k = B - 1; k >= 0; k--) { dp[i][j][k] = max(r[i] * g[j] + dp[i + 1][j + 1][k], r[i] * b[k] + dp[i + 1][j][k + 1], g[j] * b[k] + dp[i][j + 1][k + 1]); } } } return dp[0][0][0]; } private static void reverse(int[] arr) { int temp; for (int i = 0; i < arr.length >> 1; i++) { temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } } private static int max(int... a) { int max = Integer.MIN_VALUE; for (int ai : a) { max = Math.max(ai, max); } return max; } public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); // int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc. //in.nextLine(); // for (int i = 1; i <= t; ++i) { int R = in.nextInt(), G = in.nextInt(), B = in.nextInt(); int[] r = in.readArray(R), g = in.readArray(G), b = in.readArray(B); out.println(testCase(R, G, B, r, g, b)); // } out.close(); } private 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(); } char[] readCharArray(int n) { char[] arr = new char[n]; try { br.read(arr); br.readLine(); } catch (IOException e) { e.printStackTrace(); } return arr; } 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[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
c04b7bf27203ec9d54b99e43013b791f
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.io.*; import java.util.*; public class Main { private static long testCase(int R, int G, int B, int[] r, int[] g, int[] b) { int ans = 0; int[][][] dp = new int[R + 1][G + 1][B + 1]; for (int i = 0; i <= R; i++) { for (int j = 0; j <= G; j++) { Arrays.fill(dp[i][j], -1); } } Arrays.sort(r); Arrays.sort(g); Arrays.sort(b); reverse(r); reverse(g); reverse(b); return getMaxArea(r, g, b, 0, 0, 0, dp); } private static int getMaxArea(int[] r, int[] g, int[] b, int rIdx, int gIdx, int bIdx, int[][][] memo) { if (memo[rIdx][gIdx][bIdx] != -1) { return memo[rIdx][gIdx][bIdx]; } else { if (rIdx == r.length) { if (gIdx == g.length || bIdx == b.length) { memo[rIdx][gIdx][bIdx] = 0; } else { memo[rIdx][gIdx][bIdx] = g[gIdx] * b[bIdx] + getMaxArea(r, g, b, rIdx, gIdx + 1, bIdx + 1, memo); } } else if (gIdx == g.length) { if (rIdx == r.length || bIdx == b.length) { memo[rIdx][gIdx][bIdx] = 0; } else { memo[rIdx][gIdx][bIdx] = r[rIdx] * b[bIdx] + getMaxArea(r, g, b, rIdx + 1, gIdx, bIdx + 1, memo); } } else if (bIdx == b.length) { if (rIdx == r.length || gIdx == g.length) { memo[rIdx][gIdx][bIdx] = 0; } else { memo[rIdx][gIdx][bIdx] = r[rIdx] * g[gIdx] + getMaxArea(r, g, b, rIdx + 1, gIdx + 1, bIdx, memo); } } else { memo[rIdx][gIdx][bIdx] = Math.max(Math.max( g[gIdx] * b[bIdx] + getMaxArea(r, g, b, rIdx, gIdx + 1, bIdx + 1, memo), r[rIdx] * b[bIdx] + getMaxArea(r, g, b, rIdx + 1, gIdx, bIdx + 1, memo)), r[rIdx] * g[gIdx] + getMaxArea(r, g, b, rIdx + 1, gIdx + 1, bIdx, memo)); } return memo[rIdx][gIdx][bIdx]; } } public static void reverse(int[] arr) { int temp; for (int i = 0; i < arr.length >> 1; i++) { temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } } public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); // int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc. //in.nextLine(); // for (int i = 1; i <= t; ++i) { int R = in.nextInt(), G = in.nextInt(), B = in.nextInt(); int[] r = in.readArray(R), g = in.readArray(G), b = in.readArray(B); out.println(testCase(R, G, B, r, g, b)); // } out.close(); } private 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(); } char[] readCharArray(int n) { char[] arr = new char[n]; try { br.read(arr); br.readLine(); } catch (IOException e) { e.printStackTrace(); } return arr; } 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[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
d6226b53ebe59e7b235ff0b0cc44f818
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.io.*; import java.util.*; public class D { 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 FastReader s = new FastReader(); static PrintWriter out = new PrintWriter(System.out); private static int[] rai(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); } return arr; } private static int[][] rai(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextInt(); } } return arr; } private static long[] ral(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } return arr; } private static long[][] ral(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] = s.nextLong(); } } return arr; } private static int ri() { return s.nextInt(); } private static long rl() { return s.nextLong(); } private static String rs() { return s.next(); } static int[][][] dp; static List<Integer> red=new ArrayList<>(); static List<Integer> green=new ArrayList<>(); static List<Integer> blue=new ArrayList<>(); static int maxArea(int r,int g,int b) { boolean b1=r>=red.size(); boolean b2=g>=green.size(); boolean b3=b>=blue.size(); if((b1&& b2 )||(b2 && b3) || (b3 && b1)) { return 0; } if(dp[r][g][b]!=-1) { return dp[r][g][b]; } if(r<red.size() && g<green.size()) { int val=red.get(r)*green.get(g); val+=maxArea(r+1,g+1,b); dp[r][g][b]=Math.max(val,dp[r][g][b]); } if(r<red.size() && b<blue.size()) { int val=red.get(r)*blue.get(b); val+=maxArea(r+1,g,b+1); dp[r][g][b]=Math.max(val,dp[r][g][b]); } if(g<green.size() && b<blue.size()) { int val=green.get(g)*blue.get(b); val+=maxArea(r,g+1,b+1); dp[r][g][b]=Math.max(val,dp[r][g][b]); } return dp[r][g][b]; } public static void main(String[] args) { StringBuilder ans = new StringBuilder(); // int t = ri(); int t=1; while (t-- > 0) { int r=ri();int g=ri(); int b=ri(); red=new ArrayList<>(); green=new ArrayList<>(); blue=new ArrayList<>(); for(int i=0;i<r;i++) { red.add(ri()); } for(int i=0;i<g;i++) { green.add(ri()); } for(int i=0;i<b;i++) { blue.add(ri()); } red.sort(Collections.reverseOrder()); green.sort(Collections.reverseOrder()); blue.sort(Collections.reverseOrder()); dp=new int[r+1][g+1][b+1]; for(int i=0;i<=r;i++) { for(int j=0;j<=g;j++) { Arrays.fill(dp[i][j],-1); } } int res=maxArea(0,0,0); ans.append(res).append("\n"); } out.print(ans.toString()); out.flush(); } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
b934dd71bb8cead5038ee683701f38f3
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.io.*; import java.util.*; import java.util.Map.Entry; import static java.lang.Math.*; public class Ans implements Runnable { public static void main(String args[]) { Ans s = new Ans(); s.run(); } static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } InputReader sc = null; PrintWriter pw = null; static int MAXN = (int) (1e5 + 5); public void run() { // InputStream is = new FileInputStream(new File("input.txt")); sc = new InputReader(System.in); pw = new PrintWriter(System.out); int R = sc.nextInt(); int G = sc.nextInt(); int B = sc.nextInt(); long[] r = new long[R+1]; long[] b = new long[B+1]; long[] g = new long[G+1]; for(int i = 1; i <= R; i++){ r[i] = sc.nextLong(); } for(int i = 1; i <= G; i++){ g[i] = sc.nextLong(); } for(int i = 1; i <= B; i++){ b[i] = sc.nextLong(); } Arrays.sort(r, 1, R+1); Arrays.sort(g, 1, G+1); Arrays.sort(b, 1, B+1); reverse(r, 1, R); reverse(g, 1, G); reverse(b, 1, B); long[][][] dp = new long[R+1][G+1][B+1]; long ans = 0; for(int i = 0; i <= R; i++){ for(int j = 0; j <= G; j++){ for(int k = 0; k <= B; k++){ long ans1 = r[i]*g[j] + ((i > 0 && j > 0) ? dp[i-1][j-1][k] : 0); long ans2 = r[i]*b[k] + ((i > 0 && k > 0) ? dp[i-1][j][k-1] : 0); long ans3 = b[k]*g[j] + ((j > 0 && k > 0) ? dp[i][j-1][k-1] : 0); dp[i][j][k] = max(ans1, max(ans2, ans3)); ans = max(ans, dp[i][j][k]); } } } pw.println(ans); // is.close(); pw.close(); } static void reverse(long[] a, int s, int e){ while(e > s){ long temp = a[s]; a[s] = a[e]; a[e] = temp; e--;s++; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[5]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br = new BufferedReader(new InputStreamReader(stream)); String stock = ""; try { stock = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return stock; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
833c28607becaef4b9729db3d764e870
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.io.*; import java.util.*; import java.util.Map.Entry; import static java.lang.Math.*; public class Ans implements Runnable { public static void main(String args[]) { Ans s = new Ans(); s.run(); } static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } InputReader sc = null; PrintWriter pw = null; static int MAXN = (int) (1e5 + 5); public void run() { // InputStream is = new FileInputStream(new File("input.txt")); sc = new InputReader(System.in); pw = new PrintWriter(System.out); int R = sc.nextInt(); int G = sc.nextInt(); int B = sc.nextInt(); long[] r = new long[R+1]; long[] b = new long[B+1]; long[] g = new long[G+1]; for(int i = 1; i <= R; i++){ r[i] = sc.nextLong(); } for(int i = 1; i <= G; i++){ g[i] = sc.nextLong(); } for(int i = 1; i <= B; i++){ b[i] = sc.nextLong(); } Arrays.sort(r, 1, R+1); Arrays.sort(g, 1, G+1); Arrays.sort(b, 1, B+1); reverse(r, 1, R); reverse(g, 1, G); reverse(b, 1, B); long[][][] dp = new long[R+1][G+1][B+1]; long ans = 0; for(int i = 0; i <= R; i++){ for(int j = 0; j <= G; j++){ for(int k = 0; k <= B; k++){ long ans1 = r[i]*g[j] + ((i > 0 && j > 0) ? dp[i-1][j-1][k] : 0); long ans2 = r[i]*b[k] + ((i > 0 && k > 0) ? dp[i-1][j][k-1] : 0); long ans3 = b[k]*g[j] + ((j > 0 && k > 0) ? dp[i][j-1][k-1] : 0); dp[i][j][k] = max(ans1, max(ans2, ans3)); ans = max(ans, dp[i][j][k]); dp[i][j][k] = (i > 0) ? max(dp[i][j][k], dp[i-1][j][k]) : dp[i][j][k]; dp[i][j][k] = (j > 0) ? max(dp[i][j][k], dp[i][j-1][k]) : dp[i][j][k]; dp[i][j][k] = (k > 0) ? max(dp[i][j][k], dp[i][j][k-1]) : dp[i][j][k]; } } } //pw.println(ans); pw.println(dp[R][G][B]); // is.close(); pw.close(); } static void reverse(long[] a, int s, int e){ while(e > s){ long temp = a[s]; a[s] = a[e]; a[e] = temp; e--;s++; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[5]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br = new BufferedReader(new InputStreamReader(stream)); String stock = ""; try { stock = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return stock; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
71990ca068dd2357e7959c440458a86f
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.io.*; import java.util.*; public class CFJava1 { static BufferedReader in; static PrintWriter out; static StringTokenizer st; static int[][][] dp; static boolean[][][] vst; static int A, B, C; static List<Integer> al, bl, cl; static void solve(int a, int b, int c) { if (vst[a][b][c]) return; vst[a][b][c] = true; if (a > 0 && b > 0) { solve(a - 1, b - 1, c); dp[a][b][c] = Math.max(dp[a][b][c], dp[a-1][b-1][c] + al.get(a-1) * bl.get(b-1)); } if (a > 0 && c > 0) { solve(a-1, b, c-1); dp[a][b][c] = Math.max(dp[a][b][c], dp[a-1][b][c-1] + al.get(a-1) * cl.get(c-1)); } if (b > 0 && c > 0) { solve(a, b-1, c-1); dp[a][b][c] = Math.max(dp[a][b][c], dp[a][b-1][c-1] + cl.get(c-1) * bl.get(b-1)); } //System.out.println(a + " " + b + " " + c + " " + dp[a][b][c]); } public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); st = new StringTokenizer(in.readLine()); A = Integer.parseInt(st.nextToken()); B = Integer.parseInt(st.nextToken()); C = Integer.parseInt(st.nextToken()); dp = new int[A+1][B+1][C+1]; vst = new boolean[A+1][B+1][C+1]; al = new ArrayList<>(); bl = new ArrayList<>(); cl = new ArrayList<>(); for (int i = 0; i <= A; ++i) { for (int j = 0; j <= B; ++j) { for (int k = 0; k <= C; ++k) { dp[i][j][k] = 0; vst[i][j][k] = false; } } } st = new StringTokenizer(in.readLine()); for (int i = 0; i < A; ++i) al.add(Integer.parseInt(st.nextToken())); st = new StringTokenizer(in.readLine()); for (int i = 0; i < B; ++i) bl.add(Integer.parseInt(st.nextToken())); st = new StringTokenizer(in.readLine()); for (int i = 0; i < C; ++i) cl.add(Integer.parseInt(st.nextToken())); Collections.sort(al); Collections.sort(bl); Collections.sort(cl); solve(A, B, C); out.print(dp[A][B][C]); out.close(); } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
2a04ea509012acc6b4701500e5048150
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
//package Competitive; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException{ String[] arr= {"a"}; code3 obj=new code3(); obj.main(arr); } } class code3{ public static void main(String[] args) throws IOException{ Scanner sc=new Scanner(System.in); int t=1;//sc.nextInt(); while(t-->0) { int r=sc.nextInt();int g=sc.nextInt();int b=sc.nextInt(); int[]a =new int[r];int[] bg=new int[g];int[] c=new int[b]; for(int i=0;i<r;i++) { a[i]=sc.nextInt(); } for(int i=0;i<g;i++) { bg[i]=sc.nextInt(); } for(int i=0;i<b;i++) { c[i]=sc.nextInt(); } Arrays.sort(a);Arrays.sort(bg);Arrays.sort(c); reverse(a);reverse(bg);reverse(c); int[][][]dp=new int[r+1][g+1][b+1]; long ans=0; for(int i=0;i<=r;i++) { for(int j=0;j<=g;j++) { for(int k=0;k<=b;k++) { if(i<r && j<g) { dp[i+1][j+1][k]=Math.max(dp[i+1][j+1][k], dp[i][j][k]+a[i]*bg[j]); } if(i<r && k<b) { dp[i+1][j][k+1]=Math.max(dp[i+1][j][k+1], dp[i][j][k]+a[i]*c[k]); } if(j<g && k<b) { dp[i][j+1][k+1]=Math.max(dp[i][j+1][k+1], dp[i][j][k]+c[k]*bg[j]); } ans=Math.max(ans, dp[i][j][k]); } } } System.out.println(ans); } } static void reverse(int[] a) { int lo=0;int hi=a.length-1; while(lo<hi) { int temp=a[lo]; a[lo]=a[hi]; a[hi]=temp; hi--; lo++; } } } class Reader{ BufferedReader reader; Reader(){ reader=new BufferedReader(new InputStreamReader(System.in)); } int nextInt() throws IOException{ String in=reader.readLine().trim(); return Integer.parseInt(in); } long nextLong() throws IOException{ String in=reader.readLine().trim(); return Long.parseLong(in); } String next() throws IOException{ return reader.readLine().trim(); } String[] stringArray() throws IOException{ return reader.readLine().trim().split("\\s+"); } int[] intArray() throws IOException{ String[] inp=this.stringArray(); int[] arr=new int[inp.length]; int i=0; for(String s:inp) { arr[i++]=Integer.parseInt(s); } return arr; } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
21ae2966db8df26cb67d69b6e2317fe2
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.util.*; import java.io.*; public class R665D{ public static void main(String[] main) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); PrintWriter out = new PrintWriter(System.out); int R = Integer.parseInt(st.nextToken()); int G = Integer.parseInt(st.nextToken()); int B = Integer.parseInt(st.nextToken()); int[] r = new int[R]; int[] g = new int[G]; int[] b = new int[B]; st = new StringTokenizer(br.readLine()); for(int i = 0; i < R; i++) r[i] = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); for(int i = 0; i < G; i++) g[i] = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); for(int i = 0; i < B; i++) b[i] = Integer.parseInt(st.nextToken()); Arrays.sort(r); Arrays.sort(g); Arrays.sort(b); long[][][] dp = new long[R+1][G+1][B+1]; for(int i = 1; i <= R; i++) { for(int j = 1; j <= G; j++) { dp[i][j][0] = dp[i-1][j-1][0] + r[i-1]*g[j-1]; } } for(int i = 1; i <= R; i++) { for(int j = 1; j <= B; j++) { dp[i][0][j] = dp[i-1][0][j-1] + r[i-1]*b[j-1]; } } for(int i = 1; i <= B; i++) { for(int j = 1; j <= G; j++) { dp[0][j][i] = dp[0][j-1][i-1] + b[i-1]*g[j-1]; } } for(int i = 1; i <= R; i++) { for(int j = 1; j <= G; j++) { for(int k = 1; k <= B; k++) { dp[i][j][k] = dp[i-1][j-1][k] + r[i-1]*g[j-1]; dp[i][j][k] = Math.max(dp[i][j][k], dp[i-1][j][k-1] + r[i-1]*b[k-1]); dp[i][j][k] = Math.max(dp[i][j][k], dp[i][j-1][k-1] + g[j-1]*b[k-1]); } } } out.println(dp[R][G][B]); out.close(); } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
709f7c9cde87ac387da9e9d8f2f794ef
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class ColoredRectangles { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int R = sc.nextInt(); int G = sc.nextInt(); int B = sc.nextInt(); int[] r = new int[R]; for (int i = 0; i < R; i++) r[i] = sc.nextInt(); int[] g = new int[G]; for (int i = 0; i < G; i++) g[i] = sc.nextInt(); int[] b = new int[B]; for (int i = 0; i < B; i++) b[i] = sc.nextInt(); solve(r, g, b, R, G, B); } static int[][][] dp = new int[201][201][201]; private static void solve(int[] r, int[] g, int[] b, int R, int G, int B) { // TODO Auto-generated method stub Arrays.sort(r); Arrays.sort(g); Arrays.sort(b); for (int i = R; i >= 0; i--) { for (int j = G; j >= 0; j--) { for (int h = B; h >= 0; h--) { if (i > 0 && j > 0) dp[i - 1][j - 1][h] = Math.max(dp[i - 1][j - 1][h], dp[i][j][h] + r[i - 1] * g[j - 1]); if (i > 0 && h > 0) dp[i - 1][j][h - 1] = Math.max(dp[i - 1][j][h - 1], dp[i][j][h] + r[i - 1] * b[h - 1]); if (j > 0 && h > 0) dp[i][j - 1][h - 1] = Math.max(dp[i][j - 1][h - 1], dp[i][j][h] + g[j - 1] * b[h - 1]); } } } int ans = 0; for (int i = 0; i <= R; i++) for (int j = 0; j <= G; j++) for (int h = 0; h <= B; h++) ans = Math.max(ans, dp[i][j][h]); System.out.println(ans); } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
896e7164b80dafc7d51be9e954d9fc60
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class ColoredRectangles { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int R = sc.nextInt(); int G = sc.nextInt(); int B = sc.nextInt(); int[] r = new int[R]; for (int i = 0; i < R; i++) r[i] = sc.nextInt(); int[] g = new int[G]; for (int i = 0; i < G; i++) g[i] = sc.nextInt(); int[] b = new int[B]; for (int i = 0; i < B; i++) b[i] = sc.nextInt(); solve(r, g, b, R, G, B); } static int[][][] dp = new int[201][201][201]; private static void solve(int[] r, int[] g, int[] b, int R, int G, int B) { // TODO Auto-generated method stub Arrays.sort(r); Arrays.sort(g); Arrays.sort(b); for (int i = R; i >= 0; i--) { for (int j = G; j >= 0; j--) { for (int k = B; k >= 0; k--) { if (i > 0 && j > 0) dp[i - 1][j - 1][k] = Math.max(dp[i - 1][j - 1][k], dp[i][j][k] + r[i - 1] * g[j - 1]); if (i > 0 && k > 0) dp[i - 1][j][k - 1] = Math.max(dp[i - 1][j][k - 1], dp[i][j][k] + r[i - 1] * b[k - 1]); if (j > 0 && k > 0) dp[i][j - 1][k - 1] = Math.max(dp[i][j - 1][k - 1], dp[i][j][k] + g[j - 1] * b[k - 1]); } } } int ans = 0; for (int i = 0; i <= R; i++) for (int j = 0; j <= G; j++) for (int k = 0; k <= B; k++) ans = Math.max(ans, dp[i][j][k]); System.out.println(ans); } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
83abdca4f293f62e5d81d117b6322f4b
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; import static java.lang.Math.*; public class Main { static final int mod = (int)1e9+7; static Long[] arrR, arrG, arrB; static long[][][] dp; public static void main(String[] args) throws Exception { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int test = 1; while(test-- > 0) { int r = in.nextInt(); int g = in.nextInt(); int b = in.nextInt(); arrR = new Long[r]; arrG = new Long[g]; arrB = new Long[b]; for(int i = 0; i < r; i++) { arrR[i] = in.nextLong(); } for(int i = 0; i < g; i++) { arrG[i] = in.nextLong(); } for(int i = 0; i < b; i++) { arrB[i] = in.nextLong(); } Arrays.sort(arrR, Collections.reverseOrder()); Arrays.sort(arrG, Collections.reverseOrder()); Arrays.sort(arrB, Collections.reverseOrder()); dp = new long[201][201][201]; for(int i = 0; i < 201; i++) { for(int j = 0; j < 201; j++) { Arrays.fill(dp[i][j], -1); } } solve(0, 0, 0); out.println(dp[0][0][0]); } out.flush(); } static long solve(int i, int j, int k) { if(dp[i][j][k] != -1) { return dp[i][j][k]; } int r = arrR.length; int g = arrG.length; int b = arrB.length; dp[i][j][k] = 0; if(i != r && j != g) { dp[i][j][k] = max(dp[i][j][k], solve(i + 1, j + 1, k) + arrR[i] * arrG[j]); } if(i != r && k != b) { dp[i][j][k] = max(dp[i][j][k], solve(i + 1, j, k + 1) + arrR[i] * arrB[k]); } if(j != g && k != b) { dp[i][j][k] = max(dp[i][j][k], solve(i, j + 1, k + 1) + arrG[j] * arrB[k]); } return dp[i][j][k]; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { if(st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public boolean hasNext() throws IOException { if(st != null && st.hasMoreElements()) { return true; } String s = br.readLine(); if(s == null) { return false; } st = new StringTokenizer(s); return st.hasMoreElements(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
3b415444010e76b6c0496dd1870ac041
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class pb4 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int R = in.nextInt(); int G = in.nextInt(); int B = in.nextInt(); ArrayList<Integer> ra = new ArrayList<>(), ga = new ArrayList<>(), ba = new ArrayList<>(); for (int i = 0; i < R; i++) ra.add(in.nextInt()); for (int i = 0; i < G; i++) ga.add(in.nextInt()); for (int i = 0; i < B; i++) ba.add(in.nextInt()); in.close(); Collections.sort(ra, Collections.reverseOrder()); Collections.sort(ga, Collections.reverseOrder()); Collections.sort(ba, Collections.reverseOrder()); long[][][] dp = new long[R+1][G+1][B+1]; for (long[][] x1 : dp) for (long[] x2 : x1) Arrays.fill(x2, -100000000); dp[0][0][0] = 0; long ans = 0; for (int r = 0; r <= R; r++) { for (int g = 0; g <= G; g++) { for (int b = 0; b <= B; b++) { if (r > 0 && g > 0) { dp[r][g][b] = Math.max(dp[r][g][b], dp[r-1][g-1][b]+ra.get(r-1)*ga.get(g-1)); ans = Math.max(ans, dp[r][g][b]); } if (r > 0 && b > 0) { dp[r][g][b] = Math.max(dp[r][g][b], dp[r-1][g][b-1]+ra.get(r-1)*ba.get(b-1)); ans = Math.max(ans, dp[r][g][b]); } if (g > 0 && b > 0) { dp[r][g][b] = Math.max(dp[r][g][b], dp[r][g-1][b-1]+ga.get(g-1)*ba.get(b-1)); ans = Math.max(ans, dp[r][g][b]); } } } } System.out.println(ans); } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
4ecdefb0baed55fecb34d77d6404bf91
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
/** * author : stdnoob * created : 2020-08-20 23:04:45 **/ // This code will help stdnoob to become red coder on codeforces and CEO of his dream company // import java.util.*; import java.io.*; import java.lang.*; public class codeforces { static int R,G,B; static Integer r[],g[],b[]; static long dp[][][]; static long maxarea(int x,int y,int z) { if(dp[x][y][z]!=-1) return dp[x][y][z]; long ans = 0; if(x<R && y<G) ans = Math.max(ans,r[x]*g[y]+maxarea(x+1,y+1,z)); if(x<R && z<B) ans = Math.max(ans,r[x]*b[z]+maxarea(x+1,y,z+1)); if(y<G && z<B) ans = Math.max(ans,g[y]*b[z]+maxarea(x,y+1,z+1)); return dp[x][y][z]=ans; } public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); //int t = Integer.parseInt(br.readLine()); //while(t-- != 0) //{ String s1[] = (br.readLine()).split(" "); String s2[] = (br.readLine()).split(" "); String s3[] = (br.readLine()).split(" "); String s4[] = (br.readLine()).split(" "); R = Integer.parseInt(s1[0]); G = Integer.parseInt(s1[1]); B = Integer.parseInt(s1[2]); r = new Integer[R]; g = new Integer[G]; b = new Integer[B]; dp = new long[201][201][201]; for(int i=0;i<R;i++) r[i] = Integer.parseInt(s2[i]); for(int i=0;i<G;i++) g[i] = Integer.parseInt(s3[i]); for(int i=0;i<B;i++) b[i] = Integer.parseInt(s4[i]); Arrays.sort(r,Collections.reverseOrder()); Arrays.sort(g,Collections.reverseOrder()); Arrays.sort(b,Collections.reverseOrder()); for (long[][] x : dp) { for (long[] y : x) Arrays.fill(y, -1); } pw.println(maxarea(0,0,0)); //} pw.flush(); } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
9e380d2d31fc52d11abd39535be43c50
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class NewProblem { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sToken = new StringTokenizer(reader.readLine()); int R = Integer.parseInt(sToken.nextToken()); int G = Integer.parseInt(sToken.nextToken()); int B = Integer.parseInt(sToken.nextToken()); int[] r = new int[R+1]; sToken = new StringTokenizer(reader.readLine()); for (int i=1; i<=R; i++) r[i] = Integer.parseInt(sToken.nextToken()); int[] g = new int[G+1]; sToken = new StringTokenizer(reader.readLine()); for (int i=1; i<=G; i++) g[i] = Integer.parseInt(sToken.nextToken()); int[] b = new int[B+1]; sToken = new StringTokenizer(reader.readLine()); for (int i=1; i<=B; i++) b[i] = Integer.parseInt(sToken.nextToken()); Arrays.sort(r); Arrays.sort(g); Arrays.sort(b); long[][][] res = new long[R+1][G+1][B+1]; long ans = 0; for (int i=0; i<=R; i++) { for (int j=0; j<=G; j++) { for (int k=0; k<=B; k++) { long rg = r[i] * g[j], gb = g[j] * b[k], rb = r[i] * b[k]; if (i>0 && j>0 && k>0) { res[i][j][k] = Math.max(res[i][j][k], res[i-1][j-1][k] + rg); res[i][j][k] = Math.max(res[i][j][k], res[i][j-1][k-1] + gb); res[i][j][k] = Math.max(res[i][j][k], res[i-1][j][k-1] + rb); } else if (i>0 && j>0 && k==0) res[i][j][k] = res[i-1][j-1][k] + rg; else if (i==0 && j>0 && k>0) res[i][j][k] = res[i][j-1][k-1] + gb; else if (i>0 && j==0 && k>0) res[i][j][k] = res[i-1][j][k-1] + rb; ans = Math.max(ans, res[i][j][k]); } } } System.out.println(ans); } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
5ff2e80c825767eb57b35cd1a766b96f
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /* * Copyright (c) --> Arpit * Date Created : 15/8/2020 * Have A Good Day ! */ /** * Built using CHelper plug-in * Actual solution is at the top * * @author Arpit */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); OutputWriter out = new OutputWriter(outputStream); DColoredRectangles solver = new DColoredRectangles(); solver.solve(1, in, out); out.close(); } static class DColoredRectangles { long[][][] dp; Long[] red; Long[] green; Long[] blue; int R; int G; int B; long mx(long a, long b) { return Math.max(a, b); } long REC(int r, int g, int b) { if (dp[r][g][b] != -1) return dp[r][g][b]; long x = 0, y = 0, z = 0; if (r >= 1 && g >= 1) x = red[r] * green[g] + REC(r - 1, g - 1, b); if (r >= 1 && b >= 1) y = red[r] * blue[b] + REC(r - 1, g, b - 1); if (g >= 1 && b >= 1) z = green[g] * blue[b] + REC(r, g - 1, b - 1); return dp[r][g][b] = mx(x, mx(y, z)); } public void solve(int testNumber, FastReader r, OutputWriter out) { R = r.nextInt(); G = r.nextInt(); B = r.nextInt(); red = new Long[R + 1]; for (int i = 1; i <= R; i++) red[i] = r.nextLong(); green = new Long[G + 1]; for (int i = 1; i <= G; i++) green[i] = r.nextLong(); blue = new Long[B + 1]; for (int i = 1; i <= B; i++) blue[i] = r.nextLong(); dp = new long[R + 1][G + 1][B + 1]; for (long[][] row : dp) for (long[] col : row) Arrays.fill(col, -1); dp[0][0][0] = 0; Arrays.sort(red, 1, 1 + R); Arrays.sort(green, 1, 1 + G); Arrays.sort(blue, 1, 1 + B); out.println(REC(R, G, B)); } } 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 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 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); } } 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++) { writer.print(objects[i]); if (i != objects.length - 1) writer.print(" "); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
539a8558c0bc4c7fad73a2c156354df2
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /* * Copyright (c) --> Arpit * Date Created : 15/8/2020 * Have A Good Day ! */ /** * Built using CHelper plug-in * Actual solution is at the top * * @author Arpit */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); OutputWriter out = new OutputWriter(outputStream); DColoredRectangles solver = new DColoredRectangles(); solver.solve(1, in, out); out.close(); } static class DColoredRectangles { long[][][] dp; Long[] red; Long[] green; Long[] blue; int R; int G; int B; long mx(long a, long b) { return Math.max(a, b); } boolean check(int r, int g, int b) { return ((r >= 1 && g >= 1) || (r >= 1 && b >= 1) || (b >= 1 && g >= 1)); } long REC(int r, int g, int b) { if (!check(r, g, b)) return 0; if (dp[r][g][b] != -1) return dp[r][g][b]; long x = 0, y = 0, z = 0; if (r >= 1 && g >= 1) x = red[r] * green[g] + REC(r - 1, g - 1, b); if (r >= 1 && b >= 1) y = red[r] * blue[b] + REC(r - 1, g, b - 1); if (g >= 1 && b >= 1) z = green[g] * blue[b] + REC(r, g - 1, b - 1); return dp[r][g][b] = mx(x, mx(y, z)); } public void solve(int testNumber, FastReader r, OutputWriter out) { R = r.nextInt(); G = r.nextInt(); B = r.nextInt(); red = new Long[R + 1]; for (int i = 1; i <= R; i++) red[i] = r.nextLong(); green = new Long[G + 1]; for (int i = 1; i <= G; i++) green[i] = r.nextLong(); blue = new Long[B + 1]; for (int i = 1; i <= B; i++) blue[i] = r.nextLong(); dp = new long[R + 1][G + 1][B + 1]; for (long[][] row : dp) for (long[] col : row) Arrays.fill(col, -1); Arrays.sort(red, 1, 1 + R); Arrays.sort(green, 1, 1 + G); Arrays.sort(blue, 1, 1 + B); out.println(REC(R, G, B)); } } 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 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 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); } } 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++) { writer.print(objects[i]); if (i != objects.length - 1) writer.print(" "); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
6fa242ab5252d277f093b7b0115d82ae
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.util.*; import java.io.*; public class D_1398 { static int a, b, c; static int[] ar1, ar2, ar3; static long[][][] memo; public static long dp(int i, int j, int k) { int z = (i == a ? 0 : 1) + (j == b ? 0 : 1) + (k == c ? 0 : 1); if(z <= 1) return 0; if(memo[i][j][k] != -1) return memo[i][j][k]; long max = 0; if(i == a) { int x = ar2[j], y = ar3[k]; max = Math.max(max, 1l * x * y + dp(i, j + 1, k + 1)); } else if(j == b) { int x = ar1[i], y = ar3[k]; max = Math.max(max, 1l * x * y + dp(i + 1, j, k + 1)); } else if(k == c) { int x = ar1[i], y = ar2[j]; max = Math.max(max, 1l * x * y + dp(i + 1, j + 1, k)); } else { long max1 = 1l * ar1[i] * ar2[j] + dp(i + 1, j + 1, k); long max2 = 1l * ar2[j] * ar3[k] + dp(i, j + 1, k + 1); long max3 = 1l * ar1[i] * ar3[k] + dp(i + 1, j, k + 1); max = Math.max(max1, Math.max(max2, max3)); } return memo[i][j][k] = max; } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); a = sc.nextInt(); b = sc.nextInt(); c = sc.nextInt(); PriorityQueue<Integer> qu1 = new PriorityQueue<>(Collections.reverseOrder()); PriorityQueue<Integer> qu2 = new PriorityQueue<>(Collections.reverseOrder()); PriorityQueue<Integer> qu3 = new PriorityQueue<>(Collections.reverseOrder()); for(int i = 0; i < a; i++) qu1.add(sc.nextInt()); for(int i = 0; i < b; i++) qu2.add(sc.nextInt()); for(int i = 0; i < c; i++) qu3.add(sc.nextInt()); ar1 = new int[a]; ar2 = new int[b]; ar3 = new int[c]; for(int i = 0; i < a; i++) ar1[i] = qu1.poll(); for(int i = 0; i < b; i++) ar2[i] = qu2.poll(); for(int i = 0; i < c; i++) ar3[i] = qu3.poll(); memo = new long[a + 1][b + 1][c + 1]; for(long[][] i : memo) for(long[] j : i) Arrays.fill(j, -1); pw.println(dp(0, 0, 0)); pw.flush(); } public static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public int[] nextIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) array[i] = new Integer(nextInt()); return array; } public long[] nextLongArray(int n) throws IOException { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public double[] nextDoubleArray(int n) throws IOException { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } public static int[] shuffle(int[] a) { int n = a.length; Random rand = new Random(); for (int i = 0; i < n; i++) { int tmpIdx = rand.nextInt(n); int tmp = a[i]; a[i] = a[tmpIdx]; a[tmpIdx] = tmp; } return a; } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
d61d381f0cebf40b2652ea1801e81005
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.io.*; import java.util.*; public class TaskD { public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter pw = new PrintWriter(System.out); try { int n = sc.nextInt(); int m = sc.nextInt(); int o = sc.nextInt(); int[] a = new int[n]; for(int i=0 ; i<n ; i++) { a[i] = sc.nextInt(); } int[] b = new int[m]; for(int i=0 ; i<m ; i++) { b[i] = sc.nextInt(); } int[] c = new int[o]; for(int i=0 ; i<o ; i++) { c[i] = sc.nextInt(); } Arrays.sort(a); Arrays.sort(b); Arrays.sort(c); for(int i=0 ; i<n/2 ; i++) { int temp = a[i]; a[i] = a[n-i-1]; a[n-i-1] = temp; } for(int i=0 ; i<m/2 ; i++) { int temp = b[i]; b[i] = b[m-i-1]; b[m-i-1] = temp; } for(int i=0 ; i<o/2 ; i++) { int temp = c[i]; c[i] = c[o-i-1]; c[o-i-1] = temp; } long[][][] dp = new long[n+1][m+1][o+1]; long ans = 0; for(int j=1 ; j<=m ; j++) { for(int k=1 ; k<=o ; k++) { dp[0][j][k] = dp[0][j-1][k-1] + b[j-1]*c[k-1]; ans = Math.max(dp[0][j][k], ans); } } for(int i=1 ; i<=n ; i++) { for(int k=1 ; k<=o ; k++) { dp[i][0][k] = dp[i-1][0][k-1] + a[i-1]*c[k-1]; ans = Math.max(dp[i][0][k], ans); } } for(int i=1 ; i<=n ; i++) { for(int j=1 ; j<=m ; j++) { dp[i][j][0] = dp[i-1][j-1][0] + a[i-1]*b[j-1]; ans = Math.max(dp[i][j][0], ans); } } for(int i=1 ; i<=n ; i++) { for(int j=1 ; j<=m ; j++) { for(int k=1 ; k<=o ; k++) { dp[i][j][k] = Math.max(dp[i][j][k], dp[i-1][j-1][k] + a[i-1]*b[j-1]); dp[i][j][k] = Math.max(dp[i][j][k], dp[i][j-1][k-1] + b[j-1]*c[k-1]); dp[i][j][k] = Math.max(dp[i][j][k], dp[i-1][j][k-1] + a[i-1]*c[k-1]); ans = Math.max(dp[i][j][k], ans); } } } pw.println(ans); } finally { pw.flush(); pw.close(); } } static class Reader { 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 io) { io.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
f2406a5ea79b264409ceb4aa9f13c3f4
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.util.*; import java.io.*; public class Main { static Scanner sc = new Scanner(System.in); static PrintWriter out = new PrintWriter(System.out); static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); long mod = 998244353; public static void main(String[] args){ Main main = new Main(); int t = 1; while(t-->0){ main.solve(); } out.flush(); } void solve(){ int R = sc.nextInt(), G = sc.nextInt(), B = sc.nextInt(); long[][][] dp = new long[R+1][G+1][B+1]; long[] red = new long[R+1], green = new long[G+1], blue = new long[B+1]; for(int i=1; i<=R; i++) red[i] = sc.nextLong(); for(int i=1; i<=G; i++) green[i] = sc.nextLong(); for(int i=1; i<=B; i++) blue[i] = sc.nextLong(); Arrays.sort(red); Arrays.sort(green); Arrays.sort(blue); for(int i=0; i<=R; i++){ for(int j=0; j<=G; j++){ for(int k=0; k<=B; k++){ long temp = 0; if(i>0&&j>0) temp = Math.max(temp, dp[i-1][j-1][k]+red[i]*green[j]); if(j>0&&k>0) temp = Math.max(temp, dp[i][j-1][k-1]+green[j]*blue[k]); if(k>0&&i>0) temp = Math.max(temp, dp[i-1][j][k-1]+blue[k]*red[i]); if(i>0) temp = Math.max(temp, dp[i-1][j][k]); if(j>0) temp = Math.max(temp, dp[i][j-1][k]); if(k>0) temp = Math.max(temp, dp[i][j][k-1]); dp[i][j][k] = temp; } } } out.println(dp[R][G][B]); } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
af5c2e198f7f28a42492de2776b7ec02
train_002.jsonl
1597415700
You are given three multisets of pairs of colored sticks: $$$R$$$ pairs of red sticks, the first pair has length $$$r_1$$$, the second pair has length $$$r_2$$$, $$$\dots$$$, the $$$R$$$-th pair has length $$$r_R$$$; $$$G$$$ pairs of green sticks, the first pair has length $$$g_1$$$, the second pair has length $$$g_2$$$, $$$\dots$$$, the $$$G$$$-th pair has length $$$g_G$$$; $$$B$$$ pairs of blue sticks, the first pair has length $$$b_1$$$, the second pair has length $$$b_2$$$, $$$\dots$$$, the $$$B$$$-th pair has length $$$b_B$$$; You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.*; import java.math.*; public class Main { InputStream is; PrintWriter out; String INPUT = ""; //class Declaration static class pair implements Comparable < pair > { long x; long y; pair(long i, long j) { x = i; y = j; } public int compareTo(pair p) { if (this.x != p.x) { return Long.compare(this.x,p.x); } else { return Long.compare(this.y,p.y); } } public int hashCode() { return (x + " " + y).hashCode(); } public String toString() { return x + " " + y; } public boolean equals(Object o) { pair x = (pair) o; return (x.x == this.x && x.y == this.y); } } // int[] dx = {0,0,1,-1}; // int[] dy = {1,-1,0,0}; // int[] ddx = {0,0,1,-1,1,-1,1,-1}; // int[] ddy = {1,-1,0,0,1,-1,-1,1}; int inf = (int) 1e9 + 9; long biginf = (long)1e18 + 7 ; long mod = (long)1e9+7; void solve() throws Exception { int R=ni(),G=ni(),B=ni(); ArrayList<Integer> r = new ArrayList<>(); ArrayList<Integer> g = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); for(int i=0;i<R;++i) r.add(ni()); for(int i=0;i<G;++i) g.add(ni()); for(int i=0;i<B;++i) b.add(ni()); Collections.sort(r,Collections.reverseOrder()); Collections.sort(g,Collections.reverseOrder()); Collections.sort(b,Collections.reverseOrder()); int[][][] dp= new int[R+1][G+1][B+1]; //for(int i=0;i<R;++i) for(int j=0;j<G;++j) Arrays.fill(dp[i][j],Integer.MAX_VALUE); int ans = 0; for(int i=0;i<R+1;++i){ for(int j=0;j<G+1;++j){ for(int k=0;k<B+1;++k){ if(i<R && j<G) dp[i+1][j+1][k] = Math.max(dp[i][j][k] + r.get(i)*g.get(j),dp[i+1][j+1][k]); if(i<R && k<B) dp[i+1][j][k+1] = Math.max(dp[i][j][k] + r.get(i)*b.get(k),dp[i+1][j][k+1]); if(j<G && k<B) dp[i][j+1][k+1] = Math.max(dp[i][j][k] + g.get(j)*b.get(k),dp[i][j+1][k+1]); ans = Math.max(ans,dp[i][j][k]); } } } pn(ans); } long pow(long a, long b) { long result = 1; while (b > 0) { if (b % 2 == 1) result = (result * a) % mod; b /= 2; a = (a * a) % mod; } return result; } void print(Object o) { System.out.println(o); System.out.flush(); } long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Main().run(); } //output methods private void pn(Object o) { out.println(o); } private void p(Object o) { out.print(o); } private ArrayList < ArrayList < Integer >> ng(int n, int e) { ArrayList < ArrayList < Integer >> g = new ArrayList < > (); for (int i = 0; i <= n; ++i) g.add(new ArrayList < > ()); for (int i = 0; i < e; ++i) { int u = ni(), v = ni(); g.get(u).add(v); g.get(v).add(u); } return g; } private ArrayList < ArrayList < pair >> nwg(int n, int e) { ArrayList < ArrayList < pair >> g = new ArrayList < > (); for (int i = 0; i <= n; ++i) g.add(new ArrayList < > ()); for (int i = 0; i < e; ++i) { int u = ni(), v = ni(), w = ni(); g.get(u).add(new pair(w, v)); g.get(v).add(new pair(w, u)); } return g; } //input methods private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private void tr(Object...o) { if (INPUT.length() > 0) System.out.println(Arrays.deepToString(o)); } void watch(Object...a) throws Exception { int i = 1; print("watch starts :"); for (Object o: a) { //print(o); boolean notfound = true; if (o.getClass().isArray()) { String type = o.getClass().getName().toString(); //print("type is "+type); switch (type) { case "[I": { int[] test = (int[]) o; print(i + " " + Arrays.toString(test)); break; } case "[[I": { int[][] obj = (int[][]) o; print(i + " " + Arrays.deepToString(obj)); break; } case "[J": { long[] obj = (long[]) o; print(i + " " + Arrays.toString(obj)); break; } case "[[J": { long[][] obj = (long[][]) o; print(i + " " + Arrays.deepToString(obj)); break; } case "[D": { double[] obj = (double[]) o; print(i + " " + Arrays.toString(obj)); break; } case "[[D": { double[][] obj = (double[][]) o; print(i + " " + Arrays.deepToString(obj)); break; } case "[Ljava.lang.String": { String[] obj = (String[]) o; print(i + " " + Arrays.toString(obj)); break; } case "[[Ljava.lang.String": { String[][] obj = (String[][]) o; print(i + " " + Arrays.deepToString(obj)); break; } case "[C": { char[] obj = (char[]) o; print(i + " " + Arrays.toString(obj)); break; } case "[[C": { char[][] obj = (char[][]) o; print(i + " " + Arrays.deepToString(obj)); break; } default: { print(i + " type not identified"); break; } } notfound = false; } if (o.getClass() == ArrayList.class) { print(i + " al: " + o); notfound = false; } if (o.getClass() == HashSet.class) { print(i + " hs: " + o); notfound = false; } if (o.getClass() == TreeSet.class) { print(i + " ts: " + o); notfound = false; } if (o.getClass() == TreeMap.class) { print(i + " tm: " + o); notfound = false; } if (o.getClass() == HashMap.class) { print(i + " hm: " + o); notfound = false; } if (o.getClass() == LinkedList.class) { print(i + " ll: " + o); notfound = false; } if (o.getClass() == PriorityQueue.class) { print(i + " pq : " + o); notfound = false; } if (o.getClass() == pair.class) { print(i + " pq : " + o); notfound = false; } if (notfound) { print(i + " unknown: " + o); } i++; } print("watch ends "); } }
Java
["1 1 1\n3\n5\n4", "2 1 3\n9 5\n1\n2 8 5", "10 1 1\n11 7 20 15 19 14 2 4 13 14\n8\n11"]
2 seconds
["20", "99", "372"]
NoteIn the first example you can construct one of these rectangles: red and green with sides $$$3$$$ and $$$5$$$, red and blue with sides $$$3$$$ and $$$4$$$ and green and blue with sides $$$5$$$ and $$$4$$$. The best area of them is $$$4 \times 5 = 20$$$.In the second example the best rectangles are: red/blue $$$9 \times 8$$$, red/blue $$$5 \times 5$$$, green/blue $$$2 \times 1$$$. So the total area is $$$72 + 25 + 2 = 99$$$.In the third example the best rectangles are: red/green $$$19 \times 8$$$ and red/blue $$$20 \times 11$$$. The total area is $$$152 + 220 = 372$$$. Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
Java 8
standard input
[ "dp", "sortings", "greedy" ]
9e6cb1e8037e1d35b6c6fe43f805a22f
The first line contains three integers $$$R$$$, $$$G$$$, $$$B$$$ ($$$1 \le R, G, B \le 200$$$) — the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks. The second line contains $$$R$$$ integers $$$r_1, r_2, \dots, r_R$$$ ($$$1 \le r_i \le 2000$$$) — the lengths of sticks in each pair of red sticks. The third line contains $$$G$$$ integers $$$g_1, g_2, \dots, g_G$$$ ($$$1 \le g_i \le 2000$$$) — the lengths of sticks in each pair of green sticks. The fourth line contains $$$B$$$ integers $$$b_1, b_2, \dots, b_B$$$ ($$$1 \le b_i \le 2000$$$) — the lengths of sticks in each pair of blue sticks.
1,800
Print the maximum possible total area of the constructed rectangles.
standard output
PASSED
245f0cacb284edebb6129bc23ce6be20
train_002.jsonl
1432658100
What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercase English letters. What is double strange that a phone number can be associated with several bears!In that country there is a rock band called CF consisting of n bears (including Mike) numbered from 1 to n. Phone number of i-th member of CF is si. May 17th is a holiday named Phone Calls day. In the last Phone Calls day, everyone called all the numbers that are substrings of his/her number (one may call some number several times). In particular, everyone called himself (that was really strange country).Denote as call(i, j) the number of times that i-th member of CF called the j-th member of CF. The geek Mike has q questions that he wants to ask you. In each question he gives you numbers l, r and k and you should tell him the number
256 megabytes
import java.io.*; import java.util.*; public class R305qGMikeAndFriends { static int n,N,Q; static StringBuilder S; static int start[], len[]; static int sa[], rank[], lcp[]; static int logs[],z[][]; static Query q[]; static int[] ans; static BIT bit; public static void main(String args[]) { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); N = in.nextInt(); Q = in.nextInt(); S = new StringBuilder(""); len = new int[N + 1]; for (int i = 1; i <= N; i++) { int x = in.snext(); while (in.isSpaceChar(x)) x = in.snext(); while (!in.isSpaceChar(x)) { S.append((char) x); len[i]++; x = in.snext(); } S.append('$'); } n = S.length(); start = new int[N + 2]; int which = 1; for (int i = 0; i < n; i++) { if (S.charAt(i) == '$') { which++; start[which] = i + 1; } } // System.out.println("S " + S); // System.out.println("start " + Arrays.toString(start)); buildSA(); buildLCP(); buildLogs(); buildSparseTableMin(); // System.out.println("sa " + Arrays.toString(sa)); // System.out.println("lcp " + Arrays.toString(lcp)); // System.out.println("rank " + Arrays.toString(rank)); q = new Query[2*Q]; for(int q=0;q<Q;q++) go(in.nextInt(),in.nextInt(),in.nextInt(),q); solveQueriesOffline(); for(int q=0;q<Q;q++) w.println(ans[2*q + 1] - ans[2*q]); w.close(); } public static void solveQueriesOffline(){ //System.out.println("OrderedQuer " + Arrays.deepToString(q)); Arrays.sort(q); //System.out.println("SortedQuer " + Arrays.deepToString(q)); ans = new int[2*Q]; int curr = 0; bit = new BIT(n); for(int i=0;i<2*Q;i++){ while(curr < q[i].k){ ++curr; for(int j=start[curr];j<start[curr+1] - 1;j++){ //System.out.println(rank[j]); bit.update(rank[j] + 1); } } //System.out.println(q[i].l + " " + q[i].r); ans[q[i].idx] = bit.query(q[i].l + 1, q[i].r + 1); } } public static void go(int l,int r,int k,int id){ int idx = rank[start[k]]; //System.out.println(id + " " + idx); int right = -1,start = idx,end = n,mid; while(start < end){ mid = (start + end) >> 1; if(queryMin(idx,mid) >= len[k]){ right = mid; start = mid + 1; } else end = mid; } int left = -1; start = 0; end = idx; while(start < end){ mid = (start + end) >> 1; if(queryMin(mid,idx-1) >= len[k]) left = end = mid; else start = mid + 1; } if(right != -1 && right != n-1) right++; if(left == -1) left = idx; if(right == -1) right = idx; q[2*id] = new Query(left,right,l-1,2*id); //System.out.println(2*id + " " + q[2*id]); q[2*id + 1] = new Query(left,right,r,2*id+1); //System.out.println((2*id + 1) + " " + q[2*id + 1]); } public static void buildLogs(){ logs = new int[n+1]; int g=2; while(g <= n){ logs[g] = 1; g = (g << 1); } for(int i=1;i<=n;i++) logs[i] += logs[i - 1]; } public static void buildSparseTableMin(){ int p,g; z = new int[21][n]; for(int i=0;i<n;i++) z[0][i] = lcp[i]; for (int j = 1; (1<<j) < n; j++) for (int i = 0; i + (1<<j) <= n; i++){ g=z[j-1][i]; p=z[j-1][i + (1<<(j-1))]; z[j][i]= g<p?g:p; } } public static int queryMin(int l,int r){ int k = logs[r - l + 1]; return z[k][l] <= z[k][r - (1<<k) + 1] ? z[k][l] : z[k][r - (1<<k) + 1]; } public static void buildSA() { Integer[] order = new Integer[n]; for (int i = 0; i < n; i++) order[i] = n - 1 - i; Arrays.sort(order, new myComp()); sa = new int[n]; int[] classes = new int[n]; for (int i = 0; i < n; i++) { sa[i] = order[i]; classes[i] = S.charAt(i); } for (int len = 1; len < n; len *= 2) { int[] c = classes.clone(); for (int i = 0; i < n; i++) classes[sa[i]] = i > 0 && c[sa[i - 1]] == c[sa[i]] && sa[i - 1] + len < n && c[sa[i - 1] + len / 2] == c[sa[i] + len / 2] ? classes[sa[i - 1]] : i; int[] cnt = new int[n]; for (int i = 0; i < n; i++) cnt[i] = i; int[] s = sa.clone(); for (int i = 0; i < n; i++) { int s1 = s[i] - len; if (s1 >= 0) sa[cnt[classes[s1]]++] = s1; } } } public static void buildLCP() { rank = new int[n]; for (int i = 0; i < n; i++) rank[sa[i]] = i; lcp = new int[n]; for (int i = 0, h = 0; i < n; i++) { if (rank[i] < n - 1) { for (int j = sa[rank[i] + 1]; Math.max(i, j) + h < S.length() && S.charAt(i + h) == S.charAt(j + h); ++h) ; lcp[rank[i]] = h; if (h > 0) --h; } } } public static class myComp implements Comparator<Integer> { public int compare(Integer a, Integer b) { return Character.compare(S.charAt(a), S.charAt(b)); } } public static class BIT{ int tree[]; BIT(int n){ tree = new int[n + 1]; } void update(int idx){ while(idx <= n){ tree[idx]++; idx += (idx & -idx); } } int query(int l,int r){ return read(r) - read(l-1); } int read(int idx){ int sum = 0; while(idx > 0){ sum += tree[idx]; idx -= (idx & -idx); } return sum; } } //number of elements from l to r less than <= k public static class Query implements Comparable<Query>{ int l,r,k,idx; Query(int l,int r,int k,int idx){ this.idx = idx; this.l = l; this.r = r; this.k = k; } public int compareTo(Query o){ return Integer.compare(k, o.k); } public String toString(){ return l + " " + r + " " + k + " " + idx; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int 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 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 boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["5 5\na\nab\nabab\nababab\nb\n1 5 1\n3 5 1\n1 5 2\n1 5 3\n1 4 5"]
3 seconds
["7\n5\n6\n3\n6"]
null
Java 7
standard input
[ "data structures", "string suffix structures", "trees", "strings" ]
1ce1fdce039eb779382fb4ae0d4bbe35
The first line of input contains integers n and q (1 ≤ n ≤ 2 × 105 and 1 ≤ q ≤ 5 × 105). The next n lines contain the phone numbers, i-th line contains a string si consisting of lowercase English letters (). The next q lines contain the information about the questions, each of them contains integers l, r and k (1 ≤ l ≤ r ≤ n and 1 ≤ k ≤ n).
2,800
Print the answer for each question in a separate line.
standard output
PASSED
e2d1fa143a182056b3f0ab1df9c17314
train_002.jsonl
1432658100
What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercase English letters. What is double strange that a phone number can be associated with several bears!In that country there is a rock band called CF consisting of n bears (including Mike) numbered from 1 to n. Phone number of i-th member of CF is si. May 17th is a holiday named Phone Calls day. In the last Phone Calls day, everyone called all the numbers that are substrings of his/her number (one may call some number several times). In particular, everyone called himself (that was really strange country).Denote as call(i, j) the number of times that i-th member of CF called the j-th member of CF. The geek Mike has q questions that he wants to ask you. In each question he gives you numbers l, r and k and you should tell him the number
256 megabytes
//package round305; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.InputMismatchException; public class E3 { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(), Q = ni(); StringBuilder sb = new StringBuilder(); int[] offset = new int[n+1]; for(int i = 0;i < n;i++){ String ls = ns(); sb.append(ls + "{"); offset[i+1] = sb.length(); } char[] s = sb.toString().toCharArray(); int[] sa = sa(s); int[] lcp = buildLCP(s, sa); int[] isa = new int[s.length]; for(int i = 0;i < s.length;i++){ isa[sa[i]] = i; } // tr(lcp); SegmentTreeRMQ st = new SegmentTreeRMQ(lcp); int[][] rs = new int[2*Q][]; for(int i = 0;i < Q;i++){ int L = ni()-1, R = ni()-1, K = ni()-1; rs[i] = new int[]{L-1, K, i, -1}; rs[i+Q] = new int[]{R, K, i, 1}; } Arrays.sort(rs, new Comparator<int[]>() { public int compare(int[] a, int[] b) { return a[0] - b[0]; } }); // print(sa, s); long[] ret = new long[Q]; int[] ft = new int[s.length+2]; int p = 0; for(int[] e : rs){ while(p < n && p <= e[0]){ for(int i = offset[p]; i < offset[p+1];i++){ addFenwick(ft, isa[i], 1); } p++; } int cen = isa[offset[e[1]]]; int len = offset[e[1]+1]-offset[e[1]]-1; int sup = st.firstle(cen+1, len-1); if(sup == -1)sup = lcp.length; int inf = st.lastle(cen, len-1); // tr(inf, sup, sumFenwick(ft, sup-1) - sumFenwick(ft, inf-2)); // tr(restoreFenwick(ft)); ret[e[2]] += (sumFenwick(ft, sup-1) - sumFenwick(ft, inf-1))*e[3]; } for(int i = 0;i < Q;i++){ out.println(ret[i]); } } public static class SegmentTreeRMQ { public int M, H, N; public int[] st; public SegmentTreeRMQ(int n) { N = n; M = Integer.highestOneBit(Math.max(N-1, 1))<<2; H = M>>>1; st = new int[M]; Arrays.fill(st, 0, M, Integer.MAX_VALUE); } public SegmentTreeRMQ(int[] a) { N = a.length; M = Integer.highestOneBit(Math.max(N-1, 1))<<2; H = M>>>1; st = new int[M]; for(int i = 0;i < N;i++){ st[H+i] = a[i]; } Arrays.fill(st, H+N, M, Integer.MAX_VALUE); for(int i = H-1;i >= 1;i--)propagate(i); } public void update(int pos, int x) { st[H+pos] = x; for(int i = (H+pos)>>>1;i >= 1;i >>>= 1)propagate(i); } private void propagate(int i) { st[i] = Math.min(st[2*i], st[2*i+1]); } public int min(int l, int r){ return l >= r ? 0 : min(l, r, 0, H, 1);} private int min(int l, int r, int cl, int cr, int cur) { if(l <= cl && cr <= r){ return st[cur]; }else{ int mid = cl+cr>>>1; int ret = Integer.MAX_VALUE; if(cl < r && l < mid){ ret = Math.min(ret, min(l, r, cl, mid, 2*cur)); } if(mid < r && l < cr){ ret = Math.min(ret, min(l, r, mid, cr, 2*cur+1)); } return ret; } } public int firstle(int l, int v) { int cur = H+l; while(true){ if(st[cur] <= v){ if(cur < H){ cur = 2*cur; }else{ return cur-H; } }else{ cur++; if((cur&cur-1) == 0)return -1; if((cur&1)==0)cur>>>=1; } } } public int lastle(int l, int v) { int cur = H+l; while(true){ if(st[cur] <= v){ if(cur < H){ cur = 2*cur+1; }else{ return cur-H; } }else{ if((cur&cur-1) == 0)return -1; cur--; if((cur&1)==1)cur>>>=1; } } } } public static int[] restoreFenwick(int[] ft) { int n = ft.length-1; int[] ret = new int[n]; for(int i = 0;i < n;i++)ret[i] = sumFenwick(ft, i); for(int i = n-1;i >= 1;i--)ret[i] -= ret[i-1]; return ret; } public void print(int[] sa, char[] s) { int len = 20; int n = sa.length; for(int v : sa){ tr(v, v+len-1 < n ? new String(s, v, len) + "..." : new String(s, v, s.length-v)); } } public static int[] buildFenwick(int[] a) { int n = a.length; int[] ft = new int[n + 1]; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j += j & -j) { ft[j] += a[i - 1]; } } return ft; } public static int sumFenwick(int[] ft, int i) { int sum = 0; for (i++; i > 0; i -= i & -i) sum += ft[i]; return sum; } public static void addFenwick(int[] ft, int i, int v) { if (v == 0) return; int n = ft.length; for (i++; i < n; i += i & -i) ft[i] += v; } public static int[][] build(int[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); int[][] ret = new int[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; }else { ret[i] = new int[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = Math.min(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } public static int rmq(int[][] or, int a, int b) { if(a == b)return 99999999; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(b-a); return Math.min(or[t][a], or[t][b-(1<<t)]); } private static interface BaseArray { public int get(int i); public void set(int i, int val); public int update(int i, int val); } private static class CharArray implements BaseArray { private char[] m_A = null; private int m_pos = 0; CharArray(char[] A, int pos) { m_A = A; m_pos = pos; } public int get(int i) { return m_A[m_pos + i] & 0xffff; } public void set(int i, int val) { m_A[m_pos + i] = (char) (val & 0xffff); } public int update(int i, int val) { return m_A[m_pos + i] += val & 0xffff; } } private static class IntArray implements BaseArray { private int[] m_A = null; private int m_pos = 0; IntArray(int[] A, int pos) { m_A = A; m_pos = pos; } public int get(int i) { return m_A[m_pos + i]; } public void set(int i, int val) { m_A[m_pos + i] = val; } public int update(int i, int val) { return m_A[m_pos + i] += val; } } /* find the start or end of each bucket */ private static void getCounts(BaseArray T, BaseArray C, int n, int k) { int i; for(i = 0;i < k;++i){ C.set(i, 0); } for(i = 0;i < n;++i){ C.update(T.get(i), 1); } } private static void getBuckets(BaseArray C, BaseArray B, int k, boolean end) { int i, sum = 0; if(end != false){ for(i = 0;i < k;++i){ sum += C.get(i); B.set(i, sum); } }else{ for(i = 0;i < k;++i){ sum += C.get(i); B.set(i, sum - C.get(i)); } } } /* sort all type LMS suffixes */ private static void LMSsort(BaseArray T, int[] SA, BaseArray C, BaseArray B, int n, int k) { int b, i, j; int c0, c1; /* compute SAl */ if(C == B){ getCounts(T, C, n, k); } getBuckets(C, B, k, false); /* find starts of buckets */ j = n - 1; b = B.get(c1 = T.get(j)); --j; SA[b++] = (T.get(j) < c1) ? ~j : j; for(i = 0;i < n;++i){ if(0 < (j = SA[i])){ if((c0 = T.get(j)) != c1){ B.set(c1, b); b = B.get(c1 = c0); } --j; SA[b++] = (T.get(j) < c1) ? ~j : j; SA[i] = 0; }else if(j < 0){ SA[i] = ~j; } } /* compute SAs */ if(C == B){ getCounts(T, C, n, k); } getBuckets(C, B, k, true); /* find ends of buckets */ for(i = n - 1, b = B.get(c1 = 0);0 <= i;--i){ if(0 < (j = SA[i])){ if((c0 = T.get(j)) != c1){ B.set(c1, b); b = B.get(c1 = c0); } --j; SA[--b] = (T.get(j) > c1) ? ~(j + 1) : j; SA[i] = 0; } } } private static int LMSpostproc(BaseArray T, int[] SA, int n, int m) { int i, j, p, q, plen, qlen, name; int c0, c1; boolean diff; /* * compact all the sorted substrings into the first m items of SA 2*m * must be not larger than n (proveable) */ for(i = 0;(p = SA[i]) < 0;++i){ SA[i] = ~p; } if(i < m){ for(j = i, ++i;;++i){ if((p = SA[i]) < 0){ SA[j++] = ~p; SA[i] = 0; if(j == m){ break; } } } } /* store the length of all substrings */ i = n - 1; j = n - 1; c0 = T.get(n - 1); do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); for(;0 <= i;){ do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) <= c1)); if(0 <= i){ SA[m + ((i + 1) >> 1)] = j - i; j = i + 1; do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); } } /* find the lexicographic names of all substrings */ for(i = 0, name = 0, q = n, qlen = 0;i < m;++i){ p = SA[i]; plen = SA[m + (p >> 1)]; diff = true; if((plen == qlen) && ((q + plen) < n)){ for(j = 0;(j < plen) && (T.get(p + j) == T.get(q + j));++j){ } if(j == plen){ diff = false; } } if(diff != false){ ++name; q = p; qlen = plen; } SA[m + (p >> 1)] = name; } return name; } /* compute SA and BWT */ private static void induceSA(BaseArray T, int[] SA, BaseArray C, BaseArray B, int n, int k) { int b, i, j; int c0, c1; /* compute SAl */ if(C == B){ getCounts(T, C, n, k); } getBuckets(C, B, k, false); /* find starts of buckets */ j = n - 1; b = B.get(c1 = T.get(j)); SA[b++] = ((0 < j) && (T.get(j - 1) < c1)) ? ~j : j; for(i = 0;i < n;++i){ j = SA[i]; SA[i] = ~j; if(0 < j){ if((c0 = T.get(--j)) != c1){ B.set(c1, b); b = B.get(c1 = c0); } SA[b++] = ((0 < j) && (T.get(j - 1) < c1)) ? ~j : j; } } /* compute SAs */ if(C == B){ getCounts(T, C, n, k); } getBuckets(C, B, k, true); /* find ends of buckets */ for(i = n - 1, b = B.get(c1 = 0);0 <= i;--i){ if(0 < (j = SA[i])){ if((c0 = T.get(--j)) != c1){ B.set(c1, b); b = B.get(c1 = c0); } SA[--b] = ((j == 0) || (T.get(j - 1) > c1)) ? ~j : j; }else{ SA[i] = ~j; } } } /* * find the suffix array SA of T[0..n-1] in {0..k-1}^n use a working space * (excluding T and SA) of at most 2n+O(1) for a constant alphabet */ private static void SA_IS(BaseArray T, int[] SA, int fs, int n, int k) { BaseArray C, B, RA; int i, j, b, m, p, q, name, newfs; int c0, c1; int flags = 0; if(k <= 256){ C = new IntArray(new int[k], 0); if(k <= fs){ B = new IntArray(SA, n + fs - k); flags = 1; }else{ B = new IntArray(new int[k], 0); flags = 3; } }else if(k <= fs){ C = new IntArray(SA, n + fs - k); if(k <= (fs - k)){ B = new IntArray(SA, n + fs - k * 2); flags = 0; }else if(k <= 1024){ B = new IntArray(new int[k], 0); flags = 2; }else{ B = C; flags = 8; } }else{ C = B = new IntArray(new int[k], 0); flags = 4 | 8; } /* * stage 1: reduce the problem by at least 1/2 sort all the * LMS-substrings */ getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */ for(i = 0;i < n;++i){ SA[i] = 0; } b = -1; i = n - 1; j = n; m = 0; c0 = T.get(n - 1); do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); for(;0 <= i;){ do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) <= c1)); if(0 <= i){ if(0 <= b){ SA[b] = j; } b = B.update(c1, -1); j = i; ++m; do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); } } if(1 < m){ LMSsort(T, SA, C, B, n, k); name = LMSpostproc(T, SA, n, m); }else if(m == 1){ SA[b] = j + 1; name = 1; }else{ name = 0; } /* * stage 2: solve the reduced problem recurse if names are not yet * unique */ if(name < m){ if((flags & 4) != 0){ C = null; B = null; } if((flags & 2) != 0){ B = null; } newfs = (n + fs) - (m * 2); if((flags & (1 | 4 | 8)) == 0){ if((k + name) <= newfs){ newfs -= k; }else{ flags |= 8; } } for(i = m + (n >> 1) - 1, j = m * 2 + newfs - 1;m <= i;--i){ if(SA[i] != 0){ SA[j--] = SA[i] - 1; } } RA = new IntArray(SA, m + newfs); SA_IS(RA, SA, newfs, m, name); RA = null; i = n - 1; j = m * 2 - 1; c0 = T.get(n - 1); do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); for(;0 <= i;){ do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) <= c1)); if(0 <= i){ SA[j--] = i + 1; do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); } } for(i = 0;i < m;++i){ SA[i] = SA[m + SA[i]]; } if((flags & 4) != 0){ C = B = new IntArray(new int[k], 0); } if((flags & 2) != 0){ B = new IntArray(new int[k], 0); } } /* stage 3: induce the result for the original problem */ if((flags & 8) != 0){ getCounts(T, C, n, k); } /* put all left-most S characters into their buckets */ if(1 < m){ getBuckets(C, B, k, true); /* find ends of buckets */ i = m - 1; j = n; p = SA[m - 1]; c1 = T.get(p); do{ q = B.get(c0 = c1); while (q < j){ SA[--j] = 0; } do{ SA[--j] = p; if(--i < 0){ break; } p = SA[i]; }while ((c1 = T.get(p)) == c0); }while (0 <= i); while (0 < j){ SA[--j] = 0; } } induceSA(T, SA, C, B, n, k); C = null; B = null; } /* char */ public static void suffixsort(char[] T, int[] SA, int n) { if((T == null) || (SA == null) || (T.length < n) || (SA.length < n)){ return; } if(n <= 1){ if(n == 1){ SA[0] = 0; } return; } SA_IS(new CharArray(T, 0), SA, 0, n, 128); } public static int[] sa(char[] T) { int n = T.length; int[] sa = new int[n]; suffixsort(T, sa, n); return sa; } public static int[] buildLCP(char[] str, int[] sa) { int n = str.length; int h = 0; int[] lcp = new int[n]; int[] isa = new int[n]; for(int i = 0;i < n;i++)isa[sa[i]] = i; for(int i = 0;i < n;i++){ if(isa[i] > 0){ for(int j = sa[isa[i]-1]; j+h<n && i+h<n && str[j+h] == str[i+h]; h++); lcp[isa[i]] = h; }else{ lcp[isa[i]] = 0; } if(h > 0)h--; } return lcp; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new E3().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["5 5\na\nab\nabab\nababab\nb\n1 5 1\n3 5 1\n1 5 2\n1 5 3\n1 4 5"]
3 seconds
["7\n5\n6\n3\n6"]
null
Java 7
standard input
[ "data structures", "string suffix structures", "trees", "strings" ]
1ce1fdce039eb779382fb4ae0d4bbe35
The first line of input contains integers n and q (1 ≤ n ≤ 2 × 105 and 1 ≤ q ≤ 5 × 105). The next n lines contain the phone numbers, i-th line contains a string si consisting of lowercase English letters (). The next q lines contain the information about the questions, each of them contains integers l, r and k (1 ≤ l ≤ r ≤ n and 1 ≤ k ≤ n).
2,800
Print the answer for each question in a separate line.
standard output
PASSED
aa9839948d22b23c2fd5a1ac96b095eb
train_002.jsonl
1432658100
What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercase English letters. What is double strange that a phone number can be associated with several bears!In that country there is a rock band called CF consisting of n bears (including Mike) numbered from 1 to n. Phone number of i-th member of CF is si. May 17th is a holiday named Phone Calls day. In the last Phone Calls day, everyone called all the numbers that are substrings of his/her number (one may call some number several times). In particular, everyone called himself (that was really strange country).Denote as call(i, j) the number of times that i-th member of CF called the j-th member of CF. The geek Mike has q questions that he wants to ask you. In each question he gives you numbers l, r and k and you should tell him the number
256 megabytes
//package round305; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.InputMismatchException; public class E2 { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(), Q = ni(); StringBuilder sb = new StringBuilder(); int[] offset = new int[n+1]; for(int i = 0;i < n;i++){ String ls = ns(); sb.append(ls + "{"); offset[i+1] = sb.length(); } char[] s = sb.toString().toCharArray(); int[] sa = sa(s); int[] lcp = buildLCP(s, sa); int[] isa = new int[s.length]; for(int i = 0;i < s.length;i++){ isa[sa[i]] = i; } // tr(lcp); int[][] st = build(lcp); int[][] rs = new int[2*Q][]; for(int i = 0;i < Q;i++){ int L = ni()-1, R = ni()-1, K = ni()-1; rs[i] = new int[]{L-1, K, i, -1}; rs[i+Q] = new int[]{R, K, i, 1}; } Arrays.sort(rs, new Comparator<int[]>() { public int compare(int[] a, int[] b) { return a[0] - b[0]; } }); // print(sa, s); long[] ret = new long[Q]; int[] ft = new int[s.length+2]; int p = 0; for(int[] e : rs){ while(p < n && p <= e[0]){ for(int i = offset[p]; i < offset[p+1];i++){ addFenwick(ft, isa[i], 1); } p++; } int cen = isa[offset[e[1]]]; int len = offset[e[1]+1]-offset[e[1]]-1; int sup = -1, inf = -1; // tr(e, cen, len); { int low = cen+1, high = s.length+1; while(high - low > 1){ int h = high+low>>>1; if(rmq(st, cen+1, h) >= len){ low = h; }else{ high = h; } } sup = low; } { int low = -1, high = cen+1; while(high - low > 1){ int h = (high+low)/2; if(rmq(st, h, cen+1) >= len){ high = h; }else{ low = h; } } inf = high; } // tr(inf, sup, sumFenwick(ft, sup-1) - sumFenwick(ft, inf-2)); // tr(restoreFenwick(ft)); ret[e[2]] += (sumFenwick(ft, sup-1) - sumFenwick(ft, inf-2))*e[3]; } for(int i = 0;i < Q;i++){ out.println(ret[i]); } } public static int[] restoreFenwick(int[] ft) { int n = ft.length-1; int[] ret = new int[n]; for(int i = 0;i < n;i++)ret[i] = sumFenwick(ft, i); for(int i = n-1;i >= 1;i--)ret[i] -= ret[i-1]; return ret; } public void print(int[] sa, char[] s) { int len = 20; int n = sa.length; for(int v : sa){ tr(v, v+len-1 < n ? new String(s, v, len) + "..." : new String(s, v, s.length-v)); } } public static int[] buildFenwick(int[] a) { int n = a.length; int[] ft = new int[n + 1]; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j += j & -j) { ft[j] += a[i - 1]; } } return ft; } public static int sumFenwick(int[] ft, int i) { int sum = 0; for (i++; i > 0; i -= i & -i) sum += ft[i]; return sum; } public static void addFenwick(int[] ft, int i, int v) { if (v == 0) return; int n = ft.length; for (i++; i < n; i += i & -i) ft[i] += v; } public static int[][] build(int[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); int[][] ret = new int[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; }else { ret[i] = new int[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = Math.min(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } public static int rmq(int[][] or, int a, int b) { if(a == b)return 99999999; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(b-a); return Math.min(or[t][a], or[t][b-(1<<t)]); } private static interface BaseArray { public int get(int i); public void set(int i, int val); public int update(int i, int val); } private static class CharArray implements BaseArray { private char[] m_A = null; private int m_pos = 0; CharArray(char[] A, int pos) { m_A = A; m_pos = pos; } public int get(int i) { return m_A[m_pos + i] & 0xffff; } public void set(int i, int val) { m_A[m_pos + i] = (char) (val & 0xffff); } public int update(int i, int val) { return m_A[m_pos + i] += val & 0xffff; } } private static class IntArray implements BaseArray { private int[] m_A = null; private int m_pos = 0; IntArray(int[] A, int pos) { m_A = A; m_pos = pos; } public int get(int i) { return m_A[m_pos + i]; } public void set(int i, int val) { m_A[m_pos + i] = val; } public int update(int i, int val) { return m_A[m_pos + i] += val; } } /* find the start or end of each bucket */ private static void getCounts(BaseArray T, BaseArray C, int n, int k) { int i; for(i = 0;i < k;++i){ C.set(i, 0); } for(i = 0;i < n;++i){ C.update(T.get(i), 1); } } private static void getBuckets(BaseArray C, BaseArray B, int k, boolean end) { int i, sum = 0; if(end != false){ for(i = 0;i < k;++i){ sum += C.get(i); B.set(i, sum); } }else{ for(i = 0;i < k;++i){ sum += C.get(i); B.set(i, sum - C.get(i)); } } } /* sort all type LMS suffixes */ private static void LMSsort(BaseArray T, int[] SA, BaseArray C, BaseArray B, int n, int k) { int b, i, j; int c0, c1; /* compute SAl */ if(C == B){ getCounts(T, C, n, k); } getBuckets(C, B, k, false); /* find starts of buckets */ j = n - 1; b = B.get(c1 = T.get(j)); --j; SA[b++] = (T.get(j) < c1) ? ~j : j; for(i = 0;i < n;++i){ if(0 < (j = SA[i])){ if((c0 = T.get(j)) != c1){ B.set(c1, b); b = B.get(c1 = c0); } --j; SA[b++] = (T.get(j) < c1) ? ~j : j; SA[i] = 0; }else if(j < 0){ SA[i] = ~j; } } /* compute SAs */ if(C == B){ getCounts(T, C, n, k); } getBuckets(C, B, k, true); /* find ends of buckets */ for(i = n - 1, b = B.get(c1 = 0);0 <= i;--i){ if(0 < (j = SA[i])){ if((c0 = T.get(j)) != c1){ B.set(c1, b); b = B.get(c1 = c0); } --j; SA[--b] = (T.get(j) > c1) ? ~(j + 1) : j; SA[i] = 0; } } } private static int LMSpostproc(BaseArray T, int[] SA, int n, int m) { int i, j, p, q, plen, qlen, name; int c0, c1; boolean diff; /* * compact all the sorted substrings into the first m items of SA 2*m * must be not larger than n (proveable) */ for(i = 0;(p = SA[i]) < 0;++i){ SA[i] = ~p; } if(i < m){ for(j = i, ++i;;++i){ if((p = SA[i]) < 0){ SA[j++] = ~p; SA[i] = 0; if(j == m){ break; } } } } /* store the length of all substrings */ i = n - 1; j = n - 1; c0 = T.get(n - 1); do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); for(;0 <= i;){ do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) <= c1)); if(0 <= i){ SA[m + ((i + 1) >> 1)] = j - i; j = i + 1; do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); } } /* find the lexicographic names of all substrings */ for(i = 0, name = 0, q = n, qlen = 0;i < m;++i){ p = SA[i]; plen = SA[m + (p >> 1)]; diff = true; if((plen == qlen) && ((q + plen) < n)){ for(j = 0;(j < plen) && (T.get(p + j) == T.get(q + j));++j){ } if(j == plen){ diff = false; } } if(diff != false){ ++name; q = p; qlen = plen; } SA[m + (p >> 1)] = name; } return name; } /* compute SA and BWT */ private static void induceSA(BaseArray T, int[] SA, BaseArray C, BaseArray B, int n, int k) { int b, i, j; int c0, c1; /* compute SAl */ if(C == B){ getCounts(T, C, n, k); } getBuckets(C, B, k, false); /* find starts of buckets */ j = n - 1; b = B.get(c1 = T.get(j)); SA[b++] = ((0 < j) && (T.get(j - 1) < c1)) ? ~j : j; for(i = 0;i < n;++i){ j = SA[i]; SA[i] = ~j; if(0 < j){ if((c0 = T.get(--j)) != c1){ B.set(c1, b); b = B.get(c1 = c0); } SA[b++] = ((0 < j) && (T.get(j - 1) < c1)) ? ~j : j; } } /* compute SAs */ if(C == B){ getCounts(T, C, n, k); } getBuckets(C, B, k, true); /* find ends of buckets */ for(i = n - 1, b = B.get(c1 = 0);0 <= i;--i){ if(0 < (j = SA[i])){ if((c0 = T.get(--j)) != c1){ B.set(c1, b); b = B.get(c1 = c0); } SA[--b] = ((j == 0) || (T.get(j - 1) > c1)) ? ~j : j; }else{ SA[i] = ~j; } } } /* * find the suffix array SA of T[0..n-1] in {0..k-1}^n use a working space * (excluding T and SA) of at most 2n+O(1) for a constant alphabet */ private static void SA_IS(BaseArray T, int[] SA, int fs, int n, int k) { BaseArray C, B, RA; int i, j, b, m, p, q, name, newfs; int c0, c1; int flags = 0; if(k <= 256){ C = new IntArray(new int[k], 0); if(k <= fs){ B = new IntArray(SA, n + fs - k); flags = 1; }else{ B = new IntArray(new int[k], 0); flags = 3; } }else if(k <= fs){ C = new IntArray(SA, n + fs - k); if(k <= (fs - k)){ B = new IntArray(SA, n + fs - k * 2); flags = 0; }else if(k <= 1024){ B = new IntArray(new int[k], 0); flags = 2; }else{ B = C; flags = 8; } }else{ C = B = new IntArray(new int[k], 0); flags = 4 | 8; } /* * stage 1: reduce the problem by at least 1/2 sort all the * LMS-substrings */ getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */ for(i = 0;i < n;++i){ SA[i] = 0; } b = -1; i = n - 1; j = n; m = 0; c0 = T.get(n - 1); do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); for(;0 <= i;){ do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) <= c1)); if(0 <= i){ if(0 <= b){ SA[b] = j; } b = B.update(c1, -1); j = i; ++m; do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); } } if(1 < m){ LMSsort(T, SA, C, B, n, k); name = LMSpostproc(T, SA, n, m); }else if(m == 1){ SA[b] = j + 1; name = 1; }else{ name = 0; } /* * stage 2: solve the reduced problem recurse if names are not yet * unique */ if(name < m){ if((flags & 4) != 0){ C = null; B = null; } if((flags & 2) != 0){ B = null; } newfs = (n + fs) - (m * 2); if((flags & (1 | 4 | 8)) == 0){ if((k + name) <= newfs){ newfs -= k; }else{ flags |= 8; } } for(i = m + (n >> 1) - 1, j = m * 2 + newfs - 1;m <= i;--i){ if(SA[i] != 0){ SA[j--] = SA[i] - 1; } } RA = new IntArray(SA, m + newfs); SA_IS(RA, SA, newfs, m, name); RA = null; i = n - 1; j = m * 2 - 1; c0 = T.get(n - 1); do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); for(;0 <= i;){ do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) <= c1)); if(0 <= i){ SA[j--] = i + 1; do{ c1 = c0; }while ((0 <= --i) && ((c0 = T.get(i)) >= c1)); } } for(i = 0;i < m;++i){ SA[i] = SA[m + SA[i]]; } if((flags & 4) != 0){ C = B = new IntArray(new int[k], 0); } if((flags & 2) != 0){ B = new IntArray(new int[k], 0); } } /* stage 3: induce the result for the original problem */ if((flags & 8) != 0){ getCounts(T, C, n, k); } /* put all left-most S characters into their buckets */ if(1 < m){ getBuckets(C, B, k, true); /* find ends of buckets */ i = m - 1; j = n; p = SA[m - 1]; c1 = T.get(p); do{ q = B.get(c0 = c1); while (q < j){ SA[--j] = 0; } do{ SA[--j] = p; if(--i < 0){ break; } p = SA[i]; }while ((c1 = T.get(p)) == c0); }while (0 <= i); while (0 < j){ SA[--j] = 0; } } induceSA(T, SA, C, B, n, k); C = null; B = null; } /* char */ public static void suffixsort(char[] T, int[] SA, int n) { if((T == null) || (SA == null) || (T.length < n) || (SA.length < n)){ return; } if(n <= 1){ if(n == 1){ SA[0] = 0; } return; } SA_IS(new CharArray(T, 0), SA, 0, n, 128); } public static int[] sa(char[] T) { int n = T.length; int[] sa = new int[n]; suffixsort(T, sa, n); return sa; } public static int[] buildLCP(char[] str, int[] sa) { int n = str.length; int h = 0; int[] lcp = new int[n]; int[] isa = new int[n]; for(int i = 0;i < n;i++)isa[sa[i]] = i; for(int i = 0;i < n;i++){ if(isa[i] > 0){ for(int j = sa[isa[i]-1]; j+h<n && i+h<n && str[j+h] == str[i+h]; h++); lcp[isa[i]] = h; }else{ lcp[isa[i]] = 0; } if(h > 0)h--; } return lcp; } void run() throws Exception { // Random gen = new Random(); // StringBuilder sb = new StringBuilder(); // int n = 200000; // int q = 500000; // sb.append(n + " "); // sb.append(q + " "); // for(int i = 0;i < n;i++){ // int len = gen.nextInt(1)+1; // for(int j = 0;j < len;j++){ // sb.append((char)(gen.nextInt(2) + 'a')); // } // sb.append("\n"); // } // for(int i = 0;i < q;i++){ // int v1 = gen.nextInt(n); // int v2 = gen.nextInt(n); // sb.append(Math.min(v1, v2) + 1 + " "); // sb.append(Math.max(v1, v2) + 1 + " "); // sb.append(gen.nextInt(n) + 1 + " "); // } // INPUT = sb.toString(); is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new E2().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["5 5\na\nab\nabab\nababab\nb\n1 5 1\n3 5 1\n1 5 2\n1 5 3\n1 4 5"]
3 seconds
["7\n5\n6\n3\n6"]
null
Java 7
standard input
[ "data structures", "string suffix structures", "trees", "strings" ]
1ce1fdce039eb779382fb4ae0d4bbe35
The first line of input contains integers n and q (1 ≤ n ≤ 2 × 105 and 1 ≤ q ≤ 5 × 105). The next n lines contain the phone numbers, i-th line contains a string si consisting of lowercase English letters (). The next q lines contain the information about the questions, each of them contains integers l, r and k (1 ≤ l ≤ r ≤ n and 1 ≤ k ≤ n).
2,800
Print the answer for each question in a separate line.
standard output
PASSED
79659c9ed018db15734f37ae3032877a
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ import java.util.*; public class Round6151 { public static void main(String[] args) { //System.out.println("Hello World"); Scanner sc = new Scanner(System.in); int T =sc.nextInt(); for(int t=0;t<T;t++) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int N = sc.nextInt(); int[] arr = new int[3]; arr[0] = a; arr[1] = b; arr[2] = c; Arrays.sort(arr); // System.out.println(Arrays.toString(arr)); if(2*arr[2] - arr[1]-arr[0]<=N) { if((N- (2*arr[2] - arr[1]-arr[0]))%3==0) { System.out.println("YES"); } else { System.out.println("NO"); } } else { System.out.println("NO"); } } } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
72612cb47d94c016459ddf1622c3eddd
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main{ public static void main(String[]args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); for(int d=0;d<n;d++) { int c=0; int max; int a=0; int b=0; int[]arr=new int[4]; for(int i=0;i<4;i++) { int x=sc.nextInt(); arr[c]=x; c++; } max=arr[0]; for(int i=0;i<3;i++){ if(arr[i]>max) max=arr[i]; } int used =3*max-arr[0]-arr[1]-arr[2]; if((arr[3]-used)%3==0&&used<=arr[3]) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
818751794241a3a6255b9334f6a30d92
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main{ public static void main(String[]args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); while (n-->0) { int a =sc.nextInt(),b=sc.nextInt(),c=sc.nextInt(),d=sc.nextInt(),e=a+b+c+d; if (e%3!=0) System.out.println("NO"); else { e/=3; System.out.println((a>e||b>e||c>e)?"NO":"YES"); } } } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
6d8f0786d560301b85abde19a524aeec
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; import java.math.BigInteger; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { long a = sc.nextLong(); long b = sc.nextLong(); long c = sc.nextLong(); long n = sc.nextLong(); if((n+a+b+c)%3 == 0) { long temp = (n+a+b+c)/3; long t1 = temp-a; long t2 = temp-b; long t3 = temp-c; if(t1 < 0 || t2 < 0 || t3 < 0 || (t1+t2+t3) != n) { System.out.println("NO"); } else { System.out.println("YES"); } } else System.out.println("NO"); } } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
c278684cb4c5c45bf019c6a9a10025d4
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int q = scan.nextInt(); while (q-->0) { int a = scan.nextInt(); int b = scan.nextInt(); int c = scan.nextInt(); int n = scan.nextInt(); int s = a + b + c + n; int p = s/3; if(s%3==0){ if(a>p || b>p || c>p){ System.out.println("NO"); } else{ System.out.println("YES"); } } else{ System.out.println("NO"); } } } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
130bdc889b4cf3e621c4ce58cbd213ff
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.Scanner; public class Problem_A { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); for(int i = 0; i < n; i ++) { int a = input.nextInt(); int b = input.nextInt(); int c = input.nextInt(); int P = input.nextInt(); int result = 0; int max = MAX(a, b, c); int sub = 0; if(max == a) { result = (a - b) + (a - c); sub = P - result; if(sub % 3 == 0 && sub >= 0) { System.out.println("YES"); } else { System.out.println("NO"); } } else if(max == b) { result = (b - a) + (b - c); sub = P - result; if(sub % 3 == 0 && sub >= 0) { System.out.println("YES"); } else { System.out.println("NO"); } } else if(max == c){ result = (c - b) + (c - a); sub = P - result; if(sub % 3 == 0 && sub >= 0) { System.out.println("YES"); } else { System.out.println("NO"); } } } input.close(); } public static int MAX(int a, int b, int c) { int max = a; if(b > max) { max = b; } if(c > max) { max = c; } return max; } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
6205530fdfa0915f234f053e5f9f2e9e
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; import java.util.Arrays; public class Test2{ public static void main(String args[]){ Scanner in=new Scanner (System.in); int t=in.nextInt(); while(--t>=0) { int a=in.nextInt(); int b=in.nextInt(); int c=in.nextInt(); int n=in.nextInt(); int max=Math.max(a,Math.max(b,c)); int sum=3*max-(a+b+c); n=n-sum; if(n%3==0 && n>=0) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
fda245d44254e12281522ac4d44d09e8
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.Scanner; public class Div31 { public static void main(String args[]) throws Exception { Scanner scn=new Scanner(System.in); int t=scn.nextInt(); for(int i=0;i<t;i++) { long a=scn.nextLong(); long b=scn.nextLong(); long c=scn.nextLong(); long n=scn.nextLong(); long total=a+b+c+n; long eq=total/3; if((total)%3==0 &&(a<=eq) &&(b<=eq)&&(c<=eq)) { System.out.println("YES"); }else { System.out.println("NO"); } } } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
daa2ad134cc60890476b4d903a180b7f
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class d { public static void main (String[] args) { PrintWriter pw=new PrintWriter(System.out); Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++){ int[] a=new int[3]; a[0]=sc.nextInt(); a[1]=sc.nextInt(); a[2]=sc.nextInt(); long n=sc.nextInt(); Arrays.sort(a); long sum=0; sum+=a[2]-a[0]; sum+=a[2]-a[1]; //pw.println(sum); long r=n-sum; if(r>=0&&r%3==0) pw.println("YES"); else pw.println("NO"); a=null; } //pw.println("hellow"); pw.close(); } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
4b76df33eb16c939496de97b0a7501e0
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.*; import java.util.*; public class Bricks { public static void main(String []argh){ Scanner s=new Scanner(System.in); int tc=s.nextInt(); while(tc>0) { tc--; long a=s.nextLong(); long b=s.nextLong(); long c=s.nextLong(); long n=s.nextLong(); long max=Math.max(Math.max(a, b),c); n=n-(max-a)-(max-b)-(max-c); if(Math.ceil((double)n/3)==Math.floor((double)n/3)&&n>=0) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
d6e02b9346cc0d4a13f5c5a5419249a7
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.lang.*; import java.util.*; import java.io.*; public class collectingcoin { Scanner sc=new Scanner(System.in); PrintWriter pr=new PrintWriter(System.out,true); public static void main(String... args) { collectingcoin c=new collectingcoin(); c.prop(); } public void prop() { int a,b,c,n,sum,t ; t=sc.nextInt(); for(int i=0;i<t;++i) { a=sc.nextInt(); b=sc.nextInt(); c=sc.nextInt(); n=sc.nextInt(); sum=a+b+c+n ; if(sum%3==0){ sum=sum/3 ; int max ; a=sum-a ; max=a ; if(sum-b>max) {max=sum-b ;} b=sum-b ; if (sum-c>max) { max=sum-c ; } c=sum-c ; if(a>=0 && b>=0 && c>=0 && a+b+c<=n) pr.println("YES"); else pr.println("NO"); } else pr.println("NO"); } } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
3e1e7e2295a08818433964787d272a60
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; /** * @author Utkarsh * */ public class FibinacciSeries { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { 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 Character charAt(int i) { // TODO Auto-generated method stub return null; } public BigInteger nextBigInteger() { // TODO Auto-generated method stub return null; } } // Complete the hurdleRace function below. // public static void solve() { // FastReader s=new FastReader(); int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2; // If the element is present at the // middle itself if (arr[mid] == x) return mid; // If element is smaller than mid, then // it can only be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x); // Else the element can only be present // in right subarray return binarySearch(arr, mid + 1, r, x); } // We reach here when element is not present // in array return -1; } static // Driver method to test above // Java implementation of iterative Binary Search // Returns index of x if it is present in arr[], // else return -1 int lowerBound(int[] a,int n,int key){ int s =0,e=n-1; int ans = -1; while(s<=e){ int mid = (s+e)/2; if(a[mid]==key){ ans = mid; e = mid - 1; } else if(a[mid]>key){ e = mid - 1; } else{ s = mid + 1; } } return ans; } static int count(String se,char c,int l,int r) { if(l==r) { if(se.charAt(l)==c)return 0; else return 1; } int mid = (l+r)/2; int c1=0,c2=0; for(int i=l;i<=mid;i++) { if(se.charAt(i)!=c)c1++; } for(int i=r;i>mid;i--) { if(se.charAt(i)!=c)c2++; } int y = count(se,(char)((int)c+1),mid+1,r); int x = count(se,(char)((int)c+1),l,mid); return Math.min(c1+y, c2+x); } public static double log2(long N) { // calculate log2 N indirectly // using log() method double result = (double)(Math.log(N) / Math.log(2)); return result; } static boolean sign(long n) { return n>0; } public static int solve(long[] ar,int n) { long sum=0; for(int i=0;i<n;i++) { sum+=ar[i]; if(sum<=0)return 0; } sum=0; for(int i=n-1;i>=0;i--) { sum+=ar[i]; if(sum<=0)return 0; } return 1; } public static void main(String[] args) throws IOException { FastReader s=new FastReader(); int t = s.nextInt(); double b = 10; while(t-->0) { Set<Integer> se= new HashSet<>(); int[] ar = new int[3]; for(int i=0;i<3;i++) { ar[i] = s.nextInt(); } int k = s.nextInt(); int sum=0; Arrays.sort(ar); sum+=(ar[2]-ar[1])+(ar[2]-ar[0]); k-=sum; if(k>=0 && k%3==0)System.out.println("YES"); else if(se.size()==1 && k%3==0)System.out.println("YES"); else System.out.println("NO"); } } } //5 //4 8 2 6 2 //4 5 4 1 3
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
47e9cdff3406d9581bc8d877aea9e516
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class ProblemA1294 { public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int testCases = sc.nextInt(); // read input as integer for(int i =0;i<testCases;i++){ long a = sc.nextLong(); long b = sc.nextLong(); long c = sc.nextLong(); long n = sc.nextLong(); long total = a+b+c+n; long avg = total/3; if(total%3 ==0 && b<=avg && c<=avg & a<=avg){ out.println("YES"); }else{ out.println("NO"); } } out.close(); } //-----------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
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
4f103bbbc0c5416292bc9ab1223d6591
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner input = new Scanner(System.in); int noOfTests = input.nextInt(); for(int i = 0; i<noOfTests; i++ ) { int total=0; int n1,n2,n3,n4; n1 = input.nextInt(); n2 = input.nextInt(); n3 = input.nextInt(); n4 = input.nextInt(); total = n1+n2+n3+n4; if(total%3 == 0 && (n1) <=(total/3) && (n2) <=(total/3) && (n3) <=(total/3) ) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
7dfdc84d04040b06d9515cab6120f190
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.Scanner; import java.util.Arrays; public final class coins { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int[] arr = new int[3]; for(int i=0;i<t;i++) { arr[0]=sc.nextInt(); arr[1]=sc.nextInt(); arr[2]=sc.nextInt(); int n=sc.nextInt(); Arrays.sort(arr); n -= 2 * arr[2] - arr[1] - arr[0]; if (n < 0 || n % 3 != 0) { System.out.println("NO"); } else { System.out.println("Yes"); } } } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
5d6cb0baae11c8079bbb6cc6bd414974
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
/** * Created by first on 1/27/2020. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; /** * Created by first on 1/27/2020. */ public class ProblemA { public static void main(String[] args) throws IOException { (new ProblemA()).solve(); } public void solve() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int NO_test = Integer.parseInt(br.readLine()); Testcase [] tests = new Testcase[NO_test]; for (int i = 0; i < NO_test ; i++) { String line = br.readLine(); String [] num = line.split(" "); Testcase testcase = new Testcase(); testcase.a= Integer.parseInt(num[0]); testcase.b = Integer.parseInt(num[1]); testcase.c = Integer.parseInt(num[2]); testcase.n = Integer.parseInt(num[3]); tests[i] = testcase; } for (int i = 0; i < NO_test; i++) { Testcase test = tests[i]; long max = test.a; if(max< test.b){ max = test.b;} if(max< test.c){ max = test.c;} long rem = test.n - 3 * max + test.a +test.b + test.c; if(rem %3 == 0 && rem >= 0){ System.out.println("YES"); } else { System.out.println("NO"); } } } private class Testcase implements Comparable<Testcase>{ long a; long b; long c; long n; @Override public int compareTo(Testcase o) { if (a < o.a) { return -1; } else if (a == o.a) { if (b < o.b) { return -1; } else if (b > o.b) { return 1; } else { return 0; } } else { return 1; } } } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
4b62fece1764488aa4132f3410274d1e
train_002.jsonl
1579703700
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins.He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above.You have to answer $$$t$$$ independent test cases.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); int test=s.nextInt(); while(test>0) { int a=s.nextInt(); int b=s.nextInt(); int c=s.nextInt(); int total=s.nextInt(); int max=Math.max(a,Math.max(b,c)); if((total+a+b+c)%3!=0){ System.out.println("NO"); }else{ int diff=(max-a)+(max-b)+(max-c); if(total<diff){ System.out.println("NO"); } else { if((total-diff)%3==0&&(total-diff)>3){ System.out.println("YES"); }else if(total-diff==0){ System.out.println("YES"); }else{ System.out.println("YES"); } } } test--; } } }
Java
["5\n5 3 2 8\n100 101 102 105\n3 2 1 100000000\n10 20 15 14\n101 101 101 3"]
2 seconds
["YES\nYES\nNO\nNO\nYES"]
null
Java 8
standard input
[ "math" ]
bc5fb5d6882b86d83e5c86f21d806a52
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 \le a, b, c, n \le 10^8$$$) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.
800
For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise.
standard output
PASSED
fadfbca24c73bcfd14074bc50a8db41c
train_002.jsonl
1535898900
Given an array $$$a$$$ of $$$n$$$ integers and an integer $$$k$$$ ($$$2 \le k \le n$$$), where each element of the array is denoted by $$$a_i$$$ ($$$0 \le i &lt; n$$$). Perform the operation $$$z$$$ given below on $$$a$$$ and print the value of $$$z(a,k)$$$ modulo $$$10^{9}+7$$$.function z(array a, integer k): if length(a) &lt; k: return 0 else: b = empty array ans = 0 for i = 0 .. (length(a) - k): temp = a[i] for j = i .. (i + k - 1): temp = max(temp, a[j]) append temp to the end of b ans = ans + temp return ans + z(b, k)
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskF solver = new TaskF(); solver.solve(1, in, out); out.close(); } static class TaskF { int MOD = (int) 1e9 + 7; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.readInt(); int k = in.readInt(); int[] a = IOUtils.readIntArray(in, n); int[] L = new int[n]; Arrays.fill(L, -1); int[] R = new int[n]; Arrays.fill(R, n); int[] stack = new int[n]; int top = 0; for (int i = 0; i < n; ++i) { while (top > 0 && a[stack[top - 1]] <= a[i]) { top--; } if (top > 0) L[i] = stack[top - 1]; stack[top++] = i; } top = 0; for (int i = n - 1; i >= 0; --i) { while (top > 0 && a[stack[top - 1]] < a[i]) { top--; } if (top > 0) R[i] = stack[top - 1]; stack[top++] = i; } int[] num_k = new int[n + 1]; num_k[0] = 0; for (int i = 1; i <= n; ++i) { num_k[i] = (num_k[i - 1] + (i - 1) / (k - 1)) % MOD; } int res = 0; for (int i = 0; i < n; ++i) { int freq = (num_k[R[i] - L[i] - 1] - num_k[i - L[i] - 1] - num_k[R[i] - i - 1]) % MOD; if (freq < MOD) freq += MOD; long val = (long) freq * a[i]; val %= MOD; res = (res + (int) val) % MOD; } out.println(res); } } static class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] res = new int[size]; for (int i = 0; i < size; ++i) { res[i] = in.readInt(); } return res; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { // InputMismatchException -> UnknownError if (numChars == -1) throw new UnknownError(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } else if (c == '+') { c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["3 2\n9 1 10", "5 3\n5 8 7 1 9"]
2 seconds
["29", "34"]
NoteIn the first example: for $$$a=(9,1,10)$$$, $$$ans=19$$$ and $$$b=(9,10)$$$, for $$$a=(9,10)$$$, $$$ans=10$$$ and $$$b=(10)$$$, for $$$a=(10)$$$, $$$ans=0$$$. So the returned value is $$$19+10+0=29$$$.In the second example: for $$$a=(5,8,7,1,9)$$$, $$$ans=25$$$ and $$$b=(8,8,9)$$$, for $$$a=(8,8,9)$$$, $$$ans=9$$$ and $$$b=(9)$$$, for $$$a=(9)$$$, $$$ans=0$$$. So the returned value is $$$25+9+0=34$$$.
Java 8
standard input
[ "data structures", "combinatorics", "math" ]
95689b68064e7c86af938094ee3acdc3
The first line of input contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le k \le n \le 10^6$$$) — the length of the initial array $$$a$$$ and the parameter $$$k$$$. The second line of input contains $$$n$$$ integers $$$a_0, a_1, \ldots, a_{n - 1}$$$ ($$$1 \le a_{i} \le 10^9$$$) — the elements of the array $$$a$$$.
2,500
Output the only integer, the value of $$$z(a,k)$$$ modulo $$$10^9+7$$$.
standard output
PASSED
ee4e5f266764bff6db28929de77fe9b4
train_002.jsonl
1578233100
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.function.IntConsumer; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in */ 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); TaskB solver = new TaskB(in, out); try { solver.solve(); } catch (ExitException ignored) { } out.close(); } static class TaskB { FastScanner sc; PrintWriter out; public TaskB(FastScanner sc, PrintWriter out) { this.sc = sc; this.out = out; } public void solve() { times(1, this::solve); } public void solve(int time) { int n = sc.nextInt(); int k = sc.nextInt(); LongArray set = new LongArray(n); for (int i = 0; i < n; i++) { long val = 0; for (char chr : sc.nextLine().toCharArray()) { if (chr == 'S') { val = val * 3 + 0; } else if (chr == 'E') { val = val * 3 + 1; } else { val = val * 3 + 2; } } set.add(val); } set.sort(); int ans = 0; long[] array = set.array; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { long pow = 1; long m3 = 0; long m1 = array[i]; long m2 = array[j]; for (int m = 0; m < k; m++) { long v1 = m1 % 3; long v2 = m2 % 3; if (v1 == v2) { m3 += pow * v1; } else { m3 += pow * (3 - v1 - v2); } pow *= 3; m1 /= 3; m2 /= 3; } if (set.contains(m3, j)) ans++; } } answer(ans); } private void answer(Object ans) { out.println(ans); throw new ExitException(); } private void times(int n, IntConsumer consumer) { for (int i = 0; i < n; i++) { try { consumer.accept(i); } catch (ExitException ignored) { } } } } static class ExitException extends RuntimeException { } static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } 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++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public String next() { 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 (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } static class LongArray { public long[] array; public int size; public LongArray() { this(20); } public LongArray(int capacity) { this.array = new long[capacity]; } public void add(long n) { ensureCapacity(size + 1); array[size++] = n; } public void sort() { shuffle(); Arrays.sort(array, 0, size); } void shuffle() { for (int i = 0; i < size; i++) { int r = i + (int) (Math.random() * (size - i)); { long tmp = array[i]; array[i] = array[r]; array[r] = tmp; } } } public boolean contains(long n, int from) { return contains(n, from, size); } boolean contains(long n, int from, int till) { int lo = from; int hi = till; while (hi - lo > 1) { int med = (hi + lo) >> 1; long mValue = array[med]; if (mValue == n) return true; else if (mValue < n) lo = med; else hi = med; } return false; } private void ensureCapacity(int cap) { if (cap > array.length) grow(); } private void grow() { int oldCapacity = array.length; int newCapacity = oldCapacity + (oldCapacity >> 1); array = Arrays.copyOf(array, newCapacity); } } }
Java
["3 3\nSET\nETS\nTSE", "3 4\nSETE\nETSE\nTSES", "5 4\nSETT\nTEST\nEEET\nESTE\nSTES"]
3 seconds
["1", "0", "2"]
NoteIn the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
Java 8
standard input
[ "data structures", "implementation", "brute force" ]
ae7c80e068e267673a5f910bb0b121ec
The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1500$$$, $$$1 \le k \le 30$$$) — number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
Output a single integer — the number of ways to choose three cards that form a set.
standard output
PASSED
68e0c1cd56f6673c8c8bd17bf09ca563
train_002.jsonl
1578233100
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
256 megabytes
import java.io.*; import java.util.StringTokenizer; import java.util.TreeSet; public class Day9 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(reader.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); TreeSet<String> treeSet = new TreeSet<>(); String[] strings = new String[n]; for(int i = 0; i < n; ++i){ strings[i] = reader.readLine(); treeSet.add(strings[i]); } int ans = 0; for(int i = 0; i < n; ++i){ for(int j = i + 1; j < n; ++j){ char[] f = strings[i].toCharArray(); char[] s = strings[j].toCharArray(); String ss = ""; for(int l = 0; l < k; ++l){ if(f[l] == s[l]){ ss += f[l]; } else{ if(f[l] == 'S' && s[l] == 'E' || s[l] == 'S' && f[l] == 'E'){ ss += 'T'; } if(f[l] == 'S' && s[l] == 'T' || s[l] == 'S' && f[l] == 'T'){ ss += 'E'; }if(f[l] == 'E' && s[l] == 'T' || s[l] == 'E' && f[l] == 'T'){ ss += 'S'; } } } if(treeSet.contains(ss)){ ++ans; } } } writer.println(ans / 3); writer.close(); } }
Java
["3 3\nSET\nETS\nTSE", "3 4\nSETE\nETSE\nTSES", "5 4\nSETT\nTEST\nEEET\nESTE\nSTES"]
3 seconds
["1", "0", "2"]
NoteIn the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
Java 8
standard input
[ "data structures", "implementation", "brute force" ]
ae7c80e068e267673a5f910bb0b121ec
The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1500$$$, $$$1 \le k \le 30$$$) — number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
Output a single integer — the number of ways to choose three cards that form a set.
standard output
PASSED
3b3a6d0a00b28fc084a1202843e8ed18
train_002.jsonl
1578233100
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
256 megabytes
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { static Scanner scan=new Scanner(System.in); public static void main(String[] args) { int a=scan.nextInt(); int b=scan.nextInt(); Set<String>set=new HashSet<>(); String s[]=new String[a]; for(int i=0;i<a;i++) { String str=scan.next(); s[i]=str; set.add(str); } int ans=0; for(int i=0;i<a;i++) { for(int j=i+1;j<a;j++) { String str1=s[i]; String str2=s[j]; String str3=""; for(int k=0;k<b;k++) { char ch1=str1.charAt(k); char ch2=str2.charAt(k); if(ch1==ch2)str3+=(ch1); else str3+=(char)('S'+'T'+'E'-ch1-ch2); } if(set.contains(str3))ans++; } } System.out.println(ans/3); } }
Java
["3 3\nSET\nETS\nTSE", "3 4\nSETE\nETSE\nTSES", "5 4\nSETT\nTEST\nEEET\nESTE\nSTES"]
3 seconds
["1", "0", "2"]
NoteIn the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
Java 8
standard input
[ "data structures", "implementation", "brute force" ]
ae7c80e068e267673a5f910bb0b121ec
The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1500$$$, $$$1 \le k \le 30$$$) — number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
Output a single integer — the number of ways to choose three cards that form a set.
standard output
PASSED
b7152b09683e990000e21b1ce517e01e
train_002.jsonl
1578233100
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; import java.util.Vector; public class Main { public static void main(String[] args) throws IOException { StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); while (in.nextToken() != StreamTokenizer.TT_EOF) { int num1 = (int)in.nval; in.nextToken(); int num2 = (int)in.nval; String[] numLine = new String[num1]; char charSum = 'S' + 'E' + 'T'; int getNum = 0; for(int i = 0; i < num1; i++) { in.nextToken(); numLine[i] = in.sval; } Arrays.sort(numLine); for(int i1 = 0; i1 < num1; i1++) { for(int i2 = i1 + 1; i2 < num1; i2++) { StringBuilder sb = new StringBuilder(); for(int i3 = 0; i3 < num2; i3++) { if(numLine[i1].charAt(i3) == numLine[i2].charAt(i3)) { sb.append(numLine[i1].charAt(i3)); } else { sb.append((char)(charSum - numLine[i1].charAt(i3) - numLine[i2].charAt(i3))); } } String ss = sb.toString(); for (int left = 0, right = num1, mid = (left + right) / 2 ;left <= right && left < num1;) { int ans = ss.compareTo(numLine[mid]); if (ans < 0) { right = mid - 1; mid = (left + right) / 2; } else if (ans > 0) { left = mid + 1; mid = (left + right) / 2; } else if (ans == 0) { getNum++; break; } } } } out.print(getNum / 3); out.flush(); } } }
Java
["3 3\nSET\nETS\nTSE", "3 4\nSETE\nETSE\nTSES", "5 4\nSETT\nTEST\nEEET\nESTE\nSTES"]
3 seconds
["1", "0", "2"]
NoteIn the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
Java 8
standard input
[ "data structures", "implementation", "brute force" ]
ae7c80e068e267673a5f910bb0b121ec
The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1500$$$, $$$1 \le k \le 30$$$) — number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
Output a single integer — the number of ways to choose three cards that form a set.
standard output
PASSED
ac8ceb5c1d04da4d418fbc96d56e6ad2
train_002.jsonl
1578233100
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class HyperSet implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } // if modulo is required set value accordingly public static long[][] matrixMultiply2dL(long t[][],long m[][]) { long res[][]= new long[t.length][m[0].length]; for(int i=0;i<t.length;i++) { for(int j=0;j<m[0].length;j++) { res[i][j]=0; for(int k=0;k<t[0].length;k++) { res[i][j]+=t[i][k]+m[k][j]; } } } return res; } static long combination(long n,long r) { long ans=1; for(long i=0;i<r;i++) { ans=(ans*(n-i))/(i+1); } return ans; } public static void main(String args[]) throws Exception { new Thread(null, new HyperSet(),"HyperSet",1<<27).start(); } // **just change the name of class from Main to reuquired** public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n=sc.nextInt(); int k=sc.nextInt(); String s[]= new String[n]; HashSet<String> set = new HashSet<String>(); for(int i=0;i<n;++i) { s[i]=sc.next(); set.add(s[i]); } int ans=0; for(int i=0;i<n;++i) { for(int j=i+1;j<n;++j) { String req=""; for(int l=0;l<k;++l) { if(s[i].charAt(l)==s[j].charAt(l)) req+=s[i].charAt(l); else { int a[]= new int[3]; if(s[i].charAt(l)=='S' || s[j].charAt(l)=='S') a[0]++; if(s[i].charAt(l)=='E' || s[j].charAt(l)=='E') a[1]++; if(s[i].charAt(l)=='T' || s[j].charAt(l)=='T') a[2]++; if(a[0]==0) req+="S"; if(a[1]==0) req+="E"; if(a[2]==0) req+="T"; } } if(set.contains(req)) ans++; } } System.out.println(ans/3); System.out.flush(); w.close(); } }
Java
["3 3\nSET\nETS\nTSE", "3 4\nSETE\nETSE\nTSES", "5 4\nSETT\nTEST\nEEET\nESTE\nSTES"]
3 seconds
["1", "0", "2"]
NoteIn the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
Java 8
standard input
[ "data structures", "implementation", "brute force" ]
ae7c80e068e267673a5f910bb0b121ec
The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1500$$$, $$$1 \le k \le 30$$$) — number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
Output a single integer — the number of ways to choose three cards that form a set.
standard output
PASSED
2c673f3282b2be7b300e8e237afe25d9
train_002.jsonl
1578233100
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class HyperSet implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } // if modulo is required set value accordingly public static long[][] matrixMultiply2dL(long t[][],long m[][]) { long res[][]= new long[t.length][m[0].length]; for(int i=0;i<t.length;i++) { for(int j=0;j<m[0].length;j++) { res[i][j]=0; for(int k=0;k<t[0].length;k++) { res[i][j]+=t[i][k]+m[k][j]; } } } return res; } static long combination(long n,long r) { long ans=1; for(long i=0;i<r;i++) { ans=(ans*(n-i))/(i+1); } return ans; } public static void main(String args[]) throws Exception { new Thread(null, new HyperSet(),"HyperSet",1<<27).start(); } // **just change the name of class from Main to reuquired** public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n=sc.nextInt(); int k=sc.nextInt(); String s[]= new String[n]; HashSet<String> set = new HashSet<String>(); for(int i=0;i<n;++i) { s[i]=sc.next(); set.add(s[i]); } int ans=0; for(int i=0;i<n;++i) { for(int j=i+1;j<n;++j) { StringBuilder req = new StringBuilder(); for(int l=0;l<k;++l) { if(s[i].charAt(l)==s[j].charAt(l)) req.append(s[i].charAt(l)); else { int a[]= new int[3]; if(s[i].charAt(l)=='S' || s[j].charAt(l)=='S') a[0]++; if(s[i].charAt(l)=='E' || s[j].charAt(l)=='E') a[1]++; if(s[i].charAt(l)=='T' || s[j].charAt(l)=='T') a[2]++; if(a[0]==0) req.append("S"); if(a[1]==0) req.append("E"); if(a[2]==0) req.append("T"); } } if(set.contains(req.toString())) ans++; } } System.out.println(ans/3); System.out.flush(); w.close(); } }
Java
["3 3\nSET\nETS\nTSE", "3 4\nSETE\nETSE\nTSES", "5 4\nSETT\nTEST\nEEET\nESTE\nSTES"]
3 seconds
["1", "0", "2"]
NoteIn the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
Java 8
standard input
[ "data structures", "implementation", "brute force" ]
ae7c80e068e267673a5f910bb0b121ec
The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1500$$$, $$$1 \le k \le 30$$$) — number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
Output a single integer — the number of ways to choose three cards that form a set.
standard output
PASSED
e9856cc00b86e13516262052da061e33
train_002.jsonl
1578233100
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
256 megabytes
import java.io.*; import java.util.*; public class HelloWorld { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(br.readLine()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); Set<String> set = new HashSet<>(); String s[] = new String[a]; for(int i = 0; i < a; i++) { String str = br.readLine(); s[i] = str; set.add(str); } int ans = 0; for(int i = 0; i < a; i++) { for(int j = i+1; j < a; j++) { String str1 = s[i]; String str2 = s[j]; String str3 = ""; for(int k=0;k<b;k++) { char ch1 = str1.charAt(k); char ch2 = str2.charAt(k); if(ch1==ch2) str3 += (ch1); else str3 += (char)('S'+'T'+'E'-ch1-ch2); } if(set.contains(str3))ans++; } } writer.println(ans/3); br.close(); writer.close(); } }
Java
["3 3\nSET\nETS\nTSE", "3 4\nSETE\nETSE\nTSES", "5 4\nSETT\nTEST\nEEET\nESTE\nSTES"]
3 seconds
["1", "0", "2"]
NoteIn the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
Java 8
standard input
[ "data structures", "implementation", "brute force" ]
ae7c80e068e267673a5f910bb0b121ec
The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1500$$$, $$$1 \le k \le 30$$$) — number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
Output a single integer — the number of ways to choose three cards that form a set.
standard output
PASSED
4c1ca0d11fe472507e62faf13f1951b8
train_002.jsonl
1578233100
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.HashMap; 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 * * @author Jaynil */ 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); BHyperset solver = new BHyperset(); solver.solve(1, in, out); out.close(); } static class BHyperset { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); String a[] = new String[n]; long b[] = new long[n]; long ans = 0; HashMap<Long, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { String x = in.next(); long t = 0; for (int j = 0; j < k; j++) { if (x.charAt(j) == 'S') t = t * 10 + 3; else if (x.charAt(j) == 'E') t = t * 10 + 1; else { t = t * 10 + 2; } } a[i] = x; b[i] = t; if (map.containsKey(t)) { map.put(t, map.get(t) + 1); } else { map.put(t, 1); } } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { long t = 0; for (int l = 0; l < k; l++) { if (a[i].charAt(l) == a[j].charAt(l)) { if (a[i].charAt(l) == 'S') t = t * 10 + 3; else if (a[i].charAt(l) == 'E') t = t * 10 + 1; else { t = t * 10 + 2; } } else { if ((a[i].charAt(l) == 'S' && a[j].charAt(l) == 'E') || (a[i].charAt(l) == 'E' && a[j].charAt(l) == 'S')) { t = t * 10 + 2; } else if ((a[i].charAt(l) == 'T' && a[j].charAt(l) == 'E') || (a[i].charAt(l) == 'E' && a[j].charAt(l) == 'T')) t = t * 10 + 3; else { t = t * 10 + 1; } } if (map.containsKey(t)) ans += map.get(t); if (t == b[i]) { ans -= 2; } } } } out.println(ans / 3); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3 3\nSET\nETS\nTSE", "3 4\nSETE\nETSE\nTSES", "5 4\nSETT\nTEST\nEEET\nESTE\nSTES"]
3 seconds
["1", "0", "2"]
NoteIn the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
Java 8
standard input
[ "data structures", "implementation", "brute force" ]
ae7c80e068e267673a5f910bb0b121ec
The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1500$$$, $$$1 \le k \le 30$$$) — number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
Output a single integer — the number of ways to choose three cards that form a set.
standard output
PASSED
57d1062316ac9de57103fc524cfa4e81
train_002.jsonl
1578233100
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
256 megabytes
import java.util.*; import java.io.*; public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); sc.nextLine(); HashSet<String> h = new HashSet<String>(); String[] cards = new String[n]; for(int i = 0; i < n; i++) { cards[i] = sc.nextLine(); h.add(cards[i]); } int ans = 0; for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { StringBuilder t = new StringBuilder(); for(int x = 0; x < k; x++) { if(cards[i].charAt(x) == cards[j].charAt(x)) { t.append(cards[i].charAt(x)); } else { char c = (char)('S' + 'E' + 'T' - cards[i].charAt(x) - cards[j].charAt(x)); t.append(c); } } if(h.contains(t.toString())) { ans++; } } } System.out.println(ans / 3); } }
Java
["3 3\nSET\nETS\nTSE", "3 4\nSETE\nETSE\nTSES", "5 4\nSETT\nTEST\nEEET\nESTE\nSTES"]
3 seconds
["1", "0", "2"]
NoteIn the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
Java 8
standard input
[ "data structures", "implementation", "brute force" ]
ae7c80e068e267673a5f910bb0b121ec
The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1500$$$, $$$1 \le k \le 30$$$) — number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
Output a single integer — the number of ways to choose three cards that form a set.
standard output
PASSED
660e4933d147a9c833cc7c132dbf6136
train_002.jsonl
1578233100
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); String[] str = br.readLine().split(" "); int n = Integer.parseInt(str[0]), k = Integer.parseInt(str[1]); String[] data = new String[n]; char[] vals = new char[]{'S', 'E', 'T'}; HashMap<String, Integer> hm = new HashMap<>(); for(int i = 0; i<n; i++){ data[i] = br.readLine(); hm.computeIfPresent(data[i], (key, val)->val+=1); hm.putIfAbsent(data[i], 1); } int cnt = 0; for(int i = 0; i<n; i++){ String s1 = data[i]; for(int j = i+1; j<n; j++){ String s2 = data[j]; String toFind; char[] c1 = new char[k]; for(int z = 0; z<k; z++){ char s1c = s1.charAt(z), s2c = s2.charAt(z); if(s1c == s2c) c1[z] = s1c; else{ for(int z1 = 0; z1<3; z1++){ if(s1c!=vals[z1] && s2c!=vals[z1]){ c1[z] = vals[z1]; } } } } toFind = String.valueOf(c1); if(hm.containsKey(toFind)) { cnt += hm.get(toFind); if (toFind.equals(data[i])) cnt -= 2; } } } System.out.println(cnt/3); } }
Java
["3 3\nSET\nETS\nTSE", "3 4\nSETE\nETSE\nTSES", "5 4\nSETT\nTEST\nEEET\nESTE\nSTES"]
3 seconds
["1", "0", "2"]
NoteIn the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
Java 8
standard input
[ "data structures", "implementation", "brute force" ]
ae7c80e068e267673a5f910bb0b121ec
The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1500$$$, $$$1 \le k \le 30$$$) — number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
Output a single integer — the number of ways to choose three cards that form a set.
standard output
PASSED
62b50e535c2ef8c2b37e32e3b57592a3
train_002.jsonl
1578233100
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
256 megabytes
// package Strings; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class zCF_2B_HyperSet { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); ArrayList<String> list = new ArrayList<>(); HashMap<String, Integer> map = new HashMap<>(); for(int i = 1; i<= n; i++) { String s = sc.next(); list.add(s); map.put(s,i-1); } int count = 0; for(int i =0; i<= n-1;i++) { for(int j = i+1; j<= n-1; j++) { String requiredString = nextCorrectString(list.get(i), list.get(j)); if(map.containsKey(requiredString)&& map.get(requiredString) >j) { count++; } } } System.out.println(count); //below code is correct, but it gives TLE for larger inputs //T(n) - O(n*n*n*k) /* HashSet<String> set = new HashSet<>(); set.add("SSS"); set.add("TTT"); set.add("EEE"); set.add("SET"); set.add("STE"); set.add("EST"); set.add("ETS"); set.add("TSE"); set.add("TES"); int count = 0; for(int i = 0; i<= n-1; i++) { for(int j = i+1; j<= n-1; j++) { for(int t = j+1; t<= n-1; t++) { int p = 0; for(; p<= k-1; p++) { String s = ""; s+= list.get(i).charAt(p); s+= list.get(j).charAt(p); s+= list.get(t).charAt(p); if(!set.contains(s)) { break; } } if(p==k) { count++; } } } } System.out.println(count); */ } public static String nextCorrectString(String a, String b) { String req = ""; int l = a.length(); for(int i = 0; i<= l-1; i++) { char c = a.charAt(i); char d = b.charAt(i); if(c==d) { req+= c; } else { char next = ' '; if(c=='S') { next = (d=='E')? 'T':'E'; } else if(c=='E') { next = (d=='T')? 'S':'T'; } else { next = (d=='S')?'E':'S'; } req+= next; } } return req; } }
Java
["3 3\nSET\nETS\nTSE", "3 4\nSETE\nETSE\nTSES", "5 4\nSETT\nTEST\nEEET\nESTE\nSTES"]
3 seconds
["1", "0", "2"]
NoteIn the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
Java 8
standard input
[ "data structures", "implementation", "brute force" ]
ae7c80e068e267673a5f910bb0b121ec
The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1500$$$, $$$1 \le k \le 30$$$) — number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
Output a single integer — the number of ways to choose three cards that form a set.
standard output
PASSED
43190ccd583cb34ecffa4996b449171e
train_002.jsonl
1578233100
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
256 megabytes
import java.io.*; import java.util.*; public class sixtwelveB { static int[][] cards; public static void main(String[] args) throws Exception { FastScanner input = new FastScanner(System.in); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); // FastScanner input = new FastScanner("sixtwelveB.in"); // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("sixtwelveB.out"))); int N = input.nextInt(); int K = input.nextInt(); cards = new int[N][K]; for (int i = 0; i < N; i++){ String c = input.next(); for (int j = 0; j < K; j++){ if (c.charAt(j) == 'S'){ cards[i][j] = 0; } else if (c.charAt(j) == 'E'){ cards[i][j] = 1; } else{ cards[i][j] = 2; } } } // System.out.println(Arrays.deepToString(cards)); Data data = new Data(0); for (int[] i : cards){ data.members.add(i); } data.pushToChildren(); int ret = 0; for (int i = 0; i < N; i++){ outerloop: for (int j = i + 1; j < N; j++){ Data c = data; for (int d = 0; d < K; d++){ int needed = (3 - ((cards[i][d] + cards[j][d]) % 3)) % 3; c = c.children[needed]; if (c == null || c.members.isEmpty()) continue outerloop; } ret += c.members.size(); } } System.out.println(ret/3); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); st = new StringTokenizer(""); } public FastScanner(String fileName) throws Exception { br = new BufferedReader(new FileReader(new File(fileName))); 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 long nextLong() throws Exception { return Long.parseLong(next()); } public Double nextDouble() throws Exception { return Double.parseDouble(next()); } public String nextLine() throws Exception { if (st.hasMoreTokens()) { StringBuilder str = new StringBuilder(); boolean first = true; while (st.hasMoreTokens()) { if (first) { first = false; } else { str.append(" "); } str.append(st.nextToken()); } return str.toString(); } else { return br.readLine(); } } } } class Data{ int storingIndex; LinkedList<int[]> members = new LinkedList<>(); Data[] children = new Data[3]; public Data(int sIndex){ storingIndex = sIndex; } public void pushToChildren(){ if (members.isEmpty()) return; if (storingIndex == members.getFirst().length) return; for (int i = 0; i < 3; i++) children[i] = new Data(storingIndex + 1); if (storingIndex < members.getFirst().length) { for (int[] i : members) { children[i[storingIndex]].members.add(i); } } for (int i = 0; i < 3; i++){ children[i].pushToChildren(); } } public void print(){ String space = ""; for (int i = 0; i < storingIndex; i++){ space += " "; } System.out.println(space + Arrays.deepToString(members.toArray())); if (children[0] != null){ children[0].print(); children[1].print(); children[2].print(); } } }
Java
["3 3\nSET\nETS\nTSE", "3 4\nSETE\nETSE\nTSES", "5 4\nSETT\nTEST\nEEET\nESTE\nSTES"]
3 seconds
["1", "0", "2"]
NoteIn the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
Java 8
standard input
[ "data structures", "implementation", "brute force" ]
ae7c80e068e267673a5f910bb0b121ec
The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1500$$$, $$$1 \le k \le 30$$$) — number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
Output a single integer — the number of ways to choose three cards that form a set.
standard output
PASSED
4a1d5370dea01a2f12d62027ff14d30b
train_002.jsonl
1578233100
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; public class Main { public static void main(String[] args)throws Exception { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); String [] line=in.readLine().split(" "); int n=Integer.parseInt(line[0]); int k=Integer.parseInt(line[1]); HashSet<String>set=new HashSet<>(); ArrayList<String>list=new ArrayList<>(); for (int i = 0; i < n; i++) { String line1=in.readLine(); set.add(line1); list.add(line1); } int count=0; for (int i = 0; i < list.size(); i++) { String first=list.get(i); for (int j = i+1; j < list.size(); j++) { String second=list.get(j); StringBuilder StringNeeded=new StringBuilder(); for (int l = 0; l < k; l++) { if(second.charAt(l)==first.charAt(l)) StringNeeded.append(second.charAt(l)+""); else if(second.charAt(l)!='E'&&first.charAt(l)!='E') StringNeeded.append("E"); else if(second.charAt(l)!='S'&&first.charAt(l)!='S') StringNeeded.append("S"); else if(second.charAt(l)!='T'&&first.charAt(l)!='T') StringNeeded.append("T"); } if(set.contains(StringNeeded.toString())) count++; } } System.out.println(count/3); } }
Java
["3 3\nSET\nETS\nTSE", "3 4\nSETE\nETSE\nTSES", "5 4\nSETT\nTEST\nEEET\nESTE\nSTES"]
3 seconds
["1", "0", "2"]
NoteIn the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
Java 8
standard input
[ "data structures", "implementation", "brute force" ]
ae7c80e068e267673a5f910bb0b121ec
The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1500$$$, $$$1 \le k \le 30$$$) — number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
Output a single integer — the number of ways to choose three cards that form a set.
standard output
PASSED
f549cad785bb42faed5cbed9f4407373
train_002.jsonl
1578233100
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.HashSet; import java.util.StringTokenizer; public class HyperSet { public static void main(String[] args) { FastReader input=new FastReader(); int n=input.nextInt(); int l=input.nextInt(); String str[]=new String[n]; HashMap<String,Integer> map=new HashMap<>(); for(int i=0;i<n;i++) { str[i]=input.next(); map.put(str[i],i); } int count=0; int sum='S'+'T'+'E'; for(int i=0;i<n-2;i++) { for(int j=i+1;j<n-1;j++) { String s1=str[i]; String s2=str[j]; String s3=""; for(int k=0;k<l;k++) { if(s1.charAt(k)==s2.charAt(k)) { s3+=s1.charAt(k); } else { s3+=(char)(sum-s1.charAt(k)-s2.charAt(k)); } } if(map.containsKey(s3)) { int key=map.get(s3); if(key>j) { count++; } } } } System.out.println(count); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3 3\nSET\nETS\nTSE", "3 4\nSETE\nETSE\nTSES", "5 4\nSETT\nTEST\nEEET\nESTE\nSTES"]
3 seconds
["1", "0", "2"]
NoteIn the third example test, these two triples of cards are sets: "SETT", "TEST", "EEET" "TEST", "ESTE", "STES"
Java 8
standard input
[ "data structures", "implementation", "brute force" ]
ae7c80e068e267673a5f910bb0b121ec
The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1500$$$, $$$1 \le k \le 30$$$) — number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
Output a single integer — the number of ways to choose three cards that form a set.
standard output
PASSED
d9745cabda58a439097e4c3b384a539e
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); while(n-- > 0){ String temp = input.next(); if(temp.matches("R[0-9]+C[0-9]+")){ //convert from Rnum Cnum to Cchar Rnum String temp2[] = temp.replaceAll("[R|C]", " ").trim().split(" "); String str = ""; int column = Integer.parseInt(temp2[1]); while(column > 0){ column = column - 1; str = (char)(column % 26 + 'A') + str; column = column / 26; } System.out.println(str +""+ temp2[0]); }else{ //reverse of above~ String column = temp.replaceAll("[0-9]", ""); String row = temp.replaceAll("[A-Z]", ""); int col = 0; for(int j = 0; j < column.length();j++){ col = col * 26 + column.charAt(j) - 'A' + 1; } System.out.println("R"+row+"C"+col); } } } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
11602ad66fd34fde0d20aa2c7c36d48d
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.util.Arrays; import java.util.Locale; import java.util.Scanner; public class MainTest { static void print(int a[]) { for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } static int sum(int []a) { int res=0; for(int i=0;i<a.length;i++) res+=a[i]; return res; } static int getMax() { int max=0; int k=0; for(int i=0;i<a.length;i++) if(a[i]>max) {max=a[i]; k=i;} a[k]=-1; return max; } static int[] getSimple(int from,int to) { int a[]=new int[to-from]; for(int i=0;i<a.length;i++) a[i]=from+i; //print(a); int sum=0; for(int i=0;i<a.length;i++) { if(a[i]>0) { for(int j=i+1;j<a.length;j++) if(a[j]%a[i]==0) a[j]=0; sum++; } } int b[]=new int[sum]; sum=0; for(int i=0;i<a.length;i++) if(a[i]>0) {b[sum]=a[i]; sum++;} //System.out.println("SIMPLE!"); //print(a); return b; } static int a[]; public static void main(String args[]) { Scanner in=new Scanner(System.in); int n=in.nextInt(); String str; for(int i=0;i<n;i++) { str=in.next(); //System.out.println(str); if(str.charAt(0)=='R'&&(int)str.charAt(1)>=48&&(int)str.charAt(1)<=57&&str.indexOf("C")>1) { int k=0; int s=0; String res=""; int e; for(int j=0;j<str.length();j++) if((int)str.charAt(j)>=48&&(int)str.charAt(j)<=57) {e=j; break;} k=(new Integer(str.substring(str.lastIndexOf("C")+1))); while(true) { if(k<=Math.pow(26, s)) {break;} s++; } s=0; // s=k // temp=k // res=temp2; if(k>26) { while(true) { if(k<=26) { res=(char)(64+k)+res; if(s>26) { k=s; s=0; } else break; } k-=26; s++; } res=(char)(64+s)+res; System.out.println(res+str.substring(str.indexOf("R")+1, str.indexOf("C"))); } else System.out.println((char)(k+64)+str.substring(1,str.lastIndexOf("C"))); /*k=(new Integer(str.substring(str.lastIndexOf("C")+1))); res=""; if(k>26) { while(true) { res+=(char)(64+k/26)+res; if(k/26>26) k=k/26; else break; } res=(char)(64+k%26)+res; System.out.println(res+str.substring(str.indexOf("R")+1, str.indexOf("C"))); } else System.out.println((char)(k+64)+str.substring(1,str.lastIndexOf("C"))); */ } else { int k=0; int res=0; for(int j=0;j<str.length();j++) if((int)str.charAt(j)>=48&&(int)str.charAt(j)<=57) {k=j; break;} for(int j=0;j<k;j++) { res+=(int)(Math.pow(26, k-1-j))*(str.charAt(j)-64); } System.out.println("R"+str.substring(k,str.length())+"C"+res); } } //for(int i=0;i<256;i++) System.out.println(i+": "+(char)i); } } /* temp=new Integer(s[i].substring(s[i].indexOf("C")+1)); if(temp>26) { while(true) { if(temp<=26) { temp2=(char)(64+temp)+temp2; if(k>26) { temp=k.intValue(); k=0; } else break; } temp-=26; k++; } temp2=(char)(64+k)+temp2; } else temp2=(char)(64+temp)+temp2; System.out.println(temp2+s[i].substring(s[i].indexOf("R")+1, s[i].indexOf("C"))); */
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
0cc3e9967f29b20957e377a67e56ec95
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; public class B1 { static class MyScanner2 { BufferedReader br; StringTokenizer st; public MyScanner2() { 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; } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } } public static int pow(int a, int exp) { int result = 1; while(exp>0) { result *= a; exp--; } return result; } public static String convert(int num) { int test= 0; int n; int testcase = 0; while(pow(26, test) < num) { testcase += pow(26,test); test++; } test--; StringBuffer result = new StringBuffer(); //System.out.println("***" + num); //System.out.println("***" + test); //System.out.println("***" + pow(26,test)); while(num > 0) { n = num/pow(26,test); //for(int i=0;i<=test;i++) //{ // testcase += pow(26,i); //} //System.out.println(testcase); if(num < testcase) { result.append("Z"); num -= n*pow(26,test); testcase -= pow(26,test); testcase -= pow(26,test - 1); test-=2; //System.out.println(test); //System.out.println(num); } else { if (num%pow(26,test)==0 && test!=0) n--; result.append((char) ('A' + n - 1)); //System.out.println(test); //System.out.println(num); //System.out.println(pow(26,test)); //System.out.println(num/pow(26,test)); //System.out.println((char) ('A' + n - 1)); //System.out.println((char) ('A' + num/pow(26,test) - 1)); num = num - n*pow(26,test); testcase -= pow(26,test); //System.out.println(num); test--; } } return result.toString(); } static String toAlph(int N) { if(N <= 26) return String.valueOf(arr[N-1]); else return toAlph((N-1)/26) + arr[(N-1)%26]; } static char arr[] = new char[26]; public static void main(String[] args) { // TODO Auto-generated method stub MyScanner2 scan = new MyScanner2(); StringBuffer ans = new StringBuffer(); char alpha='A'; for(int i=0;i<26;i++) arr[i]=alpha++; int num = scan.nextInt(); //String[] ans = new String[num]; for(int j = 0; j < num; j++) { String str = scan.next(); StringBuffer temp = new StringBuffer(); //String test2 ="R85"; //String test3 ="R85C33"; //String test4 ="223"; //System.out.println(testmode); //System.out.println(testmode.matches("\\d")); //System.out.println(testmode.matches("\\d")); // System.out.println(test2.matches("R\\d+C\\d+")); // System.out.println(test3.matches("R\\d+C\\d+")); //System.out.println(test2.matches("\\D+\\d+")); //System.out.println(test3.matches("\\D+\\d+")); //System.out.println(test3.matches("\\d")); //System.out.println(testmode.matches("\d")); if (str.matches("R\\d+C\\d+")) { int[] abc = new int[2]; Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher(str); for (int i = 0; m.find(); i++) { abc[i] = Integer.parseInt(m.group()); } //System.out.println(Arrays.toString(abc)); //temp.append(convert(abc[1])); //temp.append(abc[0]); ans.append(toAlph(abc[1])+abc[0]+"\n"); } else { String[] part = str.split("(?<=\\D)(?=\\d)"); int result = 0; temp.append( "R" + part[1]); for(int i = 0 ; i < part[0].length(); i++) { result += pow(26, part[0].length() - i - 1) * ((int) (part[0].charAt(i) - 'A' + 1)); } temp.append("C" + result); ans.append(temp+"\n"); } } System.out.println(ans); } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
06b9a8a1f42b00e550e79ecd2047f473
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; public class B1 { static class MyScanner2 { BufferedReader br; StringTokenizer st; public MyScanner2() { 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; } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } } public static int pow(int a, int exp) { int result = 1; while(exp>0) { result *= a; exp--; } return result; } /* public static String convert(int num) { int test= 0; int n; int testcase = 0; while(pow(26, test) < num) { testcase += pow(26,test); test++; } test--; StringBuffer result = new StringBuffer(); //System.out.println("***" + num); //System.out.println("***" + test); //System.out.println("***" + pow(26,test)); while(num > 0) { n = num/pow(26,test); //for(int i=0;i<=test;i++) //{ // testcase += pow(26,i); //} //System.out.println(testcase); if(num < testcase) { result.append("Z"); num -= n*pow(26,test); testcase -= pow(26,test); testcase -= pow(26,test - 1); test-=2; //System.out.println(test); //System.out.println(num); } else { if (num%pow(26,test)==0 && test!=0) n--; result.append((char) ('A' + n - 1)); //System.out.println(test); //System.out.println(num); //System.out.println(pow(26,test)); //System.out.println(num/pow(26,test)); //System.out.println((char) ('A' + n - 1)); //System.out.println((char) ('A' + num/pow(26,test) - 1)); num = num - n*pow(26,test); testcase -= pow(26,test); //System.out.println(num); test--; } } return result.toString(); } */ static String convert(int N) { if(N <= 26) return String.valueOf(arr[N-1]); else return convert((N-1)/26) + arr[(N-1)%26]; } static char arr[] = new char[26]; public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); StringBuffer ans = new StringBuffer(); char alpha='A'; for(int i=0;i<26;i++) arr[i]=alpha++; int num = scan.nextInt(); //String[] ans = new String[num]; for(int j = 0; j < num; j++) { String str = scan.next(); //StringBuffer temp = new StringBuffer(); //String test2 ="R85"; //String test3 ="R85C33"; //String test4 ="223"; //System.out.println(testmode); //System.out.println(testmode.matches("\\d")); //System.out.println(testmode.matches("\\d")); // System.out.println(test2.matches("R\\d+C\\d+")); // System.out.println(test3.matches("R\\d+C\\d+")); //System.out.println(test2.matches("\\D+\\d+")); //System.out.println(test3.matches("\\D+\\d+")); //System.out.println(test3.matches("\\d")); //System.out.println(testmode.matches("\d")); if (str.matches("R\\d+C\\d+")) { int[] abc = new int[2]; Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher(str); for (int i = 0; m.find(); i++) { abc[i] = Integer.parseInt(m.group()); } //System.out.println(Arrays.toString(abc)); //temp.append(convert(abc[1])); //temp.append(abc[0]); ans.append(convert(abc[1])+abc[0]+"\n"); } else { String[] part = str.split("(?<=\\D)(?=\\d)"); int result = 0; for(int i = 0 ; i < part[0].length(); i++) { result += pow(26, part[0].length() - i - 1) * ((int) (part[0].charAt(i) - 'A' + 1)); } ans.append( "R" + part[1]+"C" + result+"\n"); } } System.out.println(ans); } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
8395a71fcd72eb94b6c8f12396ed04ea
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.util.*; import java.io.*; public class Spreadsheets { static char arr[]; static String toAlph(int N) { if(N <= 26) return String.valueOf(arr[N-1]); else return toAlph((N-1)/26) + arr[(N-1)%26]; } public static void main(String args[]) { MyScanner s1=new MyScanner(); PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out), true); StringBuffer sb = new StringBuffer(); int inputs=s1.nextInt(); String str; arr=new char[26]; char alpha='A'; for(int i=0;i<26;i++) arr[i]=alpha++; alpha = 'A'; for(int i=1;i<=inputs;i++) { str=s1.next(); int len = str.length(); int i1,i2; if(( i1=str.indexOf('R')) >=0 && i1+1<len && Character.isDigit(str.charAt(i1+1)) && ( i2=str.indexOf('C')) >=0 && i2+1<len && Character.isDigit(str.charAt(i2+1))) { int row = Integer.parseInt(str.substring(i1+1, i2)); int col = Integer.parseInt(str.substring(i2+1)); sb.append(toAlph(col)).append(row+"\n"); } else { int index = -1; for(int j=0;j<len;j++) { if(Character.isDigit(str.charAt(j))) { index = j; break; } } String row = str.substring(index); String c = str.substring(0,index); int col =0; int l =c.length(); for(int j=l-1;j>=0;j--) { col+=((c.charAt(j)-alpha+1)*Math.pow(26, l-j-1)); } sb.append("R"+row+"C"+col+"\n"); } } out.println(sb); out.close(); } } 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\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
20da3dab376b53b293812a3f247e0bad
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; public class B1 { static class MyScanner2 { BufferedReader br; StringTokenizer st; public MyScanner2() { 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; } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } } public static int pow(int a, int exp) { int result = 1; while(exp>0) { result *= a; exp--; } return result; } public static String convert(int num) { int test= 0; int n; int testcase = 0; while(pow(26, test) < num) { testcase += pow(26,test); test++; } test--; StringBuffer result = new StringBuffer(); //System.out.println("***" + num); //System.out.println("***" + test); //System.out.println("***" + pow(26,test)); while(num > 0) { n = num/pow(26,test); //for(int i=0;i<=test;i++) //{ // testcase += pow(26,i); //} //System.out.println(testcase); if(num < testcase) { result.append("Z"); num -= n*pow(26,test); testcase -= pow(26,test); testcase -= pow(26,test - 1); test-=2; //System.out.println(test); //System.out.println(num); } else { if (num%pow(26,test)==0 && test!=0) n--; result.append((char) ('A' + n - 1)); //System.out.println(test); //System.out.println(num); //System.out.println(pow(26,test)); //System.out.println(num/pow(26,test)); //System.out.println((char) ('A' + n - 1)); //System.out.println((char) ('A' + num/pow(26,test) - 1)); num = num - n*pow(26,test); testcase -= pow(26,test); //System.out.println(num); test--; } } return result.toString(); } static String toAlph(int N) { if(N <= 26) return String.valueOf(arr[N-1]); else return toAlph((N-1)/26) + arr[(N-1)%26]; } static char arr[] = new char[26]; public static void main(String[] args) { // TODO Auto-generated method stub MyScanner2 scan = new MyScanner2(); StringBuffer ans = new StringBuffer(); char alpha='A'; for(int i=0;i<26;i++) arr[i]=alpha++; int num = scan.nextInt(); //String[] ans = new String[num]; for(int j = 0; j < num; j++) { String str = scan.next(); //StringBuffer temp = new StringBuffer(); //String test2 ="R85"; //String test3 ="R85C33"; //String test4 ="223"; //System.out.println(testmode); //System.out.println(testmode.matches("\\d")); //System.out.println(testmode.matches("\\d")); // System.out.println(test2.matches("R\\d+C\\d+")); // System.out.println(test3.matches("R\\d+C\\d+")); //System.out.println(test2.matches("\\D+\\d+")); //System.out.println(test3.matches("\\D+\\d+")); //System.out.println(test3.matches("\\d")); //System.out.println(testmode.matches("\d")); if (str.matches("R\\d+C\\d+")) { int[] abc = new int[2]; Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher(str); for (int i = 0; m.find(); i++) { abc[i] = Integer.parseInt(m.group()); } //System.out.println(Arrays.toString(abc)); //temp.append(convert(abc[1])); //temp.append(abc[0]); ans.append(toAlph(abc[1])+abc[0]+"\n"); } else { String[] part = str.split("(?<=\\D)(?=\\d)"); int result = 0; for(int i = 0 ; i < part[0].length(); i++) { result += Math.pow(26, part[0].length() - i - 1) * ((int) (part[0].charAt(i) - 'A' + 1)); } ans.append( "R" + part[1]+"C" + result+"\n"); } } System.out.println(ans); } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
8a4f5ff8719a207eb66cf72ed842218f
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; public class B1 { public static int pow(int a, int exp) { int result = 1; while(exp>0) { result *= a; exp--; } return result; } /* public static String convert(int num) { int test= 0; int n; int testcase = 0; while(pow(26, test) < num) { testcase += pow(26,test); test++; } test--; StringBuffer result = new StringBuffer(); //System.out.println("***" + num); //System.out.println("***" + test); //System.out.println("***" + pow(26,test)); while(num > 0) { n = num/pow(26,test); //for(int i=0;i<=test;i++) //{ // testcase += pow(26,i); //} //System.out.println(testcase); if(num < testcase) { result.append("Z"); num -= n*pow(26,test); testcase -= pow(26,test); testcase -= pow(26,test - 1); test-=2; //System.out.println(test); //System.out.println(num); } else { if (num%pow(26,test)==0 && test!=0) n--; result.append((char) ('A' + n - 1)); //System.out.println(test); //System.out.println(num); //System.out.println(pow(26,test)); //System.out.println(num/pow(26,test)); //System.out.println((char) ('A' + n - 1)); //System.out.println((char) ('A' + num/pow(26,test) - 1)); num = num - n*pow(26,test); testcase -= pow(26,test); //System.out.println(num); test--; } } return result.toString(); } */ static String convert(int N) { if(N <= 26) return String.valueOf(arr[N-1]); else return convert((N-1)/26) + arr[(N-1)%26]; } static char arr[] = new char[26]; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // TODO Auto-generated method stub //Scanner scan = new Scanner(System.in); StringBuffer ans = new StringBuffer(); char alpha='A'; for(int i=0;i<26;i++) arr[i]=alpha++; int num = Integer.parseInt(br.readLine()); //String[] ans = new String[num]; for(int j = 0; j < num; j++) { String str = br.readLine(); //StringBuffer temp = new StringBuffer(); //String test2 ="R85"; //String test3 ="R85C33"; //String test4 ="223"; //System.out.println(testmode); //System.out.println(testmode.matches("\\d")); //System.out.println(testmode.matches("\\d")); // System.out.println(test2.matches("R\\d+C\\d+")); // System.out.println(test3.matches("R\\d+C\\d+")); //System.out.println(test2.matches("\\D+\\d+")); //System.out.println(test3.matches("\\D+\\d+")); //System.out.println(test3.matches("\\d")); //System.out.println(testmode.matches("\d")); if (str.matches("R\\d+C\\d+")) { int[] abc = new int[2]; Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher(str); for (int i = 0; m.find(); i++) { abc[i] = Integer.parseInt(m.group()); } //System.out.println(Arrays.toString(abc)); //temp.append(convert(abc[1])); //temp.append(abc[0]); ans.append(convert(abc[1])+abc[0]+"\n"); } else { String[] part = str.split("(?<=\\D)(?=\\d)"); int result = 0; for(int i = 0 ; i < part[0].length(); i++) { result += pow(26, part[0].length() - i - 1) * ((int) (part[0].charAt(i) - 'A' + 1)); } ans.append( "R" + part[1]+"C" + result+"\n"); } } System.out.println(ans); } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
31462f8ee6e0a90de54f4702f3663ea3
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.util.Scanner; public class Spreadsheet { public static String convert(String input) { String output = ""; if(input.charAt(0) == 'R' && Character.isDigit(input.charAt(1)) && input.indexOf('C')>1) { int row, column; row = Integer.parseInt(input.substring(1, input.indexOf('C'))); column = Integer.parseInt(input.substring(input.indexOf('C')+1)); //System.out.println(row); //System.out.println(column); String colm = ""; while(column > 0) { if(column % 26 == 0) { colm = "Z" + colm; column = column / 26 - 1; } else { colm = (char)(64 + column % 26) + colm; column = column / 26; } } output = colm + row; } else { output = "R"; String row, column; row = input.replaceAll("\\p{Alpha}",""); output += row; output += "C"; column = input.replaceAll("[^A-Z]",""); int colm = 0; int L = column.length(); int colCount = 1; for(int i = L - 1; i >= 0; i--) { colm += colCount * ((int)(column.charAt(i) - 64)); colCount *= 26; } output += Integer.toString(colm); } return output; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); //in.nextLine(); //int n = Integer.parseInt(in.nextLine()); String input; for(int i = 1; i <= n; i++) { //System.out.println(i); input = in.next(); //input = in.nextLine(); System.out.println(convert(input)); } in.close(); } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
7026cc723b10c0f81552d55d7e797417
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Jose */ import java.util.ArrayList; import java.util.Scanner; public class Excel { public static void main(String[] args) { Scanner in = new Scanner(System.in); int casos = Integer.parseInt(in.nextLine()); for (int k = 0; k < casos; k++) { String a = in.next(); int posR = a.indexOf("R"); int posC = a.indexOf("C"); String looo = "1234567890"; int rec = 0; for (int i = 0; i < a.length(); i++) { if (looo.contains(a.charAt(i) + "")) { rec = i; break; } } if (posR != -1 && posC != -1 && rec<posC) { int num = Integer.parseInt(a.substring(posC + 1, a.length())); System.out.println(ex(num) + a.substring(posR + 1, posC)); } else { String let = a.substring(0, rec); System.out.println("R" + a.substring(rec, a.length()) + "C" + (rev(let))); } } } public static String ex(long x) { String res = ""; while (x > 0) { if (x <= 26) { char b = (char) (x + 64); res += b + ""; x = 0; } else if (x % 26 == 0) { x = x - 1; long div = x / 26; long resi = x % 26 + 1; x = div; char b = (char) (resi + 64); res += b + ""; } else { long div = x / 26; long resi = x % 26; x = div; char b = (char) (resi + 64); res += b; } } String up = ""; for (int i = res.length() - 1; i >= 0; i--) { up += res.charAt(i); } return up; } public static long rev(String x) { String let = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; long resp = 0; int p = x.length() - 1; for (int i = 0; i < x.length(); i++) { for (int j = 0; j < let.length(); j++) { if (let.charAt(j) == x.charAt(i)) { resp += (j + 1) * Math.pow(26, p); p--; break; } } } return resp; } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
2c4a50bd80da301d6f6c3dc6b1b278f4
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Jose */ import java.util.ArrayList; import java.util.Scanner; public class Excel { public static void main(String[] args) { Scanner in = new Scanner(System.in); int casos = Integer.parseInt(in.nextLine()); for (int k = 0; k < casos; k++) { String a = in.next(); int posR = a.indexOf("R"); int posC = a.indexOf("C"); String looo = "1234567890"; int rec = 0; for (int i = 0; i < a.length(); i++) { if (looo.contains(a.charAt(i) + "")) { rec = i; break; } } if (posR != -1 && posC != -1 && rec<posC) { int num = Integer.parseInt(a.substring(posC + 1, a.length())); System.out.println(exa(num) + a.substring(posR + 1, posC)); } else { String let = a.substring(0, rec); System.out.println("R" + a.substring(rec, a.length()) + "C" + (rev(let))); } } } public static String exa(long a) { String letra=""; for(int i=0;a>0;i++) { if(a%26==0) { a=a/26 - 1; letra+="Z"; } else { long p=a%26; letra+= (char)(p+64); a=(a-a%26)/26; } } StringBuilder Excel=new StringBuilder(letra); String jk=Excel.reverse()+""; return jk; } public static String ex(long x) { String res = ""; while (x > 0) { if (x <= 26) { char b = (char) (x + 64); res += b + ""; x = 0; } else if (x % 26 == 0) { x = x - 1; long div = x / 26; long resi = x % 26 + 1; x = div; char b = (char) (resi + 64); res += b + ""; } else { long div = x / 26; long resi = x % 26; x = div; char b = (char) (resi + 64); res += b; } } String up = ""; for (int i = res.length() - 1; i >= 0; i--) { up += res.charAt(i); } return up; } public static long rev(String x) { String let = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; long resp = 0; int p = x.length() - 1; for (int i = 0; i < x.length(); i++) { for (int j = 0; j < let.length(); j++) { if (let.charAt(j) == x.charAt(i)) { resp += (j + 1) * Math.pow(26, p); p--; break; } } } return resp; } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
257c4ac7f86f2a89cebd6125b2fb0713
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.util.Scanner; public class Spreadsheets { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String[] input = new String[n]; for(int i=0;i<n;i++){ input[i] = sc.next(); } sc.close(); for(int i=0;i<n;i++){ if(input[i].matches("R[0-9]+C[0-9]+")){ fromRToB(input[i]); } if(input[i].matches("[A-Z]+[0-9]+")){ fromBToR(input[i]); } } } public static void fromBToR(String str) { String[] temp = str.split("[0-9]"); String te = temp[0]; int len = te.length(); int col = 0; String row = str.substring(len, str.length()); for(int i=0;i<len;i++){ col += (te.charAt(len-i-1)-'A'+1) * Math.pow(26,i); } System.out.println('R'+ row + 'C' +col); } public static void fromRToB(String str) { int start = str.indexOf('R'); int end = str.indexOf('C'); String tempRow = (String) str.subSequence(start+1, end); String tempCol = (String) str.subSequence(end+1, str.length()); int row = 0; int col = 0; int temp = 0; try{ row = Integer.parseInt(tempRow); col = Integer.parseInt(tempCol); temp = col; }catch(Exception e){ System.out.println("............"); } StringBuffer bs = new StringBuffer(); while(temp > 0){ int t = temp%26; if(t != 0){ bs.append((char)(t+'A'-1)); temp /= 26; } else{ bs.append('Z'); temp = temp / 26 - 1; } } String te = ""; for(int i=bs.length()-1;i>=0;i--){ te += bs.charAt(i); } System.out.println(te+row); } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
901293d02cf3fe7a6c2114ae10c6fba6
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static String charFromInt(int c) { return Character.toString((char)((int)'A' + c)); } public static int intFromChar(char c) { return ((int)c - (int)'A'); } public static String plainToExcel(String s) { //R23C55 to BC23 String[] RC = s.split("C"); RC[0] = RC[0].substring(1, RC[0].length()); int colNumber = Integer.parseInt(RC[1]); colNumber --; if (colNumber < 26) { RC[1] = charFromInt(colNumber); } else { colNumber -= 26; if (colNumber < 26 * 26) { RC[1] = charFromInt(colNumber / 26) + charFromInt(colNumber % 26); } else { colNumber -= 26*26; if (colNumber < 26 * 26 * 26) { RC[1] = charFromInt(colNumber / (26 * 26)) + charFromInt((colNumber / 26) % 26) + charFromInt(colNumber % 26); } else { colNumber -= 26*26*26; if (colNumber < 26*26*26*26) { RC[1] = charFromInt(colNumber / (26 * 26 * 26)) + charFromInt((colNumber / (26 * 26)) % 26) + charFromInt((colNumber / 26) % 26) + charFromInt(colNumber % 26); } else { colNumber -= 26*26*26*26; RC[1] = charFromInt(colNumber / (26 * 26 * 26 * 26)) + charFromInt((colNumber / (26 * 26 * 26)) % 26) + charFromInt((colNumber / (26 * 26)) % 26) + charFromInt(colNumber / 26 % 26) + charFromInt(colNumber % 26); } } } } return RC[1] + RC[0]; } public static String excelToPlain(String s) { String[] RC = new String[2]; Pattern patternCol = Pattern.compile("[A-Z]+"); Pattern patternRow = Pattern.compile("[0-9]+"); Matcher matcher = patternCol.matcher(s); matcher.find(); RC[0] = matcher.group(); matcher = patternRow.matcher(s); matcher.find(); RC[1] = matcher.group(); String tmp = RC[0]; int c = 0; int i = 0; int shift = 0; while (i < tmp.length()) { shift += Math.pow(26, i); c = 26 * c + intFromChar(tmp.charAt(i)); i++; } c += shift; return "R" + RC[1] + "C" + c; } public static void main( String[] args ) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); for (int i = 0; i < n; i++) { String s = sc.nextLine(); if (s.matches("[A-Z]+[0-9]+")) { System.out.println(excelToPlain(s)); } else { System.out.println(plainToExcel(s)); } } } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
a42fa512eca85a50dfce9361e761b3e1
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.util.* ; public class Main { public static void main(String [] args) { Scanner s=new Scanner(System.in); int T =s.nextInt(); s.nextLine(); for(int cas=0;cas<T;cas++) { String str=s.nextLine(); boolean flag=false ; int index=0; if(str.charAt(1)>='0'&&str.charAt(1)<='9') { for(int i=1;i<str.length();i++) { if(str.charAt(i)=='C') { index=i; flag=true ; break ; } } } if(flag) { int C=0; for(int i=index+1;i<str.length();i++) C=C*10+str.charAt(i)-'0'; print(C); //System.out.println(C+" "+index); System.out.println(str.substring(1,index)); } else { for(int i=0;i<str.length();i++) { if(str.charAt(i)>='0'&&str.charAt(i)<='9') { index=i; break ; } } System.out.print("R"+str.substring(index,str.length())+"C"); int C=0; for(int i=0;i<index;i++) C=C*26+str.charAt(i)-'A'+1; System.out.println(C); } } s.close(); } public static void print(int c) { String temp=" ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if(c==0) return ; if(c%26==0) { print(c/26-1); System.out.print(temp.charAt(26)); } else { print(c/26); System.out.print(temp.charAt(c%26)); } } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
a80cf0b75637a889a96871e2136c3fcf
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader buffReader = new BufferedReader(new InputStreamReader( System.in)); int n = Integer.parseInt(buffReader.readLine()); String[] lines = new String[n]; for (int i = 0; i < n; i++) lines[i] = buffReader.readLine(); for (int i = 0; i < n; i++) { if (checkIfTraditional(lines[i])) System.out.println(convertToRC(lines[i])); else System.out.println(convertToTraditional(lines[i])); } } private static String convertToTraditional(String line) { String[] tmp = line.split("C"); int row = Integer.parseInt(tmp[0].substring(1)); int column = Integer.parseInt(tmp[1]); StringBuilder columnTraditional = new StringBuilder(); char ch; boolean z; while (column > 0) { z = column % 26 == 0; ch = (char) (64 + (z ? 26 : column % 26)); column /= 26; column -= (z ? 1 : 0); columnTraditional.append(ch); } return columnTraditional.reverse().toString() + row; } private static String convertToRC(String line) { int numberIndex = 0; for (int i = 0; i < line.length(); i++) if (Character.isDigit(line.charAt(i))) { numberIndex = i; break; } String columnTraditional = line.substring(0, numberIndex); int row = Integer.parseInt(line.substring(numberIndex, line.length())); int column = 0; for (int i = 0; i < columnTraditional.length(); i++) column += ((int) Math.pow(26, columnTraditional.length() - i - 1) * (columnTraditional .charAt(i) - 64)); return "R" + row + "C" + column; } private static boolean checkIfTraditional(String line) { boolean numberStarted = false; for (int i = 0; i < line.length(); i++) { if (Character.isDigit(line.charAt(i))) numberStarted = true; if (numberStarted && Character.isLetter(line.charAt(i))) return false; } return true; } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
1d518238f741c173cedf2420f0bae2d7
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class B1 { public static String toRXCY(String input){ int i; for(i=0; i<input.length(); ++i){ if(input.charAt(i)<='9') break; } String row = input.substring(0, i); String column = input.substring(i, input.length()); return "R"+column+"C"+stringToInt(row); } public static int stringToInt(String input) { int sum=0; for(int i=0; i<input.length(); ++i){ sum += (input.charAt(i)-'A'+1) * Math.pow(26, input.length()-i-1); } return sum; } public static String fromRXCY(String input){ String[] str = input.split("C"); str[0] = str[0].substring(1, str[0].length()); // System.out.println(str[1]); int row = Integer.parseInt(str[0]); int column = Integer.parseInt(str[1]); return intToString(column)+row; } //R23C494 public static String intToString(int n){ // int temp = n; // int pow = -1; //25 = 0, 27 = 1 // while(temp >= 1){ // temp = temp/26; // ++pow; // } String result = ""; // for(int i=pow; i>=0; --i){ // if(n%26 == 0 && n!=26){ // --n; // int numerator = (int)(n/Math.pow(26, i)); // result += "" + (char) ('A'+numerator-1); // n -= numerator * Math.pow(26, i); // n+=1; // } // else{ // int numerator = (int)(n/Math.pow(26, i)); // result += "" + (char) ('A'+numerator-1); // n -= numerator * Math.pow(26, i); // } // } while (n > 0) { if (n%26==0) { result = (char)('Z') + result; n = n/26; n--; } else { result = (char)('A' + n%26 - 1) + result; n = n/26; } } return result; } public static int check(String input){ if(input.matches("[R][0-9]+[C][0-9]+")) return 1; return 0; } public static void main(String[] args) throws NumberFormatException, IOException { // System.out.println('R'-'A'+1); // System.out.println((int)'9'); BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bf.readLine()); for(int i=0; i<n; ++i){ String input = bf.readLine(); if(check(input)==1){ System.out.println(fromRXCY(input)); } else{ System.out.println(toRXCY(input)); } } } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
8c4b57fba340ecb74753127afbb61b3f
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.util.Scanner; /** * * @author SSD */ public class Spreadsheets { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); for (int i = 0; i < tc; i++) { String s = sc.next(), res; boolean isLetter = isLetterFormat(s); if(isLetter) res = convertToNumericFormat(s); else res = convertToLetterFormat(s); System.out.println(res); } } private static boolean isLetterFormat(String s) { boolean l = false, n = false; for (int i = 0; i < s.length(); i++) { if(s.charAt(i)>='A' && s.charAt(i) <= 'Z') { if(n) return false; else l = true; } else if(s.charAt(i)>='0' && s.charAt(i) <= '9' && l == true) n = true; } if( l && n ) return true; else return false; } private static String convertToNumericFormat(String s) { String res = ""; int col = 0, row; String c="",r=""; for (int i = 0; i < s.length(); i++) { if(s.charAt(i)>='A' && s.charAt(i) <= 'Z') c += s.charAt(i); else r += s.charAt(i); } int len = c.length(); for (int i = 0; i < len; i++) { col += (c.charAt(i)-'A'+1)*Math.pow(26, len-i-1); } row = Integer.parseInt(r); res = "R"+row+"C"+col; return res; } private static String convertToLetterFormat(String s) { String res = ""; String p[] = new StringBuilder(s).substring(1).split("C"); int row = Integer.parseInt(p[0]); int col = Integer.parseInt(p[1]); while(col>0) { if(col%26 != 0) res += (char)((col%26)+'A'-1); else { res += (char)((col%26)+'Z'); col--; } col /= 26; } res = new StringBuilder(res).reverse().toString(); res += row; return res; } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
e113f27fbdbf7261ab8c26a7f4b2ec32
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.util.Scanner; /** * * @author SSD */ public class Spreadsheets { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); for (int i = 0; i < tc; i++) { String s = sc.next(), res; boolean isLetter = isLetterFormat(s); if(isLetter) res = convertToNumericFormat(s); else res = convertToLetterFormat(s); System.out.println(res); } } private static boolean isLetterFormat(String s) { boolean l = false, n = false; for (int i = 0; i < s.length(); i++) { if(s.charAt(i)>='A' && s.charAt(i) <= 'Z') { if(n) return false; else l = true; } else if(s.charAt(i)>='0' && s.charAt(i) <= '9' && l == true) n = true; } if( l && n ) return true; else return false; } private static String convertToNumericFormat(String s) { String res = ""; int col = 0, row; String c="",r=""; for (int i = 0; i < s.length(); i++) { if(s.charAt(i)>='A' && s.charAt(i) <= 'Z') c += s.charAt(i); else r += s.charAt(i); } int len = c.length(); for (int i = 0; i < len; i++) { col += (c.charAt(i)-'A'+1)*Math.pow(26, len-i-1); } row = Integer.parseInt(r); res = "R"+row+"C"+col; return res; } private static String convertToLetterFormat(String s) { String res = ""; String p[] = new StringBuilder(s).substring(1).split("C"); int row = Integer.parseInt(p[0]); int col = Integer.parseInt(p[1]); while(col>0) { if(col%26 != 0) res += (char)((col%26)+'A'-1); else { res += (char)((col%26)+'Z'); col--; } col /= 26; } res = new StringBuilder(res).reverse().toString(); res += row; return res; } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
07467829189ccb5ee3ffe2eacc8b4668
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
/** * * @author Faruk */ import java.util.Arrays; import java.util.Scanner; import java.util.HashSet; import java.util.HashMap; import java.util.ArrayList; public class tmp { public static void main(String [] args){ Scanner scan = new Scanner(System.in); int n = scan.nextInt(); String s="",out = ""; StringBuilder sb = new StringBuilder(); char [] lookup = new char[26]; String [] outt = new String[n]; lookup[0] = 'Z'; for(int i=1;i<26;i++) lookup[i] = (char) (i-1+'A'); int indc = 0; for(int i=0; i<n; i++){ s = scan.next(); indc = s.indexOf('C'); if(indc > 0 && s.charAt(indc-1) < ':'){ int col = Integer.parseInt(s.substring(indc+1)); int offset = 0; while(col > 26){ sb.append((lookup[col%26])); if(col%26 == 0) offset = -1; else offset = 0; col = col/26 + offset; } sb.append(lookup[col%26]); sb.reverse(); sb.append(s.substring(1,indc)); }else{ int j = 0; while(!Character.isDigit(s.charAt(j))) j++; int row = Integer.parseInt(s.substring(j)); sb.append("R" + row + "C"); // sb.append(row); // sb.append("C"); int colnum = 0; for(int k=j-1; k>=0; k--){ colnum += (s.charAt(k)-'A'+1)*Math.pow(26, j-1-k); } sb.append(colnum); } outt[i] = sb.toString(); sb = new StringBuilder(); } for(int i=0; i<n; i++) System.out.println(outt[i]); } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
474ad89be8699d0d8221a7422826bad1
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
/** * * @author Faruk */ import java.util.Arrays; import java.util.Scanner; import java.util.HashSet; import java.util.HashMap; import java.util.ArrayList; public class tmp { public static void main(String [] args){ Scanner scan = new Scanner(System.in); int n = scan.nextInt(); String s="",out = ""; StringBuilder sb = new StringBuilder(); char [] lookup = new char[26]; String [] outt = new String[n]; lookup[0] = 'Z'; for(int i=1;i<26;i++) lookup[i] = (char) (i-1+'A'); int indc = 0; for(int i=0; i<n; i++){ s = scan.next(); indc = s.indexOf('C'); if(indc > 0 && s.charAt(indc-1) < ':'){ int col = Integer.parseInt(s.substring(indc+1)); int offset = 0; while(col > 26){ sb.append((lookup[col%26])); if(col%26 == 0) offset = -1; else offset = 0; col = col/26 + offset; } sb.append(lookup[col%26]); sb.reverse(); sb.append(s.substring(1,indc)); }else{ int j = 0; while(!Character.isDigit(s.charAt(j))) j++; int row = Integer.parseInt(s.substring(j)); sb.append("R"); sb.append(row); sb.append("C"); int colnum = 0; for(int k=j-1; k>=0; k--){ colnum += (s.charAt(k)-'A'+1)*Math.pow(26, j-1-k); } sb.append(colnum); } outt[i] = sb.toString(); sb = new StringBuilder(); } for(int i=0; i<n; i++) System.out.println(outt[i]); } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
5d9eb18e220dc48decb8d7b64a504098
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.util.Scanner; public class Main{ /** * @param args */ public static String changeNumSystem(String s){ int indexR = s.indexOf("R"); int indexC = s.indexOf("C"); if(s.contains("R") && s.contains("C") && indexR < indexC && s.charAt(indexR + 1) >= '1' && s.charAt(indexR + 1) <= '9'){ // RC to int indexOfC = s.indexOf('C'); String row = s.substring(1, indexOfC); String colStr = s.substring(indexOfC + 1); Long col = Long.parseLong(colStr); String colResult = new String(); while(col != 0){ char c; if(col % 26 == 0){ c = 'Z'; col = col / 26 - 1; } else{ c = (char) (col % 26 + 64); col = col / 26; } colResult = c + colResult; } return colResult + row; } else{ // to RC Long sum = 0L; int i = 0; for(; i < s.length() && s.charAt(i) >= 'A' && s.charAt(i) <= 'Z'; i++){ int index = s.charAt(i) - 64; sum = sum * 26 + index; }//end for i String row = s.substring(i); String col = sum + ""; return "R" + row + "C" + col; } } public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); String s = new String(); for(int i = 0; i < n; i++){ s = scanner.next(); System.out.println(changeNumSystem(s)); } } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
37736c0f18e1f08ba9fe5e8b35230b9d
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Problem1B { // static String spreadsheet(String s) { // int c, i = 0, j = 0, n = 0; // String u; // // for (c = s.indexOf("C"); s.charAt(i) > 64;) // i++; // // // if (i < c) { // // u = s.substring(1, c); // // for (n += new Long(s.substring(c + 1)); n > 0; n /= 26) // u = (char) (n % 26 + 64) + u; // // // } else { // for (; j < i;) // n = n * 26 + s.charAt(j++) - 64; // // u = "R" + s.substring(i) + "C" + n; // } // return u; // } static String spreadsheet(String s) { int[] R = new int[17]; int i = 0, C = 0, u = 0, o = 0; for (int c : s.getBytes()) u |= c & (o |= ~c) & 64 & (c < 58 ? R[i = o & 16] = 10 * R[i] + c - 48 : (C = 26 * C + c - 64)); o = R[i]; s = "R"+o+"C"+C; if (u > 0) for (s = R[0]+""; o > 0; s = (char) (i + 65) + s, o /= 26) o -= i = --o % 26; return s; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); String s = null; for(int i = 0; i < n ; i++){ s = sc.nextLine(); System.out.println(spreadsheet(s)); } } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
aa527073e9e4067de7af69e9b45f6218
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.io.*; public class BH { public static void main(String args[])throws IOException { BufferedReader obj=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(obj.readLine()); int c=0,f,l; while(c<n) { String s=obj.readLine(); char ch=s.charAt(0); String p="",q=""; l=s.length(); for(f=1;f<l;f++) { if(s.charAt(f)=='C') break; } if(ch=='R'&&(s.charAt(1)>='0'&&s.charAt(1)<='9')&&f!=l) { p=s.substring(1,f); int y=Integer.parseInt(s.substring(f+1)); while(y!=0) { int d=y%26; if(d==0) { d=26; y=(y/26)-1; } else y=y/26; q=((char)(64+d))+q; } } else { for(f=0;f<l;f++) { char t=s.charAt(f); if(t>='1'&& t<='9') break; } p=s.substring(0,f); q="R"+s.substring(f); f--; int w=0; int e=0; while(w<=f) { e=e+((int)Math.pow(26,w))*((int)p.charAt(f-w)-64); w++; } p="C"+e; } System.out.println(q+p); c++; } } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
a519d3d2a56813c9b9688605e1c40d91
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class B { public static String toString(String x, String y) { Integer col = Integer.parseInt(y); List<Integer> buf = new ArrayList<Integer>(); if (col == 0){ buf.add(0); } while (col > 0) { if (col % 26 == 0) { buf.add(26); col /=26; col-=1; } else { buf.add(col % 26); col /=26; } } StringBuilder sb = new StringBuilder(); for (int i = buf.size()-1; i>=0; i--) { sb.append((char)('A'-1+buf.get(i))); } sb.append(x); return sb.toString(); } public static String toRaw(String x, String y) { Integer a = 0; for (int i = 0; i<x.length(); i++) { a = a * 26 + (x.charAt(i) - 'A'+1); } StringBuilder sb = new StringBuilder(); sb.append("R"); sb.append(y); sb.append("C"); sb.append(a.toString()); return sb.toString(); } public static void main(String[] args) { Scanner in = new Scanner(System.in); long n = Long.parseLong(in.nextLine()); for (int i = 0; i<n; i++) { String str = in.nextLine(); List<String> buf = new ArrayList<>(); StringBuilder sb = new StringBuilder(); boolean last = false; for (int j = 0; j<str.length(); j++) { if (last == (Character.isDigit(str.charAt(j)))) { sb.append(str.charAt(j)); } else { buf.add(sb.toString()); sb = new StringBuilder(); last = !last; sb.append(str.charAt(j)); } } buf.add(sb.toString()); if (buf.size() == 4) { System.out.println(toString(buf.get(1), buf.get(3))); } else { System.out.println(toRaw(buf.get(0), buf.get(1))); } } } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
b761628b48f169f95e1c66cf7ca9c9a0
train_002.jsonl
1266580800
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
64 megabytes
import java.util.Scanner; public class cf { public static Scanner input=new Scanner(System.in); public static void B1_dtol(int d) { if(d!=0) { if(d%26==0) { B1_dtol(d/26-1); System.out.printf("Z"); } else { B1_dtol(d/26); System.out.printf("%c", d%26+'A'-1); } } } public static int B1_ltod(String l) { int total=0; for(int i=l.length()-1; i>=0; i--) { total+=(l.charAt(i)-'A'+1)*Math.pow(26, l.length()-1-i); } return total; } public static void B1() { int T=input.nextInt(); for(int t=0; t<T; t++) { String temp=input.next(); if(temp.charAt(0)=='R'&&temp.charAt(1)<='9'&&temp.indexOf('C')>1) { int cindex=temp.lastIndexOf('C'); String r=temp.substring(1, cindex); int c=Integer.parseInt(temp.substring(cindex+1)); B1_dtol(c); System.out.printf("%s\n",r); } else { int i=0; for(i=0; i<temp.length(); i++) { if(Character.isDigit(temp.charAt(i))) break; } String c=temp.substring(0,i); String r=temp.substring(i); System.out.printf("R%sC%d\n",r,B1_ltod(c)); } } } public static void A1() { double n=input.nextInt(); double m=input.nextInt(); double a=input.nextInt(); double ans=Math.ceil(n/a)*Math.ceil(m/a); System.out.printf("%.0f",ans); } public static void main(String[] args) { // TODO Auto-generated method stub // A1(); B1(); } }
Java
["2\nR23C55\nBC23"]
10 seconds
["BC23\nR23C55"]
null
Java 7
standard input
[ "implementation", "math" ]
910c0e650d48af22fa51ab05e8123709
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
1,600
Write n lines, each line should contain a cell coordinates in the other numeration system.
standard output
PASSED
25aa2475ee59e735f747f6cca1f75a34
train_002.jsonl
1520177700
In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i &gt; 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi &lt; i.Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { static long sx = 0, sy = 0, mod = (long) (1e9 + 7); static ArrayList<Integer>[] a; static long[][][] dp; static int[] size; static long[] farr; public static PrintWriter out; static ArrayList<pair> pa = new ArrayList<>(); static long[] fact = new long[(int) 1e6]; static boolean b = false; static StringBuilder sb = new StringBuilder(); static boolean cycle = false; // static long m = 998244353; static long[] no, col; static String s; static int k = 0, n = 0, m = 0; static int[] c; static long ans = 0; static HashMap<Integer, Integer> hm; static ArrayList<Integer> p = new ArrayList<>(); public static void main(String[] args) throws IOException { // Scanner scn = new Scanner(new BufferedReader(new // InputStreamReader(System.in))); out = new PrintWriter(System.out); Reader scn = new Reader(); int n = scn.nextInt(); a = new ArrayList[n + 1]; hm = new HashMap<>(); for (int i = 1; i <= n; i++) a[i] = new ArrayList<>(); for (int i = 2; i <= n; i++) { int x = scn.nextInt(); a[x].add(i); a[i].add(x); } dfs(1, -1, 0); int ans = 0; for(int K:hm.keySet()) ans+=hm.get(K)%2; System.out.println(ans); } private static void dfs(int v, int par, int lvl) { hm.put(lvl, hm.containsKey(lvl) ? hm.get(lvl) + 1 : 1); for (int nbr : a[v]) if (nbr != par) dfs(nbr, v, lvl + 1); } // _________________________TEMPLATE_____________________________________________________________ // public static long lcm(long x, long y) { // // return (x * y) / gcd(x, y); // } // // private static long gcd(long x, long y) { // if (x == 0) // return y; // // return gcd(y % x, x); // } static class comp implements Comparator<pair> { @Override public int compare(pair p1, pair p2) { return p1.ele - p2.ele; } } public static long pow(long a, long b) { if (b < 0) return 0; if (b == 0 || b == 1) return (long) Math.pow(a, b); if (b % 2 == 0) { long ret = pow(a, b / 2); ret = (ret % mod * ret % mod) % mod; return ret; } else { return ((pow(a, b - 1) % mod) * a % mod) % mod; } } private static class pair implements Comparable<pair> { int ele; int freq; pair(int a, int b) { ele = a; freq = b; } @Override public int compareTo(pair o) { return this.freq - o.freq; } } private static String reverse(String s) { return new StringBuilder(s).reverse().toString(); } public 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[1000000 + 1]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public int[][] nextInt2DArray(int m, int n) throws IOException { int[][] arr = new int[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) arr[i][j] = nextInt(); } return arr; } public long[][] nextInt2DArrayL(int m, int n) throws IOException { long[][] arr = new long[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) arr[i][j] = nextInt(); } return arr; } // kickstart - Solution // atcoder - Main } }
Java
["3\n1 1", "5\n1 2 2 2", "18\n1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4"]
1 second
["1", "3", "4"]
NoteIn first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
Java 11
standard input
[ "dfs and similar", "trees", "graphs" ]
a4563e6aea9126e20e7a33df664e3171
First line of input contains single integer number n (2 ≤ n ≤ 100 000)  — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi &lt; i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.
1,500
Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.
standard output
PASSED
c7d80604eb9f08d04f06c388250726e1
train_002.jsonl
1520177700
In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i &gt; 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi &lt; i.Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.
256 megabytes
// practice with kaiboy import java.io.*; import java.util.*; public class CF931D extends PrintWriter { CF931D() { super(System.out, true); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF931D o = new CF931D(); o.main(); o.flush(); } void main() { int n = sc.nextInt(); int[] dd = new int[n]; byte[] xx = new byte[n]; xx[0] = 1; for (int i = 1; i < n; i++) { int p = sc.nextInt() - 1; xx[dd[i] = dd[p] + 1] ^= 1; } int ans = 0; for (int d = 0; d < n; d++) ans += xx[d]; println(ans); } }
Java
["3\n1 1", "5\n1 2 2 2", "18\n1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4"]
1 second
["1", "3", "4"]
NoteIn first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
Java 11
standard input
[ "dfs and similar", "trees", "graphs" ]
a4563e6aea9126e20e7a33df664e3171
First line of input contains single integer number n (2 ≤ n ≤ 100 000)  — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi &lt; i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.
1,500
Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.
standard output
PASSED
e7c06166ba7027dd816997e4faf2e617
train_002.jsonl
1520177700
In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i &gt; 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi &lt; i.Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest.
256 megabytes
// package com.company; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.sql.SQLOutput; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner s = new Scanner(System.in); //BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb1 = new StringBuilder();//int n=Integer.parseInt(s.readLine()); int n=s.nextInt(); ArrayList<Integer>[] adj=new ArrayList[n+1]; for(int i=2;i<=n;i++){ int x=s.nextInt(); if(adj[i]==null) adj[i]=new ArrayList<Integer>(); if(adj[x]==null) adj[x]=new ArrayList<Integer>(); adj[i].add(x); adj[x].add(i); } int[] vis=new int[n+1]; int[] vtime=new int[n+1]; int ans=0; dfs(1,adj,vis,vtime,0,n); for(int i=0;i<=n;i++){ if(vtime[i]%2!=0) ans++; } System.out.println(ans); }static long ans=0;static long count=0; static void dfs(int i,ArrayList<Integer>[] adj,int[] vis,int [] vtime,int time,int n){ vis[i]=1; vtime[time]++; if(adj[i]==null){return;} for(Integer j:adj[i]){ if(vis[j]==0){ dfs(j,adj,vis,vtime,time+1,n); } }} static void computeLPSArray(String pat, int M, int lps[]) { int len = 0; int i = 1; lps[0] = 0; while (i < M) { if (pat.charAt(i) == pat.charAt(len)) { len++; lps[i] = len; i++; } else // (pat[i] != pat[len]) { if (len != 0) { len = lps[len - 1]; } else // if (len == 0) { lps[i] = len; i++; } } } } 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; } static double power(double x, long y, int p) { double res = 1; x = x % p; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } public static void fracion(double x) { String a = "" + x; String spilts[] = a.split("\\."); // split using decimal int b = spilts[1].length(); // find the decimal length int denominator = (int) Math.pow(10, b); // calculate the denominator int numerator = (int) (x * denominator); // calculate the nerumrator Ex // 1.2*10 = 12 int gcd = (int)gcd((long)numerator, denominator); // Find the greatest common // divisor bw them String fraction = "" + numerator / gcd + "/" + denominator / gcd; // System.out.println((denominator/gcd)); long x1=modInverse(denominator / gcd,998244353 ); // System.out.println(x1); System.out.println((((numerator / gcd )%998244353 *(x1%998244353 ))%998244353 ) ); }static int anst=Integer.MAX_VALUE; static StringBuilder sb1=new StringBuilder(); public static int bfs(int i1, ArrayList<Integer>[] h, int[] vis, int n,int val1) { Queue<Integer> q = new LinkedList<Integer>(); q.add(i1);Queue<Integer> aq=new LinkedList<Integer>(); aq.add(0); while(!q.isEmpty()){ int i=q.poll(); int val=aq.poll(); if(i==n){ return val; } if(h[i]!=null){ for(Integer j:h[i]){ if(vis[j]==0){ q.add(j);vis[j]=1; aq.add(val+1);} } } }return -1; } static int i(String a) { return Integer.parseInt(a); } static long l(String a) { return Long.parseLong(a); } static String[] inp() throws IOException { BufferedReader s = new BufferedReader(new InputStreamReader(System.in)); return s.readLine().trim().split("\\s+"); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long modInverse(int a, int m) { { // If a and m are relatively prime, then modulo inverse // is a^(m-2) mode m return (power(a, m - 2, m)); } } // To compute x^y under modulo m static long power(int x, int y, int m) { if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (p * p) % m; if (y % 2 == 0) return p; else return (x * p) % m; } // Function to return gcd of a and b } class Student { int l;int r; public Student(int l, int r) { this.l = l; this.r = r; } // Constructor // Used to print student details in main() public String toString() { return this.l+" "; } } class Sortbyroll implements Comparator<Student> { // Used for sorting in ascending order of // roll number public int compare(Student a, Student b){ return a.l-b.l; } } class Sortbyroll1 implements Comparator<Student> { // Used for sorting in ascending order of // roll number public int compare(Student a, Student b){ if(a.r==b.r) return a.l-b.l; return a.r-b.r; } }
Java
["3\n1 1", "5\n1 2 2 2", "18\n1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4"]
1 second
["1", "3", "4"]
NoteIn first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
Java 11
standard input
[ "dfs and similar", "trees", "graphs" ]
a4563e6aea9126e20e7a33df664e3171
First line of input contains single integer number n (2 ≤ n ≤ 100 000)  — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi &lt; i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down.
1,500
Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest.
standard output
PASSED
907c83105658a1df4f471d695d025f0e
train_002.jsonl
1484499900
PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. He chose a number k such that gcd(n, k) = 1. Vertices of the polygon are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1: Assume you've ended last operation in vertex x (consider x = 1 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertex x + k or x + k - n depending on which of these is a valid index of polygon's vertex.Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides.
256 megabytes
import java.util.Scanner; /** * Created by Khiem on 1/17/2017. */ public class D { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); SegmentTree st = new SegmentTree(n); int k = in.nextInt(); if (k > n / 2) { k = n - k; } int t = 1; long num = 1L; StringBuffer str = new StringBuffer(); do { st.increment(t); int t2 = (t + k) % n; st.increment(t2); int tt = (t + 1) % n; int tt2 = (t2 == 0) ? n - 1 : (t2 - 1) % n; num = num + 1 + st.rsq(tt, tt2); t = t2; str.append(num + " "); } while (t != 1); System.out.println(str.toString()); } static class SegmentTree { int[] a; int[] st; int n; /** * everything is 0 in the beginning * * @param n */ public SegmentTree(int n) { a = new int[n]; st = new int[4 * n]; this.n = n; build(1, 0, n - 1); } public SegmentTree(int n, int[] array) { a = array; this.n = n; st = new int[2 * n + 2]; build(1, 0, n - 1); } int left(int p) { return p << 1; } int right(int p) { return (p << 1) + 1; } int[] st() { return st; } void build(int p, int L, int R) { for (int i = L; i <= R; i++) { st[p] += a[i]; } if (L == R) st[p] = a[L]; else { build(left(p), L, (L + R) / 2); build(right(p), (L + R) / 2 + 1, R); } } int rsq(int i, int j) { if (i <= j) return rsq(1, 0, n - 1, i, j); else if (i == j + 1) return st[1]; else return st[1] - rsq(1, 0, n - 1, j + 1, i - 1); } int rsq(int p, int L, int R, int i, int j) { if (i > R || j < L) { return 0; } if (L >= i && R <= j) { return st[p]; } return rsq(left(p), L, (L + R) / 2, i, j) + rsq(right(p), (L + R) / 2 + 1, R, i, j); } int update(int newVal, int pos) { int oldVal = a[pos]; if (oldVal == newVal) return oldVal; a[pos] = newVal; int dif = newVal - oldVal; int p = 1; int L = 0; int R = n - 1; while (L != R) { st[p] += dif; if (pos < (L + R) / 2 + 1) { R = (L + R) / 2; p = p * 2; } else { L = (L + R) / 2 + 1; p = p * 2 + 1; } } return oldVal; } int increment(int pos) { int oldVal = a[pos]; a[pos]++; int dif = 1; int p = 1; int L = 0; int R = n - 1; st[p] += dif; while (L != R) { if (pos < (L + R) / 2 + 1) { R = (L + R) / 2; p = p * 2; } else { L = (L + R) / 2 + 1; p = p * 2 + 1; } st[p] += dif; } return oldVal; } } }
Java
["5 2", "10 3"]
4 seconds
["2 3 5 8 11", "2 3 4 6 9 12 16 21 26 31"]
NoteThe greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides both a and b without a remainder.For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines.
Java 8
standard input
[ "data structures" ]
fc82362dbda74396ad6db0d95a0f7acc
There are only two numbers in the input: n and k (5 ≤ n ≤ 106, 2 ≤ k ≤ n - 2, gcd(n, k) = 1).
2,000
You should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines.
standard output
PASSED
99849b887cf4beb59638290dad98eff8
train_002.jsonl
1484499900
PolandBall has such a convex polygon with n veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. He chose a number k such that gcd(n, k) = 1. Vertices of the polygon are numbered from 1 to n in a clockwise way. PolandBall repeats the following process n times, starting from the vertex 1: Assume you've ended last operation in vertex x (consider x = 1 if it is the first operation). Draw a new segment from vertex x to k-th next vertex in clockwise direction. This is a vertex x + k or x + k - n depending on which of these is a valid index of polygon's vertex.Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class D { public static void main(String[] args) throws NumberFormatException, IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int k = sc.nextInt(); long cur = 1; k = Math.min(k, n - k); int N = 1; while(N <= n) N *= 2; int in[] = new int[N + 1]; int inc = 0; SegmentTree t = new SegmentTree(in); int v = 0; for(int i = 0; i < n; i++) { int start = v; int end = v + k; if(end < n) { inc = t.query(start + 2, end); t.update_point(start + 1, 1); t.update_point(end + 1, 1); } else { end -= n; inc = t.query(start + 2, n) + t.query(1, end); t.update_point(start + 1, 1); t.update_point(end + 1, 1); } v = end; cur += inc + 1; out.print(cur + " "); } out.println(); out.flush(); out.close(); } static class SegmentTree { // 1-based DS, OOP int N; //the number of elements in the array as a multipls of 2 (i.e. after padding) int[] array, sTree, lazy; SegmentTree(int[] in) { array = in; N = in.length - 1; sTree = new int[N<<1]; //no. of nodes = 2*N - 1, we add one to cross out index zero lazy = new int[N<<1]; build(1,1,N); } void build(int node, int b, int e) // O(n) { if(b == e) sTree[node] = array[b]; else { build(node<<1,b,(b+e)/2); build((node<<1)+1,(b+e)/2+1,e); sTree[node] = sTree[node<<1] + sTree[(node<<1)+1]; } } void update_point(int index, int val) // O(log n) { index += N - 1; sTree[index] += val; while(index>1) { index >>= 1; sTree[index] = sTree[index<<1] + sTree[(index<<1) + 1]; } } void update_range(int i, int j, int val) // O(log n) { update_range(1,1,N,i,j,val); } void update_range(int node, int b, int e, int i, int j, int val) { if(i > e || j < b) return; if(b >= i && e <= j) { sTree[node] += (e-b+1)*val; lazy[node] += val; } else { propagate(node, b, e); update_range(node<<1,b,(b+e)/2,i,j,val); update_range((node<<1)+1,(b+e)/2+1,e,i,j,val); sTree[node] = sTree[node<<1] + sTree[(node<<1)+1]; } } void propagate(int node, int b, int e) { int mid = (b+e)/2; lazy[node<<1] += lazy[node]; lazy[(node<<1)+1] += lazy[node]; sTree[node<<1] += (mid-b+1)*lazy[node]; sTree[(node<<1)+1] += (e-mid)*lazy[node]; lazy[node] = 0; } int query(int i, int j) { return query(1,1,N,i,j); } int query(int node, int b, int e, int i, int j) // O(log n) { if(i>e || j <b) return 0; if(b>= i && e <= j) return sTree[node]; propagate(node, b, e); return query(node<<1,b,(b+e)/2,i,j) + query((node<<1)+1,(b+e)/2+1,e,i,j); } } static class Scanner { BufferedReader br; StringTokenizer st; Scanner(FileReader f) { br = new BufferedReader(f); } public boolean ready() throws IOException { return br.ready(); } Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } } }
Java
["5 2", "10 3"]
4 seconds
["2 3 5 8 11", "2 3 4 6 9 12 16 21 26 31"]
NoteThe greatest common divisor (gcd) of two integers a and b is the largest positive integer that divides both a and b without a remainder.For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines.
Java 8
standard input
[ "data structures" ]
fc82362dbda74396ad6db0d95a0f7acc
There are only two numbers in the input: n and k (5 ≤ n ≤ 106, 2 ≤ k ≤ n - 2, gcd(n, k) = 1).
2,000
You should print n values separated by spaces. The i-th value should represent number of polygon's sections after drawing first i lines.
standard output
PASSED
0010c6e400b5b5eb0d50e6e6d86872c3
train_002.jsonl
1601219100
You are given an array $$$a$$$ consisting of $$$n$$$ integers numbered from $$$1$$$ to $$$n$$$.Let's define the $$$k$$$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $$$k$$$ (recall that a subsegment of $$$a$$$ of length $$$k$$$ is a contiguous part of $$$a$$$ containing exactly $$$k$$$ elements). If there is no integer occuring in all subsegments of length $$$k$$$ for some value of $$$k$$$, then the $$$k$$$-amazing number is $$$-1$$$.For each $$$k$$$ from $$$1$$$ to $$$n$$$ calculate the $$$k$$$-amazing number of the array $$$a$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class KAmazingNumbers { public static void main(String[] args) throws IOException{ BufferedReader r=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(r.readLine()); while(t-->0){ int n=Integer.parseInt(r.readLine()); StringTokenizer t1=new StringTokenizer(r.readLine()); int[]array=new int[n]; for(int i=0;i<n;i++) array[i]=Integer.parseInt(t1.nextToken()); int []lastPos=new int[n+1]; int []freq=new int[n+1]; Arrays.fill(lastPos, -1); for(int i=0;i<n;i++){ int gap=Math.abs(i-lastPos[array[i]]); freq[array[i]]=Math.max(gap, freq[array[i]]); lastPos[array[i]]=i; } for(int i=0;i<n;i++){ int gap=Math.abs(n-lastPos[array[i]]); freq[array[i]]=Math.max(gap,freq[array[i]]); } lastPos=null; int [] least=new int [n]; Arrays.fill(least, Integer.MAX_VALUE); for(int i=0;i<=n;i++) if(freq[i]!=0) least[freq[i]-1]=Math.min(least[freq[i]-1], i); for(int i=1;i<n;i++) least[i]=Math.min(least[i], least[i-1]); StringBuilder b=new StringBuilder(); for(int i=0;i<n;i++) if(least[i]==Integer.MAX_VALUE) b.append("-1 "); else b.append(least[i]+" "); System.out.println(b); } } }
Java
["3\n5\n1 2 3 4 5\n5\n4 4 4 4 2\n6\n1 3 1 5 3 1"]
1 second
["-1 -1 3 2 1 \n-1 4 4 4 2 \n-1 -1 1 1 1 1"]
null
Java 11
standard input
[ "data structures" ]
b7abfb1103bb1796c8f89653c303d7dc
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
1,500
For each test case print $$$n$$$ integers, where the $$$i$$$-th integer is equal to the $$$i$$$-amazing number of the array.
standard output
PASSED
2e0ab658ddec234c9df2b335ac1e2d29
train_002.jsonl
1601219100
You are given an array $$$a$$$ consisting of $$$n$$$ integers numbered from $$$1$$$ to $$$n$$$.Let's define the $$$k$$$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $$$k$$$ (recall that a subsegment of $$$a$$$ of length $$$k$$$ is a contiguous part of $$$a$$$ containing exactly $$$k$$$ elements). If there is no integer occuring in all subsegments of length $$$k$$$ for some value of $$$k$$$, then the $$$k$$$-amazing number is $$$-1$$$.For each $$$k$$$ from $$$1$$$ to $$$n$$$ calculate the $$$k$$$-amazing number of the array $$$a$$$.
256 megabytes
import com.sun.source.tree.Tree; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class problemC { private static void solve() throws Exception { int n = fs.nextInt(); int[] a = fs.readArray(n); int[] last = new int[n+1]; Arrays.fill(last, -1); int[] maxDistance = new int[n+1]; Arrays.fill(maxDistance, 0); int[] minValueSatisfingThisLength = new int[n+2]; Arrays.fill(minValueSatisfingThisLength, n+1); for (int i = 0 ; i < n ; i ++) { int value = a[i]; int dist = i - last[value]; maxDistance[value] = Math.max(maxDistance[value], dist); last[value] = i; } for (int i = 1 ; i <= n ; i ++ ) { maxDistance[i] = Math.max(maxDistance[i], n-last[i]); } for (int i = 1 ; i <= n ; i ++ ) { minValueSatisfingThisLength[ maxDistance[i] ] = Math.min(minValueSatisfingThisLength[ maxDistance[i] ], i); } int[] ans = new int[n+1]; Arrays.fill(ans, -1); int mn = n+1; for (int i = 1 ; i <= n ; i ++ ) { mn = Math.min(mn, minValueSatisfingThisLength[i]); ans[i] = mn == (n+1) ? -1 : mn; } for (int i = 1 ; i <= n ; i ++ ) { out.print(ans[i]); out.print(' '); } out.println(); } private static boolean fills(TreeSet<Integer> inds, int len, int n) { if (inds.isEmpty()) return false; int end = len-1; for (int i : inds) { if (i > end) return false; end = i+1+len-1; if (end >= n) return true; } return false; } private static FastScanner fs = new FastScanner(); private static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws Exception { int T = 1; T = fs.nextInt(); for (int t = 0; t < T; t++) { solve(); } out.close(); } static void debug(Object... O) { System.err.print("DEBUG "); System.err.println(Arrays.deepToString(O)); } 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()); } double nextDouble() { return Double.parseDouble(next()); } String nextString() { return next(); } } }
Java
["3\n5\n1 2 3 4 5\n5\n4 4 4 4 2\n6\n1 3 1 5 3 1"]
1 second
["-1 -1 3 2 1 \n-1 4 4 4 2 \n-1 -1 1 1 1 1"]
null
Java 11
standard input
[ "data structures" ]
b7abfb1103bb1796c8f89653c303d7dc
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
1,500
For each test case print $$$n$$$ integers, where the $$$i$$$-th integer is equal to the $$$i$$$-amazing number of the array.
standard output
PASSED
3189486cfc828cc3a776926e9355bc9e
train_002.jsonl
1601219100
You are given an array $$$a$$$ consisting of $$$n$$$ integers numbered from $$$1$$$ to $$$n$$$.Let's define the $$$k$$$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $$$k$$$ (recall that a subsegment of $$$a$$$ of length $$$k$$$ is a contiguous part of $$$a$$$ containing exactly $$$k$$$ elements). If there is no integer occuring in all subsegments of length $$$k$$$ for some value of $$$k$$$, then the $$$k$$$-amazing number is $$$-1$$$.For each $$$k$$$ from $$$1$$$ to $$$n$$$ calculate the $$$k$$$-amazing number of the array $$$a$$$.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class C { static Scanner sc = new Scanner(System.in); static StringBuilder out = new StringBuilder(); public static void main(String[] args){ int t = sc.nextInt(); while (t-- >0){ int n = sc.nextInt(); int[] a = new int[n]; int[] interval = new int[n+1]; int[] lastindex = new int[n+1]; Arrays.fill(lastindex,-1); for (int i=0;i<n;i++){ a[i] = sc.nextInt(); int tmp = a[i]; interval[tmp] = Math.max(i-lastindex[tmp],interval[tmp]); lastindex[tmp] = i; } for (int i=0;i<n;i++){ if (lastindex[i]!=-1){ interval[i] = Math.max(n-lastindex[i],interval[i]); } } int[] ans = new int[n]; Arrays.fill(ans,-1); for (int i=1;i<n+1;i++){ if (interval[i]!=0){ for (int j=interval[i]-1;j<n;j++){ if (ans[j]==-1){ ans[j] = i; }else { break; } } } } for (int i=0;i<n;i++){ out.append(ans[i]+" "); } out.append("\n"); } System.out.println(out); } } // 10001
Java
["3\n5\n1 2 3 4 5\n5\n4 4 4 4 2\n6\n1 3 1 5 3 1"]
1 second
["-1 -1 3 2 1 \n-1 4 4 4 2 \n-1 -1 1 1 1 1"]
null
Java 11
standard input
[ "data structures" ]
b7abfb1103bb1796c8f89653c303d7dc
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
1,500
For each test case print $$$n$$$ integers, where the $$$i$$$-th integer is equal to the $$$i$$$-amazing number of the array.
standard output
PASSED
f07b5f5dd7502775966598282500a680
train_002.jsonl
1601219100
You are given an array $$$a$$$ consisting of $$$n$$$ integers numbered from $$$1$$$ to $$$n$$$.Let's define the $$$k$$$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $$$k$$$ (recall that a subsegment of $$$a$$$ of length $$$k$$$ is a contiguous part of $$$a$$$ containing exactly $$$k$$$ elements). If there is no integer occuring in all subsegments of length $$$k$$$ for some value of $$$k$$$, then the $$$k$$$-amazing number is $$$-1$$$.For each $$$k$$$ from $$$1$$$ to $$$n$$$ calculate the $$$k$$$-amazing number of the array $$$a$$$.
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 Main { static int[]arr = new int[]{1,2,3}; static boolean[]vis = new boolean[3]; public static boolean isOK(int seg , ArrayList<Integer> al ,int n) { if(al.get(0) > seg) return false; for (int i = 1 ; i < al.size(); i++) { if(al.get(i) <= al.get(i - 1) + seg ) continue; return false; } if(al.get(al.size() - 1) < n - seg + 1 ) return false; return true; } public static void solve(HashMap<Integer,ArrayList<Integer>> map , int[]ans, int n) { for (Integer a : map.keySet()) { ArrayList<Integer> al = map.get(a); int lo = 1; int hi = n; // System.out.println(al); int seg = -1; while (lo <= hi) { int possible_seg = (hi + lo)/2; if(isOK(possible_seg , al , n)) { seg = possible_seg; hi = possible_seg - 1; }else{ lo = possible_seg + 1; } } ans[seg] = Math.min(ans[seg], a); } } private static final Scanner sc = new Scanner(System.in); public static void main(String[] args) throws IOException { PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[]arr = new int[n]; HashMap<Integer,ArrayList<Integer>> map = new HashMap<>(); for (int i = 0 ;i < n ; i++) { int a = sc.nextInt(); if(map.containsKey(a)) { map.get(a).add(i + 1); }else{ ArrayList<Integer> al = new ArrayList<>(); al.add(i + 1); map.put(a, al); } } int[]ans = new int[n + 1]; Arrays.fill(ans, (int)1e9); solve(map , ans , n); // System.out.println(Arrays.toString(ans)); int i = 1; for (; i <= n ;i++) { if(ans[i] == (int)1e9) pw.print(-1 + " "); else { pw.print(ans[i] + " "); break; } } for (int j = i + 1; j <= n ; j++) { pw.print(Math.min(ans[j],ans[j - 1]) + " "); ans[j] = Math.min(ans[j],ans[j - 1]); } // System.out.println(5); pw.println(); } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public boolean check() { if (!st.hasMoreTokens()) return false; return true; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(e); } } public double nextDouble() { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() { try { return br.ready(); } catch (IOException e) { throw new RuntimeException(e); } } } }
Java
["3\n5\n1 2 3 4 5\n5\n4 4 4 4 2\n6\n1 3 1 5 3 1"]
1 second
["-1 -1 3 2 1 \n-1 4 4 4 2 \n-1 -1 1 1 1 1"]
null
Java 11
standard input
[ "data structures" ]
b7abfb1103bb1796c8f89653c303d7dc
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
1,500
For each test case print $$$n$$$ integers, where the $$$i$$$-th integer is equal to the $$$i$$$-amazing number of the array.
standard output
PASSED
f498394f6cbc9e2d8c3c92752bbb8493
train_002.jsonl
1601219100
You are given an array $$$a$$$ consisting of $$$n$$$ integers numbered from $$$1$$$ to $$$n$$$.Let's define the $$$k$$$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $$$k$$$ (recall that a subsegment of $$$a$$$ of length $$$k$$$ is a contiguous part of $$$a$$$ containing exactly $$$k$$$ elements). If there is no integer occuring in all subsegments of length $$$k$$$ for some value of $$$k$$$, then the $$$k$$$-amazing number is $$$-1$$$.For each $$$k$$$ from $$$1$$$ to $$$n$$$ calculate the $$$k$$$-amazing number of the array $$$a$$$.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class C { private static final String INPUT_FILE_PATH = ""; void solve() { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int[] a = new int[n]; int[] first = new int[n + 1]; int[] last = new int[n + 1]; int[] ms = new int[n + 1]; Arrays.fill(first, -1); Arrays.fill(last, -1); Arrays.fill(ms, 0); for (int i = 0; i < n; i++) { a[i] = in.nextInt(); if (last[a[i]] != -1) { ms[a[i]] = Math.max(ms[a[i]], i - last[a[i]] - 1); } if (first[a[i]] == -1) first[a[i]] = i; last[a[i]] = i; } for (int i = 1; i <= n; i++) { if (first[i] != -1) ms[i] = Math.max(ms[i], first[i]); if (last[i] != -1) ms[i] = Math.max(ms[i], n - last[i] - 1); } int[] ans = new int[n + 1]; Arrays.fill(ans, -1); int curr = 1; int index = n; while (index >= 1 && curr <= n) { if (first[curr] == -1) { curr++; continue; } if (ms[curr] < index) { ans[index] = curr; index--; } else { curr++; } } for (int i = 1; i <= n; i++) { out.print(ans[i] + " "); } out.println(); } } private final InputReader in; private final PrintWriter out; private C(InputReader in, PrintWriter out) { this.in = in; this.out = out; } private static class InputReader { private BufferedReader br; private StringTokenizer st; public InputReader(InputStream inputStream) { this.br = new BufferedReader(new InputStreamReader(inputStream), 32768); this.st = null; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } public static void main(String[] args) throws Exception { InputStream inputStream = INPUT_FILE_PATH.isEmpty() ? System.in : new FileInputStream(new File(INPUT_FILE_PATH)); OutputStream outputStream = System.out; InputReader inputReader = new InputReader(inputStream); PrintWriter printWriter = new PrintWriter(outputStream); new C(inputReader, printWriter).solve(); printWriter.close(); } }
Java
["3\n5\n1 2 3 4 5\n5\n4 4 4 4 2\n6\n1 3 1 5 3 1"]
1 second
["-1 -1 3 2 1 \n-1 4 4 4 2 \n-1 -1 1 1 1 1"]
null
Java 11
standard input
[ "data structures" ]
b7abfb1103bb1796c8f89653c303d7dc
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
1,500
For each test case print $$$n$$$ integers, where the $$$i$$$-th integer is equal to the $$$i$$$-amazing number of the array.
standard output
PASSED
b8b9db6d92f4b5c21e4a02f202b2e152
train_002.jsonl
1601219100
You are given an array $$$a$$$ consisting of $$$n$$$ integers numbered from $$$1$$$ to $$$n$$$.Let's define the $$$k$$$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $$$k$$$ (recall that a subsegment of $$$a$$$ of length $$$k$$$ is a contiguous part of $$$a$$$ containing exactly $$$k$$$ elements). If there is no integer occuring in all subsegments of length $$$k$$$ for some value of $$$k$$$, then the $$$k$$$-amazing number is $$$-1$$$.For each $$$k$$$ from $$$1$$$ to $$$n$$$ calculate the $$$k$$$-amazing number of the array $$$a$$$.
256 megabytes
import java.util.*; import java.io.*; public class C673 { 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 [] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = sc.nextInt(); int [] dist = new int[n + 1]; Arrays.fill(dist, -1); for (int i = 1; i <= n; i++) { dist[a[i]] = -1; } // last position of element i in last[i] int [] last = new int[n + 1]; for (int i = 1; i <= n; i++) { dist[a[i]] = Math.max(i - last[a[i]], dist[a[i]]); last[a[i]] = i; } for (int i = 1; i <= n; i++) { dist[i] = Math.max(n + 1 - last[i], dist[i]); } ArrayList<Pair> p = new ArrayList<>(); for (int i = 1; i <= n; i++) p.add(new Pair(i, dist[i])); Collections.sort(p); int cur = -1; int res = Integer.MAX_VALUE; for (int i = 1; i <= n; i++) { while (cur < n - 1 && p.get(cur + 1).d <= i) { res = Math.min(res, p.get(cur + 1).x); cur++; } out.print((res == Integer.MAX_VALUE ? -1 : res) + " "); } out.println(); } out.close(); } static class Pair implements Comparable<Pair> { int x; int d; Pair(int x, int d) { this.x = x; this.d = d; } @Override public int compareTo(Pair o) { if (d == o.d) return Integer.compare(o.x, x); else return Integer.compare(d, o.d); } } //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n5\n1 2 3 4 5\n5\n4 4 4 4 2\n6\n1 3 1 5 3 1"]
1 second
["-1 -1 3 2 1 \n-1 4 4 4 2 \n-1 -1 1 1 1 1"]
null
Java 11
standard input
[ "data structures" ]
b7abfb1103bb1796c8f89653c303d7dc
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
1,500
For each test case print $$$n$$$ integers, where the $$$i$$$-th integer is equal to the $$$i$$$-amazing number of the array.
standard output
PASSED
e1f1121498bbff642d03540a40b28739
train_002.jsonl
1601219100
You are given an array $$$a$$$ consisting of $$$n$$$ integers numbered from $$$1$$$ to $$$n$$$.Let's define the $$$k$$$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $$$k$$$ (recall that a subsegment of $$$a$$$ of length $$$k$$$ is a contiguous part of $$$a$$$ containing exactly $$$k$$$ elements). If there is no integer occuring in all subsegments of length $$$k$$$ for some value of $$$k$$$, then the $$$k$$$-amazing number is $$$-1$$$.For each $$$k$$$ from $$$1$$$ to $$$n$$$ calculate the $$$k$$$-amazing number of the array $$$a$$$.
256 megabytes
import java.util.*; import java.util.Scanner; import java.io.*; import javax.lang.model.util.ElementScanner6; import static java.lang.System.out; public class C1417 { public static void main(String args[]) { FastReader in=new FastReader(); PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int tc=1; tc=in.nextInt(); while(tc-->0) { int n=in.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++)arr[i]=in.nextInt(); HashMap<Integer,Integer> hm=new HashMap<>(); int mg[]=new int[n+1]; for(int i=0;i<n;i++) { int last=hm.getOrDefault(arr[i], -1); mg[arr[i]]=Math.max(mg[arr[i]], i-last); hm.put(arr[i], i); } int ans[]=new int[n+1]; Arrays.fill(ans, Integer.MAX_VALUE); for(int i=1;i<=n;i++) { if(mg[i]!=0) { mg[i]=Math.max(mg[i], n-hm.get(i)); ans[mg[i]]=Math.min(ans[mg[i]], i); } } if(ans[1]==Integer.MAX_VALUE)ans[1]=-1; for(int i=2;i<=n;i++) { if(ans[i-1]!=-1) { ans[i]=Math.min(ans[i], ans[i-1]); } if(ans[i]==Integer.MAX_VALUE) ans[i]=-1; } for(int i=1;i<=n;i++) { pr.print(ans[i]+" "); } pr.println(); } pr.flush(); } static class Pair implements Comparable<Pair> { int val, idx; public Pair(int val, int idx) { this.val = val; this.idx = idx + 1; } @Override public int compareTo(Pair o) { return val - o.val; } } static int gcd(int a,int b) { if(a==0) return b; return gcd(b%a,a); } static int lcm(int a,int b) { return (a*b)/gcd(a,b); } static boolean prime[]; static void sieveofe() { int n=1000000; prime=new boolean[n+1]; Arrays.fill(prime,true); prime[1]=false; for(int i=2;i*i<=n;i++) { if(prime[i]==true) { for(int j=i*i;j<=n;j+=i) { prime[j]=false; } } } } //to always sort via merge sort, as sorting a primitive ds uses quick sort //which can have O(n^2) complexity, thus not optimal static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n5\n1 2 3 4 5\n5\n4 4 4 4 2\n6\n1 3 1 5 3 1"]
1 second
["-1 -1 3 2 1 \n-1 4 4 4 2 \n-1 -1 1 1 1 1"]
null
Java 11
standard input
[ "data structures" ]
b7abfb1103bb1796c8f89653c303d7dc
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
1,500
For each test case print $$$n$$$ integers, where the $$$i$$$-th integer is equal to the $$$i$$$-amazing number of the array.
standard output
PASSED
d07c71ff7c74c5e16ec33e63bc8a04aa
train_002.jsonl
1601219100
You are given an array $$$a$$$ consisting of $$$n$$$ integers numbered from $$$1$$$ to $$$n$$$.Let's define the $$$k$$$-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length $$$k$$$ (recall that a subsegment of $$$a$$$ of length $$$k$$$ is a contiguous part of $$$a$$$ containing exactly $$$k$$$ elements). If there is no integer occuring in all subsegments of length $$$k$$$ for some value of $$$k$$$, then the $$$k$$$-amazing number is $$$-1$$$.For each $$$k$$$ from $$$1$$$ to $$$n$$$ calculate the $$$k$$$-amazing number of the array $$$a$$$.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class ProblemC { static int gcd(int a, int b) { while(a%b!=0) { int buf = b; b=a%b; a = buf; } return b; } static int lcm(int a, int b) { return Math.abs(a*b)/gcd(a,b); } // static int[] getArrayFromLine(String s, int n) { // int[] res = new int[n]; // int p = 0; // for (int l: Arrays.stream(s.split(" ")).map(Integer::parseInt).collect(Collectors.toList())) { // res[p++] = l; // } // return res; // }; public static void main(String[] args) { // genPrimes(Integer.MAX_VALUE); try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1024*1024*16)) { int _t = Integer.parseInt(br.readLine()); while (_t-- > 0) { int n = Integer.parseInt(br.readLine()); solve(n, Arrays.stream(br.readLine().split(" ")).map(Integer::parseInt).collect(Collectors.toList())); } } catch (Exception e) { System.out.println("BAD"); e.printStackTrace(); } } private static void solve(int n, List<Integer> arr) { // cached positions int[] maxDist = new int[n+1]; int[] result = new int[n+1]; int[] lastPosition = new int[n+1]; for (int i = 1; i <= n; i++) { result[i] = -1; int c = arr.get(i-1); maxDist[c] = Math.max(maxDist[c], i-lastPosition[c]); lastPosition[c] = i; } for (int i = 1; i <= n; i++) { maxDist[i] = Math.max(maxDist[i], n+1-lastPosition[i]); for (int k = maxDist[i]; k <= n && result[k]==-1; k++) { result[k] = i; } } StringBuilder sb = new StringBuilder(); for (int k = 1; k <= n; k++) { sb.append(result[k] + " "); } System.out.println(sb.toString()); } }
Java
["3\n5\n1 2 3 4 5\n5\n4 4 4 4 2\n6\n1 3 1 5 3 1"]
1 second
["-1 -1 3 2 1 \n-1 4 4 4 2 \n-1 -1 1 1 1 1"]
null
Java 11
standard input
[ "data structures" ]
b7abfb1103bb1796c8f89653c303d7dc
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
1,500
For each test case print $$$n$$$ integers, where the $$$i$$$-th integer is equal to the $$$i$$$-amazing number of the array.
standard output