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
2015b0de422d1279a23b15ae5d0a8881
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Solution { public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); int T = in.nextInt(); StringBuilder sb = new StringBuilder(); for(int t=0; t<T; t++ ){ int n = in.nextInt(); int x = in.nextInt(); int[] arr = new int[n]; for(int i=0; i<n; i++) arr[i] = in.nextInt(); int[] dp = new int[n+1]; for(int i=1; i<=n; i++){ int sum = 0; for(int j=0; j<i; j++) sum += arr[j]; int curMax = sum; for(int j=i; j<n; j++){ sum -= arr[j-i]; sum += arr[j]; curMax = Math.max(curMax, sum); } dp[i] = curMax; } for(int k=0; k<=n; k++){ int max = Integer.MIN_VALUE; for(int j=0; j<=n; j++){ max = Math.max(max, x*Math.min(k, j) + dp[j]); } sb.append(max); sb.append(" "); } sb.append("\n"); } System.out.println(sb); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
d82f5b61e798ac338d15ff5e964b721e
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class CodeforcesEdu123{ static long mod = 1000000007L; static MyScanner sc = new MyScanner(); static void solve(){ String str = sc.nextLine(); boolean r = false; boolean g = false; boolean b = false; for(int i = 0;i<str.length();i++){ if(str.charAt(i)=='r') r = true; if(str.charAt(i)=='g') g = true; if(str.charAt(i)=='b') b = true; if(str.charAt(i)=='R' && !r){ out.println("NO"); return; } if(str.charAt(i)=='G' && !g){ out.println("NO"); return; } if(str.charAt(i)=='B' && !b){ out.println("NO"); return; } } out.println("YES"); } static void solve2(){ int n = sc.nextInt(); int arr[] = new int[n]; int k = 0; for(int i = n;i>=1;i--){ arr[k++] = i; out.print(i+" "); } out.println(); for(int i = 0 ;i<n-1;i++){ int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; for(int j = 0;j<n;j++){ out.print(arr[j]+" "); } out.println(); temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; } } static void solve3() { int n = sc.nextInt(); int x = sc.nextInt(); int arr[] = sc.readIntArray(n); long ans[] = new long[n+1]; Arrays.fill(ans,Integer.MIN_VALUE); ans[0] = 0; for(int i = 0;i<n;i++){ long cur = 0; for(int j = i;j<n;j++){ cur+= arr[j]; ans[j-i+1] = Math.max(ans[j-i+1],cur); } } long max = Integer.MIN_VALUE; for(int i = 0;i<=n;i++){ for(int j = i;j<=n;j++){ max = Math.max(max,ans[j]+x*i); } out.print(max+" "); } out.println(); } static boolean isPali(String str){ int i = 0; int j = str.length()-1; while(i<j){ if(str.charAt(i)!=str.charAt(j)){ return false; } i++; j--; } return true; } static long gcd(long a,long b){ if(b==0) return a; return gcd(b,a%b); } static String reverse(String str){ char arr[] = str.toCharArray(); int i = 0; int j = arr.length-1; while(i<j){ char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } String st = new String(arr); return st; } static boolean isprime(int n){ if(n==1) return false; if(n==3 || n==2) return true; if(n%2==0 || n%3==0) return false; for(int i = 5;i*i<=n;i+= 6){ if(n%i== 0 || n%(i+2)==0){ return false; } } return true; } static class Pair implements Comparable<Pair>{ int val; int ind; int ans; Pair(int v,int f){ val = v; ind = f; } public int compareTo(Pair p){ return p.val - this.val; } } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while(t-- >0){ // solve(); solve3(); } // Stop writing your solution here. ------------------------------------- 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[] readIntArray(int n){ int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(next()); } return arr; } int[] reverse(int arr[]){ int n= arr.length; int i = 0; int j = n-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; j--;i++; } return arr; } long[] readLongArray(int n){ long arr[] = new long[n]; for(int i = 0;i<n;i++){ arr[i] = Long.parseLong(next()); } return arr; } 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; } } private static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
b360849b9176442b833ee67c348c0331
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; // cd C:\Users\Lenovo\Desktop\New //ArrayList<Integer> a=new ArrayList<>(); //List<Integer> lis=new ArrayList<>(); //StringBuilder ans = new StringBuilder(); //HashMap<Integer,Integer> map=new HashMap<>(); public class cf { static FastReader in=new FastReader(); static final Random random=new Random(); static long mod=1000000007L; //static long dp[]=new long[200002]; public static void main(String args[]) throws IOException { FastReader sc=new FastReader(); //Scanner s=new Scanner(System.in); int tt=sc.nextInt(); //int tt=1; while(tt-->0){ int n=sc.nextInt(); int x=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } int size[]=new int[n+1]; Arrays.fill(size,Integer.MIN_VALUE); size[0]=0; for(int i=0;i<n;i++){ int cursum=0; for(int j=i;j<n;j++){ cursum+=arr[j]; size[j-i+1]=Math.max(size[j-i+1],cursum); } } /*for(int i=0;i<=n;i++){ System.out.print(size[i]+" "); } System.out.println();*/ for(int i=0;i<=n;i++){ long res=0; for(int j=1;j<=n;j++){ res=Math.max(res,size[j]+x*Math.min(i,j)*1l); } System.out.print(res+" "); } System.out.println(); } } static void rotate(int ans[]){ int last=ans[0]; for(int i=0;i<ans.length-1;i++){ ans[i]=ans[i+1]; } ans[ans.length-1]=last; } static int reduce(int n){ while(n>1){ if(n%2==1){ n--; n=n/2; } else{ if(n%4==0){ n=n/4; } else{ break; } } } return n; } static long count(long n,int p,HashSet<Long> set){ if(n<Math.pow(2,p)){ set.add(n); return count(2*n+1,p,set)+count(4*n,p,set)+1; } return 0; } static int countprimefactors(int n){ int ans=0; int z=(int)Math.sqrt(n); for(int i=2;i<=z;i++){ while(n%i==0){ ans++; n=n/i; } } if(n>1){ ans++; } return ans; } /*static int count(int arr[],int idx,long sum,int []dp){ if(idx>=arr.length){ return 0; } if(dp[idx]!=-1){ return dp[idx]; } if(arr[idx]<0){ return dp[idx]=Math.max(1+count(arr,idx+1,sum+arr[idx],dp),count(arr,idx+1,sum,dp)); } else{ return dp[idx]=1+count(arr,idx+1,sum+arr[idx],dp); } }*/ static String reverse(String s){ String ans=""; for(int i=s.length()-1;i>=0;i--){ ans+=s.charAt(i); } return ans; } static int find_max(int []check,int y){ int max=0; for(int i=y;i>=0;i--){ if(check[i]!=0){ max=i; break; } } return max; } static void dfs(int [][]arr,int row,int col,boolean [][]seen,int n){ if(row<0 || col<0 || row==n || col==n){ return; } if(arr[row][col]==1){ return; } if(seen[row][col]==true){ return; } seen[row][col]=true; dfs(arr,row+1,col,seen,n); dfs(arr,row,col+1,seen,n); dfs(arr,row-1,col,seen,n); dfs(arr,row,col-1,seen,n); } static int msb(int x){ int ans=0; while(x!=0){ x=x/2; ans++; } return ans; } static void ruffleSort(int[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } /* static boolean checkprime(int n1) { if(n1%2==0||n1%3==0) return false; else { for(int i=5;i*i<=n1;i+=6) { if(n1%i==0||n1%(i+2)==0) return false; } return true; } } */ /* Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry<Integer, Integer> entry = iterator.next(); int value = entry.getValue(); if(value==1){ iterator.remove(); } else{ entry.setValue(value-1); } } */ static class Pair implements Comparable { int a,b; public String toString() { return a+" " + b; } public Pair(int x , int y) { a=x;b=y; } @Override public int compareTo(Object o) { Pair p = (Pair)o; if(p.a!=a){ return a-p.a;//in } else{ return b-p.b;// } } } /* public static boolean checkAP(List<Integer> lis){ for(int i=1;i<lis.size()-1;i++){ if(2*lis.get(i)!=lis.get(i-1)+lis.get(i+1)){ return false; } } return true; } public static int minBS(int[]arr,int val){ int l=-1; int r=arr.length; while(r>l+1){ int mid=(l+r)/2; if(arr[mid]>=val){ r=mid; } else{ l=mid; } } return r; } public static int maxBS(int[]arr,int val){ int l=-1; int r=arr.length; while(r>l+1){ int mid=(l+r)/2; if(arr[mid]<=val){ l=mid; } else{ r=mid; } } return l; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; }*/ 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\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
f53fb557b7271fdcaa1899f079971872
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; public class main1{ // public static class Pair{ // int sum; // int len; // Pair(int sum,int len){ // this.sum=sum; // this.len=len; // } // } // public static Pair kadaneAlgo(int[] arr){ // int cSum=0,bSum=0,len=0; // for(int i=0;i<arr.length;i++){ // if(cSum<0){ // cSum=arr[i]; // len=1; // }else{ // cSum+=arr[i]; // len++; // } // if(cSum>bSum){ // bSum=cSum; // } // } // Pair p=new Pair(bSum,len); // return p; // } public static void main(String[] args){ Scanner scn=new Scanner(System.in); int t=scn.nextInt(); while(t-->0){ int n=scn.nextInt(); int x=scn.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=scn.nextInt(); } int[] msarr=new int[n+1]; Arrays.fill(msarr,Integer.MIN_VALUE); msarr[0]=0; for(int i=0;i<n;i++){ int sum=0; for(int j=i;j<n;j++){ sum+=arr[j]; msarr[j-i+1]=Math.max(msarr[j-i+1],sum); } } for(int k=0;k<=n;k++){ int ans=0; for(int i=0;i<=n;i++){ ans=Math.max(ans,msarr[i]+Math.min(k,i)*x); } System.out.print(ans+" "); } System.out.println(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
6981e11685d867ee39d61519cafc0d45
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
// Problem: C. Increase Subarray Sums // Contest: Codeforces - Educational Codeforces Round 123 (Rated for Div. 2) // URL: https://codeforces.com/contest/1644/problem/C // Memory Limit: 256 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) import java.io.*; import java.math.MathContext; import java.math.RoundingMode; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.util.Collections; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.*; public class Main { public static void main(String[] args) throws IOException { // try { // Scanner in = new Scanner(System.in) ; FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); StringBuilder sb = new StringBuilder() ; int t = in.nextInt() ; while(t-- > 0){ int n = in.nextInt() ; int x = in.nextInt() ; int a[] = in.readArray(n) ; long dp[][] = new long[n+1][n+1] ; long sum = 0 , max = 0 ; for(int i=0 ; i<n ; i++){ sum += a[i] ; if(sum < 0){ sum = 0 ; } dp[0][i+1] = sum; ; max = max(max , sum ) ; } sb.append(max + " ") ; for(int j=1; j<=n ; j++){ max = 0 ; for(int i=1 ; i<=n ; i++){ dp[j][i] = a[i-1] + dp[j-1][i-1] + x ; dp[j][i] = max(dp[j][i] , a[i-1] + x) ; max = max(max , dp[j][i]) ; } sb.append(max + " " ) ; } sb.append("\n") ; } System.out.println(sb) ; out.flush(); out.close(); // } catch (Exception e) { // return; // } } // solution END // template start : static long gcd(long a, long b) {return (b == 0) ? a : gcd(b, a % b);} static int gcd(int a, int b) {return (b == 0) ? a : gcd(b, a % b);} static void sort(int ar[]) { int n = ar.length; ArrayList<Integer> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++)ar[i] = a.get(i); } static void sort1(long ar[]) { int n = ar.length; ArrayList<Long> a = new ArrayList<>(); for (int i = 0; i < n; i++)a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++)ar[i] = a.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) {e.printStackTrace();} return st.nextToken(); } int nextInt() { return Integer.parseInt(next());} int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() {return Long.parseLong(next());} } } // template end
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
39a5a200a2dcde2c85b71b543c523aa4
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; public class C { /** * Template @author William Fiset, william.alexandre.fiset@gmail.com * */ static InputReader in = new InputReader(System.in); private static StringBuilder sb = new StringBuilder(); static PrintWriter out = new PrintWriter(System.out); static class InputReader { private static final int DEFAULT_BUFFER_SIZE = 1 << 16; private static final InputStream DEFAULT_STREAM = System.in; private static final int MAX_DECIMAL_PRECISION = 21; private int c; private byte[] buf; private int bufferSize, bufIndex, numBytesRead; private InputStream stream; private static final byte EOF = -1; private static final byte NEW_LINE = 10; private static final byte SPACE = 32; private static final byte DASH = 45; private static final byte DOT = 46; private char[] charBuffer; private static byte[] bytes = new byte[58]; private static int[] ints = new int[58]; private static char[] chars = new char[128]; static { char ch = ' '; int value = 0; byte _byte = 0; for (int i = 48; i < 58; i++) { bytes[i] = _byte++; } for (int i = 48; i < 58; i++) { ints[i] = value++; } for (int i = 32; i < 128; i++) { chars[i] = ch++; } } private static final double[][] doubles = { {0.0d, 0.00d, 0.000d, 0.0000d, 0.00000d, 0.000000d, 0.0000000d, 0.00000000d, 0.000000000d, 0.0000000000d, 0.00000000000d, 0.000000000000d, 0.0000000000000d, 0.00000000000000d, 0.000000000000000d, 0.0000000000000000d, 0.00000000000000000d, 0.000000000000000000d, 0.0000000000000000000d, 0.00000000000000000000d, 0.000000000000000000000d}, {0.1d, 0.01d, 0.001d, 0.0001d, 0.00001d, 0.000001d, 0.0000001d, 0.00000001d, 0.000000001d, 0.0000000001d, 0.00000000001d, 0.000000000001d, 0.0000000000001d, 0.00000000000001d, 0.000000000000001d, 0.0000000000000001d, 0.00000000000000001d, 0.000000000000000001d, 0.0000000000000000001d, 0.00000000000000000001d, 0.000000000000000000001d}, {0.2d, 0.02d, 0.002d, 0.0002d, 0.00002d, 0.000002d, 0.0000002d, 0.00000002d, 0.000000002d, 0.0000000002d, 0.00000000002d, 0.000000000002d, 0.0000000000002d, 0.00000000000002d, 0.000000000000002d, 0.0000000000000002d, 0.00000000000000002d, 0.000000000000000002d, 0.0000000000000000002d, 0.00000000000000000002d, 0.000000000000000000002d}, {0.3d, 0.03d, 0.003d, 0.0003d, 0.00003d, 0.000003d, 0.0000003d, 0.00000003d, 0.000000003d, 0.0000000003d, 0.00000000003d, 0.000000000003d, 0.0000000000003d, 0.00000000000003d, 0.000000000000003d, 0.0000000000000003d, 0.00000000000000003d, 0.000000000000000003d, 0.0000000000000000003d, 0.00000000000000000003d, 0.000000000000000000003d}, {0.4d, 0.04d, 0.004d, 0.0004d, 0.00004d, 0.000004d, 0.0000004d, 0.00000004d, 0.000000004d, 0.0000000004d, 0.00000000004d, 0.000000000004d, 0.0000000000004d, 0.00000000000004d, 0.000000000000004d, 0.0000000000000004d, 0.00000000000000004d, 0.000000000000000004d, 0.0000000000000000004d, 0.00000000000000000004d, 0.000000000000000000004d}, {0.5d, 0.05d, 0.005d, 0.0005d, 0.00005d, 0.000005d, 0.0000005d, 0.00000005d, 0.000000005d, 0.0000000005d, 0.00000000005d, 0.000000000005d, 0.0000000000005d, 0.00000000000005d, 0.000000000000005d, 0.0000000000000005d, 0.00000000000000005d, 0.000000000000000005d, 0.0000000000000000005d, 0.00000000000000000005d, 0.000000000000000000005d}, {0.6d, 0.06d, 0.006d, 0.0006d, 0.00006d, 0.000006d, 0.0000006d, 0.00000006d, 0.000000006d, 0.0000000006d, 0.00000000006d, 0.000000000006d, 0.0000000000006d, 0.00000000000006d, 0.000000000000006d, 0.0000000000000006d, 0.00000000000000006d, 0.000000000000000006d, 0.0000000000000000006d, 0.00000000000000000006d, 0.000000000000000000006d}, {0.7d, 0.07d, 0.007d, 0.0007d, 0.00007d, 0.000007d, 0.0000007d, 0.00000007d, 0.000000007d, 0.0000000007d, 0.00000000007d, 0.000000000007d, 0.0000000000007d, 0.00000000000007d, 0.000000000000007d, 0.0000000000000007d, 0.00000000000000007d, 0.000000000000000007d, 0.0000000000000000007d, 0.00000000000000000007d, 0.000000000000000000007d}, {0.8d, 0.08d, 0.008d, 0.0008d, 0.00008d, 0.000008d, 0.0000008d, 0.00000008d, 0.000000008d, 0.0000000008d, 0.00000000008d, 0.000000000008d, 0.0000000000008d, 0.00000000000008d, 0.000000000000008d, 0.0000000000000008d, 0.00000000000000008d, 0.000000000000000008d, 0.0000000000000000008d, 0.00000000000000000008d, 0.000000000000000000008d}, {0.9d, 0.09d, 0.009d, 0.0009d, 0.00009d, 0.000009d, 0.0000009d, 0.00000009d, 0.000000009d, 0.0000000009d, 0.00000000009d, 0.000000000009d, 0.0000000000009d, 0.00000000000009d, 0.000000000000009d, 0.0000000000000009d, 0.00000000000000009d, 0.000000000000000009d, 0.0000000000000000009d, 0.00000000000000000009d, 0.000000000000000000009d} }; public InputReader() { this(DEFAULT_STREAM, DEFAULT_BUFFER_SIZE); } public InputReader(int bufferSize) { this(DEFAULT_STREAM, bufferSize); } public InputReader(InputStream stream) { this(stream, DEFAULT_BUFFER_SIZE); } public InputReader(InputStream stream, int bufferSize) { if (stream == null || bufferSize <= 0) { throw new IllegalArgumentException(); } buf = new byte[bufferSize]; charBuffer = new char[128]; this.bufferSize = bufferSize; this.stream = stream; } private byte read() throws IOException { if (numBytesRead == EOF) { throw new IOException(); } if (bufIndex >= numBytesRead) { bufIndex = 0; numBytesRead = stream.read(buf); if (numBytesRead == EOF) { return EOF; } } return buf[bufIndex++]; } private void doubleCharBufferSize() { char[] newBuffer = new char[charBuffer.length << 1]; for (int i = 0; i < charBuffer.length; i++) { newBuffer[i] = charBuffer[i]; } charBuffer = newBuffer; } private int readJunk(int token) throws IOException { if (numBytesRead == EOF) { return EOF; } do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > token) { return 0; } bufIndex++; } numBytesRead = stream.read(buf); if (numBytesRead == EOF) { return EOF; } bufIndex = 0; } while (true); } public byte nextByte() throws IOException { return (byte) nextInt(); } public int nextInt() throws IOException { if (readJunk(DASH - 1) == EOF) { throw new IOException(); } int sgn = 1, res = 0; c = buf[bufIndex]; if (c == DASH) { sgn = -1; bufIndex++; } do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { res = (res << 3) + (res << 1); res += ints[buf[bufIndex++]]; } else { bufIndex++; return res * sgn; } } numBytesRead = stream.read(buf); if (numBytesRead == EOF) { return res * sgn; } bufIndex = 0; } while (true); } public int[] nextIntArray(int n) throws IOException { int[] ar = new int[n]; for (int i = 0; i < n; i++) { ar[i] = nextInt(); } return ar; } public void close() throws IOException { stream.close(); } public long nextLong() throws IOException { if (readJunk(DASH - 1) == EOF) { throw new IOException(); } int sgn = 1; long res = 0L; c = buf[bufIndex]; if (c == DASH) { sgn = -1; bufIndex++; } do { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { res = (res << 3) + (res << 1); res += ints[buf[bufIndex++]]; } else { bufIndex++; return res * sgn; } } numBytesRead = stream.read(buf); if (numBytesRead == EOF) { return res * sgn; } bufIndex = 0; } while (true); } public String nextString() throws IOException { if (numBytesRead == EOF) { return null; } if (readJunk(SPACE) == EOF) { return null; } for (int i = 0;;) { while (bufIndex < numBytesRead) { if (buf[bufIndex] > SPACE) { if (i == charBuffer.length) { doubleCharBufferSize(); } charBuffer[i++] = (char) buf[bufIndex++]; } else { bufIndex++; return new String(charBuffer, 0, i); } } // Reload buffer numBytesRead = stream.read(buf); if (numBytesRead == EOF) { return new String(charBuffer, 0, i); } bufIndex = 0; } } } public static void main(String args[]) throws Exception { int t = in.nextInt(); while (t-- != 0) { solve(); } out.print(sb.toString()); out.close(); } static void solve() throws IOException { int n = in.nextInt(); int x = in.nextInt(); int[] arr = in.nextIntArray(n); HashMap<Integer, Integer> hm = new HashMap(); for (int i = 0;i<n;i++) { int sum = 0; int max = sum; for (int j = i;j<n;j++) { int kNeeded = j - i + 1; sum += arr[j] + x; if (sum > max) { max = sum; hm.put(kNeeded, Math.max(hm.getOrDefault(kNeeded, 0), max)); } } } for (int i = 0;i<=n;i++) { int max = 0; for (int j = 0;j<=n;j++) { int wouldHaveGotten = hm.getOrDefault(j, 0) - Math.max(0, j - i) * x; max = Math.max(max, wouldHaveGotten); } sb.append(max).append(' '); } sb.append("\n"); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
3cd24ae179d747fad46685bb26d7f77f
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class IncreaseSubArraySums { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while (t-->0){ String[] in=br.readLine().split(" "); int size=Integer.parseInt(in[0]); int k=Integer.parseInt(in[1]); String[] inp=br.readLine().split(" "); int[] arr=new int[size]; for(int i=0;i<size;i++)arr[i]=Integer.parseInt(inp[i]); int min=0; int[] max=new int[size+1]; Arrays.fill(max,Integer.MIN_VALUE); max[0]=0; for(int i=1;i<=size;i++){ int s=0; int sum=0; for(int j=0;j<size;j++){ if(s<i){ sum+=arr[j]; s++; max[i]=sum; } else{ sum+=arr[j]-arr[j-s]; max[i]=Math.max(max[i],sum); } } } int best=Integer.MIN_VALUE; for(int i=0;i<=size;i++){ int add=i*k; int m=max[i]+add; for(int j=i;j<=size;j++){ m=Math.max(max[j]+add,m); } best=Math.max(best,m); System.out.print(best+" "); } System.out.println(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
39fc6147eda047d02aa02d541e5e989e
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); // int a = 1; int t; t = in.nextInt(); //t = 1; while (t > 0) { // out.print("Case #"+(a++)+": "); solver.call(in,out); t--; } out.close(); } static class TaskA { Map<String, Integer> map; int n, x; int[][] dp = new int[5001][5001]; int[] arr = new int[5001]; public void call(InputReader in, PrintWriter out) { n = in.nextInt(); x = in.nextInt(); map = new HashMap<>(); for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } for (int i = 0; i <= n; i++) { Arrays.fill(dp[i], 0); } int max, sum, ans = 0; for (int i = 0; i < n; i++) { max = 0; sum = arr[i]; max = Math.max(max, sum); for (int j = i - 1; j >= 0; j--) { sum += arr[j]; max = Math.max(max, sum); } dp[0][i] = max; ans = Math.max(dp[0][i], ans); } out.print(ans+" "); for (int i = 1; i <= n; i++) { ans = 0; for (int j = 0; j < n; j++) { if(j==0){ dp[i][j] = Math.max(dp[i][j], x + arr[j]); } else { dp[i][j] = Math.max(dp[i][j], x + arr[j] + dp[i - 1][j - 1]); } ans = Math.max(dp[i][j],ans); } out.print(ans+" "); } out.println(); } } 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 / gcd(a, b)) * b; } static class answer implements Comparable<answer>{ int a, b; public answer(int a, int b, int c) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return Integer.compare(this.b, o.b); } } static class answer1 implements Comparable<answer1>{ int a, b, c; public answer1(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } @Override public int compareTo(answer1 o) { return (o.a - this.a); } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (Long i:a) l.add(i); l.sort(Collections.reverseOrder()); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static final Random random=new Random(); static void shuffleSort(int[] a) { int n = a.length; for (int i=0; i<n; i++) { int oi= random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
aac0bd7f47960f82b189764d9bafc3de
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; import java.util.concurrent.ThreadLocalRandom; //import javax.print.attribute.HashAttributeSet; //import com.sun.tools.javac.code.Attribute.Array; public class c731 { public static void main(String[] args) throws IOException { // try { BufferedWriter out = new BufferedWriter( new OutputStreamWriter(System.out)); BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); PrintWriter pt = new PrintWriter(System.out); FastReader sc = new FastReader(); int t = sc.nextInt(); for(int o = 0; o<t;o++) { int n = sc.nextInt(); int x = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i<n;i++) { arr[i] = sc.nextInt(); } int[] pre = new int[n+1]; for(int i = 1 ; i<=n;i++) { pre[i] = pre[i-1] + arr[i-1]; } int[] map = new int[n+1]; for(int i = 1 ; i<=n;i++) { int temp = Integer.MIN_VALUE; for(int j = 0 ; j<=n-i;j++) { temp = Math.max(temp, pre[j+i] - pre[j]); } map[i] = temp; } int m = Integer.MIN_VALUE; for(int i = 0 ; i<=n;i++) { // System.out.print(map[i] + " "); m = Math.max(m, map[i]); } // System.out.println(); int[] ans = new int[n+1]; ans[0] = m; for(int k = 1 ; k<=n;k++) { int v = Integer.MIN_VALUE; for(int j = 0 ; j<=n;j++) { if(j<k) { v = Math.max(map[j], v); }else{ v = Math.max(map[j] + x * k,v ); } } ans[k] = Math.max(ans[k-1], v); } for(int i = 0 ; i<=n;i++) { System.out.print(ans[i] + " "); } System.out.println(); } // out.flush(); // int mod = (int)1e9 + 7; // long[] fib = new long[(int) 1e6]; // fib[0] = 1; // fib[1] = 1; // for (int i = 2; i < fib.length; i++) { // fib[i] = (fib[i - 1] + fib[i - 2]) % mod; // } // for (int i = 1; i < fib.length; i++) { // fib[i] = (fib[i - 1] + fib[i]) % mod; // } // System.out.println(fib[2] + " " + fib[3] + " " + fib[5]); } // // } // // }catch(Exception e) { // return; // } // } //------------------------------------------------------------------------------------------------------------------------------------------------ public static boolean check(int[] arr , int n) { for(int i = 1 ; i<n;i++) { if(arr[i]<arr[i-1]) { return false; } } return true; } public static void dfs(int s , HashMap<Integer, ArrayList<Integer>> map, boolean[] vis) { vis[s] = true; for(int x : map.get(s)) { if(!vis[x]) { dfs(x, map, vis); } } } public static int lis(int[] arr) { int n = arr.length; ArrayList<Integer> al = new ArrayList<Integer>(); al.add(arr[0]); for(int i = 1 ; i<n;i++) { int x = al.get(al.size()-1); if(arr[i]>=x) { al.add(arr[i]); }else { int v = upper_bound(al, 0, al.size(), arr[i]); al.set(v, arr[i]); } } return al.size(); } static int cntDivisors(int n){ int cnt = 0; for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { if (n/i == i) cnt++; else cnt+=2; } } return cnt; } public static long power(long x, long y, long p){ long res = 1; x = x % p; if (x == 0) return 0; while (y > 0){ if ((y & 1) != 0) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } public static long ncr(long[] fac, int n , int r , long m) { return fac[n]*(modInverse(fac[r], m))%m *(modInverse(fac[n-r], m))%m; } public static int lower_bound(ArrayList<Integer> ar,int lo , int hi , int k) { int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (ar.get(mid) <k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return -1; } return s; } public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k) { int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (ar.get(mid) <=k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return -1; } return s; } // ----------------------------------------------------------------------------------------------------------------------------------------------- public static int gcd(int a, int b){ if (a == 0) return b; return gcd(b % a, a); } //-------------------------------------------------------------------------------------------------------------------------------------------------------- public static long modInverse(long a, long m){ long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient long q = a / m; long t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } //----------------------------------------------------------------------------------------------------------------------------------- //segment tree //for finding minimum in range public static void build(long [] seg,long []arr,int idx, int lo , int hi) { if(lo == hi) { seg[idx] = arr[lo]; return; } int mid = (lo + hi)/2; build(seg,arr,2*idx+1, lo, mid); build(seg,arr,idx*2+2, mid +1, hi); // seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]); // seg[idx] = seg[idx*2+1]+ seg[idx*2+2]; seg[idx] = Math.min(seg[idx*2+1], seg[idx*2+2]); } //for finding minimum in range public static long query(long[]seg,int idx , int lo , int hi , int l , int r) { if(lo>=l && hi<=r) { return seg[idx]; } if(hi<l || lo>r) { return 0; } int mid = (lo + hi)/2; long left = query(seg,idx*2 +1, lo, mid, l, r); long right = query(seg,idx*2 + 2, mid + 1, hi, l, r); // return Math.min(left, right); //return gcd(left, right); return Math.min(left, right); } // // for sum // //public static void update(int[]seg,int idx, int lo , int hi , int node , int val) { // if(lo == hi) { // seg[idx] += val; // }else { //int mid = (lo + hi )/2; //if(node<=mid && node>=lo) { // update(seg, idx * 2 +1, lo, mid, node, val); //}else { // update(seg, idx*2 + 2, mid + 1, hi, node, val); //} //seg[idx] = seg[idx*2 + 1] + seg[idx*2 + 2]; // //} //} //--------------------------------------------------------------------------------------------------------------------------------------- // static void shuffleArray(int[] ar) // { // // If running on Java 6 or older, use `new Random()` on RHS here // Random rnd = ThreadLocalRandom.current(); // for (int i = ar.length - 1; i > 0; i--) // { // int index = rnd.nextInt(i + 1); // // Simple swap // int a = ar[index]; // ar[index] = ar[i]; // ar[i] = a; // } // } //----------------------------------------------------------------------------------------------------------------------------------------------------------- } 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 SegmentTree{ int n; public SegmentTree(int[] arr,int n) { this.arr = arr; this.n = n; } int[] arr = new int[n]; // int n = arr.length; int[] seg = new int[4*n]; void build(int idx, int lo , int hi) { if(lo == hi) { seg[idx] = arr[lo]; return; } int mid = (lo + hi)/2; build(2*idx+1, lo, mid); build(idx*2+2, mid +1, hi); seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]); } int query(int idx , int lo , int hi , int l , int r) { if(lo<=l && hi>=r) { return seg[idx]; } if(hi<l || lo>r) { return Integer.MAX_VALUE; } int mid = (lo + hi)/2; int left = query(idx*2 +1, lo, mid, l, r); int right = query(idx*2 + 2, mid + 1, hi, l, r); return Math.min(left, right); } } //----------------------------------------------------------------------------------------------------------------------------------------------------------- class coup{ int a; int b; public coup(int a , int b) { this.a = a; this.b = b; } } class trip{ int a , b, c; public trip(int a , int b, int c) { this.a = a; this.b = b; this.c = c; } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
3dd0eec6bf164615d75a2b496ff04739
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { static class pair { long r; long d; public pair(long r , long d) { this.r= r; this.d= 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; } } public static void main (String[] args) throws java.lang.Exception { OutputStream outputStream =System.out; PrintWriter out =new PrintWriter(outputStream); FastReader sc =new FastReader(); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int k = sc.nextInt(); int a[] = new int[n]; int ans[] = new int[n+1]; Arrays.fill(ans,Integer.MIN_VALUE); ans[0]=0; for(int i =0;i<n;i++) a[i]=sc.nextInt(); for(int i =0;i<n;i++) { int te =0; for(int j=i;j<n;j++){ te+=a[j]; ans[j-i+1]=Math.max(ans[j-i+1],te); } } // out.println(Arrays.toString(ans)); // out.println(Arrays.toString(a)); int min = Integer.MIN_VALUE; for(int j=0;j<=n;j++){ for(int i =j;i<=n;i++) { min = Math.max(ans[i]+Math.min(j,i)*k,min); } out.print(min+" "); } out.println(); } // out.println(); out.close(); // your code goes here } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
fb04ec4fd8d9cf9aa080039a3e6fca33
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int test = scanner.nextInt(); while(test --> 0) { int n = scanner.nextInt(), x = scanner.nextInt(); int[] array = new int[n], ans = new int[n+1]; array[0] = scanner.nextInt(); for(int i = 1; i < n; i++) array[i] = scanner.nextInt() + array[i-1]; for(int size = 1; size <= n; size++) { ans[size] = getSum(array, 0, size - 1); for(int i = 1, j = size; j < n; i++, j++) { ans[size] = Math.max(ans[size], getSum(array, i, j)); } } for(int i = ans.length - 2; i >= 0; i--) { ans[i] = (ans[i] > ans[i+1]) ? ans[i] : ans[i+1]; } for(int i = 1; i < ans.length; i++) { ans[i] = Math.max(ans[i] + i * x, ans[i - 1]); } printArray(ans); } } public static int getSum(int[] array, int from, int to) { return array[to] - ((from == 0)? 0 : array[from-1]); } public static void printArray(int[] array) { StringBuilder sb = new StringBuilder(); sb.append("" + array[0]); for(int i = 1; i < array.length; i++) sb.append(" " + array[i]); System.out.println(sb.toString()); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
f95b402e73f1ebd11b42e02cf9d1a9e7
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; /* */ public class C{ static FastReader sc=null; public static void main(String[] args) { sc=new FastReader(); int t=sc.nextInt(); for(int tt=0;tt<t;tt++) { int n=sc.nextInt(),x=sc.nextInt(); int a[]=sc.readArray(n); int dp[][]=new int[n+1][n+1]; //dp[i][j] is the maximum subarray sum if we did j operation till now for(int i=0;i<n;i++) { for(int j=0;j<=n;j++) { //use the operation on this one if(j<n)dp[i+1][j+1]=Math.max(dp[i+1][j+1], dp[i][j]+a[i]+x); //dont use dp[i+1][j]=Math.max(dp[i+1][j], dp[i][j]+a[i]); } } int ans[]=new int[n+1]; for(int i=0;i<n;i++) for(int j=0;j<=n;j++)ans[j]=Math.max(ans[j], dp[i+1][j]); for(int e:ans)System.out.print(e+" "); System.out.println(); } } static int[] ruffleSort(int a[]) { ArrayList<Integer> al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i<a.length;i++)a[i]=al.get(i); return a; } static void print(int a[]) { for(int e:a) { System.out.print(e+" "); } System.out.println(); } static class FastReader{ StringTokenizer st=new StringTokenizer(""); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 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()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); return a; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
bfbcbff80d82d899aa654c8212bd1e5b
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class Temp { static int t,n,arr[],ans[],x; static final BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); static final StreamTokenizer in = new StreamTokenizer(bf); static final PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) throws IOException { in.nextToken();t=(int)in.nval; while(t-->0) solve(); out.flush(); } static void solve() throws IOException{ in.nextToken();n=(int)in.nval;ans=new int[n+1]; in.nextToken();x=(int)in.nval; getArray(); for(int i=0;i<=n;++i){ for(int j=i-1;j>=0;--j){ ans[i-j]=Math.max(ans[i-j],arr[i]-arr[j]+(i-j)*x); } } for(int i=ans.length-2;i>=0;--i) ans[i]=Math.max(ans[i],ans[i+1]-x); for(int i=1;i<ans.length;i++) ans[i]=Math.max(ans[i],ans[i-1]); for(int num:ans) out.print(num+" "); out.println(); } static void getArray() throws IOException{ arr=new int[n+1]; for(int i=1;i<=n;++i){ in.nextToken();arr[i]=(int)in.nval+arr[i-1]; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
426923821e2420157741e548f9552bac
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
/* TASK: template LANG: JAVA */ import java.io.*; import java.lang.*; import java.lang.reflect.Array; import java.util.*; public class C1644 { public static void main(String[] args) throws IOException{ StringBuffer ans = new StringBuffer(); StringTokenizer st; BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(f.readLine()); int t = Integer.parseInt(st.nextToken()); for(; t > 0; t--){ st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); st = new StringTokenizer(f.readLine()); int[] arr = new int[n]; int[] best = new int[n+1]; for(int i = 0; i < n; i++){ arr[i] = Integer.parseInt(st.nextToken()); } Arrays.fill(best, Integer.MIN_VALUE); best[0] = 0; for(int i = 0; i < n; i++){ int sum = 0; for(int j = i; j < n; j++){ sum+=arr[j]; best[0] = Math.max(best[0], sum); best[j-i+1] = Math.max(sum, best[j-i+1]); } } for(int i = n-1; i > -1; i--){ best[i] = Math.max(best[i+1], best[i]); } //System.out.println(Arrays.toString(best)); for(int i = 0; i < n+1; i++){ best[i] = best[i]+x*i; if(i != 0) best[i] = Math.max(best[i], best[i-1]); ans.append(best[i]).append(" "); } ans.append("\n"); } f.close(); System.out.println(ans); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
8eb96b2748f9d1788e13fe04fd1bdcfa
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.io.*; public class C1644 { static int[] a; static int n, x; static long[][] dp; public static long dp(int idx, int inc) { if (idx == 0) { return Math.max(0, a[idx] + (inc == 0 ? 0 : x)); } if (dp[idx][inc] != -1) { return dp[idx][inc]; } if (inc == 0) { return dp[idx][inc] = Math.max(0, dp(idx - 1, 0) + a[idx]); } return dp[idx][inc] = Math.max(Math.max(0, dp(idx - 1, inc) + a[idx]), Math.max(0, dp(idx - 1, inc - 1) + a[idx] + x)); } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { n = sc.nextInt(); x = sc.nextInt(); a = sc.nextIntArr(n); long[] ans = new long[n + 1]; dp = new long[n + 1][n + 1]; for (long[] x : dp) Arrays.fill(x, -1); for (int i = 0; i <= n; i++) { for (int j = 0; j < n; j++) { // System.out.println(j + " " + i + " " + dp(j, i)); ans[i] = Math.max(ans[i], dp(j, i)); } } for (long x : ans) { pw.print(x + " "); } pw.println(); } pw.close(); } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader f) { br = new BufferedReader(f); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] nextIntArr(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(next()); } return arr; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
61a34c9848409bcfc5b5f8eca73d9a54
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; import java.util.SortedMap; import java.util.StringTokenizer; import java.util.TreeMap; import java.text.DecimalFormat; 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); solve(in, out); out.close(); } static String reverse(String s) { return (new StringBuilder(s)).reverse().toString(); } static void sieveOfEratosthenes(int n, int factors[], ArrayList<Integer> ar) { Arrays.fill(factors, 0); factors[1]=1; int p; for(p = 2; p*p <=n; p++) { if(factors[p] == 0) { ar.add(p); factors[p]=p; for(int i = p*p; i <= n; i += p) factors[i] = p; } } for(;p<=n;p++){ if(factors[p] == 0) { ar.add(p); } } } static int lower_bound(int val, int high,int [] arr) { int low = 0; int ans = -1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] <= val) { low = mid + 1; ans = mid; } else high = mid - 1; } return ans; } static int upper_bound(int val, int high, int [] a) { int low = 0; int ans = high + 1; while (low <= high) { int mid = (low + high) / 2; if (a[mid] >= val) { high = mid - 1; ans = mid; } else low = mid + 1; } return ans; } static void sort(int ar[]) { int n = ar.length; ArrayList<Integer> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static void sort1(long ar[]) { int n = ar.length; ArrayList<Long> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static long ncr(long n, long r, long mod) { if (r == 0) return 1; long val = ncr(n - 1, r - 1, mod); val = (n * val) % mod; val = (val * modInverse(r, mod)) % mod; return val; } public static int convert(String a, String b) { int T = 0; if (b.charAt(0) == 'P') { if (a.charAt(0) == '1' && a.charAt(1) == '2') T = 12; else T = (a.charAt(0) - '0') * 10 + (a.charAt(1) - '0') + 12; } else if (b.charAt(0) == 'A') { if (a.charAt(0) == '1' && a.charAt(1) == '2') T = 0; else T = (a.charAt(0) - '0') * 10 + (a.charAt(1) - '0'); } T = T * 100 + (a.charAt(3) - '0') * 10 + (a.charAt(4) - '0'); return T; } static boolean areBracketsBalanced(String expr) { // Using ArrayDeque is faster than using Stack class Deque<Character> stack = new ArrayDeque<Character>(); // Traversing the Expression for (int i = 0; i < expr.length(); i++) { char x = expr.charAt(i); if (x == '(' || x == '[' || x == '{') { // Push the element in the stack stack.push(x); continue; } // IF current current character is not opening // bracket, then it must be closing. So stack // cannot be empty at this point. if (stack.isEmpty()) return false; char check; switch (x) { case ')': check = stack.pop(); if (check == '{' || check == '[') return false; break; case '}': check = stack.pop(); if (check == '(' || check == '[') return false; break; case ']': check = stack.pop(); if (check == '(' || check == '{') return false; break; } } // Check Empty Stack return (stack.isEmpty()); } public static int hr(String s) { int hh = 0; hh = (s.charAt(0) - '0') * 10 + (s.charAt(1) - '0'); return hh; } public static int mn(String s) { int mm = 0; mm = (s.charAt(3) - '0') * 10 + (s.charAt(4) - '0'); return mm; } /* * public static void countNumberOfPrimeFactors(){ boolean flag[]=new * boolean[N]; Arrays.fill(flag,true); for(int i=2;i<N;i++){ if(flag[i]){ * for(int j=i;j<N;j+=i) { prime[j]++; flag[j]=false; } } } // prime[1]=1; } */ public static String rev(String s) { char[] temp = s.toCharArray(); for (int i = 0; i < s.length() / 2; i++) { char tmp = temp[i]; temp[i] = temp[s.length() - 1 - i]; temp[s.length() - 1 - i] = tmp; } String str = ""; for (char ch : temp) { str += ch; } return str; } static boolean isP(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } public static boolean isSorted(List<Integer> l) { int n = l.size(), i = 0; for (i = 1; i < l.size(); i++) { if (l.get(i - 1) < l.get(i)) return false; } return true; } public static boolean ln(long n, int c) { int l = 0; long t = n; while (t != 0) { t /= 10; l++; } return l == c; } static int lower_bound(int val, int high,ArrayList<Integer> a) { int low = 0; int ans = -1; while (low <= high) { int mid = (low + high) / 2; if (a.get(mid) <= val) { low = mid + 1; ans = mid; } else high = mid - 1; } return ans; } static int upper_bound(int val, int high, ArrayList<Integer> a) { int low = 0; int ans = high + 1; while (low <= high) { int mid = (low + high) / 2; if (a.get(mid) >= val) { high = mid - 1; ans = mid; } else low = mid + 1; } return ans; } public static int fun(int n) { if(n==1) return 1; return n*fun(n-1); } static class Pair { char x; int y; Pair(char x, int y) { this.x = x; this.y = y; } } static int josephus(int n, int k) { if (n == 1) return 1; else return (josephus(n - 1, k) + k ) % n +1; } static int onesComplement(int n) { // Find number of bits in the // given integer int number_of_bits = (int)(Math.floor(Math.log(n) / Math.log(2))) + 1; // XOR the given integer with poe(2, // number_of_bits-1 and print the result return ((1 << number_of_bits) - 1) ^ n; } static int msb(int N) { N = N | (N >> 1); N = N | (N >> 2); N = N | (N >> 4); N =N | (N >> 8); N = N | (N >> 16); N = N + 1; return N+1; } public static void solve(InputReader sc, PrintWriter pw) { int t=sc.nextInt(); for(int k=1;k<=t;k++) { int n=sc.nextInt(); int x=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); int p[]=new int[n+1]; p[0]=0; p[1]=a[0]; for(int i=1;i<n;i++) p[i+1]=p[i]+a[i]; int ans[]=new int[n]; for(int i=0;i<n;i++) { int mn=Integer.MIN_VALUE; for(int j=0;j<n-i;j++) { mn=Math.max(mn, p[i+j+1]-p[j]); } ans[i]=mn; } for(int i=0;i<=n;i++) { int mx=0; for(int j=0;j<n;j++) { mx=Math.max(mx,ans[j]+Math.min(j+1, i)*x ); } pw.print(mx+" "); } pw.print("\n"); } pw.print("\n"); } // static class Pair1 { // int a; // int b; // // Pair1(int a, int b) { // this.a = a; // this.b = b; // } // } static boolean isPrime(long 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 long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static int _gcd(int a, int b) { if (b == 0) return a; return _gcd(b, a % b); } static long fast_pow(long base, long n, long M) { if (n == 0) return 1; if (n == 1) return base % M; long halfn = fast_pow(base, n / 2, M); if (n % 2 == 0) return (halfn * halfn) % M; else return (((halfn * halfn) % M) * base) % M; } static long modInverse(long n, long M) { return fast_pow(n, M - 2, M); } public static int LowerBound(int a[], int x) { int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } public static int UpperBound(int a[], int x) { int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String nextLine() { return null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
52a2fa7f2c8fd07786597045a2036185
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class a { public static void main(String[] args){ FastScanner sc = new FastScanner(); StringBuilder ans = new StringBuilder(); int t = sc.nextInt(); while(t-- > 0){ int n = sc.nextInt(); int x = sc.nextInt(); int arr[] = new int[n]; int prefix[] = new int[n+1]; for(int i=0; i<n; i++){ arr[i] = sc.nextInt(); prefix[i+1] = prefix[i] + arr[i]; } int dp[] = new int[n+1]; dp[0] = 0; for(int i=1; i<=n; i++){ int val = Integer.MIN_VALUE; for(int j=i; j<=n; j++){ val = Math.max(val, prefix[j] - prefix[j-i]); } dp[i] = val; } int suffixsum[] = new int[n+1]; suffixsum[n] = dp[n]; for(int i=n-1; i>=0; i--){ suffixsum[i] = Math.max(suffixsum[i+1], dp[i]); } int prev = 0; for(int i=0; i<=n; i++){ if(i==0){ ans.append(suffixsum[0] + " "); prev = suffixsum[0]; continue; } int a = Math.max(prev, suffixsum[i] + x*i); prev = a; ans.append(a + " "); } ans.append("\n"); } System.out.println(ans); } } class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
12b13d21fa8023614471515c1e3aba26
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.*; import java.util.*; public class habd{ public static long My_maxSubArraySum(long a[]) { int size = a.length; long max_so_far = Long.MIN_VALUE, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tc = sc.nextInt(); while(tc-->0){ int n = sc.nextInt(); long x = sc.nextInt(); long[] arr = sc.nextlongArray(n); long[] dp = new long[n + 1]; Arrays.fill(dp, Long.MIN_VALUE); for(int i = 0; i<n; i++){ long temp = 0; for(int j = i; j<n; j++){ temp += arr[j]; dp[j-i+1] = Math.max(dp[j-i+1], temp); } } for(int i = 0; i<=n; i++){ long ans = 0; for(int j = 1; j<=n; j++){ ans = Math.max(ans, dp[j] + Math.min(i, j) * x); } pw.print(ans + " "); } pw.println(); } pw.flush(); } public static int differences(int n, int test){ int changes = 0; while(test > 0){ if(test % 10 != n % 10)changes++; test/=10; n/=10; } return changes; } static int maxSubArraySum(int a[], int size) { int max_so_far = Integer.MIN_VALUE, max_ending_here = 0,start = 0, end = 0, s = 0; for (int i = 0; i < size; i++) { max_ending_here += a[i]; if (max_so_far < max_ending_here) { max_so_far = max_ending_here; start = s; end = i; } if (max_ending_here < 0) { max_ending_here = 0; s = i + 1; } } return start; } static int maxSubArraySum2(int a[], int size) { int max_so_far = Integer.MIN_VALUE, max_ending_here = 0,start = 0, end = 0, s = 0; for (int i = 0; i < size; i++) { max_ending_here += a[i]; if (max_so_far < max_ending_here) { max_so_far = max_ending_here; start = s; end = i; } if (max_ending_here < 0) { max_ending_here = 0; s = i + 1; } } return end; } static class SegmentTree{ long[]array,sTree; int N; public SegmentTree(long arr[]) { array=arr; N=arr.length-1; sTree=new long[N<<1]; } void update(int idx){ array[idx]++; idx+=N-1; sTree[idx]++; while(idx>1) { idx>>=1; sTree[idx]++; } } void set(int idx, long val){ array[idx]++; idx+=N-1; sTree[idx]++; while(idx>1) { idx>>=1; sTree[idx]++; } } long query(int l,int r) { return query(1,1,N,l,r); } long query(int node,int s,int e,int l,int r) { if(l>e || r<s)return 0; if(l<=s && r>=e)return sTree[node]; int mid=(s+e)>>1; return query(node<<1,s,mid,l,r)+query(node<<1|1,mid+1,e,l,r); } } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static class Pair implements Comparable<Pair>{ int x; int y; public Pair(int x, int y){ this.x = x; this.y = y; } public int compareTo(Pair x){ return this.x - x.x; } public String toString(){ return "(" + x + "," + y + ")"; } } public static boolean isSubsequence(char[] arr, String s){ boolean ans = false; for(int i = 0, j = 0; i<arr.length; i++){ if(arr[i] == s.charAt(j)){ j++; } if(j == s.length()){ ans = true; break; } } return ans; } public static void sortIdx(long[]a,long[]idx) { mergesortidx(a, idx, 0, a.length-1); } static void mergesortidx(long[] arr,long[]idx,int b,int e) { if(b<e) { int m=b+(e-b)/2; mergesortidx(arr,idx,b,m); mergesortidx(arr,idx,m+1,e); mergeidx(arr,idx,b,m,e); } return; } static void mergeidx(long[] arr,long[]idx,int b,int m,int e) { int len1=m-b+1,len2=e-m; long[] l=new long[len1]; long[] lidx=new long[len1]; long[] r=new long[len2]; long[] ridx=new long[len2]; for(int i=0;i<len1;i++) { l[i]=arr[b+i]; lidx[i]=idx[b+i]; } for(int i=0;i<len2;i++) { r[i]=arr[m+1+i]; ridx[i]=idx[m+1+i]; } int i=0,j=0,k=b; while(i<len1 && j<len2) { if(l[i]<=r[j]) { arr[k++]=l[i++]; idx[k-1]=lidx[i-1]; } else { arr[k++]=r[j++]; idx[k-1]=ridx[j-1]; } } while(i<len1) { idx[k]=lidx[i]; arr[k++]=l[i++]; } while(j<len2) { idx[k]=ridx[j]; arr[k++]=r[j++]; } return; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { 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 long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
afff5f4885314879a41a82e97c087fc2
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Codechef { static long fans[] = new long[200001]; static long inv[] = new long[200001]; static long mod = 1000000007; static void init() { fans[0] = 1; inv[0] = 1; fans[1] = 1; inv[1] = 1; for (int i = 2; i < 200001; i++) { fans[i] = ((long) i * fans[i - 1]) % mod; inv[i] = power(fans[i], mod - 2); } } static long ncr(int n, int r) { return (((fans[n] * inv[n - r]) % mod) * (inv[r])) % mod; } public static void main(String[] args) throws java.lang.Exception { FastReader in = new FastReader(System.in); StringBuilder sb = new StringBuilder(); int t = 1; t = in.nextInt(); while (t > 0) { --t; int n = in.nextInt(); long arr[] = new long[n]; long x = in.nextLong(); for(int i = 0;i<n;i++) arr[i] = in.nextLong(); long sum = 0; long arr2[] = new long[n+1]; Arrays.fill(arr2, Long.MIN_VALUE); for(int i = 0;i<n;i++) { sum = 0; for(int j = i;j<n;j++) { sum += arr[j]; arr2[j-i+1] = Math.max(arr2[j-i+1], sum); } } for(int i = 0;i<=n;i++) { long max = 0; for(int j = 1;j<=n;j++) { max = Math.max(max, arr2[j] + (long)Math.min(i, j) * x); } sb.append(max+" "); } sb.append("\n"); } System.out.print(sb); } static long power(long x, long y) { long res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = ((res % mod) * (x % mod)) % mod; // y must be even now y = y >> 1; // y = y/2 x = ((x % mod) * (x % mod)) % mod; // Change x to x^2 } return res; } static long[] generateArray(FastReader in, int n) throws IOException { long arr[] = new long[n]; for (int i = 0; i < n; i++) arr[i] = in.nextLong(); return arr; } static long[][] generatematrix(FastReader in, int n, int m) throws IOException { long arr[][] = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = in.nextLong(); } } return arr; } static long gcd(long a, long b) { if (a == 0) return b; else return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } 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); } } class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
068263bade08742941714573834ccbe8
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
//package Codeforces; import java.io.*; import java.util.*; public class C { static class Pair { long count; long sum; Pair(long l, long sum) { count = l; this.sum = sum; } Pair(){} @Override public String toString() { return "Pair{" + "count=" + count + ", sum=" + sum + '}'; } } public static void main (String[] Z) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder op = new StringBuilder(); StringTokenizer stz; int T = Integer.parseInt(br.readLine()); while(T-- > 0) { stz = new StringTokenizer(br.readLine()); int n = Integer.parseInt(stz.nextToken()); long x = Integer.parseInt(stz.nextToken()); stz = new StringTokenizer(br.readLine()); ArrayList<Long> list = new ArrayList<>(n); for (int i = 0; i < n; i++) { long num = Integer.parseInt(stz.nextToken()); list.add(num); } // ArrayList<Pair> subs = new ArrayList<>(); // subs.add(new Pair(0, 0)); long[] maxSum = new long[n+1]; Arrays.fill(maxSum, Long.MIN_VALUE); maxSum[0] = 0; for (int i = 0 ; i < n ; i++) { long currSum = 0; for (int j = i ; j < n ; j++) { currSum += list.get(j); maxSum[j - i + 1] = Math.max(maxSum[j - i + 1], currSum); } } for (int k = 0 ; k <= n ; k++) { long ans = 0; for (int len = 0 ; len <= n ; len++) { long curr = 0; long kk = k; if(k < len) { curr = maxSum[len] + x * kk; } else { curr = maxSum[len] + len * x; } ans = Math.max(curr, ans); } op.append(ans); op.append(' '); } op.append("\n"); } System.out.println(op); // END OF CODE } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 8
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
162879b89884727045abced9522c30bc
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
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 Main { public static boolean issorted(int []arr){ for(int i = 1;i<arr.length;i++){ if(arr[i]<arr[i-1]){ return false; } } return true; } public static long sum(int []arr){ long sum = 0; for(int i = 0;i<arr.length;i++){ sum+=arr[i]; } return sum; } public static class pair implements Comparable<pair>{ int x; int y; pair(int x,int y){ this.x = x; this.y = y; } public int compareTo(pair o){ return this.x - o.x; // sort increasingly on the basis of x // return o.x - this.x // sort decreasingly on the basis of x } } public static void swap(int []arr,int i,int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static int gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); } static int []parent = new int[1000]; static int []size = new int[1000]; public static void make(int v){ parent[v] = v; size[v] = 1; } public static int find(int v){ if(parent[v]==v){ return v; } else{ return parent[v] = find(parent[v]); } } public static void union(int a,int b){ a = find(a); b = find(b); if(a!=b){ if(size[a]>size[b]){ parent[b] = parent[a]; size[b]+=size[a]; } else{ parent[a] = parent[b]; size[a]+=size[b]; } } } static boolean []visited = new boolean[1000]; public static void dfs(int vertex,ArrayList<ArrayList<Integer>>graph){ if(visited[vertex] == true){ return; } System.out.println(vertex); for(int child : graph.get(vertex)){ // work to be done before entering the child dfs(child,graph); // work to be done after exitting the child } } public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-->0){ int n = scn.nextInt(); int x = scn.nextInt(); int []arr = new int[n]; for(int i = 0;i<n;i++){ arr[i] = scn.nextInt(); } long [] sub = new long[n+1]; Arrays.fill(sub,Long.MIN_VALUE); sub[0] = 0; for(int i = 0;i<n;i++){ int temp = 0; for(int j = i;j<n;j++){ temp+=arr[j]; sub[j-i+1] = Math.max(sub[j-i+1],temp); } } for(int i = 0;i<=n;i++){ long ans = Long.MIN_VALUE; for(int j = 0;j<=n;j++){ long mul = Math.min(j,i); ans = Math.max(ans,sub[j]+mul*x); } System.out.print(ans+" "); } System.out.println(); } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 17
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
efe90feef9cd72f6ef30190a83cb0676
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; import static java.lang.System.*; import static java.util.Arrays.*; import static java.util.stream.IntStream.iterate; public class Template { private static final FastScanner scanner = new FastScanner(); private static void solve() { int n = i(), x = i(); var a = scanner.readArray(n); var dp = new int[n+1]; fill(dp, (int) -1e9); dp[0]+=(int) 1e9; for (int i = 0; i<n; i++) { int sum = 0; for (int j = i; j<n; j++) { sum+=a[j]; dp[j-i+1] = max(dp[j-i+1], sum); } } // printArray(dp); StringBuilder builder = new StringBuilder(); for (int k = 0; k<=n; k++) { int mx = 0; for (int i = 0; i<=n; i++) { mx = max(mx, dp[i]+min(i, k)*x); } builder.append(mx).append(' '); } out.println(builder); } public static void main(String[] args) { int t = i(); while (t-->0) { solve(); } } private static long fac(int n) { long res = 1; for (int i = 2; i<=n; i++) { res*=i; } return res; } private static BigInteger factorial(int n) { BigInteger res = BigInteger.valueOf(1); for (int i = 2; i <= n; i++){ res = res.multiply(BigInteger.valueOf(i)); } return res; } private static long l() { return scanner.nextLong(); } private static int i() { return scanner.nextInt(); } private static String s() { return scanner.next(); } private static void yes() { out.println("YES"); } private static void no() { out.println("NO"); } private static int toInt(char c) { return Integer.parseInt(c+""); } private static void printArray(long[] a) { StringBuilder builder = new StringBuilder(); for (long i : a) builder.append(i).append(' '); out.println(builder); } private static void printArray(int[] a) { StringBuilder builder = new StringBuilder(); for (int i : a) builder.append(i).append(' '); out.println(builder); } private static int binPow(int a, int n) { if (n == 0) return 1; if (n % 2 == 1) return binPow(a, n-1) * a; else { int b = binPow(a, n/2); return b * b; } } private static boolean isPrime(long n) { return iterate(2, i -> (long) i * i <= n, i -> i + 1).noneMatch(i -> n % i == 0); } private static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } private static void stableSort(int[] a) { List<Integer> list = stream(a).boxed().sorted().toList(); setAll(a, list::get); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(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[] readLong(int n) { long[] a = new long[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()); } } static class SegmentTreeRMXQ { static int getMid(int s, int e) { return s + (e - s) / 2; } static int MaxUtil(int[] st, int ss, int se, int l, int r, int node) { if (l <= ss && r >= se) return st[node]; if (se < l || ss > r) return -1; int mid = getMid(ss, se); return max( MaxUtil(st, ss, mid, l, r, 2 * node + 1), MaxUtil(st, mid + 1, se, l, r, 2 * node + 2)); } static void updateValue(int[] arr, int[] st, int ss, int se, int index, int value, int node) { if (index < ss || index > se) { System.out.println("Invalid Input"); return; } if (ss == se) { arr[index] = value; st[node] = value; } else { int mid = getMid(ss, se); if (index <= mid) updateValue(arr, st, ss, mid, index, value, 2 * node + 1); else updateValue(arr, st, mid + 1, se, index, value, 2 * node + 2); st[node] = max(st[2 * node + 1], st[2 * node + 2]); } } static int getMax(int[] st, int n, int l, int r) { if (l < 0 || r > n - 1 || l > r) { System.out.print("Invalid Input\n"); return -1; } return MaxUtil(st, 0, n - 1, l, r, 0); } static int constructSTUtil(int[] arr, int ss, int se, int[] st, int si) { if (ss == se) { st[si] = arr[ss]; return arr[ss]; } int mid = getMid(ss, se); st[si] = max( constructSTUtil(arr, ss, mid, st, si * 2 + 1), constructSTUtil(arr, mid + 1, se, st, si * 2 + 2)); return st[si]; } static int[] constructST(int[] arr, int n) { int x = (int)Math.ceil(Math.log(n) / Math.log(2)); int max_size = 2 * (int)Math.pow(2, x) - 1; int[] st = new int[max_size]; constructSTUtil(arr, 0, n - 1, st, 0); return st; } } static class SegmentTreeRMNQ { int[] st; int minVal(int x, int y) { return min(x, y); } int getMid(int s, int e) { return s + (e - s) / 2; } int RMQUtil(int ss, int se, int qs, int qe, int index) { if (qs <= ss && qe >= se) return st[index]; if (se < qs || ss > qe) return Integer.MAX_VALUE; int mid = getMid(ss, se); return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1), RMQUtil(mid + 1, se, qs, qe, 2 * index + 2)); } int RMQ(int n, int qs, int qe) { if (qs < 0 || qe > n - 1 || qs > qe) { System.out.println("Invalid Input"); return -1; } return RMQUtil(0, n - 1, qs, qe, 0); } int constructSTUtil(int[] arr, int ss, int se, int si) { if (ss == se) { st[si] = arr[ss]; return arr[ss]; } int mid = getMid(ss, se); st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1), constructSTUtil(arr, mid + 1, se, si * 2 + 2)); return st[si]; } void constructST(int[] arr, int n) { int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); int max_size = 2 * (int) Math.pow(2, x) - 1; st = new int[max_size]; // allocate memory constructSTUtil(arr, 0, n - 1, 0); } } static class SegmentTreeRSQ { int[] st; // The array that stores segment tree nodes /* Constructor to construct segment tree from given array. This constructor allocates memory for segment tree and calls constructSTUtil() to fill the allocated memory */ SegmentTreeRSQ(int[] arr, int n) { // Allocate memory for segment tree //Height of segment tree int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); //Maximum size of segment tree int max_size = 2 * (int) Math.pow(2, x) - 1; st = new int[max_size]; // Memory allocation constructSTUtil(arr, 0, n - 1, 0); } // A utility function to get the middle index from corner indexes. int getMid(int s, int e) { return s + (e - s) / 2; } /* A recursive function to get the sum of values in given range of the array. The following are parameters for this function. st --> Pointer to segment tree si --> Index of current node in the segment tree. Initially 0 is passed as root is always at index 0 ss & se --> Starting and ending indexes of the segment represented by current node, i.e., st[si] qs & qe --> Starting and ending indexes of query range */ int getSumUtil(int ss, int se, int qs, int qe, int si) { // If segment of this node is a part of given range, then return // the sum of the segment if (qs <= ss && qe >= se) return st[si]; // If segment of this node is outside the given range if (se < qs || ss > qe) return 0; // If a part of this segment overlaps with the given range int mid = getMid(ss, se); return getSumUtil(ss, mid, qs, qe, 2 * si + 1) + getSumUtil(mid + 1, se, qs, qe, 2 * si + 2); } /* A recursive function to update the nodes which have the given index in their range. The following are parameters st, si, ss and se are same as getSumUtil() i --> index of the element to be updated. This index is in input array. diff --> Value to be added to all nodes which have I in range */ void updateValueUtil(int ss, int se, int i, int diff, int si) { // Base Case: If the input index lies outside the range of // this segment if (i < ss || i > se) return; // If the input index is in range of this node, then update the // value of the node and its children st[si] = st[si] + diff; if (se != ss) { int mid = getMid(ss, se); updateValueUtil(ss, mid, i, diff, 2 * si + 1); updateValueUtil(mid + 1, se, i, diff, 2 * si + 2); } } // The function to update a value in input array and segment tree. // It uses updateValueUtil() to update the value in segment tree void updateValue(int arr[], int n, int i, int new_val) { // Check for erroneous input index if (i < 0 || i > n - 1) { System.out.println("Invalid Input"); return; } // Get the difference between new value and old value int diff = new_val - arr[i]; // Update the value in array arr[i] = new_val; // Update the values of nodes in segment tree updateValueUtil(0, n - 1, i, diff, 0); } // Return sum of elements in range from index qs (query start) to // qe (query end). It mainly uses getSumUtil() int getSum(int n, int qs, int qe) { // Check for erroneous input values if (qs < 0 || qe > n - 1 || qs > qe) { System.out.println("Invalid Input"); return -1; } return getSumUtil(0, n - 1, qs, qe, 0); } // A recursive function that constructs Segment Tree for array[ss..se]. // si is index of current node in segment tree st int constructSTUtil(int arr[], int ss, int se, int si) { // If there is one element in array, store it in current node of // segment tree and return if (ss == se) { st[si] = arr[ss]; return arr[ss]; } // If there are more than one elements, then recur for left and // right subtrees and store the sum of values in this node int mid = getMid(ss, se); st[si] = constructSTUtil(arr, ss, mid, si * 2 + 1) + constructSTUtil(arr, mid + 1, se, si * 2 + 2); return st[si]; } } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 17
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
f73c8a6da312138ce1a364a5aba8babe
train_108.jsonl
1645540500
You are given an array $$$a_1, a_2, \dots, a_n$$$, consisting of $$$n$$$ integers. You are also given an integer value $$$x$$$.Let $$$f(k)$$$ be the maximum sum of a contiguous subarray of $$$a$$$ after applying the following operation: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions. An empty subarray should also be considered, it has sum $$$0$$$.Note that the subarray doesn't have to include all of the increased elements.Calculate the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
256 megabytes
import java.util.*; import java.util.function.*; import java.io.*; // you can compare with output.txt and expected out public class RoundEdu123C { MyPrintWriter out; MyScanner in; // final static long FIXED_RANDOM; // static { // FIXED_RANDOM = System.currentTimeMillis(); // } final static String IMPOSSIBLE = "IMPOSSIBLE"; final static String POSSIBLE = "POSSIBLE"; final static String YES = "YES"; final static String NO = "NO"; private void initIO(boolean isFileIO) { if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) { try{ in = new MyScanner(new FileInputStream("input.txt")); out = new MyPrintWriter(new FileOutputStream("output.txt")); } catch(FileNotFoundException e){ e.printStackTrace(); } } else{ in = new MyScanner(System.in); out = new MyPrintWriter(new BufferedOutputStream(System.out)); } } public static void main(String[] args){ // Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); RoundEdu123C sol = new RoundEdu123C(); sol.run(); } private void run() { boolean isDebug = false; boolean isFileIO = true; boolean hasMultipleTests = true; initIO(isFileIO); int t = hasMultipleTests? in.nextInt() : 1; for (int i = 1; i <= t; ++i) { int n = in.nextInt(); int x = in.nextInt(); int[] a = in.nextIntArray(n); if(isDebug){ out.printf("Test %d\n", i); } int[] ans = solve(a, x); out.printlnAns(ans); if(isDebug) out.flush(); } in.close(); out.close(); } private int[] solve(int[] a, int x) { int n = a.length; int[] ans = new int[n+1]; int[] sum = new int[n+1]; int[] bestSum = new int[n+1]; bestSum[0] = 0; for(int i=0; i<n; i++) { for(int j=0; j<n-i; j++) { sum[j] += a[i+j]; } int max = -Integer.MIN_VALUE; for(int j=0; j<n-i; j++) max = Math.max(sum[j], max); bestSum[i+1] = max; } for(int i=0; i<=n; i++) { int max = -Integer.MIN_VALUE; for(int j=0; j<=n; j++) max = Math.max(bestSum[j]+x*Math.min(j, i), max); ans[i] = max; } return ans; } public static class MyScanner { BufferedReader br; StringTokenizer st; // 32768? public MyScanner(InputStream is, int bufferSize) { br = new BufferedReader(new InputStreamReader(is), bufferSize); } public MyScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); // br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt"))); } public void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } 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[][] nextTreeEdges(int n, int offset){ int[][] e = new int[n-1][2]; for(int i=0; i<n-1; i++){ e[i][0] = nextInt()+offset; e[i][1] = nextInt()+offset; } return e; } int[][] nextMatrix(int n, int m) { return nextMatrix(n, m, 0); } int[][] nextMatrix(int n, int m, int offset) { int[][] mat = new int[n][m]; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { mat[i][j] = nextInt()+offset; } } return mat; } int[][] nextPairs(int n){ return nextPairs(n, 0); } int[][] nextPairs(int n, int offset) { int[][] xy = new int[2][n]; for(int i=0; i<n; i++) { xy[0][i] = nextInt() + offset; xy[1][i] = nextInt() + offset; } return xy; } int[][] nextGraphEdges(){ return nextGraphEdges(0); } int[][] nextGraphEdges(int offset) { int m = nextInt(); int[][] e = new int[m][2]; for(int i=0; i<m; i++){ e[i][0] = nextInt()+offset; e[i][1] = nextInt()+offset; } return e; } int[] nextIntArray(int len) { return nextIntArray(len, 0); } int[] nextIntArray(int len, int offset){ int[] a = new int[len]; for(int j=0; j<len; j++) a[j] = nextInt()+offset; return a; } long[] nextLongArray(int len) { return nextLongArray(len, 0); } long[] nextLongArray(int len, int offset){ long[] a = new long[len]; for(int j=0; j<len; j++) a[j] = nextLong()+offset; return a; } } public static class MyPrintWriter extends PrintWriter{ public MyPrintWriter(OutputStream os) { super(os); } public void printlnAns(long ans) { println(ans); } public void printlnAns(int ans) { println(ans); } public void printlnAns(boolean ans) { if(ans) println(YES); else println(NO); } public void printAns(long[] arr){ if(arr != null && arr.length > 0){ print(arr[0]); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]); } } } public void printlnAns(long[] arr){ printAns(arr); println(); } public void printAns(int[] arr){ if(arr != null && arr.length > 0){ print(arr[0]); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]); } } } public void printlnAns(int[] arr){ printAns(arr); println(); } public <T> void printAns(ArrayList<T> arr){ if(arr != null && arr.size() > 0){ print(arr.get(0)); for(int i=1; i<arr.size(); i++){ print(" "); print(arr.get(i)); } } } public <T> void printlnAns(ArrayList<T> arr){ printAns(arr); println(); } public void printAns(int[] arr, int add){ if(arr != null && arr.length > 0){ print(arr[0]+add); for(int i=1; i<arr.length; i++){ print(" "); print(arr[i]+add); } } } public void printlnAns(int[] arr, int add){ printAns(arr, add); println(); } public void printAns(ArrayList<Integer> arr, int add) { if(arr != null && arr.size() > 0){ print(arr.get(0)+add); for(int i=1; i<arr.size(); i++){ print(" "); print(arr.get(i)+add); } } } public void printlnAns(ArrayList<Integer> arr, int add){ printAns(arr, add); println(); } public void printlnAnsSplit(long[] arr, int split){ if(arr != null){ for(int i=0; i<arr.length; i+=split){ print(arr[i]); for(int j=i+1; j<i+split; j++){ print(" "); print(arr[j]); } println(); } } } public void printlnAnsSplit(int[] arr, int split){ if(arr != null){ for(int i=0; i<arr.length; i+=split){ print(arr[i]); for(int j=i+1; j<i+split; j++){ print(" "); print(arr[j]); } println(); } } } public <T> void printlnAnsSplit(ArrayList<T> arr, int split){ if(arr != null && !arr.isEmpty()){ for(int i=0; i<arr.size(); i+=split){ print(arr.get(i)); for(int j=i+1; j<i+split; j++){ print(" "); print(arr.get(j)); } println(); } } } } static private void permutateAndSort(long[] a) { int n = a.length; Random R = new Random(System.currentTimeMillis()); for(int i=0; i<n; i++) { int t = R.nextInt(n-i); long temp = a[n-1-i]; a[n-1-i] = a[t]; a[t] = temp; } Arrays.sort(a); } static private void permutateAndSort(int[] a) { int n = a.length; Random R = new Random(System.currentTimeMillis()); for(int i=0; i<n; i++) { int t = R.nextInt(n-i); int temp = a[n-1-i]; a[n-1-i] = a[t]; a[t] = temp; } Arrays.sort(a); } static private int[][] constructChildren(int n, int[] parent, int parentRoot){ int[][] childrens = new int[n][]; int[] numChildren = new int[n]; for(int i=0; i<parent.length; i++) { if(parent[i] != parentRoot) numChildren[parent[i]]++; } for(int i=0; i<n; i++) { childrens[i] = new int[numChildren[i]]; } int[] idx = new int[n]; for(int i=0; i<parent.length; i++) { if(parent[i] != parentRoot) childrens[parent[i]][idx[parent[i]]++] = i; } return childrens; } static private int[][][] constructDirectedNeighborhood(int n, int[][] e){ int[] inDegree = new int[n]; int[] outDegree = new int[n]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; outDegree[u]++; inDegree[v]++; } int[][] inNeighbors = new int[n][]; int[][] outNeighbors = new int[n][]; for(int i=0; i<n; i++) { inNeighbors[i] = new int[inDegree[i]]; outNeighbors[i] = new int[outDegree[i]]; } int[] inIdx = new int[n]; int[] outIdx = new int[n]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; outNeighbors[u][outIdx[u]++] = v; inNeighbors[v][inIdx[v]++] = u; } return new int[][][] {inNeighbors, outNeighbors}; } static private int[][] constructNeighborhood(int n, int[][] e) { int[] degree = new int[n]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; degree[u]++; degree[v]++; } int[][] neighbors = new int[n][]; for(int i=0; i<n; i++) neighbors[i] = new int[degree[i]]; int[] idx = new int[n]; for(int i=0; i<e.length; i++) { int u = e[i][0]; int v = e[i][1]; neighbors[u][idx[u]++] = v; neighbors[v][idx[v]++] = u; } return neighbors; } static private void drawGraph(int[][] e) { makeDotUndirected(e); try { final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot") .redirectOutput(new File("graph.png")) .start(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } static private void makeDotUndirected(int[][] e) { MyPrintWriter out2 = null; try { out2 = new MyPrintWriter(new FileOutputStream("graph.dot")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } out2.println("strict graph {"); for(int i=0; i<e.length; i++){ out2.println(e[i][0] + "--" + e[i][1] + ";"); } out2.println("}"); out2.close(); } static private void makeDotDirected(int[][] e) { MyPrintWriter out2 = null; try { out2 = new MyPrintWriter(new FileOutputStream("graph.dot")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } out2.println("strict digraph {"); for(int i=0; i<e.length; i++){ out2.println(e[i][0] + "->" + e[i][1] + ";"); } out2.println("}"); out2.close(); } }
Java
["3\n\n4 2\n\n4 1 3 2\n\n3 5\n\n-2 -7 -1\n\n10 2\n\n-6 -1 -2 4 -6 -1 -4 4 -5 -4"]
2 seconds
["10 12 14 16 18\n0 4 4 5\n4 6 6 7 7 7 7 8 8 8 8"]
NoteIn the first testcase, it doesn't matter which elements you add $$$x$$$ to. The subarray with the maximum sum will always be the entire array. If you increase $$$k$$$ elements by $$$x$$$, $$$k \cdot x$$$ will be added to the sum.In the second testcase: For $$$k = 0$$$, the empty subarray is the best option. For $$$k = 1$$$, it's optimal to increase the element at position $$$3$$$. The best sum becomes $$$-1 + 5 = 4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 2$$$, it's optimal to increase the element at position $$$3$$$ and any other element. The best sum is still $$$4$$$ for a subarray $$$[3, 3]$$$. For $$$k = 3$$$, you have to increase all elements. The best sum becomes $$$(-2 + 5) + (-7 + 5) + (-1 + 5) = 5$$$ for a subarray $$$[1, 3]$$$.
Java 17
standard input
[ "brute force", "dp", "greedy", "implementation" ]
a5927e1883fbd5e5098a8454f6f6631f
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of testcases. The first line of the testcase contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 5000$$$; $$$0 \le x \le 10^5$$$) — the number of elements in the array and the value to add. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$5000$$$.
1,400
For each testcase, print $$$n + 1$$$ integers — the maximum value of $$$f(k)$$$ for all $$$k$$$ from $$$0$$$ to $$$n$$$ independently.
standard output
PASSED
5aaed030d1503bdb4a1f2c48fad65cdd
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); boolean works = true; HashMap<Integer, Integer> vals = new HashMap<Integer, Integer>(); for(int a = 0; a < t; a++) { String s = sc.nextLine(); for(int i = 0; i < 6; i++) { int asc = (int) s.charAt(i); if(65 <= asc && asc <= 90) { if(vals.containsKey(asc + 32)) { continue; } else { works = false; break; } } else { vals.put(asc, 1); } } if(works) { System.out.println("YES"); } else { System.out.println("NO"); } works = true; vals.clear(); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
34832673054cc62edbb94209e1602669
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc= new Scanner(System.in); // Scanner ch= new Scanner(System.in); int num=sc.nextInt(); while(sc.hasNextLine()) { String s= sc.nextLine(); func(s); } } public static void func(String str) { int r=0,g=0,b=0,R=0,G=0,B=0; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='r') { r=i; } else if(str.charAt(i)=='g') { g=i; } else if(str.charAt(i)=='b') { b=i; } else if(str.charAt(i)=='R') { R=i; } else if(str.charAt(i)=='G') { G=i; } else if(str.charAt(i)=='B') { B=i; } } if(R<r||G<g||B<b) { System.out.println("NO"); } else if(R>r||G>g||B>b) { System.out.println("YES"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
c6714047cedeb44374366ae3afda87ca
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class edu_round_123_A { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); while(T-->0) { String s = br.readLine(); char[] inputs = s.toCharArray(); // for(int i = 0; i < 6; i++) { // inputs[i] = str_nums[]; // } int[] keys = new int[3]; Arrays.fill(keys,0); boolean flag = false; for(int i = 0; i < 6; i++) { if(inputs[i]=='r') { keys[0] = 1; } else if(inputs[i]=='g') { keys[1] = 1; } else if(inputs[i]=='b') { keys[2] = 1; } else if(inputs[i]=='R') { if(keys[0]==0) { System.out.println("NO"); flag = true; break; } } else if(inputs[i]=='G') { if(keys[1]==0) { System.out.println("NO"); flag = true; break; } } else { if(keys[2]==0) { System.out.println("NO"); flag = true; break; } } } if(flag==false) { System.out.println("YES"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
5d65fc73b73929a83a0b79ff24223e0a
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { // write your code here int t; String str; boolean cond=true; Scanner input=new Scanner(System.in); t=input.nextInt(); for(int i=0;i<t;i++) { str=input.next(); char[] color=str.toCharArray(); int signal=0; for(int m=0;m<color.length;m++) //loop across all color (door & keys) { int doorc=(int)color[m]; if((int)color[0]>=65&&(int)color[0]<=90) { cond=false; break; } else if((doorc>=65)&&(doorc<=90)) //checking of matching door key colors { int counter=0; while(counter<=m) { int rep=(int)color[counter]; if((rep-doorc)==32) { cond=true; signal++; } else { cond=false; } counter++; } } if(signal==3) { signal=0; cond=true; } } if(!cond) { System.out.println("NO"); cond=true; } else { System.out.println("YES"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
564734ea620655fb1cd441df322c702b
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { // Write your code here Scanner scn = new Scanner(System.in); int T = scn.nextInt(); for(int j = 0;j<T ; j++){ String s = scn.next(); int[]arr = new int[2]; int[]arr1 = new int[2]; int[]arr2 = new int[2]; for(int i=0 ; i<s.length();i++ ){ if(s.charAt(i)== 'r'){ arr[0] = i; } if(s.charAt(i)== 'R'){ arr[1] = i; } if(s.charAt(i)== 'g'){ arr1[0] = i; } if(s.charAt(i)== 'G'){ arr1[1] = i; } if(s.charAt(i)== 'b'){ arr2[0] = i; } if(s.charAt(i)== 'B'){ arr2[1] = i; } } int count = 0; if(arr[0] < arr[1]){ count ++; } if(arr1[0] < arr1[1]){ count++; } if(arr2[0] < arr2[1]){ count ++ ; } if(count == 3){ System.out.println("YES"); }else{ System.out.println("NO"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
5d86306166c3255de9bd9cc25fd7849e
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class q1 { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++){ String s = sc.next(); if (s.indexOf('r') < s.indexOf('R')){ if (s.indexOf('g') < s.indexOf('G')){ if (s.indexOf('b') < s.indexOf('B')){ System.out.println("YES"); }else{ System.out.println("NO"); } }else{ System.out.println("NO"); } }else{ System.out.println("NO"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
0fc6f10ee396fbaf378c95ccd22af37d
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
// The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it. // In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before. // Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color. // The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: // R, G, B — denoting red, green and blue doors, respectively; // r, g, b — denoting red, green and blue keys, respectively. // Each of these six characters appears in the string exactly once. // The knight is standing at the beginning of the hallway — on the left on the map. // Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway. import java.io.*; import java.util.*; public class codeforces { public static void main(String[] args) { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t != 0){ String x = scn.next(); int r = x.indexOf("r"); int g = x.indexOf("g"); int b = x.indexOf("b"); int R = x.indexOf("R"); int G = x.indexOf("G"); int B = x.indexOf("B"); if(r < R && g < G && b < B){ System.out.println("YES"); } else{ System.out.println("NO"); } t--; } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
88a5eaf004c6ccee7d35383ce750fd7b
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class s{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); // HashMap<Character,Integer> map=new HashMap<Character,Integer>();//Creating HashMap int rind=0; int Rind=0; int bind=0; int Bind=0; int gind=0; int Gind=0; int t=sc.nextInt(); for(int i=0;i<t;i++){ String b=sc.next(); char[] s = b.toCharArray(); for(int j=0;j<s.length;j++){ if(s[j]=='R') Rind=j; else if(s[j]=='r') rind=j; else if(s[j]=='B') Bind=j; else if(s[j]=='b') bind=j; else if(s[j]=='G') Gind=j; else if(s[j]=='g') gind=j; } if(rind<Rind&&gind<Gind&&bind<Bind) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
7731040bd732cb5ee170635a4ad6314b
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class force { public static void main(String [] args) { Scanner kb = new Scanner(System.in); int rounds = kb.nextInt(); for(int k = 1; k <= rounds; k++) { String map = kb.next(); boolean red = false; boolean green = false; boolean blue = false; boolean doorR = false; boolean doorB = false; boolean doorG = false; for(int i = 0; i<6;i++) { char c = map.charAt(i); if(c == 'r') { red = true; } if(c == 'b') { blue = true; } if(c == 'g') { green = true; } if(c == 'R' && red) { doorR = true; } if(c == 'G'&& green) { doorG = true; } if( c== 'B' && blue) { doorB = true; } } if(doorB && doorG && doorR) { System.out.println("YES"); } else System.out.println("NO"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
a4c767f0d22bae1d2eb120536fecb40a
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); while (tc-- > 0) { String s = sc.next(); int r = 0, g = 0, b = 0, R = 0, G = 0, B = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'G') { G = i; } else if (s.charAt(i) == 'B') { B = i; } else if (s.charAt(i) == 'R') { R = i; } else if (s.charAt(i) == 'r') { r = i; } else if (s.charAt(i) == 'g') { g = i; } else if (s.charAt(i) == 'b') { b = i; } } if (R > r && G > g && B > b) { System.out.println("Yes"); } else { System.out.println("No"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
b4ac5f4c0bc7d81f18120c7d8789d69d
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class go { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int x= sc.nextInt(); go: while (x-- !=0 ) { String s= sc.next(); boolean r=false,b=false,g=false,no=false; for(int i=0; i<6; i++){ switch (s.charAt(i)){ case ('r'):{ r=true; break;} case ('g'): {g=true; break;} case ('b'): {b=true; break;} case ('R'): {if(!r) {no=true;} break;} case ('G'): {if(!g) {no=true;} break;} case ('B'): {if(!b) {no=true;} break ;} } if(no){break;} } System.out.println(no? "NO":"YES"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
db7df211d89b46f3cbdd36f040c4bd56
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class door { public static void main(String[]args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); sc.nextLine(); while(t-->0){ String s=sc.nextLine(); if(s.charAt(0)=='R' || s.charAt(0)=='G' || s.charAt(0)=='B'){ System.out.println("NO"); continue; } int flag=0; HashSet <Character> hs=new HashSet<>(); for(int i=0;i<s.length();i++){ if(s.charAt(i)=='r' || s.charAt(i)=='g'||s.charAt(i)=='b'){ hs.add(s.charAt(i)); } else{ if(!hs.contains((char)((int)(s.charAt(i))+32))){ System.out.println("NO"); flag=-1; break; } } } if(flag==0){ System.out.println("YES"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
349f3698947e57e62e6bbe26fe8a8d8b
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class A_Doors_and_Keys { public static void main(String[] args) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); // int N = Integer.parseInt(st.nextToken()); // int M = Integer.parseInt(st.nextToken()); // /* int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); FastScanner fs = new FastScanner(); while(T-->0) { // st = new StringTokenizer(infile.readLine()); String s = infile.readLine(); Map<Character,Integer> map = new HashMap<Character,Integer>(); int i = 0; for(char c:s.toCharArray()){ map.put(c,i++); } // System.out.println(s); // System.out.println(map); if(map.get('r')<map.get('R') && map.get('b')<map.get('B') && map.get('g')<map.get('G')){ System.out.println("YES"); } else{ System.out.println("NO"); } } // */ // BufferedReader infile = new BufferedReader(new FileReader()); // System.setOut(new PrintStream(new File())); } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } public static void print(int[] arr) { //for debugging only for(int x: arr) out.print(x+' '); out.println(); } public static long[] readArr2(int N, BufferedReader infile, StringTokenizer st) throws Exception { long[] arr = new long[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Long.parseLong(st.nextToken()); return arr; } public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static long gcd(long a, long b) { if(a > b) a = (a+b)-(b=a); if(a == 0L) return b; return gcd(b%a, a); } public static ArrayList<Integer> findDiv(int N) { //gens all divisors of N ArrayList<Integer> ls1 = new ArrayList<Integer>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++) if(N%i == 0) { ls1.add(i); ls2.add(N/i); } Collections.reverse(ls2); for(int b: ls2) if(b != ls1.get(ls1.size()-1)) ls1.add(b); return ls1; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } //custom multiset (replace with HashMap if needed) public static void push(TreeMap<Integer, Integer> map, int k, int v) { //map[k] += v; if(!map.containsKey(k)) map.put(k, v); else map.put(k, map.get(k)+v); } public static void pull(TreeMap<Integer, Integer> map, int k, int v) { //assumes map[k] >= v //map[k] -= v int lol = map.get(k); if(lol == v) map.remove(k); else map.put(k, lol-v); } public static int[] compress(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int boof = 1; //min value for(int x: ls) if(!map.containsKey(x)) map.put(x, boof++); int[] brr = new int[arr.length]; for(int i=0; i < arr.length; i++) brr[i] = map.get(arr[i]); return brr; } public static long[][] multiply(long[][] left, long[][] right) { long MOD = 1000000007L; int N = left.length; int M = right[0].length; long[][] res = new long[N][M]; for(int a=0; a < N; a++) for(int b=0; b < M; b++) for(int c=0; c < left[0].length; c++) { res[a][b] += (left[a][c]*right[c][b])%MOD; if(res[a][b] >= MOD) res[a][b] -= MOD; } return res; } public static long[][] power(long[][] grid, long pow) { long[][] res = new long[grid.length][grid[0].length]; for(int i=0; i < res.length; i++) res[i][i] = 1L; long[][] curr = grid.clone(); while(pow > 0) { if((pow&1L) == 1L) res = multiply(curr, res); pow >>= 1; curr = multiply(curr, curr); } return res; } } class DSU { public int[] dsu; public int[] size; public DSU(int N) { dsu = new int[N+1]; size = new int[N+1]; for(int i=0; i <= N; i++) { dsu[i] = i; size[i] = 1; } } //with path compression, no find by rank public int find(int x) { return dsu[x] == x ? x : (dsu[x] = find(dsu[x])); } public void merge(int x, int y) { int fx = find(x); int fy = find(y); dsu[fx] = fy; } public void merge(int x, int y, boolean sized) { int fx = find(x); int fy = find(y); size[fy] += size[fx]; dsu[fx] = fy; } } class LCA { public int N, root; public ArrayDeque<Integer>[] edges; private int[] enter; private int[] exit; private int LOG = 17; //change this private int[][] dp; public LCA(int n, ArrayDeque<Integer>[] edges, int r) { N = n; root = r; enter = new int[N+1]; exit = new int[N+1]; dp = new int[N+1][LOG]; this.edges = edges; int[] time = new int[1]; //change to iterative dfs if N is large dfs(root, 0, time); dp[root][0] = 1; for(int b=1; b < LOG; b++) for(int v=1; v <= N; v++) dp[v][b] = dp[dp[v][b-1]][b-1]; } private void dfs(int curr, int par, int[] time) { dp[curr][0] = par; enter[curr] = ++time[0]; for(int next: edges[curr]) if(next != par) dfs(next, curr, time); exit[curr] = ++time[0]; } public int lca(int x, int y) { if(isAnc(x, y)) return x; if(isAnc(y, x)) return y; int curr = x; for(int b=LOG-1; b >= 0; b--) { int temp = dp[curr][b]; if(!isAnc(temp, y)) curr = temp; } return dp[curr][0]; } private boolean isAnc(int anc, int curr) { return enter[anc] <= enter[curr] && exit[anc] >= exit[curr]; } } class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
77fee1b837f9530cd710460bd3bb6cc4
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class Doors_key { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { String s = sc.next(); Map<Character,Integer> map = new HashMap<>(); int i = 0; for(char c:s.toCharArray()) { map.put(c, i++); } if(map.get('r')<map.get('R') && map.get('g')<map.get('G') && map.get('b')<map.get('B')) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
d4253a37d7d98edf4242f1204c33cbbf
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class ExpandThePath { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String[] s=new String[n]; sc.nextLine(); for(int i=0;i<n;i++) { s[i]=sc.nextLine(); // sc.nextLine(); } for(int i=0;i<n;i++) { if (s[i].indexOf('r') < s[i].indexOf('R') && s[i].indexOf('g') < s[i].indexOf('G') && s[i].indexOf('b') < s[i].indexOf('B')) System.out.println("Yes"); else System.out.println("NO"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
f4ae9e330266375485df65f48e32128a
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class Main{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt();int r=0,g=0,b=0,r1=0,g1=0,b1=0; while(t--!=0) { String s=sc.next(); for(int i=0;i<s.length();i++) { if(s.charAt(i)=='r') r=i; else if(s.charAt(i)=='g') g=i; else if(s.charAt(i)=='b') b=i; else if(s.charAt(i)=='R') r1=i; else if(s.charAt(i)=='G') g1=i; else b1=i; } if(r<r1 && g<g1 && b<b1) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
b8907687bdf2b8668bd61fcfa2d8de8a
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); scanner.nextLine(); for (int i = 1; i <=t;i++) { boolean isPossible = false; String str = scanner.nextLine(); Map<Character, Boolean> map = new HashMap<>(); map.put('R', false); map.put('G', false); map.put('B', false); forBreak: for (int j = 0; j < str.length(); j++) { char currChar = str.charAt(j); switch (currChar) { case ('r'): map.replace('R', true); break; case ('g'): map.replace('G', true); break; case ('b'): map.replace('B', true); break; case ('R'): if (!map.get('R')) { System.out.println("NO"); isPossible = false; break forBreak; } else isPossible = true; break; case ('G'): if (!map.get('G')) { System.out.println("NO"); isPossible = false; break forBreak; } else isPossible = true; break; default: if (!map.get('B')) { System.out.println("NO"); isPossible = false; break forBreak; } else isPossible = true; break; } } if (isPossible) System.out.println("YES"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
668436ab82cbe1f2890ff1e75087a27b
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class test { public static void main (String[] args) throws java.lang.Exception { Scanner scanner = new Scanner(System.in); String num = scanner.nextLine(); Integer n = Integer.valueOf(num); List<Character> keys = Arrays.asList('r','g','b'); for (int j = 0; j < n;j ++) { String line = scanner.nextLine(); Map<Character, Boolean> map = new HashMap<>(); boolean flag = true; for (int i = 0; i < line.length(); i++) { if (keys.contains(line.charAt(i))) { map.put(String.valueOf(line.charAt(i)).toUpperCase().charAt(0), true); } else { if (!map.containsKey(line.charAt(i))) { flag = false; break; } } } if (flag) { System.out.print("YES\n"); } else { System.out.print("NO\n"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
87955ac48724c48ae13320739ffd58ec
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner scn=new Scanner(System.in); int t=scn.nextInt(); scn.nextLine(); while(t-->0){ String s=scn.nextLine(); int flag=0; HashMap<Character,Integer> hm=new HashMap<>(); for(int i=0;i<s.length();i++){ if(s.charAt(i)=='r'||s.charAt(i)=='g'||s.charAt(i)=='b'){ hm.put(s.charAt(i),1); } else if(s.charAt(i)=='R' && hm.containsKey('r')){ hm.remove('r'); } else if(s.charAt(i)=='G' && hm.containsKey('g')){ hm.remove('g'); } else if(s.charAt(i)=='B' && hm.containsKey('b')){ hm.remove('b'); } else { flag=1; System.out.println("No"); break; } } if(flag==0 && hm.size()==0){ System.out.println("Yes"); } else if(flag==0){ System.out.println("No"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
bff8c29d02c2091d2649404b5cf34ccd
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); outer: while(t-->0) { String str=sc.next(); int r=0,g=0,b=0; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='r') r++; else if(str.charAt(i)=='g') g++; else if(str.charAt(i)=='b') b++; else if(str.charAt(i)=='R'&&r==0) {System.out.println("NO"); continue outer; } else if(str.charAt(i)=='G'&&g==0) { System.out.println("NO"); continue outer; } else if(str.charAt(i)=='B'&&b==0) { System.out.println("NO"); continue outer; } } System.out.println("YES"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
2a77ec93da21f7d692ff8946191ebde1
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class DoorsAndKeys { public static void main(final String[] args) { final Scanner scanner = new Scanner(System.in); int noTests = scanner.nextInt(); while (noTests-- > 0) { final String map = scanner.next(); boolean hasRedKey = false; boolean hasBlueKey = false; boolean hasGreenKey = false; boolean canEscape = true; for (int i = 0; i < map.length(); i++) { final char c = map.charAt(i); if (c == 'r') { hasRedKey = true; } if (c == 'g') { hasGreenKey = true; } if (c == 'b') { hasBlueKey = true; } if (c == 'R') { if (!hasRedKey) { canEscape = false; break; } } if (c == 'G') { if (!hasGreenKey) { canEscape = false; break; } } if (c == 'B') { if (!hasBlueKey) { canEscape = false; break; } } } if (canEscape) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
5d5ad6dfd2273b3e661087f19a502c8e
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class class371 { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int flag=0; String s=sc.next(); int l=s.length(); int i; int a[]=new int[3]; for(i=0;i<l;i++) { if(s.charAt(i)=='b') { a[0]=1; continue; } if(s.charAt(i)=='g') { a[1]=1; continue; } if(s.charAt(i)=='r') { a[2]=1; continue; } if(s.charAt(i)=='B') { if(a[0]==0) { flag=1; break; } } else if(s.charAt(i)=='G') { if(a[1]==0) { flag=1; break; } } else { if(s.charAt(i)=='R') { if(a[2]==0) { flag=1; break; } } } } if(flag==1) { System.out.println("NO"); } else { System.out.println("YES"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
924ef1a825eb0d571aeeb9192579c887
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { String str = sc.next(); ArrayList<Character> lst = new ArrayList<>(); boolean flag = true; for(int i=0;i<str.length();i++) { char ch =str.charAt(i); if(Character.isLowerCase(ch)==true) lst.add(ch); else if(Character.isUpperCase(ch)==true && !lst.contains(Character.toLowerCase(ch))) { flag = false; break; } } // HashMap<Character, Boolean> doormap = new HashMap<>(); // HashMap<Character, Boolean> keymap = new HashMap<>(); // doormap.put('R', false); // doormap.put('G', false); // doormap.put('B', false); // keymap.put('r',false); // keymap.put('g',false); // keymap.put('b',false); // boolean flag = true; // for(int i=0;i<str.length();i++) // { // char ch = str.charAt(i); // if(ch == 'r' || ch == 'g' || ch == 'b') // { // keymap.put(ch,true); // } // else { // if(ch == 'R' && keymap.get('r') == false) // { // flag = false; // break; // } // if(ch == 'G' && keymap.get('g') == false) // { // flag = false; // break; // } // if(ch == 'B' && keymap.get('b') == false) // { // flag = false; // break; // } // } // } if(flag == true) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
4253911c6d688625c54f3a905a77905c
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Arrays; import java.util.Objects; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int dataCount = Integer.parseInt(sc.nextLine()); String[] answerArray = new String[dataCount]; for (int i = 0; i < dataCount; i++){ String[] data = sc.nextLine().split(""); boolean redKey = false; boolean blueKey = false; boolean greenKey = false; boolean answer = true; for (int j = 0; j < data.length; j++){ if (Objects.equals(data[j], "r")) redKey = true; if (Objects.equals(data[j], "b")) blueKey = true; if (Objects.equals(data[j], "g")) greenKey = true; if (Objects.equals(data[j], "R") && !redKey) { answer = false; break; } if (Objects.equals(data[j], "G") && !greenKey) { answer = false; break; } if (Objects.equals(data[j], "B") && !blueKey) { answer = false; break; } } if (answer) answerArray[i] = "YES"; else answerArray[i] = "NO"; } for (String s : answerArray) { System.out.println(s); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
0bafbb1b5ef4ae5cb195279d8bc41bea
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; /* The pain you feel today will be the strength you feel tomorrow */ public class Bb { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int z = 0; z < t; z++) { String s = in.next(); boolean f = false, g = false, h = false; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'B') { if (check(s, i, s.charAt(i))) { f = true; } } else if (s.charAt(i) == 'R') { if (check(s, i, s.charAt(i))) { g = true; } } else if (s.charAt(i) == 'G') { if (check(s, i, s.charAt(i))) { h = true; } } } if (f & g & h) { System.out.println("YES"); } else { System.out.println("NO"); } } } private static boolean check(String s, int i, char a) { for (int j = 0; j < i; j++) { if (s.charAt(j)-32 == a) { return true; } } return false; } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
6d7e647e2a2f4da99dbf9805089b85dc
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; import java.io.*; public class DoorsAndKeys { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int tc = atoi(reader.readLine()); for (int t = 0; t < tc; t++) { String line = reader.readLine(); boolean hasRed = false; boolean hasGreen = false; boolean hasBlue = false; boolean pass = true; for (int i = 0; i < line.length() && pass; i++) { if (line.charAt(i) == 'r') hasRed = true; else if (line.charAt(i) == 'g') hasGreen = true; else if (line.charAt(i) == 'b') hasBlue = true; else if (line.charAt(i) == 'R' && !hasRed) pass = false; else if (line.charAt(i) == 'G' && !hasGreen) pass = false; else if (line.charAt(i) == 'B' && !hasBlue) pass = false; } if (pass) System.out.println("YES"); else System.out.println("NO"); } } private static int atoi(String s) { return Integer.parseInt(s); } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
d84ff6340815d1467a57c44b0357871c
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.IOException; import java.util.HashMap; import java.util.Scanner; public class App { public static void main(String args[]) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); String garb = sc.nextLine(); label: for (int i = 1; i <= t; i++) { String path = sc.nextLine(); HashMap<Character, Integer> hashMap = new HashMap<>(); for (char element : path.toCharArray()) { switch (element) { case 'r': hashMap.put('r', 1); break; case 'g': hashMap.put('g', 1); break; case 'b': hashMap.put('b', 1); break; case 'R': if (!hashMap.containsKey('r')) { System.out.println("NO"); continue label; } break; case 'B': if (!hashMap.containsKey('b')) { System.out.println("NO"); continue label; } break; case 'G': if (!hashMap.containsKey('g')) { System.out.println("NO"); continue label; } break; default: System.out.println("error"); } } System.out.println("YES"); } sc.close(); } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
b027d858b6c1389376dcf51e431b0d52
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class DoorsAndKeys { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { String s = sc.next(); int r1 = s.indexOf('r'); int R1 = s.indexOf('R'); int g1 = s.indexOf('g'); int G1 = s.indexOf('G'); int b1 = s.indexOf('b'); int B1 = s.indexOf('B'); if (r1 < R1 && g1 < G1 && b1 < B1){ System.out.println("YES"); }else { System.out.println("NO"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
57b2e66be770822591b3e9ed24928073
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class DoorsAndKeys { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { String s = sc.next(); if (s.indexOf('r') < s.indexOf('R') && s.indexOf('g') < s.indexOf('G') && s.indexOf('b') < s.indexOf('B')){ System.out.println("YES"); }else { System.out.println("NO"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
e0a8fa34422462667985093848720840
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import jdk.swing.interop.SwingInterOpUtils; import org.w3c.dom.ls.LSOutput; import javax.crypto.spec.PSource; import javax.imageio.metadata.IIOMetadataFormatImpl; import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; import java.util.Arrays; import java.util.Locale; import java.util.Scanner; import java.util.StringTokenizer; public class hd { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x = scan.nextInt(); for (int i = 0; i < x; i++) { String g = scan.next(); if(g.indexOf('r')<g.indexOf('R') && g.indexOf('g')<g.indexOf('G') && g.indexOf('b')<g.indexOf('B')) System.out.println("YES"); else System.out.println("NO"); } } } /*BigInteger y = new BigInteger("0"); BigInteger u = new BigInteger("0"); for (BigInteger i = new BigInteger("0"); x.compareTo(i)>=0 ; i=i.add(BigInteger.TWO)) { y=y.add(i); } for (BigInteger i = new BigInteger("1"); x.compareTo(i)>=0 ; i=i.add(BigInteger.TWO)) { u=u.subtract(i); } System.out.println(u.add(y));*/
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
1447af4279d7b0bb666bc059c1278410
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import jdk.swing.interop.SwingInterOpUtils; import org.w3c.dom.ls.LSOutput; import javax.crypto.spec.PSource; import javax.imageio.metadata.IIOMetadataFormatImpl; import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; import java.util.Arrays; import java.util.Locale; import java.util.Scanner; import java.util.StringTokenizer; public class hd { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x = scan.nextInt(); for (int i = 0; i < x; i++) { String g = scan.next(); int count = 0; int count1 = 0; int count2 = 0; int count3 = 0; int count4 = 0; int count5 = 0; for (int j = 0; j < g.length(); j++) { char ch = g.charAt(j); if(ch=='r') count=j; else if(ch=='R') count1 = j; if(ch=='g') count2=j; else if(ch=='G') count3 = j; if(ch=='b') count4=j; else if(ch=='B') count5 = j; } if(count1-count>0 && count3-count2>0 && count5-count4>0) System.out.println("YES"); else System.out.println("NO"); } } } /*BigInteger y = new BigInteger("0"); BigInteger u = new BigInteger("0"); for (BigInteger i = new BigInteger("0"); x.compareTo(i)>=0 ; i=i.add(BigInteger.TWO)) { y=y.add(i); } for (BigInteger i = new BigInteger("1"); x.compareTo(i)>=0 ; i=i.add(BigInteger.TWO)) { u=u.subtract(i); } System.out.println(u.add(y));*/
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
0f81322f8b40b7e4df0cbc412f0c67f6
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.*; import java.util.*; public class Main { static class JoinSet { int[] fa; JoinSet(int n) { fa = new int[n]; for (int i = 0; i < n; i++) fa[i] = i; } int find(int t) { if (t != fa[t]) fa[t] = find(fa[t]); return fa[t]; } void join(int x, int y) { x = find(x); y = find(y); fa[x] = y; } } static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); static int mod = (int)1e9+7; static int[][] dir1 = new int[][]{{0,1},{0,-1},{1,0},{-1,0}}; static int[][] dir2 = new int[][]{{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}}; static boolean[] prime = new boolean[10]; static { for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i < prime.length; i++) { if (prime[i]) { for (int k = 2; i * k < prime.length; k++) { prime[i * k] = false; } } } } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return a * b / gcd(a, b); } static int get() throws Exception { String ss = bf.readLine(); if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" ")); return Integer.parseInt(ss); } static long getx() throws Exception { String ss = bf.readLine(); if (ss.contains(" ")) ss = ss.substring(0, ss.indexOf(" ")); return Long.parseLong(ss); } static int[] getint() throws Exception { String[] s = bf.readLine().split(" "); int[] a = new int[s.length]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(s[i]); } return a; } static long[] getlong() throws Exception { String[] s = bf.readLine().split(" "); long[] a = new long[s.length]; for (int i = 0; i < a.length; i++) { a[i] = Long.parseLong(s[i]); } return a; } static String getstr() throws Exception { return bf.readLine(); } static void println() throws Exception { bw.write("\n"); } static void print(int a) throws Exception { bw.write(a + "\n"); } static void print(long a) throws Exception { bw.write(a + "\n"); } static void print(String a) throws Exception { bw.write(a + "\n"); } static void print(int[] a) throws Exception { for (int i : a) { bw.write(i + " "); } println(); } static void print(long[] a) throws Exception { for (long i : a) { bw.write(i + " "); } println(); } static void print(int[][] a) throws Exception { for (int i[] : a) print(i); } static void print(long[][] a) throws Exception { for (long[] i : a) print(i); } static void print(char[] a) throws Exception { for (char i : a) { bw.write(i +""); } println(); } static long pow(long a, long b) { long ans = 1; while (b > 0) { if ((b & 1) == 1) { ans *= a; } a *= a; b >>= 1; } return ans; } static int powmod(long a, long b, int mod) { long ans = 1; while (b > 0) { if ((b & 1) == 1) { ans = ans * a % mod; } a = a * a % mod; b >>= 1; } return (int) ans; } static void sort(int[] a) { int n = a.length; Integer[] b = new Integer[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[i]; } static void sort(long[] a) { int n = a.length; Long[] b = new Long[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Arrays.sort(b); for (int i = 0; i < n; i++) a[i] = b[i]; } static int max(int a, int b) { return Math.max(a,b); } static int min(int a, int b) { return Math.min(a,b); } static long max(long a, long b) { return Math.max(a,b); } static long min(long a, long b) { return Math.min(a,b); } static int max(int[] a) { int max = a[0]; for(int i : a) max = max(max,i); return max; } static int min(int[] a) { int min = a[0]; for(int i : a) min = min(min,i); return min; } static long max(long[] a) { long max = a[0]; for(long i : a) max = max(max,i); return max; } static long min(long[] a) { long min = a[0]; for(long i : a) min = min(min,i); return min; } static int abs(int a) { return Math.abs(a); } static long abs(long a) { return Math.abs(a); } public static void main(String[] args) throws Exception { int t = get(); while (t-- > 0){ String s = getstr(); Set<Character> set = new HashSet<>(); boolean ok = true; for(char c : s.toCharArray()){ if(c-'a' < 0){ if(!set.contains((char)(c+32))) { ok = false; } }else set.add(c); } if(ok) print("YES"); else print("NO"); } bw.flush(); } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
366f09a60d873871b7ff17a825f8578a
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class cf1644A { public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); while(t-->0) { String s = sc.nextLine(); if(s.indexOf("r") < s.indexOf("R") && s.indexOf("g") < s.indexOf("G") && s.indexOf("b") < s.indexOf("B")) { System.out.println("YES"); } else System.out.println("NO"); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
ab16124f5db88e512ef3060e4c354e7c
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class cf1644A { public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); while(t-->0) { String s = sc.nextLine(); Set<Character> keys = new HashSet<>(); boolean sol = true; for(char c : s.toCharArray()) { if(Character.isLowerCase(c)) keys.add(c); else if(!keys.contains(Character.toLowerCase(c))) { sol = false; break; } } System.out.println(sol ? "YES" : "NO"); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
44f928bc3281a8205e0d4b36ef5f6d00
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class DoorsAndKeys { public static void main(String[] args) { Scanner s = new Scanner(System.in); int cases = Integer.parseInt(s.nextLine()); for (int i = 0; i < cases; i++) { String str = s.nextLine(); System.out.println(canPass(str) ? "YES" : "NO"); } s.close(); } static boolean canPass(String input) { boolean[] inventory = new boolean[3]; for (int i = 0; i < input.length(); i++) { char item = input.charAt(i); if (item == 'r') inventory[0] = true; else if (item == 'g') inventory[1] = true; else if (item == 'b') inventory[2] = true; else if (item == 'R' && !inventory[0]) return false; else if (item == 'G' && !inventory[1]) return false; else if (item == 'B' && !inventory[2]) return false; } return true; } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
13a7e6dfe463b688b3fe4e046f59b921
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.*; import java.util.*; public class Contest1644C { static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { // reads in the next string while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { // reads in the next int return Integer.parseInt(next()); } public long nextLong() { // reads in the next long return Long.parseLong(next()); } public double nextDouble() { // reads in the next double return Double.parseDouble(next()); } } static InputReader r = new InputReader(System.in); static PrintWriter pw = new PrintWriter(System.out); static long mod = 1000000007; public static void main(String[] args) { int t = r.nextInt(); while (t > 0) { t--; String s = r.next(); int posR = 0; int posG = 0; int posB = 0; int posr = 0; int posg = 0; int posb = 0; for (int i = 0; i < 6; i ++) { char c = s.charAt(i); if (c == 'R') { posR = i; } else if (c == 'G') { posG = i; } else if (c == 'B') { posB = i; } else if (c == 'r') { posr = i; } else if (c == 'b') { posb = i; } else { posg = i; } } if (posb > posB || posg > posG || posr > posR) { pw.println("NO"); } else { pw.println("YES"); } } pw.close(); } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
6f107c76274ae7bc3aa662b1f043b588
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t>0){ String s=sc.next(); int rkey=0,gkey=0,bkey=0,rdr=0,gdr=0,bdr=0; for(int i=0;i<s.length();i++){ if(s.charAt(i)=='r'){ rkey=i; } else if(s.charAt(i)=='g'){ gkey=i; } else if(s.charAt(i)=='b'){ bkey=i; } else if(s.charAt(i)=='R'){ rdr=i; } else if(s.charAt(i)=='G'){ gdr=i; } else{ bdr=i; } } if((rkey<rdr)&&(gkey<gdr)&&(bkey<bdr)){ System.out.println("YES"); } else{ System.out.println("NO"); } t--; } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
2fd57980f96c36697ffa72f9704085a1
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; import java.util.List; import java.util.ArrayList; import java.util.Arrays; public class CF1644A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int n = sc.nextInt(); sc.nextLine(); for (int i = 0; i < n; i ++ ){ String valid = "YES"; List<Character> keys = new ArrayList<>(); char[] input = sc.nextLine().toCharArray(); for (char c : input) { if (Character.isUpperCase(c) && !keys.contains(Character.toLowerCase(c))) { valid = "NO"; break; } else { keys.add(c); } } sb.append(valid + "\n"); } System.out.println(sb.toString()); } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
cd54aa37f137bfa857b93357654b62b1
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); StringBuilder sb = new StringBuilder(""); while(t-- > 0) { String s = scn.next(); Set<Character> hs = new HashSet<>(); boolean flag = true; for(int i=0 ; i<s.length() ; i++) { int ch = (int)s.charAt(i); if(ch > 97) { hs.add((char)ch); }else { if(!hs.contains((char)(ch + 32))) { flag = false; break; } } } if(flag) sb.append("YES" + "\n"); else sb.append("NO" + "\n"); } System.out.println(sb); } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
c9256b51c56981285c4274f548392d7a
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; // Compiler version JDK 11.0.2 public class Dcoder { public static void main(String args[]) { Scanner s=new Scanner(System.in); int t=s.nextInt(); s.nextLine(); while(t-->0) { String str=s.nextLine(); Map<Character, Integer> l=new HashMap<Character, Integer>(); for(int i=0;i<6;i++){ l.put(str.charAt(i),i); } if((l.get('r')<l.get('R'))&&(l.get('g')<l.get('G')) && (l.get('b')<l.get('B'))) { System.out.println("YES"); } else System.out.println("NO"); } s.close(); } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
9973992eaef99cd071b7cfc7ea98bd66
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class P1644A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int bin=0; for(int i=0;i<t;i++){ String s = sc.next(); int a=0,b=0,c=0; for(int j=0;j<s.length();j++){ int pos=0; if(s.charAt(j)=='r') { pos = j; for(int k=j+1;k<s.length();k++){ if(s.charAt(k)=='R' && pos<k) a+=1; } } else if(s.charAt(j)=='b') { pos = j; for(int k=j+1;k<s.length();k++){ if(s.charAt(k)=='B' && pos<k) b+=1; } } else if(s.charAt(j)=='g') { pos = j; for(int k=j+1;k<s.length();k++){ if(s.charAt(k)=='G' && pos<k) c+=1; } } } if(a==1 && b==1 && c==1) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
f306998e2efecd59065953cefb34e43d
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i = 0 ;i <t;i++){ boolean ans = true; boolean r = false; boolean g= false; boolean b = false; String s = sc.next(); for(int j = 0; j < s.length();j++){ if(s.charAt(j) == 'r') r = true; if(s.charAt(j) == 'g') g = true; if(s.charAt(j) == 'b') b = true; if(s.charAt(j) == 'R' && !r)ans = false; if(s.charAt(j) == 'G' && !g)ans = false; if(s.charAt(j) == 'B' && !b)ans = false; } if(ans) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
6d553b031f008ef6b74da39b345e51af
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.*; import java.util.*; public class Code_Forces { static final int INT_MAX = Integer.MAX_VALUE; static FastReader in = new FastReader(); static StringBuilder sb = new StringBuilder(); public static void main(String[] args) throws java.lang.Exception { int TestCases = in.nextInt(); while(TestCases-- > 0) { char[] map = in.next().toCharArray(); int[] freq = new int[1000]; int knight = 0; boolean flag = true; while (knight < 6){ if (map[knight] == 'r' || map[knight] == 'g' || map[knight] == 'b') freq[map[knight] - '0']++; if (map[knight] == 'R' || map[knight] == 'G' || map[knight] == 'B'){ if (freq[Character.toLowerCase(map[knight]) - '0'] != 1) { flag = false; } } knight++; } sb.append(flag ? "Yes" : "No").append("\n"); } System.out.println(sb); } private static void appendArray(int[] a) { int n = a.length; for (int i = 0; i < n; i++) { sb.append(a[i] + " "); } sb.append("\n"); } private static int accumulate(int[] nums){ int n = nums.length; int sum = 0; for (int i = 0; i < n; i++) { sum += nums[i]; } return sum; } private static long[] nextLongArray(int n){ long[] A = new long[n]; for (int i = 0; i < n; i++) { A[i] = in.nextLong(); } return A; } private static int[] nextIntArray(int n){ int[] A = new int[n]; for (int i = 0; i < n; i++) { A[i] = in.nextInt(); } return A; } private static int ceilDiv(int a, int b){ int ans = a%b == 0 ? a/b : a/b + 1; return ans; } private static void print(int[] a) { int n = a.length; for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } System.out.println(); } private static void swap(int indx, int i, int[] cur) { int temp = cur[indx]; cur[indx] = cur[i]; cur[i] = temp; } private static int[] charToint(String[] arr){ int[] nums = new int[arr.length]; int indx = 0; for (String it: arr){ nums[indx++] = Integer.parseInt(it); } return nums; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Pair { long first; long second; public Pair(long first, long second) { this.first = first; this.second = second; } } //13 }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
ebaaa7b672f8481eb602f3ae7f31258c
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Code_Forces { static final int INT_MAX = Integer.MAX_VALUE; public static void main(String[] args) throws java.lang.Exception { FastReader in = new FastReader(); int TestCases = in.nextInt(); while(TestCases-- > 0) { char[] map = in.next().toCharArray(); int[] freq = new int[1000]; int knight = 0; boolean flag = true; while (knight < 6){ if (map[knight] == 'r' || map[knight] == 'g' || map[knight] == 'b') freq[map[knight] - '0']++; if (map[knight] == 'R' || map[knight] == 'G' || map[knight] == 'B'){ if (freq[Character.toLowerCase(map[knight]) - '0'] != 1) { flag = false; } } knight++; } System.out.println(flag ? "YES" : "NO"); } } 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; } private static int[] charToint(String[] arr){ int[] nums = new int[arr.length]; int indx = 0; for (var it: arr){ nums[indx++] = Integer.parseInt(it); } return nums; } } static class Pair { long first; long second; public Pair(long first, long second) { this.first = first; this.second = second; } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
64b672153fd9cb53ea3499303bbab3a4
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class DoorsAndKeys { public static void main(String[] args) { Scanner s=new Scanner (System.in); int r=0,g=0,b=0; int ans=0; int t=s.nextInt(); int x=0; String y[]=new String [t]; while (x<t){ ans=0; r=0; b=0; g=0; String a=s.next(); for (int i=0;i<a.length();i++) { if (a.charAt(i)=='r') r++; if (a.charAt(i)=='g') g++; if (a.charAt(i)=='b') b++; //......... if (a.charAt(i)=='R'&&r==1) ans++; if (a.charAt(i)=='G'&&g==1) ans++; if (a.charAt(i)=='B'&&b==1) ans++; } //System.out.println("answer="+ans); if (ans==3) y[x]="YES"; else y[x]="NO"; x++; } for (int i=0;i<t;i++){ System.out.println(y[i]); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
57e146faf2136d9aa77b9691d40a24ea
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class Princess { public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { int n = sc.nextInt(); for(int i = 0 ; i < n ; i++) { String s = sc.next(); check(s); } } } public static void check(String s) { char[] arr= {'0','0','0'}; for(int i = 0 ; i < s.length() ; i++) { if(s.charAt(i) == 'r') arr[0] = s.charAt(i); else if(s.charAt(i)=='g') arr[1] = s.charAt(i); else if(s.charAt(i)=='b') arr[2]=s.charAt(i); else if(s.charAt(i)=='R') if(arr[0]=='r'){ } else { System.out.println("NO"); return; } else if(s.charAt(i)=='G') if(arr[1]=='g') { } else { System.out.println("NO"); return; } else if(s.charAt(i)=='B') if(arr[2]=='b') { } else { System.out.println("NO"); return; } } System.out.println("YES"); } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
c3b8e325a2b8f7bec26fe7ab5bd678a4
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
/***** ---> :) Vijender Srivastava (: <--- *****/ import java.util.*; import java.lang.*; import java.lang.reflect.Array; import java.io.*; public class Main { static FastReader sc =new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=998244353; /* start */ public static void main(String [] args) { // int testcases = 1; int testcases = i(); while(testcases-->0) { solve(); } out.flush(); out.close(); } static void solve() { char c[] = inputC(); int r=0,g=0,b=0; for(int i=0;i<c.length;i++) { if(c[i]=='r')r=1; if(c[i]=='g')g=1; if(c[i]=='b')b=1; if(c[i]=='B') { if(b!=1){ pl("NO"); return ; } } if(c[i]=='R') { if(r!=1){ pl("NO"); return ; } } if(c[i]=='G') { if(g!=1){ pl("NO"); return ; } } } pl("YES"); } /* end */ 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 void p(Object o) { out.print(o); } static void pl(Object o) { out.println(o); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static long[] putL(long a[]) { long A[]=new long[a.length]; for(int i=0;i<a.length;i++) { A[i]=a[i]; } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x) ; y = y >> 1; x = (x * x); } return res; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long[] sort(long a[]) { ArrayList<Long> arr = new ArrayList<>(); for(long i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } static int[] sort(int a[]) { ArrayList<Integer> arr = new ArrayList<>(); for(Integer i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } //pair class private static class Pair implements Comparable<Pair> { int first, second; public Pair(int f, int s) { first = f; second = s; } @Override public int compareTo(Pair p) { if (first > p.first) return 1; else if (first < p.first) return -1; else { if (second < p.second) return 1; else if (second > p.second) return -1; else return 0; } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
ebe83c87283474d501247c868a85df15
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
/***** ---> :) Vijender Srivastava (: <--- *****/ import java.util.*; import java.lang.*; import java.lang.reflect.Array; import java.io.*; public class Main { static FastReader sc =new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=998244353; /* start */ public static void main(String [] args) { // int testcases = 1; int testcases = i(); while(testcases-->0) { solve(); } out.flush(); out.close(); } static void solve() { char c[] = inputC(); Map<Character,Integer> map = new HashMap<>(); for(int i=0;i<c.length;i++) { if(c[i]=='r' || c[i]=='g' || c[i]=='b'){ if(map.containsKey(c[i]))map.put(c[i],map.get(c[i])+1); else map.put(c[i],1); } if(c[i]=='B') { if(map.containsKey('b') && map.get('b')>0)map.put('b',map.get('b')-1); else { pl("NO"); return ; } } if(c[i]=='R') { if(map.containsKey('r') && map.get('r')>0)map.put('r',map.get('r')-1); else { pl("NO"); return ; } } if(c[i]=='G') { if(map.containsKey('g') && map.get('g')>0)map.put('g',map.get('g')-1); else { pl("NO"); return ; } } } pl("YES"); } /* end */ 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 void p(Object o) { out.print(o); } static void pl(Object o) { out.println(o); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static long[] putL(long a[]) { long A[]=new long[a.length]; for(int i=0;i<a.length;i++) { A[i]=a[i]; } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x) ; y = y >> 1; x = (x * x); } return res; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long[] sort(long a[]) { ArrayList<Long> arr = new ArrayList<>(); for(long i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } static int[] sort(int a[]) { ArrayList<Integer> arr = new ArrayList<>(); for(Integer i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } //pair class private static class Pair implements Comparable<Pair> { int first, second; public Pair(int f, int s) { first = f; second = s; } @Override public int compareTo(Pair p) { if (first > p.first) return 1; else if (first < p.first) return -1; else { if (second < p.second) return 1; else if (second > p.second) return -1; else return 0; } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
a2664e577cb8a3d9a2777464d2176002
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; import java.lang.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); while(t>0){ t--; char [] k = new char[3]; int key=0; char [] d = new char[3]; int door=0; String str = sc.nextLine(); char[] arr = str.toCharArray(); int flag=1; for(int i=0 ; i<arr.length; i++){ if(arr[i] == 'r'||arr[i] == 'g'||arr[i] == 'b'){ k[key] = arr[i]; key++; continue; } else if(arr[i]=='R'||arr[i]=='G'||arr[i]=='B'){ char p = Character.toLowerCase(arr[i]); for(int j =0;j<k.length;j++){ if(p==k[j]){ flag=1; break; } else{ flag=0; } } if(flag==0) break; } } if(flag==1) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
d6334bf0f7d3128feaddba6303515b20
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); while(n>0) { n--; String str = sc.next(); char[] cr = new char[6]; int g = 0, f = 0, h = 0, m = 0, j = 0, k = 0; for (int i = 0; i < 6; i++) { cr[i] = str.charAt(i); } for (int i = 0; i < 6; i++) { if (cr[i] == 'r') g += i; else if (cr[i] == 'R') f += i; else if (cr[i] == 'g') h += i; else if (cr[i] == 'G') m += i; else if (cr[i] == 'b') j += i; else if (cr[i] == 'B') k += i; } if (g < f && h < m && j < k){ System.out.println("Yes"); } else if(g > f || h > m || j > k){ System.out.println("No"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
fdb4b45219ae74a690f855ece906b514
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
//package codevita; //import java.io.*; import java.util.*; public class trysample { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++) { String s=sc.next(); if((s.indexOf("R")>s.indexOf("r")) && (s.indexOf("B")>s.indexOf("b")) && (s.indexOf("G")>s.indexOf("g"))) { System.out.println("YES"); }else { System.out.println("NO"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
627d29de50e5c03170f79012eba3ccd3
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class Door { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc= new Scanner(System.in); int t = sc.nextInt(); HashMap <Character, Integer>mp = new HashMap<>(); while(t-->0) { int count =3; String s = sc.next(); String str = ""; for(int i =0;i< s.length();i++) { char c = s.charAt(i); if(i == 0 &&(c == 'R' || c == 'G' || c== 'B')) { System.out.println("NO"); break; } else if(c == 'r' || c == 'g' || c== 'b') { mp.put(c, 1); } else if(c == 'R' || c == 'G' || c== 'B') { char lower1 = Character.toLowerCase(c); if(!mp.isEmpty()&& mp.containsKey(lower1)) { mp.remove(lower1); count--; } else { System.out.println("NO"); break; } } else { System.out.println("NO"); break; } } if(count ==0) System.out.println("YES"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
bb3c6a5415622d89a8ba432eebfb38d5
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class DoorsAndKeys{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ String s=sc.next(); boolean valid=true; int r=0,g=0,b=0; for(char c : s.toCharArray()) { if(c=='R' && r==0){ System.out.println("NO"); valid=false; break; }else if(c=='G' && g==0){ System.out.println("NO"); valid=false; break; }else if(c=='B' && b==0){ System.out.println("NO"); valid=false; break; }else{ if(c=='r'){ r++; }else if(c=='b'){ b++; }else if(c=='g'){ g++; } } } if(valid) System.out.println("YES"); } return; } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
3f92fddd94bf7414528c5bf8fcd21ff1
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Scanner; import java.util.Set; import java.util.StringTokenizer; import java.util.stream.Collectors; public class CodeForces { static int MOD = 100007; public static void main(String[] args) throws java.lang.Exception { DoorsAndKeys(); } public static void DoorsAndKeys() { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { String s = sc.next(); if(s.indexOf('r') < s.indexOf('R') && s.indexOf('g') < s.indexOf('G') && s.indexOf('b') < s.indexOf('B')){ System.out.println("YES"); }else{ System.out.println("NO"); } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
1fff3157a3dfc6a111bdd73695855c04
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class ProblemA { public static void main(String[]args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t--!=0) { String s=sc.next(); int r=0,b=0,g=0,R=0,B=0,G=0; for(int i=0;i<s.length();i++) { if(s.charAt(i)=='r') r=i; else if(s.charAt(i)=='b') b=i; else if(s.charAt(i)=='g') g=i; else if(s.charAt(i)=='R') R=i; else if(s.charAt(i)=='B') B=i; else G=i; } if(r<R && b<B && g<G) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
92b922ac73885a9dbbb6dffb16ad0f38
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; // Compiler version JDK 11.0.2 public class Dcoder { public static void main(String args[]) { Scanner s=new Scanner(System.in); int t=s.nextInt(); s.nextLine(); while(t-->0) { String str=s.nextLine(); Map<Character, Integer> l=new HashMap<Character, Integer>(); for(int i=0;i<6;i++){ l.put(str.charAt(i),i); } if((l.get('r')<l.get('R'))&&(l.get('g')<l.get('G')) && (l.get('b')<l.get('B'))) { System.out.println("YES"); } else System.out.println("NO"); } s.close(); } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
bdb31219a4ad7c49f2ec0bc14dd702bb
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { static class FastScanner { private BufferedReader br; private StringTokenizer st; FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); }catch (IOException e) { throw new IllegalStateException(e); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static void main(String[] args) { FastScanner cin = new FastScanner(); PrintWriter cout = new PrintWriter(System.out); int t = cin.nextInt(); for (int i = 0; i < t; i++) { String colors = cin.next(); boolean flag = false; Set<String> set = new HashSet<>(); for (int j = 0; j < 6; j++) { if (colors.charAt(j) == 'r' || colors.charAt(j) == 'g'|| colors.charAt(j) == 'b') { set.add(Character.toString(colors.charAt(j)).toUpperCase(Locale.ROOT)); }else if (!set.contains(Character.toString(colors.charAt(j)))) { flag = true; } } // boolean[] keys = {false, false, false}; // for (int j = 0; j < 6; j++) { // if (colors.charAt(j) == 'r') { // keys[0] = true; // }else if (colors.charAt(j) == 'g') { // keys[1] = true; // }else if (colors.charAt(j) == 'b') { // keys[2] = true; // }else if (colors.charAt(j) == 'R' && !keys[0]) { // flag = true; // break; // }else if (colors.charAt(j) == 'G' && !keys[1]) { // flag = true; // break; // }else if (!keys[2]) { // flag = true; // break; // } // } if (!flag) { System.out.println("YES"); }else { System.out.println("NO"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
453f31e586d67bbe6689cc75b6bea5c7
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; import java.util.HashSet; public class Main { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int t= sc.nextInt(); while(t-- > 0){ String str= sc.next(); boolean check= false; HashSet<Character> st = new HashSet<>(); for (int i = 0; i < str.length(); i++) { char ch= str.charAt(i); if(ch == 'r' || ch == 'g' || ch == 'b') { st.add(ch); } else if(ch == 'R') { if(st.contains('r')) { check= true; } else { check= false; break; } } else if(ch == 'G') { if(st.contains('g')) { check= true; } else { check= false; break; } } else if(ch == 'B' ) { if(st.contains('b')) { check= true; } else { check= false; break; } } } System.out.println(check ? "YES" : "NO"); } sc.close(); } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
1fc918eeda80bb23ac38cc059b19102c
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class Solution { public static void main(String[]args) throws Exception{ try { Scanner scan = new Scanner(System.in); int test = scan.nextInt(); scan.nextLine(); for (int t = 0; t < test; t++) { String s = scan.nextLine(); boolean r = false; boolean g = false; boolean b = false; boolean ans = true; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'r') { r = true; } else if (s.charAt(i) == 'g') { g = true; } else if (s.charAt(i) == 'b') { b = true; } else { if (s.charAt(i) == 'R' && !r) { ans = false; //System.out.println('R'+" "+r+" "+ans); } else if (s.charAt(i) == 'G' && !g) { ans = false; } else if (s.charAt(i) == 'B' && !b) { ans = false; } } } if (ans) { System.out.println("YES"); } else { System.out.println("NO"); } } }catch (Exception e) { return; } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
3b6c986a209a81f5ca139c4e125a828d
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ String s=sc.next(); int n=s.length(); int r=0; int g=0; int b=0; int ref=0; for(int i=0;i<n;++i){ if(s.charAt(i)=='r')r++; if(s.charAt(i)=='b')b++; if(s.charAt(i)=='g')g++; if(s.charAt(i)=='R'){ if(r==0){ ref++; } } if(s.charAt(i)=='B'){ if(b==0){ ref++; } } if(s.charAt(i)=='G'){ if(g==0){ ref++; } } } if(ref>0) System.out.println("NO"); else System.out.println("YES"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
eadc38a8b84304a135cc38a597f3408d
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.*; import java.util.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(r.readLine()); int tt = Integer.parseInt(st.nextToken()); while(tt-- > 0){ String in = r.readLine(); String key = ""; int i = 0; for(; i < in.length(); i++){ if(in.charAt(i) == 'R' || in.charAt(i) == 'B' || in.charAt(i) == 'G'){ char ch = Character.toLowerCase(in.charAt(i)); String k = ch + ""; if(key.contains(k)==false) break; }else key += in.charAt(i); } if(i == in.length()) pw.println("YES"); else pw.println("NO"); } pw.close(); } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
572edb7660fe3c249d78c1357da541e0
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
//package codeforces; import java.util.*; public class A123Edu { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = 1; t = scn.nextInt(); while (t-- > 0) { // char c='A'; 65 - to 90 // a= 97 to 122 // pt(97+25); String s = scn.next(); int n = s.length(); int sum = 0; boolean flag = true; int[] arr=new int[26]; for (int i = 0; i < n; i++) { char c = s.charAt(i); if (c == 'R' || c == 'G' || c == 'B') { int asi = c - 65; // ?sum -= asi; if(arr[asi]==0) { flag=false; break; } arr[asi]-=1; // pt(asi); } else { int asi = c - 97; // sum += asi; arr[asi]+=1; // pt(asi); } // if (sum < 0) { // flag = false; // break; // } } if (flag && arr[17]==0 && arr[6] ==0&& arr[1]==0) pt("YES"); else pt("NO"); } } public static <T> void pt(T x) { System.out.println(x); return; } public static <T> void pt(T str, T x) { System.out.println(str + "" + x); return; } public static <T> void p(T x, T y) { System.out.print(x + "" + y); return; } public static <T> void p(T x) { System.out.print(x); return; } public static int max(int x, int y) { return Math.max(x, y); } public static int min(int x, int y) { return Math.min(x, y); } public static int abs(int x, int y) { return Math.abs(x - y); } public static <T> void sort(T arr) { Arrays.sort((int[]) arr); return; } public static void sortr(int[] arr) { int n = arr.length; Integer[] ar = new Integer[n]; for (int i = 0; i < n; i++) ar[i] = arr[i]; Arrays.sort(ar, (a, b) -> { return b - a; }); for (int i = 0; i < n; i++) arr[i] = ar[i]; } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
d97e3e30a18fd693bb7f1f57baf9774d
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class DoorsAndKeys { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.hasNextInt() ? sc.nextInt() : 0; sc.nextLine(); boolean[] sol = new boolean[t]; for(int j = 0; j < t; j++) { String s = sc.hasNextLine() ? sc.nextLine() : ""; var c = s.toCharArray(); boolean r = false, g = false, b = false; if(c.length > 0) { for(int i = 0; i < 6; i++) { if(c[i] == 'R' || c[i] == 'G' || c[i] == 'B') { if(c[i] == 'R') sol[j] = r ? true : false; else if(c[i] == 'G') sol[j] = g ? true : false; else if(c[i] == 'B') sol[j] = b ? true : false; if(!sol[j]) break; } else { if(c[i] == 'r') r = true; else if(c[i] == 'g') g = true; else if(c[i] == 'b') b = true; } } } } for(var b : sol) { System.out.println(b ? "YES" : "NO"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
e86939f02de1f5fe789665f082504f3d
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class Doors_and_Keys { public static boolean rgb (String s) { boolean r = false, g = false, b = false; for (int i=0;i<6;i++) { char ch = s.charAt(i); if (ch == 'r') { r = true; } else if (ch == 'g') { g = true; } else if (ch == 'b') { b = true; } if (ch == 'R') { if (!r) { return false; } } else if (ch == 'G') { if (!g) { return false; } } else if (ch == 'B') { if (!b) { return false; } } } return true; } public static void main (String[] Args) { Scanner obj = new Scanner(System.in); int t = obj.nextInt(); while (t > 0) { t--; String s = obj.next(); if (rgb(s)) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
a6da10e7a348e05444bc6aa1e418ebb3
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.*; import java.util.*; public final class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); while (t-- != 0) { String str = sc.nextLine(); if (str.indexOf('r') > str.indexOf('R') || str.indexOf('g') > str.indexOf('G') || str.indexOf('b') > str.indexOf('B')) System.out.println("NO"); else System.out.println("YES"); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
cbf64e875ce2afbfc2f704dfa42efe52
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; import java.io.*; import java.time.*; import static java.lang.Math.*; @SuppressWarnings("unused") public class A { static boolean DEBUG = false; static Reader fs; static PrintWriter pw; static void solve() { char a[] = fs.next().toCharArray(); TreeSet<Character>st = new TreeSet<Character>(); for(int i = 0 ; i < a.length ; i++) { if(a[i] >= 'a' && a[i] <= 'z') { st.add(a[i]); } else { String s = "" + a[i]; s = s.toLowerCase(); if(!st.contains(s.charAt(0))) { pw.println("NO"); return; } } } pw.println("YES"); } public static void main(String[] args) throws IOException { if (args.length == 2) { System.setErr(new PrintStream("D:\\program\\javaCPEclipse\\CodeForces\\src\\error.txt")); DEBUG = true; } Instant start = Instant.now(); fs = new Reader(); pw = new PrintWriter(System.out); int t = fs.nextInt(); while (t-- > 0) { solve(); } Instant end = Instant.now(); if (DEBUG) { pw.println(Duration.between(start, end)); } pw.close(); } static void sort(int a[]) { ArrayList<Integer> l = new ArrayList<Integer>(); for (int x : a) l.add(x); Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } public static void print(long a, long b, long c, PrintWriter pw) { pw.println(a + " " + b + " " + c); return; } static class Reader { BufferedReader br; StringTokenizer st; public Reader() { 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[] readArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[][] read2Array(int n, int m) { int a[][] = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextInt(); } } return a; } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
1469e2afc6578ac5d7d3b695a0b94019
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.*; import java.util.*; public class A { public static void main(String[] args) { while (N-- > 0) { solve(); } out.close(); } public static void solve() { String s = sc.nextLine(); if (s.indexOf('r') < s.indexOf('R') && s.indexOf('g') < s.indexOf('G') && s.indexOf('b') < s.indexOf('B')) { out.println("YES"); } else { out.println("NO"); } } private static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); private static MyScanner sc = new MyScanner(); private static int N = sc.nextInt(); private final static int MOD = 1000000007; @SuppressWarnings("unused") private 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
3e96942be8376a84f1b9b8987555249b
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; public class F{ public static void main(String[] args) throws IOException { // br = new BufferedReader(new FileReader(".in")); // out = new PrintWriter(new FileWriter(".out")); //new Thread(null, new (), "peepee", 1<<28).start(); int t =readInt(); while(t-->0) { char[] ch = read().toCharArray(); int[] c = new int[256]; for (int i = 0 ;i < ch.length; i++) c[ch[i]] = i; out.println(c['r'] < c['R'] && c['b'] < c['B'] && c['g'] < c['G'] ?"Yes":"No"); } out.close(); } /* Stupid things to try if stuck: * n=1, expand bsearch range * brute force small patterns * submit stupid intuition */ static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static StringTokenizer st = new StringTokenizer(""); static String read() throws IOException{return st.hasMoreTokens() ? st.nextToken():(st = new StringTokenizer(br.readLine())).nextToken();} static int readInt() throws IOException{return Integer.parseInt(read());} static long readLong() throws IOException{return Long.parseLong(read());} static double readDouble() throws IOException{return Double.parseDouble(read());} }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
4cb5a7a973f9c3d116ad1d04ff0efae2
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int z = 0; z < t; z++){ String s = in.next(); boolean r = false; boolean g = false; boolean b = false; boolean ans = true; for(int i = 0; i < s.length(); i++){ switch (s.charAt(i)){ case 'r': r = true; break; case 'g': g = true; break; case 'b': b = true; break; case 'R': if(!r) ans = false; break; case 'G': if(!g) ans = false; break; case 'B': if(!b) ans = false; break; } } if(ans) System.out.println("YES"); else System.out.println("NO"); } } } /* * BAN * */
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
b656cd8de565569cdac3ce7d2ee58b2b
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.*; import java.util.*; public class Keys { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); int numberSets = Integer.parseInt(br.readLine()); for (int nos = 0; nos < numberSets; nos++) { char [] data = br.readLine().toCharArray(); List <Character> list = new ArrayList <Character>(); boolean res = true; for (int i = 0; i<6; i++) { if (data[i] == 'r' || data[i] == 'g'|| data[i] == 'b') { list.add(data[i]); } if (data[i] == 'R' && list.contains('r') == false || data[i] == 'G' && list.contains('g') == false|| data[i] == 'B' && list.contains('b') == false) { System.out.println("NO"); res = false; break; } } if (res) {System.out.println("YES");} } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
9ecddef828de62c1e6528a3a7ca79542
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class code { public static void main(String args[]){ Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-->0){ String str = s.next(); int n = str.length(); boolean open = false; for(int i = 0;i<n;i++){ if(str.charAt(i)=='B'){ String str2 = str.substring(0,i); if(str2.contains("b") == true){ open = true; }else{ open = false; break; } }else if (str.charAt(i)=='R'){ String str2 = str.substring(0,i); if(str2.contains("r")==true){ open = true; }else{ open = false; break; } }else if (str.charAt(i)=='G'){ String str2 = str.substring(0,i); if(str2.contains("g")==true){ open = true; }else{ open = false; break; } } } if(open == true){ System.out.println("YES"); }else{ System.out.println("NO"); } // System.out.println(open); } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
1c08bdf1668752f1e8f7d6fbaef48711
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.InputStreamReader; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int tests = scanner.nextInt(); scanner.nextLine(); while (tests-- > 0) { solve(scanner); } } private static void solve(Scanner scanner) { String input = scanner.nextLine(); boolean hasRedKey = false; boolean hasGreenKey = false; boolean hasBlueKey = false; for (int i = 0; i < input.length(); i++) { if (input.charAt(i) == 'R') { if (!hasRedKey) { System.out.println("NO"); return; } } else if (input.charAt(i) == 'G') { if (!hasGreenKey) { System.out.println("NO"); return; } } else if (input.charAt(i) == 'B'){ if (!hasBlueKey) { System.out.println("NO"); return; } } if (input.charAt(i) == 'r') { hasRedKey = true; } else if (input.charAt(i) == 'g') { hasGreenKey = true; } else if (input.charAt(i) == 'b') { hasBlueKey = true; } } System.out.println("YES"); } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
a192513ad90094b39055496cbde104d3
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.io.*; import java.util.*; public class CodeForces { public static void main (String[] args) throws java.lang.Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(System.out); int testCases=Integer.parseInt(br.readLine()); while(testCases-->0){ String s=br.readLine(); int map[]=new int[26]; boolean ans=true; for(int i=0;i<s.length();i++) { char ch=s.charAt(i); if(ch=='R'||ch=='G'||ch=='B') { int idx=ch-'A'; if(map[idx]==0) { ans=false; break; } }else map[ch-'a']++; } if(ans) out.println("YES"); else out.println("NO"); } out.close(); } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
1f8c04993951ca2b79573c8d60ac1701
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class q2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); String arr[] = new String[n]; for (int i=0;i<arr.length;i++) arr[i]=sc.next(); for (int i=0;i<arr.length;i++) System.out.println(find(arr[i])); sc.close(); } static String find(String str) { if (str.length()!=6) return "No"; else{ ArrayList<Character> al = new ArrayList<>(); for (int i=0;i<str.length();i++) { char ch=str.charAt(i); if (ch=='r' || ch=='g' || ch=='b') { al.add(ch); } else if (ch=='R' || ch=='G' || ch=='B') { if (ch=='R') ch='r'; if (ch=='G') ch='g'; if (ch=='B') ch='b'; if (!(al.contains(ch))) return "No"; } else return "No"; } return "Yes"; } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
ddac321b26c29de96d323a1d58acf544
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class KeysAndDoors { public static void main(String... strings) { try (Scanner sc = new Scanner(System.in)) { int testCase = sc.nextInt(); for (int i = 0; i < testCase; i++) { String line = sc.next(); char array[] = line.toCharArray(); char[] keys = new char[3]; int location = 0; int yes = 0; int no = 0; for (int j = 0; j < 6; j++) { if (Character.isLowerCase(array[j])) { keys[location] = array[j]; location++; } else { if (KeysAndDoors.checkIt(array[j], keys)) { yes++; } else { no++; } } } if (yes == 3) { System.out.println("YES"); } else { System.out.println("NO"); } } } } public static boolean checkIt(char door, char[] keys) { for (char ch : keys) { if (Character.toUpperCase(ch) == door) { return true; } } return false; } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
0329ac129551f6e82ea4f8c36f274c20
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
import java.util.Scanner; public class Solution{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int test_cases = sc.nextInt(); for(int h=0;h<test_cases;h++){ String s = sc.next(); boolean r = false, g=false, b=false, done = false; for(int i=0;i<s.length();i++){ if(s.charAt(i)=='R' && r){ continue; } else if(s.charAt(i)=='B' && b){ continue; } else if(s.charAt(i)=='G' && g){ continue; } else if(s.charAt(i)=='r'){ r=true; } else if(s.charAt(i)=='g'){ g=true; } else if(s.charAt(i)=='b'){ b = true; } else{ done =true; System.out.println("NO"); break; } } if(!done){ System.out.println("YES"); } } } }
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output
PASSED
d0e6273137d55b4228f339724e7806bb
train_108.jsonl
1645540500
The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it.In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before.Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue key — are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color.The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, B — denoting red, green and blue doors, respectively; r, g, b — denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once.The knight is standing at the beginning of the hallway — on the left on the map.Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway.
256 megabytes
//codeforces //package someAlgorithms; import java.util.*; import java.io.*; import java.lang.*; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException{ String[] strNums = br.readLine().split(" "); int t=Integer.parseInt(strNums[0]); while(t-->0) { //todo // // String[] strNums1 = br.readLine().split(" "); // long n=Long.parseLong(strNums1[0]); // long q=Long.parseLong(strNums1[1]); // long c=Long.parseLong(strNums1[2]); // long m=Long.parseLong(strNums1[3]); String str = br.readLine(); // Long[] arr = new Long[(int)n]; // StringTokenizer tk = new StringTokenizer(br.readLine().trim()); // for(int i=0;i<n;i++) { // arr[i]=Long.parseLong(tk.nextToken()); // } HashSet<Character> set = new HashSet<>(); boolean flag=true; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='r' || str.charAt(i)=='g' || str.charAt(i)=='b') { set.add(str.charAt(i)); } else { if(!set.contains(Character.toLowerCase(str.charAt(i))) ){ flag=false; break; } } } if(flag) { System.out.println("YES"); } else { System.out.println("NO"); } } } } /* bw.write(n+""); bw.newLine(); bw.flush(); */ //class Pair{ // public long a=0; // public long b=0; // public Pair(long val,long id){ // this.a=val; // this.b=id; // } // //} //class Pair{ // public long val; // public long id; // public Pair(long val,long id){ // this.val=val; // this.id=id; // } // //} //class Node{ // public int val; //// public Node left=null; //// public Node right=null; // ArrayList<Node> children = new ArrayList<>(); // //} //class Comp implements Comparator<Pair>{ // public int compare(Pair p1,Pair p2) { // if(p1.val<p2.val) { // return -1; // } // if(p1.val>p2.val){ // return 1; // } // else return 0; //MUST WRITE THIS RETURN 0 FOR EQUAL CASE SINCE GIVE RUNTIME ERROR IN SOME COMPLIERS // } //} //if take gcd of whole array the take default gcd=0 and keep doing gcd of 2 elements of array! //public static int gcd(int a, int b){ // if(b==0){ // return a; // } // return gcd(b,a%b); //} // //ArrayList<Long>[] adjlist = new ArrayList[(int)n+1]; //array of arraylist //for(int i=0;i<n;i++) { // String[] strNums2 = br.readLine().split(" "); // long a=Long.parseLong(strNums2[0]); // long b=Long.parseLong(strNums2[1]); // // adjlist[(int)a].add(b); // adjlist[(int)b].add(a); //} //int[][] vis = new int[(int)n+1][(int)n+1]; //OR can make list of list :- //List<List<Integer>> adjlist = new ArrayList<>(); //for(int i=0;i<n;i++){ // adjlist.add(new ArrayList<>()); //} //OR 1-D vis array //int[] vis = new int[(int)n+1]; /* Long[] arr = new Long[(int)n]; StringTokenizer tk = new StringTokenizer(br.readLine().trim()); for(int i=0;i<n;i++) { arr[i]=Long.parseLong(tk.nextToken()); } Long[][] arr = new Long[(int)n][(int)m]; for(int i=0;i<n;i++) { String[] strNums2 = br.readLine().split(" "); for(int j=0;j<m;j++) { arr[i][j]=Long.parseLong(strNums2[j]); } } 4 4 4 3 2 4 4 4 3 Main m = new Main(); //no need since pair class main class ne niche banao Pair p = m.new Pair(i,i+1); li.add(p); */ //double num = 3.9634; //double onum=num; //num = (double)((int)(num*100))/100; //double down = (num); //float up =(float) down; //if take up as double then will get large value when add (3.96+0.01)!! //if(down<onum) { // up=(float)down+(float)0.01; //// System.out.println(((float)3.96+(float)0.01)); //// System.out.println(3.96+0.01);//here both double, so output double , so here get large output other than 3.97!! //} ////in c++ 3.96+0.01 is by default 3.97 but in java need to type cast to float to get this output!! //System.out.println(down +" "+up); /* #include <iostream> #include <string> #include<vector> #include<queue> #include<utility> #include<limits.h> #include <unordered_set> #include<algorithm> using namespace std; */
Java
["4\n\nrgbBRG\n\nRgbrBG\n\nbBrRgG\n\nrgRGBb"]
2 seconds
["YES\nNO\nYES\nNO"]
NoteIn the first testcase, the knight first collects all keys, then opens all doors with them.In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it.In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times.In the fourth testcase, the knight can't open the blue door.
Java 11
standard input
[ "implementation" ]
60eb29b2dfb4d6b136c58b33dbd2558e
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 720$$$) — the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once.
800
For each testcase, print YES if the knight can open all doors. Otherwise, print NO.
standard output