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
19423ec307724e2c40db74e4fb34f7da
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.IOException; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.Scanner; public class Edu_1 { public static void main(String[] args){ FasterScanner in=new FasterScanner(); PrintWriter out=new PrintWriter(System.out); int n=in.nextInt(); int k=in.nextInt(); String s=in.nextLine(); char[] a=new char[n]; for(int i=0;i<n;i++){ a[i]=(char)(s.charAt(i)-'a'); } char[] ans=new char[n]; int i=0; for(i=0;i<n;i++){ int max=0; if(a[i]>=25-a[i]){ max=a[i]; if(k>=max){ ans[i]='a'; k-=max; } else if(k==0){ ans[i]=(char)(a[i]+'a'); } else{ int c=a[i]-k>=0?a[i]-k:-1; if(c==-1){ c=a[i]+k; } ans[i]=(char)(c+'a'); k-=k; } } else{ max=25-a[i]; if(k>=max){ ans[i]='z'; k-=max; } else if(k==0){ ans[i]=(char)(a[i]+'a'); } else{ int c=a[i]-k>=0?a[i]-k:-1; if(c==-1){ c=a[i]+k; } ans[i]=(char)(c+'a'); k-=k; } } } if(k>0){ out.println(-1); } else{ String j=new String(ans); out.println(j); } out.close(); } static class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
6ded9b835c2e7a370e4dd1239b6a6e4d
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.awt.Point; import java.awt.geom.Line2D; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.OutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.security.GuardedObject; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.io.InputStream; import java.math.BigInteger; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in; PrintWriter out; in=new FastScanner(System.in); out=new PrintWriter(System.out); //in=new FastScanner(new FileInputStream(new File("C://Users//KANDARP//Desktop//coding_contest_creation (1).txt"))); TaskC solver = new TaskC(); long var=System.currentTimeMillis(); solver.solve(1, in, out); /*if(System.getProperty("ONLINE_JUDGE")==null){ out.println("["+(double)(System.currentTimeMillis()-var)/1000+"]"); }*/ out.close(); } } class Pair { long x,y,index; long r1,r2; HashMap<Double,Long> map=new HashMap<>(); Pair(long x,long y,int index){ this.x=x; this.y=y; this.index=index; } public String toString(){ return this.x+" "+this.y+" "; } } class tupple{ long x,y,z; ArrayList<String> list=new ArrayList<>(); tupple(long x,long y,long z){ this.x=x; this.y=y; this.z=z; } tupple(tupple cl){ this.x=cl.x; this.y=cl.y; this.z=cl.z; this.list=cl.list; } } class Edge implements Comparable<Edge>{ int u; int v; int time; long cost; long wcost; public Edge(int u,int v,long cost,long wcost){ this.u=u; this.v=v; this.cost=cost; this.wcost=wcost; time=Integer.MAX_VALUE; } public int other(int x){ if(u==x)return v; return u; } @Override public int compareTo(Edge o) { // TODO Auto-generated method stub return Long.compare(cost, o.cost); } } class Ss{ Integer a[]=new Integer[3]; Ss(Integer a[]){ this.a=a; } public int hashCode(){ return a[0].hashCode()+a[1].hashCode()+a[2].hashCode(); } public boolean equals(Object o){ Ss x=(Ss)o; for(int i=0;i<3;i++){ if(x.a[i]!=this.a[i]){ return false; } } return true; } } class queary{ int type; int l; int r; int index; queary(int type,int l,int r){ this.type=type; this.l=l; this.r=r; } } class boat implements Comparable<boat>{ int capacity; int index; boat(int capacity,int index) { this.capacity=capacity; this.index=index; } @Override public int compareTo(boat o) { // TODO Auto-generated method stub return this.capacity-o.capacity; } } class angle{ double x,y,angle; int index; angle(int x,int y,double angle){ this.x=x; this.y=y; this.angle=angle; } } class TaskC { static long mod=1000000007; // SegTree tree; // int min[]; /* static int prime_len=1000010; static int prime[]=new int[prime_len]; static long n_prime[]=new long[prime_len]; static ArrayList<Integer> primes=new ArrayList<>(); static{ for(int i=2;i<=Math.sqrt(prime.length);i++){ for(int j=i*i;j<prime.length;j+=i){ prime[j]=i; } } for(int i=2;i<prime.length;i++){ n_prime[i]=n_prime[i-1]; if(prime[i]==0){ n_prime[i]++; } } // System.out.println("end"); // prime[0]=true; // prime[1]=1; } /* * long pp[]=new long[10000001]; pp[0]=0; pp[1]=1; for(int i=2;i<pp.length;i++){ if(prime[i]!=0){ int gcd=(int)greatestCommonDivisor(i/prime[i], prime[i]); pp[i]=(pp[i/prime[i]]*pp[prime[i]]*gcd)/pp[(int)gcd]; } else pp[i]=i-1; } } * */ class Rmq { int[] logTable; int[][] rmq; int[] a; public Rmq(int[] a) { this.a = a; int n = a.length; logTable = new int[n + 1]; for (int i = 2; i <= n; i++) logTable[i] = logTable[i >> 1] + 1; rmq = new int[logTable[n] + 1][n]; for (int i = 0; i < n; ++i) rmq[0][i] = i; for (int k = 1; (1 << k) < n; ++k) { for (int i = 0; i + (1 << k) <= n; i++) { int x = rmq[k - 1][i]; int y = rmq[k - 1][i + (1 << k - 1)]; rmq[k][i] = a[x] <= a[y] ? x : y; } } } public int minPos(int i, int j) { int k = logTable[j - i]; int x = rmq[k][i]; int y = rmq[k][j - (1 << k) + 1]; return a[x] <= a[y] ? a[x] : a[y]; } } public void solve(int testNumber, FastScanner in, PrintWriter out) throws IOException { int n=in.nextInt(); int k=in.nextInt(); String s=in.readString(); StringBuilder sb=new StringBuilder(); for(int i=0;i<s.length();i++){ if(Math.max('z'-s.charAt(i),s.charAt(i)-'a')<=k ){ k-=Math.max('z'-s.charAt(i),s.charAt(i)-'a'); if('z'-s.charAt(i)>s.charAt(i)-'a'){ sb.append('z'); } else{ sb.append('a'); } } else { if(s.charAt(i)+k<='z') sb.append((char)(s.charAt(i)+k)); else{ sb.append((char)(s.charAt(i)-k)); } k=0; // break; } } if(k!=0){ out.println(-1); } else{ out.println(sb.toString()); } } static long greatestCommonDivisor (long m, long n){ long x; long y; while(m%n != 0){ x = n; y = m%n; m = x; n = y; } return n; } } /* * long res[]=new long[m]; int cl=1; int cr=0; for(int i=0;i<m;i++){ int l=(int)q[i].x; int r=(int)q[i].y; while(cl>l){ cl--; ans+=add(cl); } while(cl<l){ ans-=remove(cl); cl++; } while(cr>r){ ans-=remove(cr); cr--; } while(cr<r){ cr++; ans+=add(cr); } res[(int)q[i].index]=ans; } for(int i=0;i<m;i++) out.println(res[i]); */ class SegmentTree2D { public long max(int[][] t, int x1, int y1, int x2, int y2) { int n = t.length >> 1; x1 += n; x2 += n; int m = t[0].length >> 1; y1 += m; y2 += m; long res = 0; for (int lx = x1, rx = x2; lx <= rx; lx = (lx + 1) >> 1, rx = (rx - 1) >> 1) for (int ly = y1, ry = y2; ly <= ry; ly = (ly + 1) >> 1, ry = (ry - 1) >> 1) { if ((lx & 1) != 0 && (ly & 1) != 0) res = (res+ t[lx][ly]); if ((lx & 1) != 0 && (ry & 1) == 0) res = (res+ t[lx][ry]); if ((rx & 1) == 0 && (ly & 1) != 0) res =(res+t[rx][ly]); if ((rx & 1) == 0 && (ry & 1) == 0) res = (res+ t[rx][ry]); } return res; } public void add(int[][] t, int x, int y, int value) { x += t.length >> 1; y += t[0].length >> 1; t[x][y] += value; for (int tx = x; tx > 0; tx >>= 1) for (int ty = y; ty > 0; ty >>= 1) { if (tx > 1) t[tx >> 1][ty] = (t[tx][ty]+t[tx ^ 1][ty]); if (ty > 1) t[tx][ty >> 1] = (t[tx][ty]+ t[tx][ty ^ 1]); } // for (int ty = y; ty > 1; ty >>= 1) // t[x][ty >> 1] = Math.max(t[x][ty], t[x][ty ^ 1]); // for (int tx = x; tx > 1; tx >>= 1) // for (int ty = y; ty > 0; ty >>= 1) // t[tx >> 1][ty] = Math.max(t[tx][ty], t[tx ^ 1][ty]); // for (int lx=x; lx> 0; lx >>= 1) { // if (lx > 1) // t[lx >> 1][y] = Math.max(t[lx][y], t[lx ^ 1][y]); // for (int ly = y; ly > 1; ly >>= 1) // t[lx][ly >> 1] = Math.max(t[lx][ly], t[lx][ly ^ 1]); // } } } class SegmentTree { // Modify the following 5 methods to implement your custom operations on the tree. // This example implements Add/Max operations. Operations like Add/Sum, Set/Max can also be implemented. long modifyOperation(long x, long y) { return y; } // query (or combine) operation long queryOperation(long leftValue, long rightValue) { return (leftValue| rightValue); } long deltaEffectOnSegment(long delta, long segmentLength) { // Here you must write a fast equivalent of following slow code: // int result = delta; // for (int i = 1; i < segmentLength; i++) result = queryOperation(result, delta); // return result; return delta; } int getNeutralDelta() { return 0; } int getInitValue() { return 0; } // generic code int n; long[] value; long[] delta; // delta[i] affects value[i], delta[2*i+1] and delta[2*i+2] long joinValueWithDelta(long value, long delta) { if (delta == getNeutralDelta()) return value; return modifyOperation(value, delta); } long joinDeltas(long delta1, long delta2) { if (delta1 == getNeutralDelta()) return delta2; if (delta2 == getNeutralDelta()) return delta1; return modifyOperation(delta1, delta2); } void pushDelta(int root, int left, int right) { value[root] = joinValueWithDelta(value[root], deltaEffectOnSegment(delta[root], right - left + 1)); delta[2 * root + 1] = joinDeltas(delta[2 * root + 1], delta[root]); delta[2 * root + 2] = joinDeltas(delta[2 * root + 2], delta[root]); delta[root] = getNeutralDelta(); } public SegmentTree(int n) { this.n = n; value = new long[4 * n]; delta = new long[4 * n]; init(0, 0, n - 1); } void init(int root, int left, int right) { if (left == right) { value[root] = getInitValue(); delta[root] = getNeutralDelta(); } else { int mid = (left + right) >> 1; init(2 * root + 1, left, mid); init(2 * root + 2, mid + 1, right); value[root] = queryOperation(value[2 * root + 1], value[2 * root + 2]); delta[root] = getNeutralDelta(); } } public long query(int from, int to) { return query(from, to, 0, 0, n - 1); } long query(int from, int to, int root, int left, int right) { if (from == left && to == right) return joinValueWithDelta(value[root], deltaEffectOnSegment(delta[root], right - left + 1)); pushDelta(root, left, right); int mid = (left + right) >> 1; if (from <= mid && to > mid) return queryOperation( query(from, Math.min(to, mid), root * 2 + 1, left, mid), query(Math.max(from, mid + 1), to, root * 2 + 2, mid + 1, right)); else if (from <= mid) return query(from, Math.min(to, mid), root * 2 + 1, left, mid); else if (to > mid) return query(Math.max(from, mid + 1), to, root * 2 + 2, mid + 1, right); else throw new RuntimeException("Incorrect query from " + from + " to " + to); } public void modify(int from, int to, long delta) { modify(from, to, delta, 0, 0, n - 1); } void modify(int from, int to, long delta, int root, int left, int right) { if (from == left && to == right) { this.delta[root] = joinDeltas(this.delta[root], delta); return; } pushDelta(root, left, right); int mid = (left + right) >> 1; if (from <= mid) modify(from, Math.min(to, mid), delta, 2 * root + 1, left, mid); if (to > mid) modify(Math.max(from, mid + 1), to, delta, 2 * root + 2, mid + 1, right); value[root] = queryOperation( joinValueWithDelta(value[2 * root + 1], deltaEffectOnSegment(this.delta[2 * root + 1], mid - left + 1)), joinValueWithDelta(value[2 * root + 2], deltaEffectOnSegment(this.delta[2 * root + 2], right - mid))); } } class LcaSparseTable { int len; int[][] up; int[][] max; int[] tin; int[] tout; int time; int []lvel; void dfs(List<Integer>[] tree, int u, int p) { tin[u] = time++; lvel[u]=lvel[p]+1; up[0][u] = p; if(u!=p) //max[0][u]=weight(u,p); for (int i = 1; i < len; i++) { up[i][u] = up[i - 1][up[i - 1][u]]; max[i][u]=Math.max(max[i-1][u],max[i-1][up[i-1][u]]); } for (int v : tree[u]) if (v != p) dfs(tree, v, u); tout[u] = time++; } public LcaSparseTable(List<Integer>[] tree, int root) { int n = tree.length; len = 1; while ((1 << len) <= n) ++len; up = new int[len][n]; max=new int[len][n]; tin = new int[n]; tout = new int[n]; lvel=new int[n]; lvel[root]=0; dfs(tree, root, root); } boolean isParent(int parent, int child) { return tin[parent] <= tin[child] && tout[child] <= tout[parent]; } public int lca(int a, int b) { if (isParent(a, b)) return a; if (isParent(b, a)) return b; for (int i = len - 1; i >= 0; i--) if (!isParent(up[i][a], b)) a = up[i][a]; return up[0][a]; } public long max(int a,int b){ int lca=lca(a,b); // System.out.println("LCA "+lca); long ans=0; int h=lvel[a]-lvel[lca]; // System.out.println("Height "+h); int index=0; while(h!=0){ if(h%2==1){ ans=Math.max(ans,max[index][a]); a=up[index][a]; } h/=2; index++; } h=lvel[b]-lvel[lca]; // System.out.println("Height "+h); index=0; while(h!=0){ if(h%2==1){ ans=Math.max(ans,max[index][b]); b=up[index][b]; } h/=2; index++; } return ans; } static void dfs(List<Integer>[] graph, boolean[] used, List<Integer> res, int u,int parent,List<Integer> collection) { used[u] = true; Integer uu=new Integer(u); collection.add(uu); for (int v : graph[u]){ if (!used[v]){ dfs(graph, used, res, v,u,collection); } else if(collection.contains(v)){ System.out.println("Impossible"); System.exit(0); } } collection.remove(uu); res.add(u); } public static List<Integer> topologicalSort(List<Integer>[] graph) { int n = graph.length; boolean[] used = new boolean[n]; List<Integer> res = new ArrayList<>(); for (int i = 0; i < n; i++) if (!used[i]) dfs(graph, used, res, i,-1,new ArrayList<Integer>()); Collections.reverse(res); return res; } static class pairs{ String company,color; String type; boolean visit=false; pairs(String company,String color,String type){ this.company=company; this.color=color; this.type=type; } } static void dfs1(List<Integer>[] graph, boolean[] used, List<Integer> res, int u) { used[u] = true; for (int v : graph[u]) if (!used[v]) dfs1(graph, used, res, v); res.add(u); } public static List<Integer> topologicalSort1(List<Integer>[] graph) { int n = graph.length; boolean[] used = new boolean[n]; List<Integer> res = new ArrayList<>(); for (int i = n-1; i >0; i--) if (!used[i]) dfs1(graph, used, res, i); Collections.reverse(res); return res; } int phi(int n) { int res = n; for (int i = 2; i * i <= n; ++i) { if (n % i == 0) { while (n % i == 0) { n /= i; } res -= res / i; } } if (n != 1) { res -= res / n; } return res; } long pow(long x,long y,long mod){ if(y<=0){ return 1; } if(y==1){ return x%mod; } long temp=pow(x,y/2,mod); if(y%2==0){ return (temp*temp)%mod; } else{ return (((temp*temp)%mod)*x)%mod; } } /* long DP(int id,int mask){ //System.out.println("hi"+dp[id][mask]+" "+id+" "+Integer.toBinaryString(mask)); if(id==0 && mask==0){ dp[0][mask]=1; return 1; } else if(id==0){ dp[0][mask]=0; return 0; } if(dp[id][mask]!=-1){ return dp[id][mask]; } long ans=0; for(int i=0;i<n;i++){ if((mask & (1<<i))!=0 && c[i][id]>=1){ ans+=DP(id-1,mask ^ (1 << i)); ans%=mod; } } ans+=DP(id-1,mask); ans%=mod; dp[id][mask]=ans; return ans; }*/ long no_of_primes(long m,long n,long k){ long count=0,i,j; int primes []=new int[(int)(n-m+2)]; if(m==1) primes[0] = 1; for(i=2; i<=Math.sqrt(n); i++) { j = (m/i); j *= i; if(j<m) j+=i; for(; j<=n; j+=i) { if(j!=i) primes[(int)(j-m)] = 1; } } for(i=0; i<=n-m; i++) if(primes[(int)i]==0 && (i-1)%k==0) count++; return count; } } class SegTree { int n; long t[]; long mod=(long)(1000000007); SegTree(int n,long t[]){ this.n=n; this.t=t; build(); } void build() { // build the tree for (int i = n - 1; i > 0; --i) t[i]=(t[i<<1]|t[i<<1|1]); } void modify(int p, long value) { // set value at position p for (t[p += n]+=value; p > 1; p >>= 1) t[p>>1] = (t[p]|t[p^1]); } long query(int l, int r) { // sum on interval [l, r) long res=0; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if ((l&1)!=0) res=Math.max(res,t[l++]); if ((r&1)!=0) res=Math.max(res,t[--r]); } return res; } // // // } class FastScanner { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int snumChars; private SpaceCharFilter filter; public FastScanner(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class UF { private int[] parent; // parent[i] = parent of i private byte[] rank; // rank[i] = rank of subtree rooted at i (never more than 31) private int count; // number of components public UF(int N) { if (N < 0) throw new IllegalArgumentException(); count = N; parent = new int[N]; rank = new byte[N]; for (int i = 0; i < N; i++) { parent[i] = i; rank[i] = 0; } } public int find(int p) { if (p < 0 || p >= parent.length) throw new IndexOutOfBoundsException(); while (p != parent[p]) { parent[p] = parent[parent[p]]; // path compression by halving p = parent[p]; } return p; } public int count() { return count; } public boolean connected(int p, int q) { return find(p) == find(q); } public boolean union(int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return false; // make root of smaller rank point to root of larger rank if (rank[rootP] < rank[rootQ]) parent[rootP] = rootQ; else if (rank[rootP] > rank[rootQ]) parent[rootQ] = rootP; else { parent[rootQ] = rootP; rank[rootP]++; } count--; return true; } } class MyArrayList { private int[] myStore; private int actSize = 0; public MyArrayList(){ myStore = new int[2]; } public int get(int index){ if(index < actSize) return myStore[index]; else throw new ArrayIndexOutOfBoundsException(); } public void add(int obj){ if(myStore.length-actSize <= 1) increaseListSize(); myStore[actSize++] = obj; } public int size(){ return actSize; } private void increaseListSize(){ myStore = Arrays.copyOf(myStore, myStore.length*2); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
22bf1b5032d2f2a7b42e3b76ef296677
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.StringTokenizer; public class C628C { public static void main(String[] args)throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st = new StringTokenizer(in.readLine()); int n = Integer.valueOf(st.nextToken()) , k = Integer.valueOf(st.nextToken()); String s = in.readLine(); int max = 0; for(int i = 0;i < n;i ++) max += Math.max(s.charAt(i) - 'a','z' - s.charAt(i)); if(max < k){ out.write("-1"); out.close(); return; } for(int i = 0;i < n;i ++) if(s.charAt(i) - 'a'> 'z' - s.charAt(i)){ if(s.charAt(i) - 'a' > k){ out.write("" + (char)(s.charAt(i) - k)); k = 0; }else{ out.write("a"); k -= s.charAt(i) - 'a'; } }else{ if('z' - s.charAt(i) > k){ out.write("" + (char)(s.charAt(i) + k)); k = 0; }else{ out.write("z"); k -= 'z' - s.charAt(i); } } out.close(); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
7b9a10ed6c31246424381d4f24502ec4
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.util.Scanner; public class C { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); scanner.nextLine(); String s = scanner.nextLine(); StringBuffer newS = new StringBuffer(); for(int i = 0; i < n; i++) { char c = s.charAt(i); int max = Math.max(Math.abs(c - 'a'), Math.abs('z' - c)); // Get most points if(k > max) { // System.out.println(1); k -= max; newS.append(furtherTo('a', 'z', c)); } // Get 0 else if(k == 0) { // System.out.println(2); newS.append(c); } // Get exact points else { // System.out.println(3); if(isLetter((char) (c + k))) { newS.append((char) (c + k)); } else { newS.append((char) (c - k)); } k = 0; } } if(k > 0) { System.out.println(-1); } else { System.out.println(newS); } } private static char furtherTo(char first, char end, char current) { int f = Math.abs(first - current); int s = Math.abs(end - current); if(f > s) { return first; } return end; } private static boolean isLetter(char c) { return 'a' <= c && c <= 'z'; } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
b13a928e72c492503f68c540a5f39b45
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.*; import java.util.StringTokenizer; /** * Created by peacefrog on 2/20/16. * 9:00 AM */ public class CF628C { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; PrintWriter out; long timeBegin, timeEnd; public void runIO() throws IOException { timeBegin = System.currentTimeMillis(); InputStream inputStream; OutputStream outputStream; if (ONLINE_JUDGE) { inputStream = System.in; Reader.init(inputStream); outputStream = System.out; out = new PrintWriter(outputStream); } else { inputStream = new FileInputStream("/home/peacefrog/Dropbox/IdeaProjects/Problem Solving/input"); Reader.init(inputStream); out = new PrintWriter(System.out); } solve(); out.flush(); out.close(); timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } /* * Start Solution Here */ private void solve() throws IOException { int n = Reader.nextInt(), k = Reader.nextInt(); char[] s = Reader.nextLine().toCharArray(); char[] changed = new char[n]; for (int i = 0; i < n; i++) { if(('z'-s[i]) > Math.abs('a'-s[i])) { if (('z' - s[i]) < k) { changed[i] = 'z'; k -= ('z' - s[i]); } else { changed[i] = (char) ((int) s[i] + k); k = 0; } } else { if (Math.abs(s[i]- 'a') < k) { changed[i] = 'a'; k -= Math.abs('a' - s[i]); } else { changed[i] = (char) ((int) s[i] - k); k = 0; } } // maxPossible+= ('z'- s[i]); } if( k != 0) out.println(-1); else out.println(changed); } public static void main(String[] args) throws IOException { new CF628C().runIO(); } static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** * call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** * get next word */ static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } static String nextLine() { try { return reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return ""; } static int nextChar() throws IOException { return reader.read(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } static int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
73e8b548186d7b20a1a6624bb649379e
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; import java.math.BigInteger; import java.util.ArrayList; /** * Built using CHelper plug-in * Actual solution is at the top * @author Erasyl Abenov * * */ public class Main { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); try (PrintWriter out = new PrintWriter(outputStream)) { TaskB solver = new TaskB(); solver.solve(1, in, out); } } } class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException{ int n = in.nextInt(); int k = in.nextInt(); String s = in.next(); if(k/25 > n){ out.println(-1); return; } String ans = ""; ArrayList<Character> ll = new ArrayList<>(); for(int i = 0; i < n; ++i){ if(k == 0){ ll.add(s.charAt(i)); continue; } int z = 'z' - s.charAt(i); int d = s.charAt(i) - 'a'; char l = 'a'; if(z > d){ d = z; l = 'z'; } if(k >= d){ ll.add(l); k -= d; continue; } if(s.charAt(i) - k >= 'a') ll.add((char)(s.charAt(i) - k)); else ll.add((char)(s.charAt(i) + k)); k = 0; } if(k > 0) out.println(-1); else{ for(char c : ll) out.print(c); } } } class InputReader { private final BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(nextLine()); } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public BigInteger nextBigInteger(){ return new BigInteger(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
e62b029d7fe0cfc61fac24e805347be0
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Codef{ public static void main(String[] args) throws IOException{ BufferedReader vod=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(vod.readLine()); int n=Integer.parseInt(st.nextToken()); int m=Integer.parseInt(st.nextToken()); char ar[]=vod.readLine().toCharArray(); int k=0; boolean p; StringBuilder s=new StringBuilder(); for (int i=0; i<n; i++){ int a=ar[i]-'a'; int b='z'-ar[i]; if (a>b) p=true; else p=false; if (Math.max(a, b)<m-k){ k+=Math.max(a,b); if (p) s.append('a'); else s.append('z'); } else{ int c=m-k; if (p) s.append((char)(ar[i]-c)); else s.append((char)(ar[i]+c)); k+=c; } } if (m!=k) System.out.print(-1); else System.out.print(s); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
d730bf36bfd4f180f3e3614d63976a66
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) throws IOException { FastScanner qwe = new FastScanner(System.in); int n = qwe.nextInt(); int k = qwe.nextInt(); char[] ans = new char[n]; char[] word = qwe.next().toCharArray(); for (int i = 0; i < word.length; i++) { if( k >= word[i]-'a' && k >= 'z'-word[i]){ if(word[i]-'a' >= 'z'-word[i]){ ans[i] = 'a'; k -= word[i]-'a'; } else{ ans[i] = 'z'; k -= 'z'-word[i]; } } else { if(word[i]-'a' >= 'z'-word[i]){ ans[i] = (char)(word[i]-k); k = 0; } else{ ans[i] = (char)(word[i]+k); k = 0; } } } if(k > 0)System.out.println(-1); else System.out.println(String.valueOf(ans)); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream in) throws IOException{ br = new BufferedReader(new InputStreamReader(in)); st = new StringTokenizer(br.readLine().trim()); } public FastScanner(File in) throws IOException{ br = new BufferedReader(new FileReader(in)); st = new StringTokenizer(br.readLine().trim()); } public int numTokens() throws IOException { if(!st.hasMoreTokens()){ st = new StringTokenizer(br.readLine().trim()); return numTokens(); } return st.countTokens(); } public String next() throws IOException { if(!st.hasMoreTokens()){ st = new StringTokenizer(br.readLine().trim()); return next(); } return st.nextToken(); } public double nextDouble() throws IOException{ return Double.parseDouble(next()); } public float nextFloat() throws IOException{ return Float.parseFloat(next()); } public long nextLong() throws IOException{ return Long.parseLong(next()); } public int nextInt() throws IOException{ return Integer.parseInt(next()); } public String nextLine() throws IOException{ return br.readLine().trim(); } } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
16e0b1027ec76badcc47bfeaefef053b
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.*; public class C { public static void main (String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String[] nums = reader.readLine().split(" "); int n = Integer.parseInt(nums[0]); int k = Integer.parseInt(nums[1]); String str = reader.readLine(); char[] arr = str.toCharArray(); for (int i = 0; i < arr.length; i++) { int fromA = arr[i] - 'a'; int toZ = 'z' - arr[i]; int dist; char next; if (fromA > toZ) { dist = fromA; next = 'a'; } else { dist = toZ; next = 'z'; } if (k > dist) { k -= dist; arr[i] = next; } else if (next == 'a') { arr[i] -= k; k = 0; break; } else { arr[i] += k; k = 0; break; } } if (k > 0) { System.out.println(-1); } else { System.out.println(arr); } } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
8644f4274a6ef16ec6d582b6a4d318f0
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; public class Main{ // split de array public static int[] readInts(String cad) { String read[] = cad.split(" "); int res[] = new int[read.length]; for (int i = 0; i < read.length; i++) { res[i] = Integer.parseInt(read[i]); } return res; } public static void main(String[] args) throws IOException { long inicio = System.currentTimeMillis(); StringBuilder out = new StringBuilder(); BufferedReader in; File archivo = new File("entrada"); if (archivo.exists()) in = new BufferedReader(new FileReader(archivo)); else in = new BufferedReader(new InputStreamReader(System.in)); int arr[] = readInts(in.readLine()); int k = arr[1]; char s[] = in.readLine().toCharArray(); long count = 0; for (int i = 0; i < s.length; i++) { count += Math.max(s[i] - 'a', 'z' - s[i]); } if (count < k) { out.append("-1\n"); } else { for (int i = 0; i < s.length; i++) { int f = Math.max(s[i] - 'a', 'z' - s[i]); if (k - f >= 0) { if (s[i] - 'a' > 'z' - s[i]) { out.append('a'); k -= s[i] - 'a'; } else { out.append('z'); k -= 'z' - s[i]; } } else { if (k > 0) { if (s[i] - 'a' > 'z' - s[i]) { out.append((char) ('a' + f - k)); } else { out.append((char) ('z' - (f - k))); } k = 0; } else { out.append(s[i]); } } } } System.out.print(out); if (archivo.exists()) System.out.println("Tiempo transcurrido : " + (System.currentTimeMillis() - inicio) + " milisegundos."); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
46351492c14bcfd8a3ebef519129ddb3
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.util.*; import java.io.*; public class Major { public static void main(String [] args)throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); String str = br.readLine(); StringBuilder sb = new StringBuilder(); for(int i = 0;i < str.length();i++){ int a = str.charAt(i) - 'a'; int b = 'z' - str.charAt(i); if(a > b){ int t = Math.min(k,a); sb.append((char)(str.charAt(i) - t)); k -= t; } else{ int t = Math.min(k,b); sb.append((char)(str.charAt(i) + t)); k -= t; } } if(k > 0){ System.out.print("-1"); return; } System.out.print(sb.toString()); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
d0e29dbfc18e4a1c01f1ceeabbc482ec
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.awt.Point; import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; import java.math.BigDecimal; /** * * @author Mojtaba */ public class Main { public static void main(String[] args) throws IOException { MyScanner in = new MyScanner(System.in); //Scanner in = new Scanner(System.in); //Scanner in = new Scanner(new File("input.txt")); PrintWriter writer = new PrintWriter(new BufferedOutputStream(System.out)); StringBuilder sb = new StringBuilder(""); int n = in.nextInt(); int k = in.nextInt(); char[] a = in.next().toCharArray(); char[] b = new char[a.length]; for (int i = 0; i < a.length; i++) { char ch = a[i]; int fa = abs(ch - 'a'); int fz = abs(ch - 'z'); int m = max(fa, fz); if (k == 0) { b[i] = a[i]; } else if (k <= m) { if (m == fa) { b[i] = (char) (ch - k); } else { b[i] = (char) (ch + k); } k = 0; } else { k -= m; if (m == fa) { b[i] = 'a'; } else { b[i] = 'z'; } } } if (k != 0) { sb.append(-1); } else { for (int i = 0; i < b.length; i++) { sb.append(b[i]); } } writer.println(sb.toString().trim()); writer.flush(); in.close(); } } class MyScanner { BufferedReader reader; StringTokenizer tokenizer; public MyScanner(InputStream stream) { this.reader = new BufferedReader(new InputStreamReader(stream)); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public int[] nextIntegerArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = nextInt(); } return a; } public long[] nextLongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < a.length; i++) { a[i] = nextLong(); } return a; } public int nextInt(int radix) throws IOException { return Integer.parseInt(next(), radix); } public long nextLong() throws IOException { return Long.parseLong(next()); } public long nextLong(int radix) throws IOException { return Long.parseLong(next(), radix); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public BigInteger nextBigInteger() throws IOException { return new BigInteger(next()); } public BigInteger nextBigInteger(int radix) throws IOException { return new BigInteger(next(), radix); } public String next() throws IOException { if (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); return this.next(); } return tokenizer.nextToken(); } public void close() throws IOException { this.reader.close(); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
7951391529dcdb640157667baa0de85c
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.*; import java.util.*; public class C628 { public static void main(String[] args) throws IOException { input.init(System.in); PrintWriter out = new PrintWriter(System.out); int n = input.nextInt(), k = input.nextInt(); String s = input.next(); char[] res = new char[n]; int dist = 0; for(int i = 0; i<n; i++) { char c = s.charAt(i); if(c <= 'm') res[i] = (char) Math.min('z', c+k-dist); else res[i] = (char) Math.max('a', c-(k-dist)); dist += Math.abs(c - res[i]); } out.println(dist == k ? new String(res) : -1); out.close(); } public static class input { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
88c70a3e6f931f0cdc34e24f518971c5
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.util.*; import java.io.*; public class C { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); String[] nk = in.readLine().split(" "); int n = Integer.parseInt(nk[0]); int k = Integer.parseInt(nk[1]); char[] s = in.readLine().toCharArray(); char[] t = new char[n]; for (int i = 0; i < n; i++) { if (k == 0) t[i] = s[i]; else { t[i] = getDist(s[i], Math.min(k, maxDist(s[i]))); k -= getCharDist(t[i], s[i]); } } if (k == 0) { for (int i = 0; i < n; i++) { out.print(t[i]); } out.println(); out.close(); } else { out.println("-1"); out.close(); } } static int getCharDist(char c, char d) { return Math.abs(c-d); } static int maxDist(char c) { return Math.max(c-'a', 'z'-c); } static char getDist(char c, int diff) { if (c+diff <= 'z') return (char)(c+diff); return (char)(c-diff); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
76bca9be45cc54808f317e60c38e9af1
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner stdin = new Scanner(new BufferedInputStream(System.in)); int n = stdin.nextInt(); int k = stdin.nextInt(); stdin.nextLine(); String s = stdin.nextLine(); char[] ss = s.toCharArray(); // System.out.println(ss.length); // for (int i = 0; i < n; i++) // System.out.print(ss[i]); for (int i = 0; i < ss.length && k > 0; i++) { char c = 'z'; if (c - ss[i] < ss[i] - 'a') c = 'a'; // System.out.print("c = " + c); if (Math.abs(c - ss[i]) > k) { if (ss[i] + k > 'z') { ss[i] -= k; } else { ss[i] += k; } k = 0; } else { k -= Math.abs(c - ss[i]); ss[i] = c; } // System.out.print(ss[i]); } // System.out.println(); if (k == 0) System.out.println(String.valueOf(ss)); else System.out.println(-1); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
a17eb40e9b1760b7ea667a0da6349966
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class P628C { InputStream is; PrintWriter out; String INPUT = "3 1000 hey"; void solve() { int n = ni(); int k = ni(); String s = ns(); char a[] = s.toCharArray(); for (int i = 0; i < n && k > 0; i++) { int up = Math.abs(a[i] - 'a'); int down = Math.abs(a[i] - 'z'); if (up > down) { a[i] -= Math.min(k, up); k -= Math.min(k, up); } else { a[i] += Math.min(k, down); k -= Math.min(k, down); } } if (k > 0) { System.out.println(-1); } else { System.out.println(new String(a)); } } public static void main(String[] args) throws Exception { new P628C().run(); } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis() - s + "ms"); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
f14e7ab40723eaeded91450a9a2ca24a
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; /** * Feb 19, 2016 | 4:18:25 PM * <pre> * <u>Description</u> * * </pre> * * @author Essiennta Emmanuel (colourfulemmanuel@gmail.com) */ public class ProblemC{ String solve(String s, int rem){ int max = 0; for (int i = s.length() - 1; i >= 0; i--) max += Math.max(s.charAt(i) - 'a', 'z' - s.charAt(i)); if (rem > max) return null; int used = 0; char[] ans = new char[s.length()]; for (int i = 0; i < ans.length; i++) ans[i] = s.charAt(i); for (int i = s.length() - 1; used != rem && i >= 0; i--){ char ch = s.charAt(i); char base; if (ch - 'a' > 'z' - ch) base = 'a'; else base = 'z'; if (rem < Math.abs(base - ch)){ if (base == 'a'){ ans[i] = (char)('a' + ((ch - 'a') - rem)); }else{ ans[i] = (char)('z' - (('z' - ch) - rem)); } rem = 0; }else{ rem -= Math.abs(base - ch); ans[i] = base; } } return new String(ans); } public static void main(String[] args) throws IOException{ PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] split = in.readLine().split(" "); int k = Integer.parseInt(split[1]); String s = in.readLine(); String solve = new ProblemC().solve(s, k); out.println(solve == null ? -1 : solve); out.close(); in.close(); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
98386cdaaa9cc8ff4095b3373eaa1ea2
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class C { static int max(char c) { c -= 'a'; return Math.max(25 - c, c); } public static void main(String[] args) throws IOException { Reader.init(System.in); PrintWriter pw=new PrintWriter(System.out); int n = Reader.nextInt(); int k = Reader.nextInt(); char c, d; int maxdist; String line = Reader.nextLine(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { maxdist = max(line.charAt(i)); if (k - maxdist >= 0) { k -= maxdist; c = (char) (line.charAt(i) + maxdist); d = (char) (line.charAt(i) - maxdist); if (Character.isLowerCase(c)) { sb.append(c); } else{ sb.append(d); } } else { c = (char) (line.charAt(i) + k); d = (char) (line.charAt(i) - k); if (Character.isLowerCase(c)) { sb.append(c); } else sb.append(d); k=0; } } if(k!=0) System.out.println("-1"); else { pw.println(sb.toString()); pw.flush(); } } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { // TODO add check for eof if necessary tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static String nextLine() throws IOException { return reader.readLine(); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
15915937141a19b9f3f172193655116f
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.util.Scanner; public class BearAndStringDistance { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long k = in.nextLong(); in.nextLine(); String s = in.nextLine(); char[] ar = new char[n]; char c; int max; boolean nearZ = false; boolean nearA = false; for (int i = 0; i < n; i++) { nearA = false; nearZ = false; c = s.charAt(i); if ('z' - c > c - 'a') { max = 'z' - c; nearZ = true; } else { max = c - 'a'; nearA = true; } if (k >= max) { if (nearA) { ar[i] = 'a'; } else if (nearZ) { ar[i] = 'z'; } k = k - max; } else { if (c + k <= 'z') { ar[i] = (char) (c + k); } else if (c - k >= 'a') { ar[i] = (char) (c - k); } k = k - k; } } if (k > 0) { System.out.println(-1); } else { StringBuilder str = new StringBuilder(""); for (int i = 0; i < n; i++) { str.append(ar[i]); } System.out.println(str); } } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
3aa6b7c768a79510f647e624e86db5e8
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.util.Scanner; public class C { public static void main(String[] args){ Scanner in = new Scanner(System.in); int len = in.nextInt(); int dist = in.nextInt(); char[] ch = in.next().toCharArray(); StringBuilder sb = new StringBuilder(); for(int i = 0; i<ch.length; i++){ // System.out.println(dist); if(dist==0){ sb.append(ch[i]); } else{ int top = ch[i]-'a'; int bot = 'z'-ch[i]; if(top>bot){ if(dist>=top){ sb.append("a"); dist-=top; } else{ char now = (char)(ch[i]-dist); sb.append(now); dist = 0; } } else{ if(dist>=bot){ sb.append("z"); dist-=bot; } else{ char now = (char)(ch[i]+dist); dist = 0; sb.append(now); } } } } if(dist>0){ System.out.println(-1); } else{ System.out.println(sb.toString()); } } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
359e0d1f2b71d55e60c290e62cb62de7
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Limak { public static Integer[] parse(String st) { String[] a = st.split(" "); Integer[] ar = new Integer[a.length]; for (int i = 0; i < a.length; i++) ar[i] = Integer.parseInt(a[i]); return ar; } public static void solve(String s, int k) { StringBuffer out = new StringBuffer(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); int dmax = dmax(c); if (dmax > k) { out.append(shift(c, k)); k = k - k; } else { out.append(shift(c, dmax)); k = k - dmax; } } if (k == 0) { System.out.println(out.toString()); } else { System.out.println(-1); } } public static char shift(char c, int d) { char mid = ('a' + 'z') / 2; if (c > mid) { return (char) (c - d); } else { return (char) (c + d); } } public static int dmax(char c) { return Math.max( (c - 'a'), ('z' - c) ); } public static void main(String[] args) { try { InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); Integer[] p = parse(br.readLine()); solve(br.readLine(), p[1]); } catch (Exception e) {e.printStackTrace();} } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
7bb64bae930de9c6ed2674703d7e2944
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.util.Scanner; public class CodeforcesD { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int b = in.nextInt(); String c = in.next(); char[] s = c.toCharArray(); for(int i=0; i<s.length; i++){ if(s[i]-'a'>=b){ s[i]=(char) (s[i]-b); System.out.println(s); return; } if('z'-s[i]>=b){ s[i]=(char) (s[i]+b); System.out.println(s); return; } if(s[i]<='m'){ b-='z'-s[i]; s[i]='z'; } else{ b-=s[i]-'a'; s[i]='a'; } } System.out.println(-1); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
4d6a4e7ba9c1c1cbf0ce5b6b655136e3
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.*; import java.util.*; public final class string_dis { static FastScanner sc=new FastScanner(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter out=new PrintWriter(System.out); public static void main(String args[]) throws Exception { int n=sc.nextInt(),k=sc.nextInt(); char[] a=sc.next().toCharArray(),b=a; for(int i=0;i<a.length;i++) { //out.println(k); if(k==0) { break; } int maxdiff=-1,maxpos=-1; for(int j=97;j<=122;j++) { if(Math.abs(j-a[i])>maxdiff) { maxdiff=Math.abs(j-a[i]); maxpos=j; } } if(maxdiff>=0 && k>=maxdiff) { b[i]=(char)maxpos; k-=maxdiff; } else if(maxdiff>=0) { int val1=a[i]+k,val2=a[i]-k; if(val1>=97 && val1<=122) { b[i]=(char)val1; k-=k; } else if(val2>=97 && val2<=122) { b[i]=(char)val2; k-=k; } else { continue; } } } if(k==0) { out.println(new String(b)); } else { out.println("-1"); } out.close(); } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public String next() throws Exception { return nextToken().toString(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
8070ec25eef85cc90d95edb95139fce7
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.util.InputMismatchException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; QuickScanner in = new QuickScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, QuickScanner in, PrintWriter out) { int n=in.nextInt(); int k=in.nextInt(); char s[]=in.next().toCharArray(); char r[]=new char[n]; for(int i=0;i<n;i++) { char to='a'; int dc=-1; int df=Math.abs(to-s[i]); if(Math.abs('z'-s[i])>df) { to='z'; dc=1; df=Math.abs(to-s[i]); } df=Math.min(df, k); r[i]= (char) (s[i]+dc*df); k-=df; } if(k>0) out.println(-1); else out.println(new String(r)); } } class QuickScanner { BufferedReader in; StringTokenizer token; String delim; public QuickScanner(InputStream inputStream) { this.in=new BufferedReader(new InputStreamReader(inputStream)); this.delim=" \n\t"; this.token=new StringTokenizer("",delim); } public boolean hasNext() { while(!token.hasMoreTokens()) { try { String s=in.readLine(); if(s==null) return false; token=new StringTokenizer(s,delim); } catch (IOException e) { throw new InputMismatchException(); } } return true; } public String next() { hasNext(); return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
b3d59baec51967aed2e5b42cd07f6f08
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n=in.nextInt(); int k=in.nextInt(); int a[]=new int [n]; String s=in.next(); int max1=0; for(int i=0;i<n;i++){ a[i]=s.charAt(i)-'a'; max1+=Math.max(25-a[i], a[i]-0); } if(k>max1)System.out.println(-1); else{int i=0; while(k>0){ int max=Math.max(25-a[i], a[i]-0); if(k>=max){a[i]=(25-a[i]>a[i])?25:0; k-=max; } else { if(a[i]+k<=25){a[i]+=k;k=0;} else if(a[i]-k>0){ a[i]-=k; k=0; } } i++; } for(int q:a)System.out.print((char)(q+'a')); System.out.println(); } } }
Java
["4 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
687e5a59302322beb4bb31ba5dd5ee64
train_000.jsonl
1455894000
Limak is a little polar bear. He likes nice strings — strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print "-1" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class ProblemC { public static void main(String[] args) { InputReader in = new InputReader(); PrintWriter out = new PrintWriter(System.out); new ProblemC().solve(in, out); out.close(); } public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); int k = in.nextInt(); String s = in.next(); long max = 0l; for (int i = 0; i < n; i++) { int d1 = Math.abs(s.charAt(i) - 'a'); int d2 = Math.abs(s.charAt(i) - 'z'); max += Math.max(d1, d2); } if (k > max) { out.println(-1); return; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { int d1 = Math.abs(s.charAt(i) - 'a'); int d2 = Math.abs(s.charAt(i) - 'z'); char c; int m; if (d1 >= d2) { m = d1; c = 'a'; } else { m = d2; c = 'z'; } if (k >= m) { k -= m; sb.append(c); } else if (k == 0) { sb.append(s.charAt(i)); } else { if (s.charAt(i) - 'a' - k >= 0) { sb.append((char)(s.charAt(i) - k)); } else { sb.append((char)(s.charAt(i) + k)); } k = 0; } } out.println(sb.toString()); } static class InputReader { public BufferedReader br; public StringTokenizer st; public InputReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public 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 26\nbear", "2 7\naf", "3 1000\nhey"]
1 second
["roar", "db", "-1"]
null
Java 7
standard input
[ "greedy", "strings" ]
b5d0870ee99e06e8b99c74aeb8e81e01
The first line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 106). The second line contains a string s of length n, consisting of lowercase English letters.
1,300
If there is no string satisfying the given conditions then print "-1" (without the quotes). Otherwise, print any nice string s' that .
standard output
PASSED
ee8ec0d829f72dba6b477251666e8b48
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
//package Practice_Problems; import java.util.*; import java.io.*; public class uniqueness { static int arr[],pref[],suf[]; static Set<Integer> set; static final int mod=(int)1e9+7; static final int inf=(int)1e9; static final long INF=(long)1e18; public static void main(String[] args) { FastScanner fs=new FastScanner(); int n=fs.nextInt(); arr=new int[n]; pref=new int[n]; set=new HashSet<>(); for(int i=0;i<n;i++) { arr[i]=fs.nextInt(); set.add(arr[i]); pref[i]=set.size(); } int left=0,right=n; int ans=inf; while(left<=right) { int mid=(left+right)/2; //System.out.println(mid+" "+isValid(mid)); if(isValid(mid)) { ans=Math.min(ans,mid); right=mid-1; } else left=mid+1; } System.out.println(ans); } public static boolean isValid(int mid) { for(int i=0;i<=arr.length-mid;i++) { Set<Integer> set2 = new HashSet<>(); if(i==0) { for(int j=i+mid;j<=arr.length-1;j++) { set2.add(arr[j]); } if(set2.size()==arr.length-mid)return true; } else { for(int k=0;k<i;k++)set2.add(arr[k]); for(int j=i+mid;j<=arr.length-1;j++)set2.add(arr[j]); if(set2.size()==arr.length-mid)return true; } } return false; } } class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int[] sort(int arr[]) { ArrayList<Integer> list = new ArrayList<Integer>(); for(int i:arr)list.add(i); Collections.sort(list); for(int i=0;i<arr.length;i++) { arr[i]=list.get(i); } return arr; } char[] charsort(char arr[]) { ArrayList<Character> list = new ArrayList<>(); for(char c:arr)list.add(c); Collections.sort(list); for(int i=0;i<list.size();i++) { arr[i]=list.get(i); } return arr; } long[] longsort(long arr[]) { ArrayList<Long> list = new ArrayList<Long>(); for(long i:arr)list.add(i); Collections.sort(list); for(int i=0;i<arr.length;i++) { arr[i]=list.get(i); } return arr; } public int nextInt() { return Integer.parseInt(next()); } public int[] readArray(int n) { int[] arr=new int[n]; for (int i=0; i<n; i++) arr[i]=nextInt(); return arr; } public long nextLong() { return Long.parseLong(next()); } public long[] longreadArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
ee42dfee5febecdad6acb485c79a4aa5
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
//package Practice_Problems; import java.util.*; import java.io.*; public class uniqueness { static int arr[],pref[],suf[]; static Set<Integer> set; static final int mod=(int)1e9+7; static final int inf=(int)1e9; static final long INF=(long)1e18; public static void main(String[] args) { FastScanner fs=new FastScanner(); Scanner in=new Scanner(System.in); int n=in.nextInt(); arr=new int[n]; pref=new int[n]; set=new HashSet<>(); for(int i=0;i<n;i++) { arr[i]=in.nextInt(); set.add(arr[i]); pref[i]=set.size(); } int left=0,right=n; int ans=inf; while(left<=right) { int mid=(left+right)/2; //System.out.println(mid+" "+isValid(mid)); if(isValid(mid)) { ans=Math.min(ans,mid); right=mid-1; } else left=mid+1; } System.out.println(ans); } public static boolean isValid(int mid) { for(int i=0;i<=arr.length-mid;i++) { Set<Integer> set2 = new HashSet<>(); if(i==0) { for(int j=i+mid;j<=arr.length-1;j++) { set2.add(arr[j]); } if(set2.size()==arr.length-mid)return true; } else { for(int k=0;k<i;k++)set2.add(arr[k]); for(int j=i+mid;j<=arr.length-1;j++)set2.add(arr[j]); if(set2.size()==arr.length-mid)return true; } } return false; } } class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int[] sort(int arr[]) { ArrayList<Integer> list = new ArrayList<Integer>(); for(int i:arr)list.add(i); Collections.sort(list); for(int i=0;i<arr.length;i++) { arr[i]=list.get(i); } return arr; } char[] charsort(char arr[]) { ArrayList<Character> list = new ArrayList<>(); for(char c:arr)list.add(c); Collections.sort(list); for(int i=0;i<list.size();i++) { arr[i]=list.get(i); } return arr; } long[] longsort(long arr[]) { ArrayList<Long> list = new ArrayList<Long>(); for(long i:arr)list.add(i); Collections.sort(list); for(int i=0;i<arr.length;i++) { arr[i]=list.get(i); } return arr; } public int nextInt() { return Integer.parseInt(next()); } public int[] readArray(int n) { int[] arr=new int[n]; for (int i=0; i<n; i++) arr[i]=nextInt(); return arr; } public long nextLong() { return Long.parseLong(next()); } public long[] longreadArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
e2633d0b3a28e874980ef5b8f93e3bbb
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(); int a[] = new int[n]; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { a[i] = ni(); map.put(a[i], map.getOrDefault(a[i], 0) + 1); } Map<Integer, Integer> temp1 = new HashMap<Integer, Integer>(); temp1.putAll(map); for (int k : temp1.keySet()) { if (temp1.get(k) == 1) { map.remove(k); } } /*for(int k: map.keySet()){ out.println(k+" "+map.get(k)); } out.println(map.size());*/ Map<Integer, Integer> temp = new HashMap<Integer, Integer>(); int c = 0; int ans = 0; int min = Integer.MAX_VALUE; if (map.size() > 0) { for (int i = 0; i < n; i++) { temp.putAll(map); c = 0; for (int j = i; j < n; j++) { if (temp.containsKey(a[j])) { temp.put(a[j], temp.get(a[j]) - 1); if (temp.get(a[j]) == 1) { c++; //out.println(c); } if (c == map.size()) { ans = j - i + 1; break; } } } if (ans < min) { min = ans; } } } else { min = 0; } out.println((min == Integer.MAX_VALUE) ? 0 : min); } void run() throws Exception { is = System.in;//oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Codeforces().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) { throw new InputMismatchException(); } if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) { return -1; } } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) { map[i] = ns(m); } return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if (!oj) { System.out.println(Arrays.deepToString(o)); } } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
dd48d97d9fd00d7f407eff8574da91e4
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashSet; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author unknown */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); BUniqueness solver = new BUniqueness(); solver.solve(1, in, out); out.close(); } static class BUniqueness { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int[] arr = in.nextIntArray(n); int min = n - 1; for (int i = 0; i < n; i++) { HashSet<Integer> hs = new HashSet<>(); boolean works = true; for (int k = 0; k < i; k++) { if (hs.contains(arr[k])) { works = false; break; } else { hs.add(arr[k]); } } if (!works) { break; // continue; } int goodS = n; for (int k = n - 1; k >= i; k--) { if (hs.contains(arr[k])) { break; } else { goodS = k; hs.add(arr[k]); } } min = _F.min(min, goodS - i); } out.println(min); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int startFrom) { int[] array = new int[n]; for (int i = startFrom; i < n; ++i) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(long i) { writer.println(i); } } static class _F { public static <T extends Comparable<T>> T min(T... list) { T candidate = list[0]; for (T i : list) { if (candidate.compareTo(i) > 0) { candidate = i; } } return candidate; } } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
29f6d24e2cbad81ae7e31ca65c998183
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.io.*; import java.util.*; import java.util.regex.*; public class UNT { public static void main(String[]stp) throws Exception { Scanner scan=new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()),i,j,min=Integer.MAX_VALUE; Integer a[]=new Integer[n]; st=new StringTokenizer(br.readLine()); for(i=0;i<n;i++) a[i]=Integer.parseInt(st.nextToken()); for(i=0;i<n;i++) { HashSet ms=new HashSet(); int count=0; for(j=0;j<i;j++) { if(ms.contains(a[j])) break; else { ms.add(a[j]); count++; } } for(j=n-1;j>=0;j--) { if(ms.contains(a[j])) break; else { ms.add(a[j]); count++; } } min=Math.min(min,n-count); } pw.println(min); pw.flush(); } public static class MultiSet { class Pair { public int first; public int second; Pair() {} Pair(int a, int b) { first = a; second = b; } } TreeSet<Pair> ts = new TreeSet( new Comparator<Pair>() { public int compare(Pair p1, Pair p2) { return p1.first - p2.first; } } ); int sz = 0; public void add(int x) { if (contains(x)) { Pair t = ts.ceiling(new Pair(x, 1)); t.second++; } else { ts.add(new Pair(x, 1)); } sz++; } public boolean contains(int x) { return ts.contains(new Pair(x, 1)); } public void remove(int x) { if (contains(x)) { Pair p = ts.ceiling(new Pair(x, 1)); if (p.second > 1) { p.second--; } else { ts.remove(p); } sz--; } } public int count(int x) { if (contains(x)) { return ts.ceiling(new Pair(x, 1)).second; } return 0; } public int ceiling(int x) { return ts.ceiling(new Pair(x, 1)).first; } public int first() { return ts.first().first; } public int last() { return ts.last().first; } public int pollFirst() { int x; remove(x = first()); return x; } public int pollLast() { int x; remove(x = last()); return x; } public int size() { return sz; } public void clear() { ts.clear(); sz = 0; } } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
0b6f5707ee8fd8fc7c2c5dd50b09f333
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; /** * @author AnonymousP * @__WHEN YOU FEEL LIKE QUITTING, THINK ABOUT WHY YOU STARTED__@ */ //COMBINATON = nCr = n*(n-1)/2 public class uniqueness { public static void main(String[] args) throws IOException { int n = sc.nextInt(); int a[] = new int[n]; int min = 10000000; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { //TreeSet<Integer> hs = new TreeSet<>(); HashSet<Integer> hs = new HashSet<>(); for (int k = 0; k < i; k++) { // prln(i); if (!hs.contains(a[k])) { hs.add(a[k]); } else { break; } } for (int j = n - 1; j >= 0; j--) { if (!hs.contains(a[j])) { hs.add(a[j]); } else { break; } } min = Math.min(min, n - hs.size()); } prln(min); close(); } static FastReader sc = new FastReader(); static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer input; static Random __rand = new Random(); // input //*******FAST IO*************FAST IO***************FAST IO****************// 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; } } // output static void pr(int i) { __out.print(i); } static void prln(int i) { __out.println(i); } static void pr(long l) { __out.print(l); } static void prln(long l) { __out.println(l); } static void pr(double d) { __out.print(d); } static void prln(double d) { __out.println(d); } static void pr(char c) { __out.print(c); } static void prln(char c) { __out.println(c); } static void pr(char[] s) { __out.print(new String(s)); } static void prln(char[] s) { __out.println(new String(s)); } static void pr(String s) { __out.print(s); } static void prln(String s) { __out.println(s); } static void pr(Object o) { __out.print(o); } static void prln(Object o) { __out.println(o); } static void prln() { __out.println(); } static void pryes() { __out.println("yes"); } static void pry() { __out.println("Yes"); } static void prY() { __out.println("YES"); } static void prno() { __out.println("no"); } static void prn() { __out.println("No"); } static void prN() { __out.println("NO"); } static void pryesno(boolean b) { __out.println(b ? "yes" : "no"); } ; static void pryn(boolean b) { __out.println(b ? "Yes" : "No"); } static void prYN(boolean b) { __out.println(b ? "YES" : "NO"); } static void prln(int... a) { for (int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]); } static void prln(long... a) { for (int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]); } static <T> void prln(Collection<T> c) { int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if (n >= 0) { __out.println(iter.next()); } else { __out.println(); } } static void h() { __out.println("hlfd"); flush(); } static void flush() { __out.flush(); } static void close() { __out.close(); } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
07cdbaa27568672f9f35742f7f62faa4
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.io.*; import java.util.*; import java.util.regex.*; public class UNT { public static void main(String[] stp) throws Exception { Scanner scan = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()), i, j, min = Integer.MAX_VALUE; Integer a[] = new Integer[n]; st = new StringTokenizer(br.readLine()); for (i = 0; i < n; i++) { a[i] = Integer.parseInt(st.nextToken()); } for (i = 0; i < n; i++) { HashSet hs = new HashSet(); // int count=0; for (j = 0; j < i; j++) { if (hs.contains(a[j])) { break; } else { hs.add(a[j]); } } for (j = n - 1; j >= 0; j--) { if (hs.contains(a[j])) { break; } else { hs.add(a[j]); } } min = Math.min(min, n - hs.size()); } pw.println(min); pw.flush(); } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
8169bbb42d4c0083b45dbf1525cca068
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.HashMap; import java.io.IOException; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); BUniqueness solver = new BUniqueness(); solver.solve(1, in, out); out.close(); } static class BUniqueness { public void solve(int testNumber, Scanner sc, PrintWriter pw) { int n = sc.nextInt(); int[] arr = new int[n]; HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); map.put(arr[i], map.getOrDefault(arr[i], 0) + 1); } int countOnes = 0; for (int x : map.values()) { if (x == 1) countOnes++; } int min = Integer.MAX_VALUE; if (countOnes == n) min = 0; for (int i = 0; i < n; i++) { int temp = countOnes; HashMap<Integer, Integer> count = (HashMap<Integer, Integer>) map.clone(); for (int j = i; j < n; j++) { count.replace(arr[j], count.get(arr[j]) - 1); if (count.get(arr[j]) == 1) { temp++; } else if (count.get(arr[j]) == 0) { temp--; } if (temp == n - (j - i + 1)) { min = Math.min(j - i + 1, min); } } } pw.println(min); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
53cb44d60009f382fd799ab09a1145de
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.TreeMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int f = 0; int[] a = new int[n]; TreeMap<Integer, Integer> map = new TreeMap<>(); for(int i = 0 ; i < n ; i++) { a[i] = scanner.nextInt(); if(map.containsKey(a[i])) { map.put(a[i], map.get(a[i]) + 1); if(map.get(a[i]) == 2) ++f; } else map.put(a[i], 1); } int ans = (f != 0 ? Integer.MAX_VALUE : 0); for(int i = 0, j ; i < n ; i++) { for(j = i ; j < n ; j++) { map.put(a[j], map.get(a[j]) - 1); if(map.get(a[j]) == 1) --f; if(f == 0) break; } if(j != n) ans = Math.min(ans, j - i + 1); else j = n - 1; while(j >= i) { map.put(a[j], map.get(a[j]) + 1); if(map.get(a[j]) == 2) ++f; --j; } } System.out.println(ans); scanner.close(); } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
5679550e203c3f2835b470b566c77637
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); HashMap<Integer,Integer> hm=new HashMap<>(); Set<Integer> hs=new HashSet<>(); int count=0; int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); if(!hm.containsKey(a[i])) { hm.put(a[i],1); }else { hs.add(a[i]); hm.put(a[i],hm.get(a[i])+1); count++; } } if(hs.size()==0) { System.out.println(0); System.exit(0); } int res=n; for(int i=0;i<n;i++) { HashMap<Integer,Integer> hm1=new HashMap<>(); int to=0; int j=i; for(;j<n;j++) { hm1.put(a[j],hm1.getOrDefault(a[j],0)+1); if(hs.contains(a[j]) && hm1.get(a[j])-1<hm.get(a[j])-1) to++; if(to==count) break; } if(to==count) res=Math.min(res,j-i+1); } System.out.println(res); } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
747554d4a6b02a79afacd068e66d7c09
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.util.*; public class Uniqueness { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = s.nextInt(); HashSet<Integer> hs = new HashSet<>(); int l = 0; for (; l < n; l++) { if (!hs.contains(a[l])) hs.add(a[l]); else break; } int r = a.length - 1; for (; r > l; r--) { if (!hs.contains(a[r])) hs.add(a[r]); else break; } int max = hs.size(); if (max == n) { System.out.println(0); return; } while (--l >= 0) { hs.remove(a[l]); while (!hs.contains(a[r])) { hs.add(a[r]); r--; } if (max < hs.size()) { max = hs.size(); } } System.out.println(n - max); s.close(); } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
08459b4a8b7f8422eaaace68fb05d48a
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.io.*; import java.util.*; public class A implements Runnable { boolean judge = false; FastReader scn; PrintWriter out; String INPUT = "10\r\n" + "1 2 3 4 5 5 5 6 6 6"; void solve() { int n = scn.nextInt(); int[] arr = scn.nextIntArray(n); HashMap<Integer, Integer> map = new HashMap<>(); HashSet<Integer> set = new HashSet<>(); int cnt = 0; for (int i = 0; i < n; i++) { if (!map.containsKey(arr[i])) map.put(arr[i], 1); else { map.put(arr[i], map.get(arr[i]) + 1); set.add(arr[i]); cnt++; } } if (set.size() == 0) { out.println(0); return; } int ans = n; for (int i = 0; i < n; i++) { HashMap<Integer, Integer> map1 = new HashMap<>(); int total = 0, j = i; for (; j < n; j++) { map1.put(arr[j], map1.getOrDefault(arr[j], 0) + 1); if (set.contains(arr[j]) && map1.get(arr[j]) - 1 < map.get(arr[j]) - 1) total++; if (total == cnt) break; } if (total == cnt) ans = Math.min(ans, j - i + 1); } out.println(ans); } public void run() { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null || judge; out = new PrintWriter(System.out); scn = new FastReader(oj); solve(); out.flush(); if (!oj) { System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } } public static void main(String[] args) { new Thread(null, new A(), "Main", 1 << 28).start(); } class FastReader { InputStream is; public FastReader(boolean onlineJudge) { is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes()); } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return (char) skip(); } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } char[][] nextMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[][] next2DInt(int n, int m) { int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { arr[i] = nextIntArray(m); } return arr; } long[][] next2DLong(int n, int m) { long[][] arr = new long[n][]; for (int i = 0; i < n; i++) { arr[i] = nextLongArray(m); } return arr; } int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); int c = arr[i]; arr[i] = arr[j]; arr[j] = c; } return arr; } long[] shuffle(long[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); long c = arr[i]; arr[i] = arr[j]; arr[j] = c; } return arr; } int[] uniq(int[] arr) { arr = scn.shuffle(arr); Arrays.sort(arr); int[] rv = new int[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } long[] uniq(long[] arr) { arr = scn.shuffle(arr); Arrays.sort(arr); long[] rv = new long[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } int[] reverse(int[] arr) { int l = 0, r = arr.length - 1; while (l < r) { arr[l] = arr[l] ^ arr[r]; arr[r] = arr[l] ^ arr[r]; arr[l] = arr[l] ^ arr[r]; l++; r--; } return arr; } long[] reverse(long[] arr) { int l = 0, r = arr.length - 1; while (l < r) { arr[l] = arr[l] ^ arr[r]; arr[r] = arr[l] ^ arr[r]; arr[l] = arr[l] ^ arr[r]; l++; r--; } return arr; } int[] compress(int[] arr) { int n = arr.length; int[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } long[] compress(long[] arr) { int n = arr.length; long[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
0a4bc9636996c096724a3d3deee81b82
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Ideone { public static void main (String[] args) throws java.lang.Exception { Scanner s = new Scanner(System.in); int n = s.nextInt(); Set<Long> set = new LinkedHashSet<Long>(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } int ans = n - 1; for (int j = 0; j < n; j++) { boolean flag = true; int k = 0; for (int i = 0; i < j; i++) { set.add(arr[i]); if (set.size() == k) { flag = false; break; } k = set.size(); } int su = n; for (int i = n - 1; i >= j; i--) { if (set.contains(arr[i])) { break; } else { set.add(arr[i]); su = i; } } if (flag) { ans = Math.min(ans, su - j); } set.clear(); } System.out.println(ans); } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
f66c5e406999c82c364ed31ecf8a1acd
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Ideone { public static void main (String[] args) throws java.lang.Exception { Scanner s = new Scanner(System.in); int n = s.nextInt(); Set<Long> set = new TreeSet<Long>(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } int ans = n - 1; for (int j = 0; j < n; j++) { boolean flag = true; int k = 0; for (int i = 0; i < j; i++) { set.add(arr[i]); if (set.size() == k) { flag = false; break; } k = set.size(); } int su = n; for (int i = n - 1; i >= j; i--) { if (set.contains(arr[i])) { break; } else { set.add(arr[i]); su = i; } } if (flag) { ans = Math.min(ans, su - j); } set.clear(); } System.out.println(ans); } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
23225e75a9ecf6297b30e3f7545cb0fe
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Ideone { public static void main (String[] args) throws java.lang.Exception { Scanner s = new Scanner(System.in); int n = s.nextInt(); Set<Long> set = new HashSet<Long>(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } int ans = n - 1; for (int j = 0; j < n; j++) { boolean flag = true; int k = 0; for (int i = 0; i < j; i++) { set.add(arr[i]); if (set.size() == k) { flag = false; break; } k = set.size(); } int su = n; for (int i = n - 1; i >= j; i--) { if (set.contains(arr[i])) { break; } else { set.add(arr[i]); su = i; } } if (flag) { ans = Math.min(ans, su - j); } set.clear(); } System.out.println(ans); } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
c430f6234bd3699c174780844ac14ee1
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.InputMismatchException; import java.io.BufferedOutputStream; import java.util.HashSet; import java.util.StringTokenizer; import java.io.Flushable; import java.io.OutputStream; import java.io.PrintStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Closeable; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; Input in = new Input(inputStream); Output out = new Output(outputStream); BUniqueness solver = new BUniqueness(); solver.solve(1, in, out); out.close(); } } public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1<<29); thread.start(); thread.join(); } static class BUniqueness { private final int INF = 1_000_000_000; public BUniqueness() { } public void solve(int kase, Input in, Output pw) { int n = in.nextInt(); int[] arr = new int[n]; for(int i = 0; i<n; i++) { arr[i] = in.nextInt(); } int ans = INF; HashSet<Integer> soFar = new HashSet<>(); for(int i = 0; i<n; i++) { HashSet<Integer> cur = new HashSet<>(soFar); int j; for(j = n-1; j>=0&&j>=i; j--) { if(cur.contains(arr[j])) { break; } cur.add(arr[j]); } ans = Math.min(ans, j-i+1); Utilities.Debug.dbg(i, j); if(soFar.contains(arr[i])) { break; } soFar.add(arr[i]); } pw.println(ans); } } static class Input { BufferedReader br; StringTokenizer st; public Input(InputStream is) { this(is, 1<<20); } public Input(InputStream is, int bs) { br = new BufferedReader(new InputStreamReader(is), bs); st = null; } public boolean hasNext() { try { while(st==null||!st.hasMoreTokens()) { String s = br.readLine(); if(s==null) { return false; } st = new StringTokenizer(s); } return true; }catch(Exception e) { return false; } } public String next() { if(!hasNext()) { throw new InputMismatchException(); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } static class Utilities { public static class Debug { public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null; private static <T> String ts(T t) { if(t==null) { return "null"; } try { return ts((Iterable) t); }catch(ClassCastException e) { if(t instanceof int[]) { String s = Arrays.toString((int[]) t); return "{"+s.substring(1, s.length()-1)+"}"; }else if(t instanceof long[]) { String s = Arrays.toString((long[]) t); return "{"+s.substring(1, s.length()-1)+"}"; }else if(t instanceof char[]) { String s = Arrays.toString((char[]) t); return "{"+s.substring(1, s.length()-1)+"}"; }else if(t instanceof double[]) { String s = Arrays.toString((double[]) t); return "{"+s.substring(1, s.length()-1)+"}"; }else if(t instanceof boolean[]) { String s = Arrays.toString((boolean[]) t); return "{"+s.substring(1, s.length()-1)+"}"; } try { return ts((Object[]) t); }catch(ClassCastException e1) { return t.toString(); } } } private static <T> String ts(T[] arr) { StringBuilder ret = new StringBuilder(); ret.append("{"); boolean first = true; for(T t: arr) { if(!first) { ret.append(", "); } first = false; ret.append(ts(t)); } ret.append("}"); return ret.toString(); } private static <T> String ts(Iterable<T> iter) { StringBuilder ret = new StringBuilder(); ret.append("{"); boolean first = true; for(T t: iter) { if(!first) { ret.append(", "); } first = false; ret.append(ts(t)); } ret.append("}"); return ret.toString(); } public static void dbg(Object... o) { if(LOCAL) { System.err.print("Line #"+Thread.currentThread().getStackTrace()[2].getLineNumber()+": ["); for(int i = 0; i<o.length; i++) { if(i!=0) { System.err.print(", "); } System.err.print(ts(o[i])); } System.err.println("]"); } } } } static class Output implements Closeable, Flushable { public StringBuilder sb; public OutputStream os; public int BUFFER_SIZE; public boolean autoFlush; public String LineSeparator; public Output(OutputStream os) { this(os, 1<<16); } public Output(OutputStream os, int bs) { BUFFER_SIZE = bs; sb = new StringBuilder(BUFFER_SIZE); this.os = new BufferedOutputStream(os, 1<<17); autoFlush = false; LineSeparator = System.lineSeparator(); } public void println(int i) { println(String.valueOf(i)); } public void println(String s) { sb.append(s); println(); if(autoFlush) { flush(); }else if(sb.length()>BUFFER_SIZE >> 1) { flushToBuffer(); } } public void println() { sb.append(LineSeparator); } private void flushToBuffer() { try { os.write(sb.toString().getBytes()); }catch(IOException e) { e.printStackTrace(); } sb = new StringBuilder(BUFFER_SIZE); } public void flush() { try { flushToBuffer(); os.flush(); }catch(IOException e) { e.printStackTrace(); } } public void close() { flush(); try { os.close(); }catch(IOException e) { e.printStackTrace(); } } } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
e3e2c7fd0829ae52b09e912f2e95c1da
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.io.*; import java.util.*; public class Main{ static int mod=(int)1e9+7; public static void main(String[] args) throws IOException { Writer out=new Writer(System.out); Reader in=new Reader(System.in); int ts=1; outer: while(ts-->0) { int n=in.nextInt(); int a[]=in.readArray(n); int ans=n-1; for(int i=0; i<=n; i++) { //i is the length of the prefix, it can be 0 or n boolean valid_prefix=true; Set<Integer> set=new HashSet<>(); for(int j=0; j<i; j++) { if(set.contains(a[j])) { valid_prefix=false; break; }else { set.add(a[j]); } } int max_length_suffix=0; for(int j=n-1; j>=i; j--) { if(set.contains(a[j])) break; set.add(a[j]); max_length_suffix++; } if(valid_prefix) ans=Math.min(ans, n-i-max_length_suffix); } out.println(ans); } out.close(); } /*********************************** UTILITY CODE BELOW **************************************/ static int abs(int a) { return a>0 ? a : -a; } static int max(int a, int b) { return a>b ? a : b; } static long pow(long n, long m) { if(m==0) return 1; long temp=pow(n,m/2); long res=((temp*temp)%mod); if(m%2==0) return res; return (res*n)%mod; } static class Pair{ int u, v; Pair(int u, int v){this.u=u; this.v=v;} static void sort(Pair [] coll) { List<Pair> al=new ArrayList<>(Arrays.asList(coll)); Collections.sort(al,new Comparator<Pair>() { public int compare(Pair p1, Pair p2) { return p1.u-p2.u; } }); for(int i=0; i<al.size(); i++) { coll[i]=al.get(i); } } } static void sort(int[] a) { ArrayList<Integer> list=new ArrayList<>(); for (int i:a) list.add(i); Collections.sort(list); for (int i=0; i<a.length; i++) a[i]=list.get(i); } static void sort(long a[]) { ArrayList<Long> list=new ArrayList<>(); for(long i: a) list.add(i); Collections.sort(list); for(int i=0; i<a.length; i++) a[i]=list.get(i); } static int [] array(int n, int value) { int a[]=new int[n]; for(int i=0; i<n; i++) a[i]=value; return a; } static class Reader{ BufferedReader br; StringTokenizer to; Reader(InputStream stream){ br=new BufferedReader(new InputStreamReader(stream)); to=new StringTokenizer(""); } String next() { while(!to.hasMoreTokens()) { try { to=new StringTokenizer(br.readLine()); }catch(IOException e) {} } return to.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]=nextInt(); return a; } long [] readLongArray(int n) { long a[]=new long[n]; for(int i=0; i<n ;i++) a[i]=nextLong(); return a; } } static class Writer extends PrintWriter{ Writer(OutputStream stream){ super(stream); } void println(int [] array) { for(int i=0; i<array.length; i++) { print(array[i]+" "); } println(); } void println(long [] array) { for(int i=0; i<array.length; i++) { print(array[i]+" "); } println(); } } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
d469fa66783b602556abd30ffc666970
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.io.*; import java.util.*; // CODE JAM SHIT public class Q2 { public static void main(String[] args) { InputReader in = new InputReader(true); PrintWriter out = new PrintWriter(System.out); int N =in.nextInt(); int arr[]=new int[N+N]; for(int i =0;i<N;i++) arr[i]=in.nextInt(); for(int i=N;i<2*N;i++) arr[i]=arr[i%N]; HashMap<Integer,Integer> set=new HashMap<>(); int ans =0,l=0; for(int i =0;i<2*N;i++){ if(set.size()==N){ ans=N; break; } if(!set.containsKey(arr[i])){ set.put(arr[i],1); if(l==0 || (l<N && i>=(N-1))) ans=Math.max(ans,set.size()); }else{ while(l<i){ if(set.get(arr[l])==1) set.remove(arr[l]); else set.put(arr[l],set.get(arr[l])-1); l++; if(!set.containsKey(arr[i])){ set.put(arr[i],1); if(l==0 || (l<N && i>=(N-1))) ans=Math.max(ans,set.size()); break; } } } } out.println(N-ans); out.close(); } // public static void main(String[] args) { // // InputReader in = new InputReader(); // PrintWriter out = new PrintWriter(System.out); // // int t = in.nextInt(), l = 0; // // while (t-- > 0) { // // out.print("Case #" + (++l) + ": "); // // int N = in.nextInt(), x = in.nextInt(), max = 0; // HashMap<Double, Integer> map = new HashMap<>(); // double arr[] = new double[N]; // // for (int i = 0; i < N; i++) { // arr[i] = in.nextDouble(); // map.put(arr[i], map.getOrDefault(arr[i], 0) + 1); // max = Math.max(max, map.get(arr[i])); // } // // if (max >= x) // out.println(0); // else if (x == 2) // out.println(1); // else { // // int flag = 0; // for (int i = 0; i < N; i++) { // if (arr[i] % 2 == 0) { // if (map.containsKey(arr[i] / 2)) // flag = 1; // } // } // // if (N == 1) // out.println(2); // else if (flag == 1) // out.println(1); // else{ // // double min = Integer.MAX_VALUE; // for (int i = 0; i < N; i++) { // if (map.get(arr[i]) == 2) { // min = Math.min(min, arr[i]); // } // } // // if (min < Integer.MAX_VALUE) { // for (int i = 0; i < N; i++) { // if (arr[i] > min) // flag = 1; // } // } // // if (flag == 1 && max!=1) // out.println(1); // else // out.println(2); // } // } // } // out.close(); // // } static class InputReader { InputStream is; public InputReader(boolean onlineJudge) { is = System.in; } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return (char) skip(); } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } char[][] nextMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[][] next2DInt(int n, int m) { int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { arr[i] = nextIntArray(m); } return arr; } long[][] next2DLong(int n, int m) { long[][] arr = new long[n][]; for (int i = 0; i < n; i++) { arr[i] = nextLongArray(m); } return arr; } int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); int c = arr[i]; arr[i] = arr[j]; arr[j] = c; } return arr; } long[] shuffle(long[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); long c = arr[i]; arr[i] = arr[j]; arr[j] = c; } return arr; } int[] uniq(int[] arr) { Arrays.sort(arr); int[] rv = new int[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } long[] uniq(long[] arr) { Arrays.sort(arr); long[] rv = new long[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } int[] reverse(int[] arr) { int l = 0, r = arr.length - 1; while (l < r) { arr[l] = arr[l] ^ arr[r]; arr[r] = arr[l] ^ arr[r]; arr[l] = arr[l] ^ arr[r]; l++; r--; } return arr; } long[] reverse(long[] arr) { int l = 0, r = arr.length - 1; while (l < r) { arr[l] = arr[l] ^ arr[r]; arr[r] = arr[l] ^ arr[r]; arr[l] = arr[l] ^ arr[r]; l++; r--; } return arr; } int[] compress(int[] arr) { int n = arr.length; int[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } long[] compress(long[] arr) { int n = arr.length; long[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } void deepFillInt(Object array, int val) { if (!array.getClass().isArray()) { throw new IllegalArgumentException(); } if (array instanceof int[]) { int[] intArray = (int[]) array; Arrays.fill(intArray, val); } else { Object[] objArray = (Object[]) array; for (Object obj : objArray) { deepFillInt(obj, val); } } } void deepFillLong(Object array, long val) { if (!array.getClass().isArray()) { throw new IllegalArgumentException(); } if (array instanceof long[]) { long[] intArray = (long[]) array; Arrays.fill(intArray, val); } else { Object[] objArray = (Object[]) array; for (Object obj : objArray) { deepFillLong(obj, val); } } } } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
1923a4d508e264cfd387fbad12a592d1
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.util.*; import java.io.*; public class File { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Try each prefix that does not contain any duplicates. // Extend the suffix to the longest possible. int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int result = n; // Expand a prefix of unique vals only. Set<Integer> prefixes = new HashSet<>(); for (int i = 0; i < n; i++) { Set<Integer> suffixes = new HashSet<>(); int min = n; for (int j = n-1; j >= i; j--) { int sVal = arr[j]; if (prefixes.contains(sVal) || suffixes.contains(sVal)) { break; } min = j; suffixes.add(sVal); } result = Math.min(result, min - i); int val = arr[i]; // Expanding prefix no longer is unique. if (prefixes.contains(val)) { break; } prefixes.add(val); } System.out.println(result); } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
5c07a7bcb6d6c0eabfa0fcfc6c4d006d
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; int len = n; for (int j = 0; j < n; j++) { a[j] = sc.nextInt(); } HashMap<Integer, Integer> cnt = new HashMap<Integer, Integer>(); int start = 1; int end = n; for (int i = 0; i < n; i++) { start = 1; end=n; for (int j = 0; j < i; j++) { if(!cnt.containsKey(a[j])){ cnt.put(a[j],1); }else { cnt.put(a[j], cnt.get(a[j]) + 1); } if (cnt.get(a[j]) > 1) { start = 0; break; } } for (int j = n - 1; j >= i; --j) { //System.out.println(cnt); if(!cnt.containsKey(a[j])){ cnt.put(a[j],1); }else { cnt.put(a[j], cnt.get(a[j]) + 1); } if (cnt.get(a[j]) == 1) { end = j; } else break; } if (start == 1) { len = Math.min(len, end - i); } cnt.clear(); } System.out.println(len); } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
7b702dee4f3200c1080a39f3abf2961c
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; int len = n; for (int j = 0; j < n; j++) { a[j] = sc.nextInt(); } HashMap<Integer, Integer> cnt = new HashMap<Integer, Integer>(); int start = 1; int end = n; for (int i = 0; i < n; i++) { start = 1; end=n; for (int j = 0; j < i; j++) { if(!cnt.containsKey(a[j])){ cnt.put(a[j],1); }else { cnt.put(a[j], cnt.get(a[j]) + 1); } if (cnt.get(a[j]) > 1) { start = 0; break; } } for (int j = n - 1; j >= i; j--) { //System.out.println(cnt); if(!cnt.containsKey(a[j])){ cnt.put(a[j],1); }else { cnt.put(a[j], cnt.get(a[j]) + 1); } if (cnt.get(a[j]) == 1) { end = j; //System.out.println(end); } else { break; } } if (start == 1) { len = Math.min(len, end - i); } cnt.clear(); } System.out.println(len); } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
cffdf07b05b0fa546add31deeade1254
train_000.jsonl
1566743700
You are given an array $$$a_{1}, a_{2}, \ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq n$$$) and delete integers $$$a_l, a_{l+1}, \ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; public class Main implements Runnable { boolean multiple = false; long MOD; @SuppressWarnings({"Duplicates", "ConstantConditions"}) void solve() throws Exception { int n = sc.nextInt(); /*read int arr a of size n*/ int[] a = new int[n]; for (int I = 0; I < n; I++) a[I] = sc.nextInt(); int ans = n; outer: for (int left = 0; left < n; left++) { TreeSet<Integer> remain = new TreeSet<>(); for (int i = 0; i < left; i++) if (remain.contains(a[i])) continue outer; else remain.add(a[i]); int ptr = n - 1; while (ptr >= left && !remain.contains(a[ptr])) { remain.add(a[ptr]); ptr--; } ans = min(ans, ptr - left + 1); } System.out.println(ans); } long pow(long x, long y){ if(y==0) return 1L; long t=powmod(x,y/2); if (y%2==0) return (t*t); return (((x*t))*t); } long powmod(long x, long y){ if(y==0) return 1L; long t=powmod(x,y/2); if (y%2==0) return (t*t)%MOD; return (((x*t)%MOD)*t)%MOD; } StringBuilder ANS = new StringBuilder(); void p(Object s) { ANS.append(s); } void p(double s) {ANS.append(s); } void p(long s) {ANS.append(s); } void p(char s) {ANS.append(s); } void pl(Object s) { ANS.append(s); ANS.append('\n'); } void pl(double s) { ANS.append(s); ANS.append('\n'); } void pl(long s) { ANS.append(s); ANS.append('\n'); } void pl(char s) { ANS.append(s); ANS.append('\n'); } void pl() { ANS.append(('\n')); } /*I/O, and other boilerplate*/ @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in));out = new PrintWriter(System.out);sc = new FastScanner(in);if (multiple) { int q = sc.nextInt();for (int i = 0; i < q; i++) solve(); } else solve(); System.out.print(ANS); } catch (Throwable uncaught) { Main.uncaught = uncaught; } finally { out.close(); }} public static void main(String[] args) throws Throwable{ Thread thread = new Thread(null, new Main(), "", (1 << 26));thread.start();thread.join();if (Main.uncaught != null) {throw Main.uncaught;} } static Throwable uncaught; BufferedReader in; FastScanner sc; PrintWriter out; } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) {this.in = in;}public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); }return st.nextToken(); }public int nextInt() throws Exception { return Integer.parseInt(nextToken()); }public long nextLong() throws Exception { return Long.parseLong(nextToken()); }public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["3\n1 2 3", "4\n1 1 2 2", "5\n1 4 1 4 9"]
2 seconds
["0", "2", "2"]
NoteIn the first example all the elements are already distinct, therefore no subsegment needs to be removed.In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$.In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$.
Java 11
standard input
[ "two pointers", "binary search", "implementation", "brute force" ]
9873a576d00630fa6e5fd4448a1a65ad
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$1 \le a_{i} \le 10^{9}$$$) — the elements of the array.
1,500
Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$.
standard output
PASSED
f36d34f7367bf54141808c1f8d39e4de
train_000.jsonl
1410103800
Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di → ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 → 00" transforms s to 10031003, and query "3 → " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!
256 megabytes
// Codeforces Round #265 (Div.1) C. Substitutes in Number (464/C) import java.util.*; import java.util.AbstractMap.SimpleEntry; public class SubstitutesInNumber { public static void main(String[] args) { Solver solver = new Solver(); solver.solve(); } } class Solver { private final long MOD = 1000000007; private String s; private int n; private Rule[] rules; private DPEntry[][] dp; public void solve() { Scanner scan = new Scanner(System.in); this.s = scan.next(); this.n = scan.nextInt(); this.rules = new Rule[this.n + 1]; this.dp = new DPEntry[this.n + 1][10]; for (int i = 1; i <= n; ++i) { String[] rule = scan.next().split("->"); int key = Integer.parseInt(rule[0]); String value = rule.length > 1 ? rule[1] : ""; this.rules[i] = new Rule(key, value); } this.rules[0] = new Rule(0, this.s); System.out.println(Search(0, 0).value); } private DPEntry Search(int depth, int key) { if (depth > n) { return new DPEntry(10, key); } if (this.dp[depth][key] != null) { return this.dp[depth][key]; } if (this.rules[depth].key == key) { DPEntry entry = new DPEntry(1, 0); for (char c : this.rules[depth].value.toCharArray()) { DPEntry res = Search(depth + 1, (int)c - '0'); entry.digits = (entry.digits * res.digits) % MOD; entry.value = (entry.value * res.digits + res.value) % MOD; } this.dp[depth][key] = entry; } else { this.dp[depth][key] = Search(depth + 1, key); } return this.dp[depth][key]; } class Rule { public int key; public String value; public Rule() { this.key = -1; this.value = ""; } public Rule(int key, String value) { this.key = key; this.value = value; } } class DPEntry { public long digits; public long value; public DPEntry() { this.digits = -1; this.value = -1; } public DPEntry(long digits, long value) { this.digits = digits; this.value = value; } } }
Java
["123123\n1\n2-&gt;00", "123123\n1\n3-&gt;", "222\n2\n2-&gt;0\n0-&gt;7", "1000000008\n0"]
1 second
["10031003", "1212", "777", "1"]
NoteNote that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample).
Java 8
standard input
[ "dp" ]
c315870f5798dfd75ddfc76c7e3f6fa5
The first line contains string s (1 ≤ |s| ≤ 105), consisting of digits — the string before processing all the requests. The second line contains a single integer n (0 ≤ n ≤ 105) — the number of queries. The next n lines contain the descriptions of the queries. The i-th query is described by string "di-&gt;ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.
2,100
Print a single integer — remainder of division of the resulting number by 1000000007 (109 + 7).
standard output
PASSED
adf7767268aaf95f23f63adbdcdc8ec1
train_000.jsonl
1410103800
Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di → ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 → 00" transforms s to 10031003, and query "3 → " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Bat-Orgil */ 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); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { static long mod = 1000000007; public void solve(int testNumber, InputReader in, PrintWriter out) { String s = in.next(); int m = in.nextInt(); int[] left = new int[m]; String[] right = new String[m]; for (int i = 0; i < m; i++) { String t = in.next(); left[i] = t.charAt(0) - '0'; right[i] = t.substring(3); } long[] val = new long[10]; long[] shift = new long[10]; for (int i = 0; i < 10; i++) { val[i] = i; shift[i] = 10; } for (int i = m - 1; i >= 0; i--) { long newVal = 0; long newShift = 1; for (int j = 0; j < right[i].length(); j++) { int c = right[i].charAt(j) - '0'; newVal = (newVal * shift[c] + val[c]) % mod; newShift = (newShift * shift[c]) % mod; } val[left[i]] = newVal; shift[left[i]] = newShift; } long v = 0; for (int i = 0; i < s.length(); i++) { v = (v * shift[s.charAt(i) - '0'] + val[s.charAt(i) - '0']) % mod; } out.println(v); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["123123\n1\n2-&gt;00", "123123\n1\n3-&gt;", "222\n2\n2-&gt;0\n0-&gt;7", "1000000008\n0"]
1 second
["10031003", "1212", "777", "1"]
NoteNote that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample).
Java 8
standard input
[ "dp" ]
c315870f5798dfd75ddfc76c7e3f6fa5
The first line contains string s (1 ≤ |s| ≤ 105), consisting of digits — the string before processing all the requests. The second line contains a single integer n (0 ≤ n ≤ 105) — the number of queries. The next n lines contain the descriptions of the queries. The i-th query is described by string "di-&gt;ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.
2,100
Print a single integer — remainder of division of the resulting number by 1000000007 (109 + 7).
standard output
PASSED
716cc30a98802bfdcbe0cc1792bd8faf
train_000.jsonl
1410103800
Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di → ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 → 00" transforms s to 10031003, and query "3 → " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author ilyakor */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { static final long mod = 1000000007; public void solve(int testNumber, InputReader in, OutputWriter out) { long[] vals = new long[10]; long[] shifts = new long[10]; for (int i = 0; i < 10; ++i) { vals[i] = i; shifts[i] = 10; } String s = in.nextToken(); int m = in.nextInt(); int[] left = new int[m]; String[] right = new String[m]; for (int i = 0; i < m; ++i) { String cur = in.nextToken(); left[i] = cur.charAt(0) - '0'; right[i] = cur.substring(3); } for (int i = m - 1; i >= 0; --i) { long newVal = 0; long newShift = 1; for (int j = 0; j < right[i].length(); ++j) { int c = right[i].charAt(j) - '0'; newVal = (newVal * shifts[c] + vals[c]) % mod; newShift = (newShift * shifts[c]) % mod; } vals[left[i]] = newVal; shifts[left[i]] = newShift; } long val = 0; for (int j = 0; j < s.length(); ++j) { int c = s.charAt(j) - '0'; val = (val * shifts[c] + vals[c]) % mod; } out.printLine(val); } } class InputReader { private InputStream stream; private byte[] buffer = new byte[10000]; private int cur; private int count; public InputReader(InputStream stream) { this.stream = stream; } public static boolean isSpace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (count == -1) { throw new InputMismatchException(); } try { if (cur >= count) { cur = 0; count = stream.read(buffer); if (count <= 0) return -1; } } catch (IOException e) { throw new InputMismatchException(); } return buffer[cur++]; } public int readSkipSpace() { int c; do { c = read(); } while (isSpace(c)); return c; } public String nextToken() { int c = readSkipSpace(); StringBuilder sb = new StringBuilder(); while (!isSpace(c)) { sb.append((char) c); c = read(); } return sb.toString(); } public int nextInt() { int sgn = 1; int c = readSkipSpace(); if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res = res * 10 + c - '0'; c = read(); } while (!isSpace(c)); res *= sgn; return res; } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
Java
["123123\n1\n2-&gt;00", "123123\n1\n3-&gt;", "222\n2\n2-&gt;0\n0-&gt;7", "1000000008\n0"]
1 second
["10031003", "1212", "777", "1"]
NoteNote that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample).
Java 8
standard input
[ "dp" ]
c315870f5798dfd75ddfc76c7e3f6fa5
The first line contains string s (1 ≤ |s| ≤ 105), consisting of digits — the string before processing all the requests. The second line contains a single integer n (0 ≤ n ≤ 105) — the number of queries. The next n lines contain the descriptions of the queries. The i-th query is described by string "di-&gt;ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.
2,100
Print a single integer — remainder of division of the resulting number by 1000000007 (109 + 7).
standard output
PASSED
0d8a5c2dd9103aefd720b931edc40dde
train_000.jsonl
1410103800
Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di → ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 → 00" transforms s to 10031003, and query "3 → " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; import java.math.BigInteger; import static java.math.BigInteger.*; import static java.util.Arrays.*; import javax.xml.ws.Holder; //<editor-fold defaultstate="collapsed" desc="Main"> public class Task{ // https://netbeans.org/kb/73/java/editor-codereference_ru.html#display private void run() { try { Locale.setDefault(Locale.US); } catch (Exception e) { } boolean oj = true; try { oj = System.getProperty("MYLOCAL") == null; } catch (Exception e) { } if (oj) { sc = new FastScanner(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); } else { try { sc = new FastScanner(new FileReader("input.txt")); out = new PrintWriter(new FileWriter("output.txt")); } catch (IOException e) { MLE(); } } Solver s = new Solver(); s.sc = sc; s.out = out; s.solve(); if (!oj) { err.println("Time: " + (System.currentTimeMillis() - timeBegin) / 1e3); err.printf("Mem: %d\n", (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20); } out.flush(); } private void show(int[] arr) { for (int v : arr) { err.print(" " + v); } err.println(); } public static void exit() { err.println("Time: " + (System.currentTimeMillis() - timeBegin) / 1e3); out.flush(); out.close(); System.exit(0); } public static void MLE() { int[][] arr = new int[1024 * 1024][]; for (int i = 0; i < arr.length; i++) { arr[i] = new int[1024 * 1024]; } } public static void main(String[] args) { new Task().run(); } static long timeBegin = System.currentTimeMillis(); static FastScanner sc; static PrintWriter out; static PrintStream err = System.err; } //</editor-fold> //<editor-fold defaultstate="collapsed" desc="FastScanner"> class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStreamReader reader) { br = new BufferedReader(reader); st = new StringTokenizer(""); } String next() { while (!st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException ex) { Task.MLE(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } //</editor-fold> class Solver { void ass(boolean OK) { if(!OK)Task.MLE(); } FastScanner sc; PrintWriter out; static PrintStream err = System.err; final int mod = (int)1e9 + 7; final int mod1 = mod - 1; int[] str; long[] val, len; int q; int[] from; int[][] to; long pow10Mod( long p ){ if( p < 0 ) Task.MLE(); if( p == 0 ) return 1; long ans = pow10Mod(p/2); ans = (ans * ans) % mod; if( p%2==1 ) ans = (ans * 10) % mod; return ans; } void solve(){ { String s = sc.next(); str = new int[s.length()]; for (int i = 0; i < s.length(); i++) str[i] = s.charAt(i) - '0'; } val = new long[10]; len = new long[10]; for (int i = 0; i <= 9; i++) { val[i] = i; len[i] = 1; } q = sc.nextInt(); from = new int[q]; to = new int[q][]; for (int i = 0; i < q; i++) { String s; try { s = sc.br.readLine(); } catch (IOException e) { throw new RuntimeException(); } int pos = s.indexOf("->"); String[] arr = { s.substring(0,pos), s.substring(pos+2) }; from[i] = Integer.parseInt(arr[0].trim()); arr[1] = arr[1].trim(); to[i] = new int[arr[1].length()]; for (int j = 0; j < arr[1].length(); j++) to[i][j] = arr[1].charAt(j) - '0'; } for( int i = q-1; 0 <= i; --i ){ long newVal = 0, newLen = 0; for( int j = to[i].length-1; 0 <= j; --j ){ int x = to[i][j]; newVal = (newVal + val[x] * pow10Mod(newLen)%mod) % mod; newLen = (newLen + len[x]) % mod1; } val[from[i]] = newVal; len[from[i]] = newLen; } long ans = 0, len = 0; for (int i = str.length-1; 0 <= i; --i) { int x = str[i]; ans = (ans + val[x]*pow10Mod(len)%mod) % mod; len = (len + this.len[x]) % mod1; } out.println( ans ); } }
Java
["123123\n1\n2-&gt;00", "123123\n1\n3-&gt;", "222\n2\n2-&gt;0\n0-&gt;7", "1000000008\n0"]
1 second
["10031003", "1212", "777", "1"]
NoteNote that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample).
Java 8
standard input
[ "dp" ]
c315870f5798dfd75ddfc76c7e3f6fa5
The first line contains string s (1 ≤ |s| ≤ 105), consisting of digits — the string before processing all the requests. The second line contains a single integer n (0 ≤ n ≤ 105) — the number of queries. The next n lines contain the descriptions of the queries. The i-th query is described by string "di-&gt;ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.
2,100
Print a single integer — remainder of division of the resulting number by 1000000007 (109 + 7).
standard output
PASSED
98ee5deb7463352f8ea5dc078a25d92e
train_000.jsonl
1410103800
Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di → ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 → 00" transforms s to 10031003, and query "3 → " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!
256 megabytes
import java.util.*; import java.io.*; public class C { public static PrintStream out = System.out; public static InputReader in = new InputReader(System.in); static long MOD = (long) 1e9 + 7; static class Node { static Node EMPTY_LEAF = new Node(0); int i; List<Node> children = new ArrayList<Node>(); Node(int i) { this.i = i; } long _ans = -3; long _size = -3; long ans() { if (_ans != -3) return _ans; if (children.size() == 0) { if (this == EMPTY_LEAF) return 0; return i; } long digits = 0; long ret = 0; for (int i = children.size() - 1; i >= 0; i--) { Node node = children.get(i); ret = (ret + (node.ans() * modPow(digits)) % MOD) % MOD; digits += node.size(); digits %= MOD - 1; } _ans = ret; return ret; } long size() { if (_size != -3) return _size; if (children.size() == 0) { if (this == EMPTY_LEAF) return 0; return 1; } int ret = 0; for (int i = 0; i < children.size(); i++) { ret += children.get(i).size(); ret %= MOD - 1; } _size = ret; return ret; } } static long modPow(long digits) { if (digits == 0) return 1; if (digits % 2 == 1) return 10L * modPow(digits - 1) % MOD; long c = modPow(digits / 2); return c * c % MOD; } static long[] tenPow = new long[200001]; public static void main(String args[]) { long pow = 1; for (int i = 0; i < tenPow.length; i++) { tenPow[i] = pow; pow = (pow * 10) % MOD; } String S = in.next(); int N = in.nextInt(); nodes = new Node[10]; Node root = new Node(-1); for (int i = 0; i < S.length(); i++) { root.children.add(get((int) S.charAt(i) - '0')); } for (int i = 0; i < N; i++) { String Q = in.next(); int d = (int) Q.charAt(0) - '0'; if (nodes[d] != null) { Node nd = nodes[d]; nodes[d] = null; for (int j = 3; j < Q.length(); j++) { nd.children.add(get((int) Q.charAt(j) - '0')); } if (Q.length() == 3) { nd.children.add(Node.EMPTY_LEAF); } } } out.println(root.ans()); } static Node[] nodes; static Node get(int i) { if (nodes[i] == null) { nodes[i] = new Node(i); } return nodes[i]; } } 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 double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } }
Java
["123123\n1\n2-&gt;00", "123123\n1\n3-&gt;", "222\n2\n2-&gt;0\n0-&gt;7", "1000000008\n0"]
1 second
["10031003", "1212", "777", "1"]
NoteNote that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample).
Java 8
standard input
[ "dp" ]
c315870f5798dfd75ddfc76c7e3f6fa5
The first line contains string s (1 ≤ |s| ≤ 105), consisting of digits — the string before processing all the requests. The second line contains a single integer n (0 ≤ n ≤ 105) — the number of queries. The next n lines contain the descriptions of the queries. The i-th query is described by string "di-&gt;ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.
2,100
Print a single integer — remainder of division of the resulting number by 1000000007 (109 + 7).
standard output
PASSED
cadc9c949c687eca80e82578eb55467a
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int numOfFriends = scn.nextInt(), fenceHeight = scn.nextInt(), count = 0, n; for (int i = 0; i < numOfFriends; i++) { n = scn.nextInt(); if (n > fenceHeight) count += 2; else count++; } System.out.println(count); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
7b57aa1e06c21fbf8d3ce8c4f706257f
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class VanyaAndFence { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int friendNum = scanner.nextInt(); int fenceHeight =scanner.nextInt(); int minWidthOfRoad = 0; for (int i = 0; i < friendNum; i++) { int friendHeight = scanner.nextInt(); if (friendHeight > fenceHeight) minWidthOfRoad += 2; else minWidthOfRoad += 1; } System.out.println(minWidthOfRoad); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
68f32573db9e4dee52ce63224f39333a
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class VenyaAndFence { public static void main(String[] args) { Scanner input = new Scanner (System.in); int num =input.nextInt() , h =input.nextInt(), width =0; for (int i=1; i<=num; i++){ int ai = input.nextInt(); if (ai <= h) width +=1; else width +=2; } System.out.println(width); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
9687557d24b8b2f854f96be4fbd0bcac
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class problem { public static void main(String[] args) { int n,h,w=0 , s; Scanner input = new Scanner(System.in); n=input.nextInt(); h=input.nextInt(); int [] arr=new int[n]; for(int i=0;i<n;i++) { s =input.nextInt(); if (s <=h) w++; else w+=2; } System.out.println(w); }}
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
56b740461104239c756a9e0e1b97e39f
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
//package test; import java.util.Scanner; public class test { public static void main(String[] args) { // TODO Auto-generated method stub Scanner IN = new Scanner(System.in); int n,h,len,x; n = IN.nextInt(); h = IN.nextInt(); len = 0; for (int i = 1; i<=n; i++){ x = IN.nextInt(); if(x>h) len++; len++; } System.out.println(len); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
665034e91b7cc7d2a3a8a32b906167e4
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; /** * * @author i7 */ public class ProblemSolving { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int n,h,w=0,s; Scanner input = new Scanner(System.in); n=input.nextInt(); h=input.nextInt(); for(int i=0;i<n;i++) { s=input.nextInt(); if (s<=h) w++; else w+=2; } System.out.println(w); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
7f3a4adc453d44c00385e9aab2c08595
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class test { public static Scanner myScanner = new Scanner(System.in); public static void main(String[] args) { int n = myScanner.nextInt(); int h = myScanner.nextInt(); int[] arr = new int[n]; int result = 0; for(int X = 0; X < n ; X++) { arr[X] = myScanner.nextInt(); } for(int X = 0; X < n; X++) { if(arr[X] <= h) result++; else result += 2; } System.out.println(result); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
c5ee0faa75b1f854443bcf7049e84ea7
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class Test { public static Scanner myScanner = new Scanner(System.in); public static void main(String[] args) { int n, h; n = myScanner.nextInt(); h = myScanner.nextInt(); int[] numOfFriends = new int[n]; int minimumWidth = 0; for(int p = 0; p < n; p++) { numOfFriends[p] = myScanner.nextInt(); if(numOfFriends[p] <= h) minimumWidth++; else minimumWidth += 2; } System.out.println(minimumWidth); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
dc7a02440fa32a74e247d6b637871a59
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; /** * * @author CesarAlfonsoMendoza */ public class A { public static void main(String[]args){ Scanner in = new Scanner(System.in); int n=in.nextInt(),h=in.nextInt(),persona,suma=0; for(int i=0;i<n;i++) { persona=in.nextInt(); if(persona>h) suma+=2; else suma++; } System.out.println(suma); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
3b830d85a368e3f8fa218bba869287c7
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class Main { private static int PersonsNumber; private static int FenceHeight ; private static int RoadSize = 0 ; public static void main(String[] args) { // write your code here Scanner myObj = new Scanner(System.in); PersonsNumber = myObj.nextInt(); FenceHeight = myObj.nextInt(); for (int i = 0 ; i<PersonsNumber ; i++){ if (myObj.nextInt() > FenceHeight) RoadSize++; RoadSize++; } System.out.println(RoadSize); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
7b822d99a3f860cc28130b1c337982d7
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class Main { private static int RoadSize = 0 ; public static void main(String[] args) { // write your code here Scanner myObj = new Scanner(System.in); int personsNumber = myObj.nextInt(); int fenceHeight = myObj.nextInt(); for (int i = 0; i< personsNumber; i++){ if (myObj.nextInt() > fenceHeight) RoadSize++; RoadSize++; } System.out.println(RoadSize); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
d47b5077cc0e13bbcde5e9ac95561505
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class Cf_677_A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), h = sc.nextInt(); int sum = 0; for(int i = 0; i < n; i++) { int num = sc.nextInt(); if(num <= h) sum += 1; else if(num > h) sum+= 2; } System.out.print(sum); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
3612566e0e69b95274dff7baf504dd76
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author mohanad */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); VanyaandFence solver = new VanyaandFence(); solver.solve(1, in, out); out.close(); } static class VanyaandFence { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(), h = in.nextInt(); int sum = 0; for (int i = 0; i < n; i++) { int x = in.nextInt(); if (x > h) sum += 2; else sum++; } out.print(sum); out.flush(); out.close(); } } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
c5400b22047db518de6bcc3c8b7f3eb0
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class practice { public static void main(String [] args){ Scanner sc =new Scanner(System.in); int n = sc.nextInt(); int h = sc.nextInt(); int sum = 0; for(int i = 0; i< n; i++){ int b = sc.nextInt(); if(b <= h) sum += 1; else sum+=2; } System.out.println(sum); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
19da74548b74f60dc5884ec27d6a02e1
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class Sol { public static void main(String args[]) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int h=s.nextInt(); int a[]=new int[n]; int count=0; for(int i=0;i<n;i++) a[i]=s.nextInt(); for(int k=0;k<n;k++) { if(a[k]>h) count++; } System.out.println((n-count)+(count*2)); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
cff8e54df8e7617295a3d14d78a2d805
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class CF { public static void main(String[] args) { Scanner input= new Scanner(System.in); long maxH; int n = input.nextInt(); int h = input.nextInt(); maxH = n; for(int i = 0 ; i < n ; i++){ int a = input.nextInt(); if(a > h) maxH++; } System.out.println(maxH); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
0ad22cf0596c4c0106de16a1850b627c
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner (System.in); //takes in an input let first one a be the number of people and b be the height of the fence int n = scan.nextInt(); int h = scan.nextInt(); int sum =0; int arr[] = new int [n]; for(short i=0;i<n;i++) { arr[i]=scan.nextInt(); if(arr[i]<=h) sum+=1; else sum+=2; } System.out.println(sum); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
1a41318c8090ecf0d435b54a901387a3
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(), h = input.nextInt(),sum=0; int []a = new int[n]; for(int i=0; i<n; i++){ a[i]=input.nextInt(); } for(int i=0; i<n; i++){ if(a[i]<=h) sum ++; else sum =sum+2; } System.out.println(sum); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
7d55b64f2b181cf32a2c5c34ae1fe93a
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(), h = input.nextInt(),sum=0; for(int i=0; i<n; i++){ int x= input.nextInt(); if(x<=h) sum ++; else sum =sum+2; } System.out.println(sum); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
94051436a3e9b64853a8d114c99dc73d
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(), h = input.nextInt(),sum=0; for(int i=0; i<n; i++){ int x= input.nextInt(); if(x>h) sum ++; sum++; } System.out.println(sum); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
9afd1fc337a431e3efa3da320a27d1c4
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); // Start writing your solution here. ------------------------------------- int n = sc.nextInt(), h = sc.nextInt(); int cnt = n; for (int i=0; i<n; i++) { int hs = sc.nextInt(); if (hs>h) cnt++; } out.println(cnt); // 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 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 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
965a9b2dbc4dd1785cb2e984075c5122
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); // Solution int n = sc.nextInt(),h = sc.nextInt(); List<Integer> a = new ArrayList<Integer>(); for (int i = 0; i<n ; i++) { a.add(sc.nextInt()); } long count = a.stream().filter(num -> num>h).count(); out.println(count + a.size()); // Solution end out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //-------------------------------------------------------- }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
fd0e5979c0348ff40b532d44eeb2d323
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); out = new PrintWriter(new BufferedOutputStream(System.out)); // Solution int n = sc.nextInt(),h = sc.nextInt(); List<Integer> a = new ArrayList<Integer>(); long count = 0; for (int i = 0; i<n ; i++) { a.add(sc.nextInt()); if (a.get(i) > h) count++; } //long count = a.stream().filter(num -> num>h).count(); out.println(count + a.size()); // Solution end out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-------------------------------------------------------- }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
61ad75809460b250cb24024f2abace8c
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; public class Dis { public static void main(String[] args) { Scanner input = new Scanner(System.in); int x=input.nextInt(); int y=input.nextInt(); int c=0; int arr[]=new int [10000]; for(int i=0;i<x;i++) { arr[i]=input.nextInt(); } for(int i=0;i<x;i++) { if(arr[i]>y) { c+=2; } else { c++; } } System.out.println(c); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
b567e87e80168a47a27f0c9dd815bd4e
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.util.Scanner; import java.util.*; public class JavaApplication5 { public static void main(String[] args) { Scanner in = new Scanner(System.in); short n ,h,sum=0; n=in.nextShort(); h=in.nextShort(); short arr[]= new short[n]; for(short i=0;i<n;i++) { arr[i]=in.nextShort(); if(arr[i]<=h) sum+=1; else sum+=2; } System.out.println(sum); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
59c422a1ac518bfa7a44c900b540a657
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class VanyaAndFence1 { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws IOException { Reader s=new Reader(); int n, h; //n = number of friends, h = height of fence int tempWidth, width = 0; // System.out.println("Enter number of friends : "); n = s.nextInt(); // System.out.println("Enter the height of the fence : "); h = s.nextInt(); int a[] = new int[n]; //array of height of the girls for (int i = 0; i < n; i++) { a[i] = s.nextInt(); if (a[i] <= h) { //System.out.println("Height is less than fence"); tempWidth = 1; } else { //System.out.println("Height is more"); tempWidth = 2; } width += tempWidth; } System.out.println(width); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
99109fa23d37bf7119a3e5abbbcd4500
train_000.jsonl
1464798900
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class VanyaAndFence { 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()); } } public static void main(String[] args) { // TODO Auto-generated method stub FastReader fr = new FastReader(); int n, h; //n = number of friends, h = height of fence int tempWidth, width = 0; // System.out.println("Enter number of friends : "); n = fr.nextInt(); // System.out.println("Enter the height of the fence : "); h = fr.nextInt(); int a[] = new int[n]; //array of height of the girls for (int i = 0; i < n; i++) { a[i] = fr.nextInt(); if (a[i] <= h) { //System.out.println("Height is less than fence"); tempWidth = 1; } else { //System.out.println("Height is more"); tempWidth = 2; } width += tempWidth; } System.out.println(width); } }
Java
["3 7\n4 5 14", "6 1\n1 1 1 1 1 1", "6 5\n7 6 8 9 10 5"]
1 second
["4", "6", "11"]
NoteIn the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.
Java 8
standard input
[ "implementation" ]
e7ed5a733e51b6d5069769c3b1d8d22f
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively. The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.
800
Print a single integer — the minimum possible valid width of the road.
standard output
PASSED
e1049491bfbc118af591e828da1e7a0f
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
/** * Created with IntelliJ IDEA. * User: olethra * Date: 25/10/13 * Time: 18:56 * To change this template use File | Settings | File Templates. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class TaskB { StringTokenizer st; BufferedReader in; PrintWriter out; public static void main(String[] args) throws NumberFormatException, IOException { TaskB solver = new TaskB(); solver.open(); long time = System.currentTimeMillis(); solver.solve(); if (!"true".equals(System.getProperty("ONLINE_JUDGE"))) { System.out.println("Spent time: " + (System.currentTimeMillis() - time)); System.out.println("Memory: " + (Runtime.getRuntime().totalMemory() - Runtime .getRuntime().freeMemory())); } solver.close(); } public void open() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); //in = new BufferedReader(new FileReader("input.txt")); //out = new PrintWriter(new FileWriter("output.txt")); } public String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = in.readLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } boolean hasMoreTokens() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = in.readLine(); if (line == null) return false; st = new StringTokenizer(line); } return true; } public void solve() throws NumberFormatException, IOException { int n = nextInt(); String[] words = new String[n]; for (int i = 0; i < n; i++) { words[i] = nextToken(); } String message = nextToken(); int i = 0; int j = 0; int wordsNum = 0; boolean heartWasLast = false; while (i < message.length()) { while (i < message.length() && message.charAt(i) != '<') i++; if (i == message.length()) { System.out.println("no"); return; } while (i < message.length() && message.charAt(i) != '3') i++; if (i == message.length()) { System.out.println("no"); return; } heartWasLast = true; j = 0; if (wordsNum < words.length) { while (j < words[wordsNum].length() && i < message.length()) { while (i < message.length() && words[wordsNum].charAt(j) != message.charAt(i)) { i++; } if (i < message.length()) { j++; i++; } } if (j < words[wordsNum].length()) { System.out.println("no"); return; } else { heartWasLast = false; wordsNum++; } } else break; } if (heartWasLast && wordsNum == words.length) System.out.println("yes"); else System.out.println("no"); // if (message.charAt(0) != '<' || 1 == message.length() || message.charAt(1) != '3') { // System.out.println("no"); // return; // } // boolean heartWasLast = true; // int lastLess = 0; // boolean lessWasFound = false; // int i = 2; // int cntWords = 0; // // while (i < message.length()) { // String currentToken = ""; // heartWasLast = false; // cntWords++; // lessWasFound = false; // boolean inWord = true; // while (inWord) { // if (message.charAt(i) >= 'a' && message.charAt(i) <= 'z') { // currentToken += message.charAt(i); // i++; // } else { // if (message.charAt(i) == '<') { //// if (i + 1 < message.length()) { //// if (message.charAt(i + 1) == '3') { //// heartWasLast = true; //// i += 2; //// inWord = false; //// } else i++; // lessWasFound = true; // lastLess = i; // i++; // } // else if (message.charAt(i) == '3' && lessWasFound){ // // } // } // if (i == message.length()) inWord = false; // } // if (!heartWasLast) { // System.out.println("no"); // return; // } // // int j = 0; // int k = 0; // while (j < words[cntWords - 1].length() && k < currentToken.length()) { // while (k < currentToken.length() && words[cntWords - 1].charAt(j) != currentToken.charAt(k)) { // k++; // } // if (k<currentToken.length()) j++; // } // if (j != words[cntWords - 1].length()) { // System.out.println("no"); // return; // } // } // if (cntWords < words.length || !heartWasLast) { // System.out.println("no"); // } else System.out.println("yes"); } public void close() { out.flush(); out.close(); } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
9ddb6f41ed642c4b2c70039da85844f9
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.util.*; public class Main { private static final long MOD = 1000000007; public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); scan.nextLine(); StringBuilder builder = new StringBuilder(); HashSet<Integer> checkpoints = new HashSet<Integer>(); for (int i = 0; i < n; i++) { builder.append(scan.nextLine()); checkpoints.add(builder.length()); } char[] require = builder.toString().toCharArray(); char[] input = scan.nextLine().toCharArray(); int head = 0; while (head < input.length) { if (input[head] == '<') { head++; break; } head++; } while (head < input.length) { if (input[head] == '3') { head++; break; } head++; } int tail = input.length - 1; while (tail >= 0) { if (input[tail] == '3') { tail--; break; } tail--; } while (tail >= 0) { if (input[tail] == '<') { tail--; break; } tail--; } if (head > tail) { System.out.println("no"); } else { HashSet<Integer> current = new HashSet<Integer>(); HashSet<Integer> next = new HashSet<Integer>(); int[] check = new int[require.length]; current.add(0); boolean pass = false; for (int i = head; i <= tail; i++) { for (int pos : current) { if (checkpoints.contains(pos) && check[pos] < 2) { if (check[pos] == 0 && input[i] == '<') { check[pos] = 1; } else if (check[pos] == 1 && input[i] == '3') { check[pos] = 2; } if (pos + tail - i >= require.length) { next.add(pos); } continue; } if (require[pos] == input[i]) { next.add(pos + 1); } else if (pos + tail - i >= require.length) { next.add(pos); } } if (next.contains(require.length)) { pass = true; break; } HashSet<Integer> temp = current; current = next; next = temp; next.clear(); } System.out.println(pass ? "yes" : "no"); } scan.close(); } private static void print(int... arr) { System.out.print(arr[0]); for (int i = 1; i < arr.length; i++) { System.out.print(" "); System.out.print(arr[i]); } System.out.println(); } private static void print(long... arr) { System.out.print(arr[0]); for (int i = 1; i < arr.length; i++) { System.out.print(" "); System.out.print(arr[i]); } System.out.println(); } private static <T> void print(T... arr) { System.out.print(arr[0]); for (int i = 1; i < arr.length; i++) { System.out.print(" "); System.out.print(arr[i]); } System.out.println(); } private static void read(Scanner scan, int[]... arrs) { int len = arrs[0].length; for (int i = 0; i < len; i++) { for (int[] arr : arrs) { arr[i] = scan.nextInt(); } } } private static void read(Scanner scan, long[]... arrs) { int len = arrs[0].length; for (int i = 0; i < len; i++) { for (long[] arr : arrs) { arr[i] = scan.nextLong(); } } } private static void decreaseByOne(int[]... arrs) { for (int[] arr : arrs) { for (int i = 0; i < arr.length; i++) { arr[i] --; } } } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
710aa40ea210f130f12ab7daa2c05f98
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import java.io.StreamCorruptedException; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Solution { public static void main(String[] args) { new Solution().solve(); } PrintWriter out; public void solve() { out=new PrintWriter(System.out); int n=in.nextInt(); String[] s=new String[n]; for(int i=0;i<n;i++) { s[i]=in.nextLine(); } // System.out.println(sb); String str=in.nextLine(); int strcount=0; for(int i=0;i<n;i++) { if(strcount==str.length()) { System.out.println("no"); System.exit(0); } while(str.charAt(strcount++)!='<') { if(strcount==str.length()) { System.out.println("no"); System.exit(0); } } if(strcount==str.length()) { System.out.println("no"); System.exit(0); } while(str.charAt(strcount++)!='3') { if(strcount==str.length()) { System.out.println("no"); System.exit(0); } } if(strcount==str.length()) { System.out.println("no"); System.exit(0); } for(int j=0;j<s[i].length();j++) { while(str.charAt(strcount++)!=s[i].charAt(j)) { if(strcount==str.length()) { System.out.println("no"); System.exit(0); } } if(strcount==str.length()) { System.out.println("no"); System.exit(0); } } } if(strcount==str.length()) { System.out.println("no"); System.exit(0); } while(str.charAt(strcount++)!='<') { if(strcount==str.length()) { System.out.println("no"); System.exit(0); } } if(strcount==str.length()) { System.out.println("no"); System.exit(0); } while(str.charAt(strcount++)!='3') { if(strcount==str.length()) { System.out.println("no"); System.exit(0); } } System.out.println("yes"); out.close(); } FasterScanner in=new FasterScanner(); public class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
3aeff2330333a174428489b70044b9fb
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.util.Scanner; import java.io.File; import java.lang.Math; public class B { private Scanner sc; int n; static int t = 0; static int len; String[] s; static String mess; public static void main(String[] args) throws Exception { B cl = new B(); cl.input(); cl.solve(); cl.output(); } public void input() throws Exception { sc = new Scanner(System.in); // sc = new Scanner(new File("input.txt")); n = sc.nextInt(); s = new String [n + 1]; for (int i = 0; i <= n; i++) { s[i] = sc.nextLine(); // System.out.println(s[i]); } mess = sc.nextLine(); } public void solve() throws Exception { len = (mess.toString()).length(); for (int i = 1; i <= n; i++) { fff('<'); fff('3'); for (int j = 0; j < s[i].length(); j++) { fff(s[i].charAt(j)); } // System.out.println(s[i]); } fff('<'); fff('3'); } static void fff(char c) { for (; t < len; t++) { if (mess.charAt(t) == c) { t++; return; } } System.out.println("no"); System.exit(0); } public void output() throws Exception { System.out.println("yes"); } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
5136bc5207e2453db662c19f0d2e6739
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.util.Scanner; public class Main { private static boolean matches(String needle,String haystack) { int n = 0; for (int i=0;i<haystack.length();i++) { if (needle.charAt(n) == haystack.charAt(i)) { n++; if (n == needle.length()) return true; } } return false; } public static void main(String[] args) { Scanner in = new Scanner(System.in); StringBuilder lineB = new StringBuilder("<3"); int max = in.nextInt(); in.nextLine(); for (int i=0;i<max;i++) { lineB.append(in.nextLine()); lineB.append("<3"); } String line = lineB.toString(); String line2 = in.nextLine(); if (matches(line,line2)) { System.out.println("yes"); } else System.out.println("no"); } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
425c2c877afb1679af550f00a9a3dd49
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.util.*; import java.util.regex.*; import java.text.*; import java.math.*; public class Solution { public static void main(String args[]){ Scanner scan = new Scanner(System.in); String line = scan.nextLine(); Scanner sc = new Scanner(line); int n = sc.nextInt(); String[] x = new String[n]; for(int i=0;i<n;i++){ x[i] = scan.nextLine(); } String msg = scan.nextLine(); int k = 0; for(int i=0;i<n;i++){ String temp = "<3" + x[i]; int j=0; while(temp.length()>j){ if(msg.length()==k){ System.out.println("no"); return; } if(msg.charAt(k)==temp.charAt(j)){ k++; j++; } else k++; } } String temp = "<3"; int j=0; while(temp.length()>j){ if(msg.length()==k){ System.out.println("no"); return; } if(msg.charAt(k)==temp.charAt(j)){ k++; j++; } else k++; } System.out.println("yes"); } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
1828ae3cfe3a7d6b4ea0b16e6058b167
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Main{ public static void main(String args[]){ Scanner in=new Scanner(System.in); int n=in.nextInt(); in.nextLine(); ArrayList<String>a=new ArrayList<String>(); for(int i=0;i<n;i++) a.add(in.nextLine()); String after=in.nextLine(); int pos=0; char test[]=after.toCharArray(); boolean get3=false; boolean getLess=false; boolean getWord=false; int index=0; int getLast=-1; for(int i=0;i<test.length;){ for(int j=i;j<test.length;j++) if(test[j]=='<'){ pos=i; getLess=true; break; } if(getLess) for(int j=pos+1;j<test.length;j++) if(test[j]=='3'){ pos=i; get3=true; break; } int j=0; if(get3&&getLess) for(int k=pos+1;k<test.length;k++){ if(test[k]==a.get(index).charAt(j)){ j++; if(j==a.get(index).length()){ getWord=true; pos=k+1; break; } } } if(!get3||!getLess||!getWord){ break; } else{ index++; if(index==a.size()){ getLast=pos; break; } i=pos; } get3=false; getLess=false; getWord=false; } get3=false; getLess=false; getWord=false; if(getLast!=-1){ for(int j=pos;j<test.length;j++) if(test[j]=='<'){ pos=j; getLess=true; break; } if(getLess) for(int j=pos+1;j<test.length;j++) if(test[j]=='3'){ get3=true; break; } if(getLess&&get3) System.out.println("yes"); else System.out.println("no"); }else System.out.println("no"); } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
c0ac438577f5b021d9179cf9e9968bd7
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.io.BufferedReader; import java.io.OutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); String s[] = new String[n]; for(int i=0;i<n;i++){ in.readLine(); s[i] = in.next() + "<3"; } in.readLine(); String v = in.next(); int wi = 0; char nc = '<'; s[0] = "3" + s[0]; int si = 0; for(int i=0;i<v.length();i++) { if(nc == v.charAt(i)){ if(wi == s.length){ out.println("yes"); return; } nc = s[wi].charAt(si); if(si == s[wi].length()-1){ wi++; si = 0; } else{ si++; } } } out.println("no"); } } class InputReader { private BufferedReader br; private StringTokenizer st; public InputReader(InputStream in) { br=new BufferedReader(new InputStreamReader(in)); try { st=new StringTokenizer(br.readLine()); } catch (IOException ignored) { } } public void readLine() { try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { return; } } public int nextInt(){ return Integer.parseInt(st.nextToken()); } public String next(){ return st.nextToken(); } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
37c2e2863053a26c93399a050db59fa4
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
/* Date : Nov 27, 2013 Problem Name : dima and text messages Location : http://codeforces.com/contest/358/problem/B Algorithm : bruteforces Status : solving CodingTime : 10:05- ReadingTime : */ import java.util.*; import java.io.*; public class Main { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception{ int n = Integer.parseInt(reader.readLine()); String[] words = new String[n]; for (int i = 0; i < n; i++){ words[i] = reader.readLine(); } char[] message = reader.readLine().toCharArray(); int messageInd = 0; try { for (int i = 0; i < n; i++){ // removing <3 while (message[messageInd] != '<'){ messageInd++; } while (message[messageInd] != '3'){ messageInd++; } char[] word = words[i].toCharArray(); for (int j = 0; j < word.length; j++){ while (word[j] != message[messageInd]){ messageInd++; } messageInd++; } } while (message[messageInd] != '<'){ messageInd++; } while (message[messageInd] != '3'){ messageInd++; } } catch (Exception e){ System.out.println("no"); return; } System.out.println("yes"); return; } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
a02178896a1c14831271950aaf49bc05
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
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; public class C358B { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = Integer.parseInt(bf.readLine().trim()); String[] s = new String[2 * n + 1]; int count = 0; s[count++] = "<3"; for (int i = 0; i < n; i++) { s[count++] = bf.readLine().trim(); s[count++] = "<3"; } String sms = bf.readLine().trim(); int index = 0, pos = 0; for (int i = 0; i < sms.length(); i++) { if (s[index].charAt(pos) == sms.charAt(i)){ pos++; if (s[index].length() == pos) { index++; pos = 0; if (index == count) { break; } } } } if (index == count) pw.println("yes"); else pw.println("no"); pw.close(); } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
79025b18b76d82a47772107ef1ec7f44
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.util.Scanner; public class B { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); String[] words = new String[2*n+1]; for(int i = 0; i< 2*n; i+=2){ words[i] = "<3"; words[i+1] = sc.nextLine(); } words[2*n] = "<3"; boolean ok = false; String msg = sc.nextLine(); int point = 0; for(int i = 0; i <2*n+1; i++){ int index = 0; while(point < msg.length() && index < words[i].length()){ if(words[i].charAt(index) == msg.charAt(point)){ point++; index++; } else{ point++; } } if(index == words[i].length() && i == 2*n){ ok = true; continue; } } if(ok){ System.out.println("yes"); } else{ System.out.println("no"); } } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
6c6c8053045781e69cbb262c8dbaa075
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Locale; public class Solution implements Runnable { static BufferedReader in; static PrintWriter out; public static void main(String[] args) { new Thread(null, new Solution(), "", 256 * (1L << 20)).start(); } public void run() { try { long t1 = System.currentTimeMillis(); if (System.getProperty("ONLINE_JUDGE") != null) { out = new PrintWriter(System.out); in = new BufferedReader(new InputStreamReader(System.in)); } else { out = new PrintWriter("output.txt"); in = new BufferedReader(new FileReader("input.txt")); } Locale.setDefault(Locale.US); solve(); long t2 = System.currentTimeMillis(); System.err.println("Time = " + (t2 - t1)); } catch (Throwable t) { t.printStackTrace(System.err); System.exit(-1); } finally { try { in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); System.exit(-1); } } } // solution void solve() throws IOException { String line = in.readLine(); int wordCount; ArrayList<String> words = new ArrayList<String>(); String sms; if (line != null) { wordCount = Integer.parseInt(line); for (int i = 0; i < wordCount; i++) { words.add(in.readLine()); } sms = in.readLine(); StringBuilder stringBuilder = new StringBuilder("<3"); for (String word : words) { stringBuilder.append(word).append("<3"); } String correctSms = stringBuilder.toString(); int i = 0; for (int it = 0; it < sms.length(); it++) { if (i < correctSms.length()) { if (correctSms.charAt(i) == sms.charAt(it)) { i++; } } else { out.print("yes"); return; } } if (i < correctSms.length()) { out.print("no"); } else { out.print("yes"); } } else { out.print("no"); } } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
d1ff006fa5546f65046c30dd80afe98a
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.io.*; public class N358B { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); for(int i = 0; i < n; i++){ sb.append("<3").append(br.readLine()); } sb.append("<3"); String str = sb.toString(); String message = br.readLine(); int index = 0; for(int i = 0; i < message.length() && index < str.length(); i++){ if(str.charAt(index) == message.charAt(i)) index++; } if(index == str.length()) System.out.println("yes"); else System.out.println("no"); } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
ba8ec76ed0d873ee45b20bb7af79d267
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.util.*; import java.io.*; public class TaskB { FastScanner in; PrintWriter out; public void solve() throws IOException { int n = in.nextInt(); String[] words = new String[n]; for (int i = 0; i < n; i++) { words[i] = in.next(); } String sms = in.next(); int i = 0; int j = 0; boolean ans = true; while (i < sms.length() && j < n) { boolean br = false; boolean th = false; while (i < sms.length() && (sms.charAt(i) != words[j].charAt(0) || !th)) { if (sms.charAt(i) == '<') { br = true; } if (br && sms.charAt(i) == '3') { th = true; } i++; } if (i == sms.length()) { ans = false; break; } int l = 0; while (l < words[j].length()) { if (i == sms.length()) { ans = false; break; } if (sms.charAt(i) == words[j].charAt(l)) { l++; } i++; } j++; } boolean br = false; boolean th = false; while (i < sms.length()) { if (sms.charAt(i) == '<') { br = true; } if (br && sms.charAt(i) == '3') { th = true; } i++; } if (!th) { ans = false; } if (ans) { out.print("yes"); } else { out.print("no"); } } public void run() { try { in = new FastScanner(); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] arg) { new TaskB().run(); } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
ec06eef4a00df78b51a010bbe25a93c3
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.io.*; import java.util.*; public class CF { void solve() { int n = in.nextInt() + 1; String[] s = new String[n]; s[0] = "<3"; for (int i = 1; i < n; i++) s[i] = in.next() + "<3"; String ss = in.next(); int it = 0; for (int i1 = 0; i1 < n; i1++) for (int i2 = 0; i2 < s[i1].length(); i2++) { char c = s[i1].charAt(i2); while (it < ss.length() && ss.charAt(it) != c) it++; if (it == ss.length()) { out.println("no"); return; } it++; } out.println("yes"); } FastScaner in; PrintWriter out; void run() { in = new FastScaner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } void runWithFiles() { in = new FastScaner(new File("input.txt")); try { out = new PrintWriter(new File("output.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } solve(); out.close(); } public static void main(String[] args) { Locale.setDefault(Locale.US); new CF().run(); } class FastScaner { BufferedReader br; StringTokenizer st; FastScaner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } FastScaner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreElements()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
1e713351d77297a5933a2ac31e21d192
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class B implements Runnable { private void solve() throws IOException { int n = nextInt(); StringBuilder all = new StringBuilder(); all.append("<3"); for (int i = 0; i < n; i++) { all.append(nextToken() + "<3"); } StringBuilder sentence = new StringBuilder(nextToken()); int cur = 0; for (int i = 0; i < sentence.length(); i++) { if (cur < all.length() && sentence.charAt(i) == all.charAt(cur)) { cur++; } } if (cur == all.length()) { pl("yes"); } else { pl("no"); } } public static void main(String[] args) { new B().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new BufferedReader(new InputStreamReader(System.in))); writer = new PrintWriter(System.out); tokenizer = null; solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } BigInteger nextBigInteger() throws IOException { return new BigInteger(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } void p(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } void pl(Object... objects) { p(objects); writer.println(); } int cc; void pf() { writer.printf("Case #%d: ", ++cc); } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
316db31989be408c4b01b0c67516c96c
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.InputMismatchException; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author karan173 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { int n; String s[]; String encode; public void solve(int testNumber, FastReader in, PrintWriter out) { n = in.ni (); s = new String[n]; for (int i = 0; i < n; i++) { s[i] = in.ns (); } encode = in.ns (); int wCtr = 0; int curIndex =0 ; while (wCtr != n) { curIndex = encode.indexOf ("<", curIndex); if (curIndex == -1) { break; } curIndex++; curIndex = encode.indexOf ("3", curIndex); if (curIndex == -1) { break; } curIndex++; curIndex = getIndex (encode, curIndex, s[wCtr++]); if (curIndex == -1) { break; } } //find last heart if (curIndex == -1) { out.println ("no"); return; } curIndex = encode.indexOf ("<", curIndex); if (curIndex == -1) { out.println ("no"); return; } curIndex++; curIndex = encode.indexOf ("3", curIndex); if (curIndex == -1) { out.println ("no"); return; } curIndex++; out.println ("yes"); } private int getIndex(String encode, int curIndex, String s) { for (char c : s.toCharArray ()) { curIndex = encode.indexOf (c, curIndex); if (curIndex == -1) { return -1; } curIndex++; } return curIndex; } } class FastReader { public InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException (); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read (buf); } catch (IOException e) { throw new InputMismatchException (); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int ni() { int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-') { sgn = -1; c = read (); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } while (!isSpaceChar (c)); return res * sgn; } public String ns() { int c = read (); while (isSpaceChar (c)) c = read (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = read (); } while (!isSpaceChar (c)); return res.toString (); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar (c); } return isWhitespace (c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
b266b474c6cdb57825e87c7ad69da182
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Stack; public class Q1 { public static void main(String[] args) { FasterScanner s= new FasterScanner(); PrintWriter out=new PrintWriter(System.out); int n=s.nextInt(); StringBuilder sb=new StringBuilder(); for(int i=0;i<n;i++) { sb.append("<3"); sb.append(s.nextLine()); } char[] b=s.nextLine().toCharArray(); sb.append("<3"); // System.out.println(sb); char[] a=sb.toString().toCharArray(); int j=0; for(int i=0;i<b.length;i++) { if(j<a.length && a[j]==b[i]) { j++; } else { if((b[i]>='a' && b[i]<='z') || (b[i]>='0' && b[i] <='9') || b[i]=='>' || b[i]=='<') continue; else { System.out.println("no"); return; } } } if(j==a.length) { System.out.println("yes"); } else System.out.println("no"); out.close(); } static class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
9db85ddf5983f6414bb7a982971d07c2
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.io.*; import java.util.*; public class cf358b { static FastIO in = new FastIO(), out = in; public static void main(String[] args) { int n = in.nextInt(); StringBuilder sb = new StringBuilder("<3"); for(int i=0; i<n; i++) { sb.append(in.next().trim()); sb.append("<3"); } String a = sb.toString(); String b = in.next().trim(); int apos = 0, bpos = 0; while(apos < a.length() && bpos < b.length()) { if(a.charAt(apos) == b.charAt(bpos)) apos++; bpos++; } out.println(apos==a.length()?"yes":"no"); out.close(); } static class FastIO extends PrintWriter { BufferedReader br; StringTokenizer st; public FastIO() { this(System.in, System.out); } public FastIO(InputStream in, OutputStream out) { super(new BufferedWriter(new OutputStreamWriter(out))); br = new BufferedReader(new InputStreamReader(in)); scanLine(); } public void scanLine() { try { st = new StringTokenizer(br.readLine().trim()); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } public int numTokens() { if (!st.hasMoreTokens()) { scanLine(); return numTokens(); } return st.countTokens(); } public String next() { if (!st.hasMoreTokens()) { scanLine(); return next(); } return st.nextToken(); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output
PASSED
2d5818682c3b70c0a85ae47a487af2a5
train_000.jsonl
1382715000
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (&lt;) and the digit three (3). After applying the code, a test message looks like that: &lt;3word1&lt;3word2&lt;3 ... wordn&lt;3.Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message.Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above.
256 megabytes
import java.io.IOException; import java.io.InputStreamReader; import java.util.InputMismatchException; import java.io.PrintStream; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.TreeSet; import java.io.Reader; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Nipuna Samarasekara */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { ///////////////////////////////////////////////////////////// public void solve(int testNumber, FastScanner in, FastPrinter out) { int N=in.nextInt(); TreeSet<Integer> ts= new TreeSet<Integer>(); StringBuilder sb= new StringBuilder(); ts.add(0); for (int i = 0; i < N; i++) { String ss=in.next(); sb.append(ss); ts.add(ts.last()+ss.length()); } String S=in.next(); int i=0,j=0; int gg=0; while(gg<2&&i<S.length()){ if(gg==0&&S.charAt(i)=='<'){ gg++;i++; } else if(gg==1&&S.charAt(i)=='3'){ gg=2;i++; break; } else i++; } if(gg!=2){ out.println("no"); return; } else gg=0; while(i<S.length()&&j<sb.length()){ if(S.charAt(i)==sb.charAt(j)){ i++;j++; if(ts.contains(j)){ while(gg<2&&i<S.length()){ if(gg==0&&S.charAt(i)=='<'){ gg++;i++; } else if(gg==1&&S.charAt(i)=='3'){ gg=2;i++; break; } else i++; } if(gg!=2){ out.println("no"); return; } else gg=0; } } else i++; } if(j==sb.length()) out.println("yes"); else out.println("no"); } } class FastScanner extends BufferedReader { public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try { int ret = super.read(); // if (isEOF && ret < 0) { // throw new InputMismatchException(); // } // isEOF = ret == -1; return ret; } catch (IOException e) { throw new InputMismatchException(); } } public String next() { StringBuilder sb = new StringBuilder(); int c = read(); while (isWhiteSpace(c)) { c = read(); } if (c < 0) { return null; } while (c >= 0 && !isWhiteSpace(c)) { sb.appendCodePoint(c); c = read(); } return sb.toString(); } static boolean isWhiteSpace(int c) { return c >= 0 && c <= 32; } public int nextInt() { int c = read(); while (isWhiteSpace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int ret = 0; while (c >= 0 && !isWhiteSpace(c)) { if (c < '0' || c > '9') { throw new NumberFormatException("digit expected " + (char) c + " found"); } ret = ret * 10 + c - '0'; c = read(); } return ret * sgn; } public String readLine() { try { return super.readLine(); } catch (IOException e) { return null; } } } class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } }
Java
["3\ni\nlove\nyou\n&lt;3i&lt;3love&lt;23you&lt;3", "7\ni\nam\nnot\nmain\nin\nthe\nfamily\n&lt;3i&lt;&gt;3am&lt;3the&lt;3&lt;main&lt;3in&lt;3the&lt;3&gt;&lt;3family&lt;3"]
2 seconds
["yes", "no"]
NotePlease note that Dima got a good old kick in the pants for the second sample from the statement.
Java 7
standard input
[ "brute force", "strings" ]
36fb3a01860ef0cc2a1065d78e4efbd5
The first line contains integer n (1 ≤ n ≤ 105) — the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less.
1,500
In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise.
standard output