filename_1
stringlengths
8
15
filename_2
stringlengths
8
12
code_1
stringlengths
591
24.4k
code_2
stringlengths
591
35.2k
labels
int64
0
1
notes
stringclasses
2 values
0fd5b95a
6490bbe8
//package codeforces; import java.io.PrintWriter; import java.util.*; public class codeforces { static int dp[][]=new int[5001][5001]; public static void main(String[] args) { Scanner s=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int t=1; for(int tt=0;tt<t;tt++) { int n=s.nextInt(); int a[]=new int[n]; ArrayList<Integer> z=new ArrayList<>(); ArrayList<Integer> o=new ArrayList<>(); for(int i=0;i<n;i++) { a[i]=s.nextInt(); if(a[i]==1) { o.add(i); }else { z.add(i); } } for(int i=0;i<5001;i++) { Arrays.fill(dp[i], -1); } System.out.println(sol(0,0,z,o)); } out.close(); s.close(); } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sortcol(int a[][],int c) { Arrays.sort(a, (x, y) -> { if (x[c] != y[c]) return(int)( x[c] - y[c]); return (int)-(x[1]+x[2] - y[1]-y[2]); }); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static int sol(int i,int j,ArrayList<Integer> z,ArrayList<Integer> o) { if(j==o.size()) { return 0; } int h=z.size()-i; int l=o.size()-j; if(i==z.size()) { return 10000000; } if(dp[i][j]!=-1) { //System.out.println(i+" "+j); return dp[i][j]; } int ans1=sol(i+1,j,z,o); int ans2=sol(i+1,j+1,z,o)+Math.abs(z.get(i)-o.get(j)); dp[i][j]=Math.min(ans1, ans2); return dp[i][j]; } }
import java.util.*; // import java.lang.*; import java.io.*; // THIS TEMPLATE MADE BY AKSH BANSAL. public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static boolean[] isPrime; private static void primes(){ int num = (int)1e6; // PRIMES FROM 1 TO NUM isPrime = new boolean[num]; for (int i = 2; i< isPrime.length; i++) { isPrime[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(isPrime[i] == true) { for(int j = (i*i); j<num; j = j+i) { isPrime[j] = false; } } } } private static long gcd(long a, long b){ if(b==0)return a; return gcd(b,a%b); } private static long pow(long x,long y){ if(y==0)return 1; long temp = pow(x, y/2); if(y%2==1){ return x*temp*temp; } else{ return temp*temp; } } // static ArrayList<Integer>[] adj; // static void getAdj(int n,int q, FastReader sc){ // adj = new ArrayList[n+1]; // for(int i=1;i<=n;i++){ // adj[i] = new ArrayList<>(); // } // for(int i=0;i<q;i++){ // int a = sc.nextInt(); // int b = sc.nextInt(); // adj[a].add(b); // adj[b].add(a); // } // } public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); // primes(); // ________________________________ // int t = sc.nextInt(); // StringBuilder output = new StringBuilder(); // while (t-- > 0) { // output.append(solver()).append("\n"); // } // out.println(output); // _______________________________ int n = sc.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); } out.println(solver(n, arr)); // ________________________________ out.flush(); } public static long solver(int n, int[] arr) { ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); for(int i=0;i<n;i++){ if(arr[i] ==1){ a.add(i); } else{ b.add(i); } } // System.out.println("__"+ a); // System.out.println("__"+ b); long inf = (long)1e10; int aLen = a.size(), bLen = b.size(); long[][] dp = new long[bLen+1][aLen+1]; for(int i=0;i<bLen+1;i++)Arrays.fill(dp[i],inf); // dp[0][0] = 0; for(int i=0;i<=bLen;i++){ dp[i][0] = 0; } for(int i=1;i<=bLen;i++){ for(int j=1;j<=i && j<=aLen;j++){ int aa = a.get(j-1); int bb = b.get(i-1); // System.out.println((i-1)+" "+(j-1)+"__"+ aa+" "+bb); dp[i][j] = Math.min( Math.abs(aa-bb)+dp[i-1][j-1], dp[i-1][j] ); // System.out.println((i-1)+" "+(j-1)+"__"+ dp[i][j]); } } // for(int i=0;i<=bLen;i++){ // for(int j=0;j<=aLen;j++){ // System.out.print(dp[i][j]+" "); // } // System.out.println("__" ); // } return dp[bLen][aLen]==inf?0:dp[bLen][aLen]; } }
0
Non-plagiarised
1d43139f
fdd41565
import java.util.*; import java.io.*; public class experiment { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while( t-- > 0) { char arr[] = new char[5]; for( int i = 0 ;i< 5 ;i++) { arr[i] = (char)(i + 97); } int n =sc.nextInt(); ArrayList<String> input = new ArrayList<>(); for( int i = 0 ;i< n;i++) { input.add( sc.next()); } int max = 0; for( int i = 0 ; i< 5 ;i++) { int test = 0; int sum = 0; char now = arr[i]; ArrayList<Integer> temp = new ArrayList<>(); for( int j = 0 ; j < input.size(); j++) { int local = 0; for( int k = 0 ; k < input.get(j).length(); k++) { if( input.get(j).charAt(k) == now) { local++; } } temp.add( local - (input.get(j).length() - local)); } Collections.sort(temp , Collections.reverseOrder()); //out.println( temp); for( int j = 0 ; j < n ; j++) { sum+=temp.get(j); if( sum<=0) { break; } test++; } max = Math.max(max, test); } out.println( max); } out.flush(); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String args[]) { FastReader s=new FastReader(); int t=s.nextInt(); while(t>0) { Solve solve=new Solve(); t--; int n=s.nextInt(); String str[]=new String[n]; for(int i=0;i<n;i++) str[i]=s.nextLine(); char array[]=new char[]{'a','b','c','d','e'}; int arr[]=new int[n]; int ans=0; for(int i=0;i<5;i++) { Arrays.fill(arr,0); for(int j=0;j<n;j++) { for(int k=0;k<str[j].length();k++) { if(str[j].charAt(k)==array[i]) arr[j]++; else arr[j]--; } } ans=(ans>solve.solve(arr,n))?ans:solve.solve(arr,n); } System.out.println(ans); } } } class Solve{ public int solve(int arr[],int n) { int ans=0; int sum=0; Arrays.sort(arr); for(int i=n-1;i>=0;i--) { if(sum+arr[i]>0) { sum+=arr[i]; ans++; } else break; } return ans; } } 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; } }
0
Non-plagiarised
6bcc5afd
cb87df79
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int a[]=new int[n]; ArrayList<Integer> lt1=new ArrayList<>(); ArrayList<Integer> lt0=new ArrayList<>(); for(int i=0;i<n;i++) { int l=s.nextInt(); if(l==0) lt0.add(i+1); else lt1.add(i+1); } int dp[][]=new int[lt1.size()+1][lt0.size()+1]; for(int i=1;i<=lt1.size();i++) { dp[i][i]=dp[i-1][i-1]+Math.abs(lt0.get(i-1)-lt1.get(i-1)); for(int j=i+1;j<=lt0.size();j++) { dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(lt1.get(i-1)-lt0.get(j-1))); } } System.out.println(dp[lt1.size()][lt0.size()]); } }
import java.util.*; public class Longjumps { public static void main(String[] args){ Scanner sc=new Scanner(System.in); ArrayList<Integer> o=new ArrayList<Integer>(), e=new ArrayList<Integer>(); int n = sc.nextInt(); for(int i=1;i<=n;i++){ int x=sc.nextInt(); if(x==1)o.add(i); else e.add(i); } int dp[][]=new int[o.size()+1][e.size()+1]; for(int i=1;i<=o.size();i++){ dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1)); for(int j=i+1;j<=e.size();j++) dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1))); } System.out.println(dp[o.size()][e.size()]); } }
1
Plagiarised
0b91922c
71a4f6d2
import java.util.*; import java.lang.*; import java.io.*; import java.math.*; public class Prac{ static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nia(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public int[] nia1(int n) { int a[] = new int[n+1]; for (int i = 1; i <=n; i++) { a[i] = ni(); } return a; } public long[] nla(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public long[] nla1(int n) { long a[] = new long[n+1]; for (int i = 1; i <= n; i++) { a[i] = nl(); } return a; } public Long[] nLa(int n) { Long a[] = new Long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public Long[] nLa1(int n) { Long a[] = new Long[n+1]; for (int i = 1; i <= n; i++) { a[i] = nl(); } return a; } public Integer[] nIa(int n) { Integer a[] = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public Integer[] nIa1(int n) { Integer a[] = new Integer[n+1]; for (int i = 1; i <= n; i++) { a[i] = ni(); } return a; } public String rs() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static class Key { private final int x, y; public Key(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Key)) return false; Key key = (Key) o; return x == key.x && y == key.y; } @Override public int hashCode() { int result = x; result = 31 * result + y; return result; } } static class Pair{ int x,y; Pair(int a,int b){ x=a;y=b; } // @Override // public int compareTo(Pair p) { // if(x==p.x) return y-p.y; // return x-p.x; // } } static void shuffleArray(long temp[]){ int n = temp.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = temp[i]; int randomPos = i + rnd.nextInt(n-i); temp[i] = temp[randomPos]; temp[randomPos] = tmp; } } static int gcd(int a,int b){ return b==0?a:gcd(b,a%b);} //static long lcm(long a,long b){return (a/gcd(a,b))*b;} static PrintWriter w = new PrintWriter(System.out); static long mod1=998244353L,mod=1000000007; //static int r[]={0,1,0,-1}, c[]={1,0,-1,0}; static int[] nextG(int arr[]){ int n = arr.length; Stack<Integer> s = new Stack<>(); int ng[] = new int[n]; for(int i = 0 ; i < n ; i++){ while(!s.isEmpty() && arr[s.peek()] <= arr[i]){ ng[s.pop()] = i; } s.add(i); } while(!s.isEmpty()){ ng[s.pop()] = n; } return ng; } static int[] nextS(int arr[]){ int n = arr.length; Stack<Integer> s = new Stack<>(); int ns[] = new int[n]; for(int i = 0 ; i < n ; i++){ while(!s.isEmpty() && arr[s.peek()] >= arr[i]){ ns[s.pop()] = i; } s.add(i); } while(!s.isEmpty()){ ns[s.pop()] = n; } return ns; } static int[] prevG(int arr[]){ int n = arr.length; Stack<Integer> s = new Stack<>(); int pg[] = new int[n]; for(int i = n-1 ; i >= 0 ; i--){ while(!s.isEmpty() && arr[s.peek()] <= arr[i]){ pg[s.pop()] = i; } s.add(i); } while(!s.isEmpty()){ pg[s.pop()] = -1; } return pg; } static int[] prevS(int arr[]){ int n = arr.length; Stack<Integer> s = new Stack<>(); int ps[] = new int[n]; for(int i = n-1 ; i >= 0 ; i--){ while(!s.isEmpty() && arr[s.peek()] >= arr[i]){ ps[s.pop()] = i; } s.add(i); } while(!s.isEmpty()){ ps[s.pop()] = -1; } return ps; } public static void main(String [] args){ InputReader sc=new InputReader(System.in); int n = sc.ni(); int arr [] = sc.nia(n); int ng[] = nextG(arr); int ns [] = nextS(arr); int pg[] = prevG(arr); int ps[] = prevS(arr); int ans[]=new int[n]; Arrays.fill(ans,10000000); ans[n-1] = 0; for(int i = n -1 ; i >= 0 ; i --){ if(ns[i] != n){ ans[i] = Math.min(ans[i] , ans[ns[i]]+1); } if(ng[i] != n){ ans[i] = Math.min(ans[i] , ans[ng[i]]+1); } if(pg[i] != -1){ ans[pg[i]] = Math.min(ans[pg[i]] , ans[i]+1); } if(ps[i] != -1){ ans[ps[i]] = Math.min(ans[ps[i]] , ans[i]+1); } } w.println(ans[0]); w.close(); } }
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Vector; import java.util.InputMismatchException; import java.io.IOException; import java.util.Stack; import java.io.InputStream; 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); DDiscreteCentrifugalJumps solver = new DDiscreteCentrifugalJumps(); solver.solve(1, in, out); out.close(); } static class DDiscreteCentrifugalJumps { public void solve(int testNumber, InputReader s, PrintWriter w) { int n = s.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = s.nextInt(); int[] dp = new int[n]; for (int i = 0; i < n; i++) dp[i] = i; Stack<Integer> dec = new Stack<>(); dec.push(0); Stack<Integer> inc = new Stack<>(); inc.push(0); for (int i = 1; i < n; i++) { while (!dec.isEmpty() && a[dec.peek()] < a[i]) { dp[i] = Math.min(dp[i], dp[dec.peek()] + 1); dec.pop(); } if (!dec.isEmpty()) { dp[i] = Math.min(dp[i], dp[dec.peek()] + 1); if (a[dec.peek()] == a[i]) dec.pop(); } dec.push(i); while (!inc.isEmpty() && a[inc.peek()] > a[i]) { dp[i] = Math.min(dp[i], dp[inc.peek()] + 1); inc.pop(); } if (!inc.isEmpty()) { dp[i] = Math.min(dp[i], dp[inc.peek()] + 1); if (a[inc.peek()] == a[i]) inc.pop(); } inc.push(i); } w.println(dp[n - 1]); } } 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
0
Non-plagiarised
08b9908d
f5fb1b62
import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc=new FastReader(); static long dp[][]; static int mod=1000000007; static int max; static long bit[]; static long x; static long y; static long B[][]; static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) { //CHECK FOR N=1 //CHECK FOR N=1 //StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); outer :while (ttt-- > 0) { int n=i(); int k=i(); int P[]=input(k); int Q[]=input(k); long A[]=new long[n+1]; Arrays.fill(A, Integer.MAX_VALUE); for(int i=0;i<k;i++) { A[P[i]]=Q[i]; } long B[]=new long[n+1]; long C[]=new long[n+1]; Arrays.fill(B, Integer.MAX_VALUE); Arrays.fill(C, Integer.MAX_VALUE); for(int i=n;i>0;i--) { if(i+1<=n) { B[i]=min(A[i],B[i+1]+1); } else { B[i]=A[i]; } } for(int i=1;i<=n;i++) { if(i-1>0) { C[i]=min(A[i],C[i-1]+1); } else { C[i]=A[i]; } } for(int i=1;i<=n;i++) { out.print(min(B[i],C[i])+" "); } out.println(); } //System.out.println(sb.toString()); out.close(); //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 } static class Pair implements Comparable<Pair> { int x; int y; Pair(int x,int y,int z){ this.x=x; this.y=y; } @Override public int compareTo(Pair o) { if(this.x>o.x) return 1; else if(this.x<o.x) return -1; else { if(this.y>o.y) return 1; else if(this.y<o.y) return -1; else return 0; } } // public int hashCode() // { // final int temp = 14; // int ans = 1; // ans =x*31+y*13; // return ans; // } // @Override // public boolean equals(Object o) // { // if (this == o) { // return true; // } // if (o == null) { // return false; // } // if (this.getClass() != o.getClass()) { // return false; // } // Pair other = (Pair)o; // if (this.x != other.x || this.y!=other.y) { // return false; // } // return true; // } // /* FOR TREE MAP PAIR USE */ // public int compareTo(Pair o) { // if (x > o.x) { // return 1; // } // if (x < o.x) { // return -1; // } // if (y > o.y) { // return 1; // } // if (y < o.y) { // return -1; // } // return 0; // } } //FENWICK TREE static void update(int i, int x){ for(; i < bit.length; i += (i&-i)) bit[i] += x; } static int sum(int i){ int ans = 0; for(; i > 0; i -= (i&-i)) ans += bit[i]; return ans; } //END //static void add(int v) { // if(!map.containsKey(v)) { // map.put(v, 1); // } // else { // map.put(v, map.get(v)+1); // } //} //static void remove(int v) { // if(map.containsKey(v)) { // map.put(v, map.get(v)-1); // if(map.get(v)==0) // map.remove(v); // } //} public static int upper(int A[],int k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { ans=mid; l=mid+1; } else { u=mid-1; } } return ans; } public static int lower(int A[],int k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<k) { l=mid+1; } else { ans=mid; u=mid-1; } } return ans; } static int[] copy(int A[]) { int B[]=new int[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long[] copy(long A[]) { long B[]=new long[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void reverse(long A[]) { int n=A.length; long B[]=new long[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void reverse(int A[]) { int n=A.length; int B[]=new int[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static int[][] input(int n,int m){ int A[][]=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=i(); } } return A; } static char[][] charinput(int n,int m){ char A[][]=new char[n][m]; for(int i=0;i<n;i++) { String s=s(); for(int j=0;j<m;j++) { A[i][j]=s.charAt(j); } } return A; } static int find(int A[],int a) { if(A[a]==a) return a; return A[a]=find(A, A[a]); } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long [] prefix(long A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] prefix(int A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] suffix(long A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static long [] suffix(int A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static void fill(int dp[]) { Arrays.fill(dp, -1); } static void fill(int dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(int dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(int dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static void fill(long dp[]) { Arrays.fill(dp, -1); } static void fill(long dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(long dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(long dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long power(long x, long y, long p) { if(y==0) return 1; if(x==0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static HashMap<Integer,Integer> hash(int A[]){ HashMap<Integer,Integer> map=new HashMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Integer,Integer> tree(int A[]){ TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static 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; } } }
//package graphs; import java.util.*; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.StringTokenizer; //import graphs.Segment_Trees.FastReader; public class PW { //private static BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); public static int memo[][]; public static FastReader s = new FastReader(); public static List<Long> primes; public static int sz= 1000000; //public static int arr[]; public static int MOD=1000000007; public static List<List<Integer>> adj; public static void main(String[] args) { // TODO Auto-generated method stub //FastScanner sc=new FastScanner(); int t=s.nextInt(); while(t-- >0) { //int n=0,a=0,b=0; int n=s.nextInt(); int k=s.nextInt(); long arr[]=new long[n]; Arrays.fill(arr,Long.MAX_VALUE/10); int ac[]=new int[k]; long tmp[]=new long[k]; for(int i = 0 ; i < k ; i++){ ac[i]=s.nextInt(); ac[i]--; } for(int i = 0 ; i < k ; i++){ tmp[i]=s.nextLong(); arr[ac[i]] = tmp[i]; } long P[] = getP(arr, n); int i=0,j=arr.length-1; while(i<=j) { long temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; i++; j--; } //reverse(all(arr)); long[] S = getP(arr, n); //reverse(all(S)); i=0;j=S.length-1; while(i<=j) { long temp=S[i]; S[i]=S[j]; S[j]=temp; i++; j--; } for(i = 0 ; i < n ; i++){ System.out.print(Math.min(P[i], S[i])+" "); } System.out.println(); } } public static long[] getP(long []arr, int n){ long mn = Long.MAX_VALUE/10; long P[]=new long[n]; Arrays.fill(P, Long.MAX_VALUE/10); int cnt = 0; for(int i = 0 ; i < n ; i++){ long curr = arr[i]; if(mn + cnt < curr){ P[i] = mn + cnt; }else{ mn = arr[i]; P[i] = arr[i]; cnt = 0; } cnt++; } return P; } public static void solve(int a[],int b[],int n,int m,int k) { List<Integer> ans = new ArrayList<>(); int i = 0; int j = 0; while (i < n && j < m) { if (a[i] == 0) { ans.add(a[i]); k++; i++; continue; } if (b[j] == 0) { ans.add(b[j]); k++; j++; continue; } if (a[i] > 0 && b[j] > 0) { if (a[i] <= b[j]) { if (a[i] > k) { System.out.println(-1); return; } ans.add(a[i]); i++; } else { if (b[j] > k) { System.out.println(-1); return; } ans.add(b[j]); j++; } } } while (i < n) { if (a[i] > k) { System.out.println(-1); return; } if (a[i] == 0) k++; ans.add(a[i++]); } while (j < m) { if (b[j] > k) { System.out.println(-1); return; } if (b[j] == 0) k++; ans.add(b[j++]); } for (int x : ans) { System.out.print(x + " "); } System.out.println(); } public static void dfs(int u, boolean visited[]){ visited[u] = true; for(int v : adj.get(u)){ if(!visited[v]) dfs(v, visited); } } public static boolean check(char c[][]) { for(int i=0;i<c.length;i++) { for(int j=0;j<c[i].length;j++) { if(j+1<c[i].length && c[i][j]==c[i][j+1]) return false; if(i+1<c.length && c[i][j]==c[i+1][j]) return false; } } return true; } public static int solve(long n) { int c=0; for(long i=2;i<Math.sqrt(n);i++) { if(n%i==0) { while(n%i==0) { n/=i; c++; } } } if(n!=1) c++; return c; } public static boolean helper(int arr[], int i, int sum){ if(sum == 0){ return true; } if(i>=arr.length || sum < 0){ return false; } if(memo[i][sum] != -1){ return memo[i][sum]==0?false:true; } boolean ans1 = helper(arr,i+1,sum-arr[i]); boolean ans2 = helper(arr,i+1,sum); boolean fin=ans1||ans2; if(fin==true) memo[i][sum]=1; else memo[i][sum]=0; return fin ; } // public static long fastExpo(long a, long n, long mod) { // long result = 1; // while (n > 0) { // if ((n & 1)>0) // result = (result * a) % mod; // a = (a * a) % mod; // n >>= 1; // } // return result; // } // 1 4 8 public static int digit(int n) { int c=0; while(n!=0) { n/=10; c++; } return c; } public static int rev(String n) { String ans=""; for(int i=n.length()-1;i>=0;i--) ans=ans+n.charAt(i); return Integer.valueOf(ans); } public static boolean solve(HashMap<Integer,Integer> mp1,HashMap<Integer,Integer> mp2) { for(int i:mp2.keySet()) { if(!mp1.containsKey(i)) return false; if(mp1.get(i)<mp2.get(i)) return false; } return true; } public static int count(int b[]){ int s = 0; for (int i = 0; i < 32; i++) { if(b[i]>0){ s |= (1<<i); } } return s; } public static void remove(int b[], int val){ for (int i = 0; i < 32; i++) { if(((val>>i)&1)==1) b[i]--; } } public static void add(int b[], int val){ for (int i = 0; i < 32; i++) { if(((val>>i)&1)==1) b[i]++; } } public static String largestNumber( List<String> ab) { // List<String> ab= new ArrayList<>(); // for(int i=0;i<A.size();i++) // { // ab.add(String.valueOf(A.get(i))); // } Collections.sort(ab, new Comparator<String>(){ public int compare(String X, String Y) { // first append Y at the end of X String XY=X + Y; // then append X at the end of Y String YX=Y + X; // Now see which of the two formed numbers // is greater return XY.compareTo(YX)>0?-1:1; } }); StringBuilder abc= new StringBuilder(); for(int i=0;i<ab.size();i++) { abc.append(ab.get(i)); } if(abc.length()==0) return abc.toString(); if(abc.charAt(0)=='0') return "0"; else return abc.toString(); } public static boolean pal(String s) { int i=0; int j=s.length()-1; while(i<=j) { if(s.charAt(i)!=s.charAt(j)) return false; i++; j--; } return true; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static long solve(int n, int r) { long p = 1, k = 1; if (n - r < r) { r = n - r; } if (r != 0) { while (r > 0) { p *= n; k *= r; long m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } //System.out.println(p); return p; } public static long gcd(long a,long b) { if(a==0||b==0) return a+b; return gcd(b,(a%b)); } public static long __gcd(long n1, long n2) { long gcd = 1; for (int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) { gcd = i; } } return gcd; } public static boolean prime(int n) { if(n<=2) return true; for(int i=2;i<=Math.sqrt(n);i++) { if(n%i==0) return false; } return true; } public static long fastExpo(long a,long n,long mod){ if (n == 0) return 1; else{ long x = fastExpo(a,n/2,mod); if ((n&1) == 1){ return (((a*x)%mod)*x)%mod; } else{ return (((x%mod)*(x%mod))%mod)%mod; } } } } class pair{ //public: int f; long s; pair(int x,long y) { f=x; s=y; } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int 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; } }
0
Non-plagiarised
00c0b82a
1ea771ea
import java.util.*; public class E1547 { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int q = sc.nextInt(); for(int i = 0; i < q; i++){ int n = sc.nextInt(); int k = sc.nextInt(); int[][] t = new int[k][2]; for(int j = 0; j < k; j++){ t[j][0] = sc.nextInt();//room } for(int j = 0; j < k; j++){ t[j][1] = sc.nextInt();//air } long[] left = new long[n]; long[] right = new long[n]; long tmp = Integer.MAX_VALUE; long[] max =new long[n]; for(int j = 0; j < n; j++){ max[j] = Integer.MAX_VALUE; } for (int j = 0; j < k; j++) { max[t[j][0]-1] = t[j][1]; } for (int j = 1; j <= n; j++) { tmp = Math.min(tmp+1, max[j-1]); left[j-1] = tmp; } for(int j = n; j >= 1; j--){ tmp = Math.min(tmp+1, max[j-1]); right[j-1] = tmp; } for(int j = 0; j < n; j++){ System.out.print(Math.min(left[j], right[j]) + " "); } System.out.println(); } } }
import java.io.*; import java.util.*; public class CODECHEF { static class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } static long MOD=1000000000; static class Pair{ long a; int b; Pair(long i,int j){ a=i; b=j; } } static long[] solve(int[] pos,long[] arr,int n,int k){ long[] ans=new long[n]; long[] left=new long[n]; long[] right=new long[n]; long min=Integer.MAX_VALUE; for(int i=0;i<n;i++){ min=Math.min(min+1,arr[i]); left[i]=min; } min=Integer.MAX_VALUE; for(int i=n-1;i>=0;i--){ min=Math.min(min+1,arr[i]); right[i]=min; } for(int i=0;i<n;i++){ ans[i]=Math.min(left[i],right[i]); } return ans; } public static void main(String[] args) throws java.lang.Exception { FastReader fs=new FastReader(System.in); // StringBuilder sb=new StringBuilder(); // PrintWriter out=new PrintWriter(System.out); int t=fs.nextInt(); while (t-->0){ int n=fs.nextInt(); int k=fs.nextInt(); int[] pos=new int[k]; for(int i=0;i<k;i++) pos[i]=fs.nextInt()-1; long[] temp=new long[n]; int ptr=0; Arrays.fill(temp,Integer.MAX_VALUE); for(int i=0;i<k;i++) temp[pos[ptr++]]=fs.nextLong(); long[] ans=solve(pos,temp,n,k); for(int i=0;i<n;i++) System.out.print(ans[i]+" "); System.out.println(); } //out.close; } }
0
Non-plagiarised
73f57af1
9a20c823
/*input 3 2 1 6 3 8 1 2 3 1 3 4 6 7 9 1 2 2 3 6 3 14 12 20 12 19 2 12 10 17 3 17 3 2 6 5 1 5 2 6 4 6 */ import java.util.*; import java.lang.*; import java.io.*; public class Main { static PrintWriter out; static int MOD = 1000000007; static FastReader scan; /*-------- I/O usaing short named function ---------*/ public static String ns(){return scan.next();} public static int ni(){return scan.nextInt();} public static long nl(){return scan.nextLong();} public static double nd(){return scan.nextDouble();} public static String nln(){return scan.nextLine();} public static void p(Object o){out.print(o);} public static void ps(Object o){out.print(o + " ");} public static void pn(Object o){out.println(o);} /*-------- for output of an array ---------------------*/ static void iPA(int arr []){ StringBuilder output = new StringBuilder(); for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output); } static void lPA(long arr []){ StringBuilder output = new StringBuilder(); for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output); } static void sPA(String arr []){ StringBuilder output = new StringBuilder(); for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output); } static void dPA(double arr []){ StringBuilder output = new StringBuilder(); for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output); } /*-------------- for input in an array ---------------------*/ static void iIA(int arr[]){ for(int i=0; i<arr.length; i++)arr[i] = ni(); } static void lIA(long arr[]){ for(int i=0; i<arr.length; i++)arr[i] = nl(); } static void sIA(String arr[]){ for(int i=0; i<arr.length; i++)arr[i] = ns(); } static void dIA(double arr[]){ for(int i=0; i<arr.length; i++)arr[i] = nd(); } /*------------ for taking input faster ----------------*/ 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; } } // Method to check if x is power of 2 static boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0);} //Method to return lcm of two numbers static int gcd(int a, int b){return a==0?b:gcd(b % a, a); } //Method to count digit of a number static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);} //Method for sorting static void ruffle_sort(int[] a) { //shandom_ruffle Random r=new Random(); int n=a.length; for (int i=0; i<n; i++) { int oi=r.nextInt(n); int temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //sort Arrays.sort(a); } //Method for checking if a number is prime or not static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long[] l, r; public static void main (String[] args) throws java.lang.Exception { OutputStream outputStream =System.out; out =new PrintWriter(outputStream); scan =new FastReader(); //for fast output sometimes StringBuilder sb = new StringBuilder(); int t = ni(); while(t-->0){ int n = ni(); l = new long[n]; r = new long[n]; for(int i=0; i<n; i++){ l[i] = nl(); r[i] = nl(); } //lPA(l); //lPA(r); ArrayList<Integer> adj[] = new ArrayList[n]; for(int i=0; i<n; i++) adj[i] = new ArrayList<Integer>(); for(int i=0; i<n-1; i++){ int u = ni()-1, v = ni()-1; adj[u].add(v); adj[v].add(u); } dp = new Long[n][2]; visited = new boolean[n]; long ans = Math.max(solve(adj, 0, 0, visited), solve(adj, 0, 1, visited)); pn(ans); } out.flush(); out.close(); } static Long dp[][]; static boolean visited[]; static long solve(ArrayList<Integer> adj[], int vertex, int prev, boolean visited[]){ visited[vertex] = true; if(dp[vertex][prev] != null) return dp[vertex][prev]; long ans = 0; for(int x : adj[vertex]){ if(!visited[x]){ if(prev == 0){ ans += Math.max(Math.abs(l[vertex] - l[x]) + solve(adj, x, 0, visited), Math.abs(l[vertex] - r[x]) + solve(adj, x, 1, visited)); //pn(vertex + " " + x + " " + ans); }else{ ans += Math.max(Math.abs(r[vertex] - l[x]) + solve(adj, x, 0, visited), Math.abs(r[vertex] - r[x]) + solve(adj, x, 1, visited)); //pn(vertex + " " + x + " " + ans); } } } visited[vertex] = false; //pn(ans); return dp[vertex][prev] = ans; } }
import java.util.*; import java.io.*; public class Parsas_Humongous_Tree { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void shuffle(int[] a) { Random r = new Random(); for (int i = 0; i <= a.length - 2; i++) { int j = i + r.nextInt(a.length - i); swap(a, i, j); } } public static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void main(String[] args) { // TODO Auto-generated method stub FastReader t = new FastReader(); PrintWriter o = new PrintWriter(System.out); int test = t.nextInt(); while (test-- > 0) { int n = t.nextInt(); long[][] cost = new long[n][2]; List<Integer>[] graph = new ArrayList[n]; dp = new long[n][2]; for (int i = 0; i < n; ++i) { cost[i][0] = t.nextLong(); cost[i][1] = t.nextLong(); graph[i] = new ArrayList<>(); } for (int i = 0; i < n - 1; ++i) { int x = t.nextInt() - 1; int y = t.nextInt() - 1; graph[x].add(y); graph[y].add(x); } o.println(Math.max(dfs(graph, cost, 0, 0, -1), dfs(graph, cost, 0, 1, -1))); } o.flush(); o.close(); } private static long[][] dp; private static long dfs(List<Integer>[] graph, long[][] cost, int u, int j, int par) { if (dp[u][j] != 0) return dp[u][j]; for (int v : graph[u]) if (v != par) { long c1 = Math.abs(cost[u][j] - cost[v][0]) + dfs(graph, cost, v, 0, u); long c2 = Math.abs(cost[u][j] - cost[v][1]) + dfs(graph, cost, v, 1, u); dp[u][j] += Math.max(c1, c2); } return dp[u][j]; } }
0
Non-plagiarised
680ba922
6e207cbf
import java.util.*; import java.io.*; public class Solution { 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') { if (cnt != 0) { break; } else { continue; } } 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(); } } static int parent(int a , int p[]) { if(a == p[a]) return a; return p[a] = parent(p[a],p); } static void union(int a , int b , int p[] , int size[]) { a = parent(a,p); b = parent(b,p); if(a == b) return; if(size[a] < size[b]) { int temp = a; a = b; b = temp; } p[b] = a; size[a] += size[b]; } static long getSum(int index , long BITree[]) { long sum = 0; // Iniialize result // index in BITree[] is 1 more than // the index in arr[] // index = index + 1; // Traverse ancestors of BITree[index] while(index>0) { // Add current element of BITree // to sum sum += BITree[index]; // Move index to parent node in // getSum View index -= index & (-index); } return sum; } // Updates a node in Binary Index Tree (BITree) // at given index in BITree. The given value // 'val' is added to BITree[i] and all of // its ancestors in tree. public static void updateBIT(int n, int index, long val , long BITree[]) { // index in BITree[] is 1 more than // the index in arr[] // index = index + 1; // Traverse all ancestors and add 'val' while(index <= n) { // Add 'val' to current node of BIT Tree BITree[index] += val; // Update index to that of parent // in update View index += index & (-index); } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int dp[][]; static int f(int pos , int take , int arr[]) { if(pos == -1) { if(take == 0) return 0; return -10000000; } if(dp[pos][take] != -1) return dp[pos][take]; if(pos+1-take == arr[pos]) dp[pos][take] = Math.max(dp[pos][take],1 + f(pos-1,take,arr)); dp[pos][take] = Math.max(dp[pos][take],f(pos-1,take,arr)); if(take > 0) dp[pos][take] = Math.max(dp[pos][take],f(pos-1,take-1,arr)); return dp[pos][take]; } public static void main(String []args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); sc.nextLine(); String a = sc.nextLine(); String b = sc.nextLine(); int same = 0 , zo = 0 , oz = 0 , oo = 0 , zz = 0; for(int i = 0 ; i < n ; i++) { if(a.charAt(i) == '0' && b.charAt(i) == '1') oz++; else if(a.charAt(i) == '1' && b.charAt(i) == '0') zo++; else if(a.charAt(i) == '1' && b.charAt(i) == '1') oo++; else zz++; } if(oz == zo || (zz == oo-1)) { int mx = Integer.MAX_VALUE; if(oz == zo) mx = Math.min(mx,2*oz); if(oo-1 == zz) mx = Math.min(mx,zz+oo); System.out.println(mx); } else { System.out.println(-1); } } } }
import java.util.*; import java.lang.*; import java.io.*; public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int sm, n; while(t > 0) { t--; n = sc.nextInt(); String s1,s2; s1 = sc.next(); s2 = sc.next(); int a[] = new int[4]; a[0] = 0; a[1] = 0; a[2] = 0; a[3] = 0; for(int i = 0 ; i < n ; i++) { if(s1.charAt(i) == '0'&& s2.charAt(i) == '1') a[0]++; else if(s1.charAt(i) == '1'&& s2.charAt(i) == '0') a[1]++; else if(s1.charAt(i) == '1'&& s2.charAt(i) == '1') a[2]++; else a[3]++; } // System.out.println(a[0] + " " + a[1] + " " + a[2] + " " + a[3]); int n1 = Integer.MAX_VALUE, n2 = Integer.MAX_VALUE, n3 = Integer.MAX_VALUE; if (a[0] == a[1]) { n1 = 2*a[0]; } if((a[2] - 1) == a[3]) { // System.out.println(a[3] + 1); n2 = 2*a[3] + 1; } if((a[3] + 1) == a[2]) { // System.out.println(a[2] + 1); n3 = 2*a[2] + 1; } int ans = Math.min(n1, Math.min(n2,n3)); if(ans == Integer.MAX_VALUE) { System.out.println("-1"); } else { System.out.println(ans); } } } }
0
Non-plagiarised
169e34bf
2bbf754b
import java.util.*; public class D{ static Scanner sc; public static void solve(){ int n=sc.nextInt(); Integer a[]=new Integer[n]; int flag; for(int i=0;i<n;i++) a[i]=sc.nextInt(); String s=sc.next(); ArrayList<Integer> x=new ArrayList<>(); ArrayList<Integer> y=new ArrayList<>(); for(int i=0;i<n;i++){ if(s.charAt(i)=='B') x.add(a[i]); else y.add(a[i]); } Collections.sort(x); Collections.sort(y); int p=n; int q=1; for(int i=y.size()-1;i>=0;i--){ if(y.get(i)>p){System.out.println("NO"); return;} p-=1; } for(int i=0;i<x.size();i++){ if(x.get(i)<q){System.out.println("NO"); return;} q+=1; } System.out.println("YES"); } public static void main(String args[]){ sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) solve(); } }
import java.util.*; /** __ __ ( _) ( _) / / \\ / /\_\_ / / \\ / / | \ \ / / \\ / / |\ \ \ / / , \ , / / /| \ \ / / |\_ /| / / / \ \_\ / / |\/ _ '_| \ / / / \ \\ | / |/ 0 \0\ / | | \ \\ | |\| \_\_ / / | \ \\ | | |/ \.\ o\o) / \ | \\ \ | /\\`v-v / | | \\ | \/ /_| \\_| / | | \ \\ | | /__/_ `-` / _____ | | \ \\ \| [__] \_/ |_________ \ | \ () / [___] ( \ \ |\ | | // | [___] |\| \| / |/ /| [____] \ |/\ / / || ( \ [____ / ) _\ \ \ \| | || \ \ [_____| / / __/ \ / / // | \ [_____/ / / \ | \/ // | / '----| /=\____ _/ | / // __ / / | / ___/ _/\ \ | || (/-(/-\) / \ (/\/\)/ | / | / (/\/\) / / // _________/ / / \____________/ ( */ public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-- >0) { int n=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } String str=sc.next(); ArrayList<Pair> plist=new ArrayList<>(); for(int i=0;i<n;i++) { char ch=str.charAt(i); plist.add(new Pair(arr[i],ch)); } //B-reduce //R-increse Collections.sort(plist); int counter=1; boolean flag=false; for(int i=0;i<plist.size();i++) { int val=plist.get(i).number; int clr=plist.get(i).color; if(clr=='B') { if(val<counter) { flag=true; break; } } else { if(val>counter) { flag=true; break; } } counter++; } System.out.println(flag?"NO":"YES"); } } public static class Pair implements Comparable<Pair>{ int number; char color; Pair(int number,char color){ this.number=number; this.color=color; } @Override public int compareTo(Pair o) { if(o.color==this.color) { return this.number-o.number; } else { return this.color-o.color; } } public String toString() { return number+"-"+color+"."; } } }
0
Non-plagiarised
4da08761
6f393cfe
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Solution { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Main solver = new Main(); boolean multipleTC = true; int testCount = multipleTC ? Integer.parseInt(in.next()) : 1; for (int i = 1; i <= testCount; i++) solver.solve(in, out, i); out.close(); } static class Main { PrintWriter out; InputReader in; public void solve(InputReader in, PrintWriter out, int test) { this.out = out; this.in = in; int n = ni(); String[] arr = new String[n]; int[][] freq = new int[n][5]; int[][] rem = new int[n][5]; for(int i = 0; i < n; i++){ arr[i] = n(); for(int j = 0; j < arr[i].length(); j++) freq[i][arr[i].charAt(j) - 'a']++; for(int j = 0; j < 5; j++) rem[i][j] = arr[i].length() - freq[i][j]; } int ans = 0; for(int i = 0; i < 5; i++){ int[] vals = new int[n]; for(int j = 0; j < n; j++) vals[j] = freq[j][i] - rem[j][i]; Arrays.sort(vals); int sum = 0, x = 0; for(int j = n - 1; j >= 0; j--){ if(sum + vals[j] > 0){ x++; sum += vals[j]; } else { break; } } if(x > ans) { ans = x; } } pn(ans); } int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } String n(){ return in.next(); } int ni() { return in.nextInt(); } long nl() { return in.nextLong(); } void pn(long zx) { out.println(zx); } void pn(String sz) { out.println(sz); } void pn(double dx){ out.println(dx); } class Tuple { long x; long y; Tuple(long a, long b) { x = a; y = b; } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new UnknownError(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
import java.io.*; import java.util.*; public class C { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); try{ int t = Integer.parseInt(br.readLine()); while(t-->0){ int n = Integer.parseInt(br.readLine()); int lst[][] = new int[n][5]; for(int i=0; i<n; i++){ String s = br.readLine(); for(int j=0; j<s.length(); j++){ lst[i][s.charAt(j)-'a']++; } } int fans = Integer.MIN_VALUE; for(int i=0; i<5; i++){ int val[] = new int[n]; for(int k=0; k<n; k++){ int sum = 0; for(int j=0; j<5; j++){ if(i==j){ sum += lst[k][j]; }else{ sum -= lst[k][j]; } } val[k] = sum; } Arrays.sort(val); int sum = 0; int ans = 0; for(int x = n-1; x>=0; x--){ sum+=val[x]; if(sum>0){ ans++; }else{ break; } } fans = Math.max(fans, ans); } bw.write(fans+"\n"); } bw.flush(); }catch(Exception e){ return; } } }
0
Non-plagiarised
317a209c
6b97058e
import java.io.*; import java.util.*; public class D_Java { public static final int MOD = 998244353; public static int mul(int a, int b) { return (int)((long)a * (long)b % MOD); } int[] f; int[] rf; public int C(int n, int k) { return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n-k], rf[k])); } public static int pow(int a, int n) { int res = 1; while (n != 0) { if ((n & 1) == 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } static void shuffleArray(int[] a) { Random rnd = new Random(); for (int i = a.length-1; i > 0; i--) { int index = rnd.nextInt(i + 1); int tmp = a[index]; a[index] = a[i]; a[i] = tmp; } } public static int inv(int a) { return pow(a, MOD-2); } public void doIt() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tok.nextToken()); int k = Integer.parseInt(tok.nextToken()); f = new int[n+42]; rf = new int[n+42]; f[0] = rf[0] = 1; for (int i = 1; i < f.length; ++i) { f[i] = mul(f[i-1], i); rf[i] = mul(rf[i-1], inv(i)); } int[] events = new int[2*n]; for (int i = 0; i < n; ++i) { tok = new StringTokenizer(in.readLine()); int le = Integer.parseInt(tok.nextToken()); int ri = Integer.parseInt(tok.nextToken()); events[i] = le*2; events[i + n] = ri*2 + 1; } shuffleArray(events); Arrays.sort(events); int ans = 0; int balance = 0; for (int r = 0; r < 2*n;) { int l = r; while (r < 2*n && events[l] == events[r]) { ++r; } int added = r - l; if (events[l] % 2 == 0) { // Open event ans += C(balance + added, k); if (ans >= MOD) ans -= MOD; ans += MOD - C(balance, k); if (ans >= MOD) ans -= MOD; balance += added; } else { // Close event balance -= added; } } in.close(); System.out.println(ans); } public static void main(String[] args) throws IOException { (new D_Java()).doIt(); } }
import java.io.*; import java.util.*; public class D_Java { public static final int MOD = 998244353; public static int mul(int a, int b) { return (int)((long)a * (long)b % MOD); } int[] f; int[] rf; public int C(int n, int k) { return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n-k], rf[k])); } public static int pow(int a, int n) { int res = 1; while (n != 0) { if ((n & 1) == 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } static void shuffleArray(int[] a) { Random rnd = new Random(); for (int i = a.length-1; i > 0; i--) { int index = rnd.nextInt(i + 1); int tmp = a[index]; a[index] = a[i]; a[i] = tmp; } } public static int inv(int a) { return pow(a, MOD-2); } public void doIt() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tok.nextToken()); int k = Integer.parseInt(tok.nextToken()); f = new int[n+42]; rf = new int[n+42]; f[0] = rf[0] = 1; for (int i = 1; i < f.length; ++i) { f[i] = mul(f[i-1], i); rf[i] = mul(rf[i-1], inv(i)); } int[] events = new int[2*n]; for (int i = 0; i < n; ++i) { tok = new StringTokenizer(in.readLine()); int le = Integer.parseInt(tok.nextToken()); int ri = Integer.parseInt(tok.nextToken()); events[i] = le*2; events[i + n] = ri*2 + 1; } shuffleArray(events); Arrays.sort(events); int ans = 0; int balance = 0; for (int r = 0; r < 2*n;) { int l = r; while (r < 2*n && events[l] == events[r]) { ++r; } int added = r - l; if (events[l] % 2 == 0) { // Open event ans += C(balance + added, k); if (ans >= MOD) ans -= MOD; ans += MOD - C(balance, k); if (ans >= MOD) ans -= MOD; balance += added; } else { // Close event balance -= added; } } in.close(); System.out.println(ans); } public static void main(String[] args) throws IOException { (new D_Java()).doIt(); } }
1
Plagiarised
3666e0e8
ac7187d8
import java.io.*; import java.lang.*; import java.util.*; public class C1499 { public static void main(String[] args) throws IOException{ StringBuffer ans = new StringBuffer(); StringTokenizer st; BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(f.readLine()); int t = Integer.parseInt(st.nextToken()); for(int i = 0; i < t; i++){ st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); long op = Long.MAX_VALUE; long[] arr = new long[n]; st = new StringTokenizer(f.readLine()); for(int x = 0; x < n; x++){ arr[x] = Integer.parseInt(st.nextToken()); } long sum = arr[0]; long min = arr[0]; long min1 = arr[1]; long howMany = 1; long howMany1 = 0; long osum = sum; for(int x = 1; x < n; x++){ osum+=arr[x]; if(x % 2 != 0){ sum+= (n - howMany1) *arr[x]; sum+=( min *(n-howMany)); min1 = Math.min(arr[x], min1); howMany1++; }else{ sum+= (n - howMany) *arr[x]; sum+=( min1 *(n-howMany1)); min = Math.min(arr[x], min); howMany++; } //System.out.println(min1 + " " + min1); //System.out.println(sum); op = Math.min(op, sum); sum = osum; } ans.append(op); ans.append("\n"); } f.close(); System.out.println(ans); } public static class point implements Comparable<point>{ int x; int y; public point(int x,int y){ this.x = x; this.y = y; } public String toString(){ return(x + " " + y); } public boolean equals(Object x){ point y = ((point)(x)); if (this.x == y.x && this.y == y.y){ return true; } return false; } public int hashCode(){ return Objects.hash(x,y); } public int compareTo(point other){ if(this.x < other.x){ return -1; }else if(this.x == other.x && this.y == other.y){ return 0; } return 1; } } }
import java.io.*; import java.util.*; public class C { public static void main (String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(f.readLine()); int t = Integer.parseInt(st.nextToken()); while (t-->0) { st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); st = new StringTokenizer(f.readLine()); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = Long.parseLong(st.nextToken()); } solve(n, arr); } } static void solve(long n, long[] arr) { long minEven = Integer.MAX_VALUE; long minOdd = arr[0]; long evenSum = 0; long oddSum = arr[0]; long finans = Long.MAX_VALUE; long oddAns, evenAns; long oddcount=1; long evencount=0; for (int k = 1; k < n; k++) { if (k%2==1) { evenSum+=arr[k]; evencount++; minEven = Math.min(minEven, arr[k]); } else { oddSum+=arr[k]; oddcount++; minOdd = Math.min(minOdd, arr[k]); } oddAns = oddSum+(n-oddcount)*minOdd; evenAns = evenSum+(n-evencount)*minEven; finans = Math.min(finans, oddAns+evenAns); } System.out.println(finans); } }
0
Non-plagiarised
23cb8587
a5f41b95
import java.io.*; import java.util.*; public class C { static long mod = (long) (1e9 + 7); public static void main(String[] args) throws IOException { Scanner scn = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); StringBuilder sb = new StringBuilder(); int T = scn.ni(), tcs = 0; C: while (tcs++ < T) { int n = scn.ni(); tree = new ArrayList[n + 1]; range = new long[n + 1][2]; for (int i = 0; i <= n; i++) tree[i] = new ArrayList<>(); for (int i = 1; i <= n; i++) { range[i][0] = scn.nl(); range[i][1] = scn.nl(); } for (int i = 0; i < n - 1; i++) { int x = scn.ni(); int y = scn.ni(); tree[x].add(y); tree[y].add(x); } strg = new long[n + 1][2]; for (long a1[] : strg) Arrays.fill(a1, -1L); sb.append(Math.max(DFS(1, -1, 0), DFS(1, -1, 1))); sb.append("\n"); } out.print(sb); out.close(); } static ArrayList<Integer> tree[]; static long range[][], strg[][]; static long DFS(int u, int pa, int ok) { if (strg[u][ok] != -1) return strg[u][ok]; long tg = 0; for (int ch : tree[u]) { if (ch == pa) continue; long sg = 0; if (ok == 0) { sg = Math.max(DFS(ch, u, 0) + Math.abs(range[u][0] - range[ch][0]), DFS(ch, u, 1) + Math.abs(range[u][0] - range[ch][1])); } else { sg = Math.max(DFS(ch, u, 0) + Math.abs(range[u][1] - range[ch][0]), DFS(ch, u, 1) + Math.abs(range[u][1] - range[ch][1])); } tg += sg; } return strg[u][ok] = tg; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int ni() throws IOException { return Integer.parseInt(next()); } public long nl() throws IOException { return Long.parseLong(next()); } public int[] nia(int n) throws IOException { int a[] = new int[n]; String sa[] = br.readLine().split(" "); for (int i = 0; i < n; i++) a[i] = Integer.parseInt(sa[i]); return a; } public long[] nla(int n) throws IOException { long a[] = new long[n]; String sa[] = br.readLine().split(" "); for (int i = 0; i < n; i++) a[i] = Long.parseLong(sa[i]); return a; } public void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int v : a) l.add(v); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } public void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long v : a) l.add(v); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } } }
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class First { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); int t; t = in.nextInt(); //t = 1; while (t > 0) { solver.call(in,out); t--; } out.close(); } static class TaskA { Map<Integer, ArrayList<Integer>> map; long[][] arr; long[][] dp = new long[2][100005]; public void call(InputReader in, PrintWriter out) { int n; n = in.nextInt(); map = new HashMap<>(); arr = new long[n][2]; for (int i = 0; i < n; i++) { arr[i][0] = in.nextLong(); arr[i][1] = in.nextLong(); } int u, v; for (int i = 0; i < 2; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = -1; } } for (int i = 0; i < n-1; i++) { u = in.nextInt()-1; v = in.nextInt()-1; if(map.getOrDefault(u,null)==null){ map.put(u, new ArrayList<>()); } map.get(u).add(v); if(map.getOrDefault(v,null)==null){ map.put(v, new ArrayList<>()); } map.get(v).add(u); } out.println(Math.max(ans(0, -1,0), ans(0,-1,1))); } public long ans (int child, int par, int choice){ if(dp[choice][child]!=-1){ return dp[choice][child]; } long opt = 0; for (Integer i : map.get(child)) { if(i!=par) { opt += Math.max(Math.abs(arr[i][0] - arr[child][choice]) + ans(i, child, 0), Math.abs(arr[child][choice] - arr[i][1]) + ans(i, child, 1)); } } return dp[choice][child] = opt; } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class answer implements Comparable<answer>{ long a; long b; public answer(long a, long b) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return 0; } } static class answer1 implements Comparable<answer1>{ int a, b, c; public answer1(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } @Override public int compareTo(answer1 o) { if(o.c==this.c){ return this.a - o.a; } return o.c - this.c; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (Long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static final Random random=new Random(); static void shuffleSort(int[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
0
Non-plagiarised
1c8bb204
4b7646f4
import javax.print.DocFlavor; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class BST { static class pair implements Comparable{ int high; int idx; pair(int x , int y){ high = x; idx = y; } public String toString(){ return high + " " + idx; } @Override public int compareTo(Object o) { pair p = (pair)o; return high-p.high; } } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-->0){ int n = Integer.parseInt(br.readLine()); long [] arr = new long[n]; StringTokenizer st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { long tmp = Long.parseLong(st.nextToken()); arr[i] = tmp; } int h = 1; int v = 1; long minHor = arr[0]; long minVir = arr[1]; long sum0 = arr[0]; long sum1 = arr[1]; long total = (arr[0] + arr[1])*n; for (int i = 2; i < n; i++) { if(i%2==0){ h++; sum0 += arr[i]; minHor = Math.min(arr[i] , minHor); total = Math.min(total , minHor*(n-h+1)+(sum0-minHor)+minVir*(n-v+1)+(sum1-minVir)); }else { v++; sum1 += arr[i]; minVir = Math.min(arr[i] , minVir); total = Math.min(total , minHor*(n-h+1)+(sum0-minHor)+minVir*(n-v+1)+(sum1-minVir)); } } System.out.println(total); } } }
import javax.print.DocFlavor; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class BST { static class pair implements Comparable{ int high; int idx; pair(int x , int y){ high = x; idx = y; } public String toString(){ return high + " " + idx; } @Override public int compareTo(Object o) { pair p = (pair)o; return high-p.high; } } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t-->0){ int n = Integer.parseInt(br.readLine()); long [] arr = new long[n]; StringTokenizer st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { long tmp = Long.parseLong(st.nextToken()); arr[i] = tmp; } int h = 1; int v = 1; long minHor = arr[0]; long minVir = arr[1]; long sum0 = arr[0]; long sum1 = arr[1]; long total = (arr[0] + arr[1])*n; for (int i = 2; i < n; i++) { if(i%2==0){ h++; sum0 += arr[i]; minHor = Math.min(arr[i] , minHor); total = Math.min(total , minHor*(n-h+1)+(sum0-minHor)+minVir*(n-v+1)+(sum1-minVir)); }else { v++; sum1 += arr[i]; minVir = Math.min(arr[i] , minVir); total = Math.min(total , minHor*(n-h+1)+(sum0-minHor)+minVir*(n-v+1)+(sum1-minVir)); } } System.out.println(total); } } }
1
Plagiarised
464a03b8
ff1fc018
import java.util.*; public class Soltion{ public static void main(String []args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); Integer[] arr = new Integer[n]; for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); } String s = sc.next(); List<Integer> blue = new ArrayList<>(); List<Integer> red = new ArrayList<>(); for(int i=0;i<s.length();i++){ if(s.charAt(i)=='B'){ blue.add(arr[i]); } else{ red.add(arr[i]); } } Collections.sort(blue); Collections.sort(red); int p=1,q=n; boolean flag = true; for(int i=red.size()-1;i>=0;i--){ if(red.get(i)>q){ flag = false; break; } q--; } for(int i=0;i<blue.size();i++){ if(blue.get(i)<p){ flag = false; break; } p++; } System.out.println(flag? "Yes" : "No"); } } }
import java.util.*; public class mentor1 { public static boolean solve(int n, String color, int[] arr){ List<Integer> Barr = new ArrayList<Integer>(); List<Integer> Rarr = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { if(color.charAt(i) == 'B')Barr.add(arr[i]); else Rarr.add(arr[i]); } Barr.sort(Comparator.naturalOrder()); Rarr.sort(Comparator.reverseOrder()); for (int i = 0; i < Barr.size(); i++) { if(Barr.get(i)< i + 1)return false; } for (int i = 0; i < Rarr.size(); i++) { int expect = n-i; if(Rarr.get(i) > expect)return false; } return true; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); for (int i = 0; i < n; i++) { int m = input.nextInt(); int[] arr = new int[m]; for(int j = 0;j<m; j++)arr[j] = input.nextInt(); String color = input.next(); if(solve(m,color,arr)) System.out.println("YES"); else System.out.println("NO"); } } }
0
Non-plagiarised
4552d8a0
d3a96420
import java.util.*; import java.io.*; ////*************************************************************************** /* public class E_Gardener_and_Tree implements Runnable{ public static void main(String[] args) throws Exception { new Thread(null, new E_Gardener_and_Tree(), "E_Gardener_and_Tree", 1<<28).start(); } public void run(){ WRITE YOUR CODE HERE!!!! JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!! } } */ /////************************************************************************** public class D_Blue_Red_Permutation{ public static void main(String[] args) { FastScanner s= new FastScanner(); // PrintWriter out=new PrintWriter(System.out); //end of program //out.println(answer); //out.close(); StringBuilder res = new StringBuilder(); int t=s.nextInt(); int p=0; while(p<t){ int n=s.nextInt(); long array[]= new long[n]; for(int i=0;i<n;i++){ array[i]=s.nextLong(); } String str=s.nextToken(); ArrayList<Long> red = new ArrayList<Long>(); ArrayList<Long> blue = new ArrayList<Long>(); for(int i=0;i<n;i++){ if(str.charAt(i)=='R'){ red.add(array[i]); } else{ blue.add(array[i]); } } Collections.sort(blue); int check1=0; for(int i=0;i<blue.size();i++){ int yo=i+1; if(blue.get(i)<yo){ check1=1; break; } } Collections.sort(red,Collections.reverseOrder()); int number=n; int check2=0; for(int i=0;i<red.size();i++){ if(red.get(i)>number){ check2=1; break; } number--; } if(check1==0 && check2==0){ res.append("YES\n"); } else{ res.append("NO\n"); } p++; } System.out.println(res); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); String x=sc.next(); Vector<Integer> R=new Vector<>(); Vector<Integer> B=new Vector<>(); for(int i=0;i<n;i++){ if(x.charAt(i)=='B') R.add(a[i]); else B.add(a[i]); } Collections.sort(R); Collections.sort(B); boolean yes=true; for(int i=0;i<R.size();i++){ if(R.get(i)-i<1){System.out.println("NO");yes=false;break;} } if(yes) { int s=B.size(); for(int j=0;j<s;j++){ if(B.get(j)+s-j>n+1){System.out.println("NO");yes=false;break;} } } if(yes)System.out.println("YES"); } sc.close(); } }
0
Non-plagiarised
aa4b840f
e99c14b9
import java.io.*; import java.util.*; public class Contest1615C { static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { // reads in the next string while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { // reads in the next int return Integer.parseInt(next()); } public long nextLong() { // reads in the next long return Long.parseLong(next()); } public double nextDouble() { // reads in the next double return Double.parseDouble(next()); } } static InputReader r = new InputReader(System.in); static PrintWriter pw = new PrintWriter(System.out); static long mod = 1000000007; public static void main(String[] args) { int t = r.nextInt(); while (t > 0) { t--; int n = r.nextInt(); String a = r.next(); String b = r.next(); int sum1 = 0; int sum2 = 0; for (int i = 0; i < n; i ++) { sum1 += (a.charAt(i) == '1'?1:0); sum2 += (b.charAt(i) == '1'?1:0); } if (sum1!=sum2 && sum1+sum2 != n+1) { pw.println(-1); continue; } int[][] count = new int[2][2]; for (int i = 0; i < n; i ++) { count[(int)a.charAt(i)-(int)'0'][(int)b.charAt(i)-(int)'0']++; } int min = 10000000; if (count[0][1] == count[1][0]) { min = Math.min(min, count[0][1]*2); } if (count[1][1] == count[0][0] + 1) { min = Math.min(min, count[1][1] + count[0][0]); } pw.println(min == 10000000 ? -1:min); } pw.close(); } }
import java.io.*; import java.util.*; /* */ public class A{ static FastReader sc=null; public static void main(String[] args) { sc=new FastReader(); int t=sc.nextInt(); for(int tt=0;tt<t;tt++) { int n=sc.nextInt(); char a[]=sc.next().toCharArray(),b[]=sc.next().toCharArray(); int fa=0,fb=0,da=0,db=0,sum=0; boolean dif=false; for(int i=0;i<n;i++) { sum+=a[i]-'0'; if(a[i]!=b[i]) { dif=true; if(a[i]=='1')fa++; else fb++; } else { if(a[i]=='1')da++; else db++; } } if(sum==0) { System.out.println(dif?-1:0); continue; } int ans=n+1; if(fa==fb) ans=(fa+fb); if(da==db+1) ans=Math.min(da+db, ans); System.out.println(ans==(n+1)?-1:ans); } } static int[] ruffleSort(int a[]) { ArrayList<Integer> al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i<a.length;i++)a[i]=al.get(i); return a; } static void print(int a[]) { for(int e:a) { System.out.print(e+" "); } System.out.println(); } static class FastReader{ StringTokenizer st=new StringTokenizer(""); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String next() { while(!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); return a; } } }
0
Non-plagiarised
752ea9a5
b2ec0eff
//package Codeforces; import java.io.*; import java.util.*; public class PheonixAndTowers { public static void main(String[] args)throws Exception{ new PheonixAndTowers().run();} long mod=1000000000+7; // int[][] ar; void solve() throws Exception { for(int tt=ni();tt>0;tt--){ //int n = ni(); int n = ni(); int m =ni(); int x =ni(); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i] =ni(); } PriorityQueue<Pair> pq = new PriorityQueue<>(); for(int i=1;i<=m;i++) pq.add(new Pair(i)); out.println("YES"); for(int i:a){ Pair r = pq.remove(); r.sum+=i; pq.add(r); out.print(r.id+" "); } out.println(); } } class Pair implements Comparable<Pair>{ int id; long sum=0; public Pair(int i){ this.id=i; } @Override public int compareTo(Pair o) { return Long.compare(this.sum, o.sum); } } boolean issubset(int[] a,int k){ int n = a.length; boolean[][]dp = new boolean[n+1][k+1]; for(int i=0;i<=n;i++){ for(int j=0;j<=k;j++){ if(i==0) {dp[i][j] = false;continue;} if(j==0){dp[i][j]= true; continue;} if(a[i-1]<=j){ dp[i][j] = dp[i][j-a[i-1]] || dp[i-1][j]; }else{ dp[i][j] = dp[i-1][j]; } } } return dp[n][k]; } void ruffleSort(int[] a) { //ruffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n), temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort Arrays.sort(a); } /*FAST INPUT OUTPUT & METHODS BELOW*/ private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; private SpaceCharFilter filter; PrintWriter out; int min(int... ar){int min=Integer.MAX_VALUE;for(int i:ar)min=Math.min(min, i);return min;} long min(long... ar){long min=Long.MAX_VALUE;for(long i:ar)min=Math.min(min, i);return min;} int max(int... ar) {int max=Integer.MIN_VALUE;for(int i:ar)max=Math.max(max, i);return max;} long max(long... ar) {long max=Long.MIN_VALUE;for(long i:ar)max=Math.max(max, i);return max;} void shuffle(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for(int i=0;i<a.length;i++) al.add(a[i]); Collections.sort(al); for(int i=0;i<a.length;i++) a[i]=al.get(i); } long lcm(long a,long b) { return (a*b)/(gcd(a,b)); } int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } /* for (1/a)%mod = ( a^(mod-2) )%mod ----> use expo to calc -->(a^(mod-2)) */ long expo(long p,long q) /* (p^q)%mod */ { long z = 1; while (q>0) { if (q%2 == 1) { z = (z * p)%mod; } p = (p*p)%mod; q >>= 1; } return z; } void run()throws Exception { in=System.in; out = new PrintWriter(System.out); solve(); out.flush(); } private int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } private int ni() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); int sgn = 1; if (c == '-') { sgn = -1; c = scan(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = scan(); } while (!isSpaceChar(c)); return res * sgn; } private long nl() throws IOException { long num = 0; int b; boolean minus = false; while ((b = scan()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = scan(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = scan(); } } private double nd() throws IOException{ return Double.parseDouble(ns()); } private String ns() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = scan(); } while (!isSpaceChar(c)); return res.toString(); } private String nss() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } private char nc() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); return (char) c; } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } private boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhiteSpace(c); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
//Implemented By Aman Kotiyal Date:-02-May-2021 Time:-7:58:39 pm import java.io.*; import java.util.*; public class ques3 { public static void main(String[] args)throws Exception{ new ques3().run();} long mod=1000000000+7; void solve() throws Exception { for(int ii=ni();ii>0;ii--) { int n=ni(); int m=ni(); int x=ni(); long h[]=new long[n]; for (int i = 0; i <n; i++) h[i]=nl(); int dp[]=new int[n]; PriorityQueue<long[]> pq=new PriorityQueue<long[]>(new Comparator<long[]>() { public int compare(long[] x, long[] y) { if(x[1]-y[1]>0)return 1; if(x[1]-y[1]<0)return -1; return 0; } }); for (int i = 1; i <=m; i++) pq.add(new long[] {i,0}); for(int i=0;i<n;i++) { long tem[]=pq.poll(); tem[1]+=h[i]; dp[i]=(int)tem[0]; pq.add(tem); } long min=Integer.MAX_VALUE; long max=Integer.MIN_VALUE; while(!pq.isEmpty()) { long tem[]=pq.poll(); min=min(min,tem[1]); max=max(max,tem[1]); } if(max-min>x) { out.println("NO"); } else { out.println("YES"); for (int i = 0; i < dp.length; i++) { out.print(dp[i]+" "); } out.println(); } } } /*FAST INPUT OUTPUT & METHODS BELOW*/ private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; private SpaceCharFilter filter; PrintWriter out; int min(int... ar){int min=Integer.MAX_VALUE;for(int i:ar)min=Math.min(min, i);return min;} long min(long... ar){long min=Long.MAX_VALUE;for(long i:ar)min=Math.min(min, i);return min;} int max(int... ar) {int max=Integer.MIN_VALUE;for(int i:ar)max=Math.max(max, i);return max;} long max(long... ar) {long max=Long.MIN_VALUE;for(long i:ar)max=Math.max(max, i);return max;} void reverse(int a[]){for(int i=0;i<a.length>>1;i++){int tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}} void reverse(long a[]){for(int i=0;i<a.length>>1;i++){long tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}} String reverse(String s){StringBuilder sb=new StringBuilder(s);sb.reverse();return sb.toString();} void shuffle(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for(int i=0;i<a.length;i++) al.add(a[i]); Collections.sort(al); for(int i=0;i<a.length;i++) a[i]=al.get(i); } long lcm(long a,long b) { return (a*b)/(gcd(a,b)); } int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } /* for (1/a)%mod = ( a^(mod-2) )%mod ----> use expo to calc -->(a^(mod-2)) */ long expo(long p,long q) /* (p^q)%mod */ { long z = 1; while (q>0) { if (q%2 == 1) { z = (z * p)%mod; } p = (p*p)%mod; q >>= 1; } return z; } void run()throws Exception { in=System.in; out = new PrintWriter(System.out); solve(); out.flush(); } private int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } private int ni() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); int sgn = 1; if (c == '-') { sgn = -1; c = scan(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = scan(); } while (!isSpaceChar(c)); return res * sgn; } private long nl() throws IOException { long num = 0; int b; boolean minus = false; while ((b = scan()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = scan(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = scan(); } } private double nd() throws IOException{ return Double.parseDouble(ns()); } private String ns() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = scan(); } while (!isSpaceChar(c)); return res.toString(); } private String nss() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } private char nc() throws IOException { int c = scan(); while (isSpaceChar(c)) c = scan(); return (char) c; } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } private boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhiteSpace(c); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
0
Non-plagiarised
b9595381
d6fb3b9e
import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main{ static int dest1; static int dest2; public static void main(String args[]){ FastScanner in = new FastScanner(); int test=in.nextInt(); while(test-->0){ int n=in.nextInt(); int count[][]=new int[n][5]; int total[]=new int[n]; String words[]=new String[n]; for(int i=0;i<n;i++){ words[i]=in.next(); for(int j=0;j<words[i].length();j++) count[i][words[i].charAt(j)-'a']++; total[i]=words[i].length(); } int max=Integer.MIN_VALUE; for(int i=0;i<5;i++){ Integer ans[]=new Integer[n]; for(int j=0;j<n;j++){ ans[j]=count[j][i]-(total[j]-count[j][i]); } Arrays.sort(ans,Collections.reverseOrder()); int j=0; int r=0; while(j<n && r+ans[j]>0){ r+=ans[j]; j++; } max=Math.max(j,max); } System.out.println(max); } } public static int solve(int start[], int end[], int n) { int distance[][]=new int[n][n]; int r=n; int c=n; boolean visited[][]=new boolean[n][n]; int startx=start[0]; int starty=start[1]; int endx=end[0]; int endy=end[1]; Pair tmp=new Pair(startx,starty); Queue<Pair> q=new LinkedList<>(); q.add(tmp); visited[startx][starty]=true; distance[startx][starty]=0; int dx[]={-2,-1,1,2,2,1,-1,-2}; int dy[]={1,2,2,1,-1,-2,-2,-1}; while(!q.isEmpty()) { Pair cell=q.poll(); int x=cell.x; int y=cell.y; int d=distance[x][y]; for(int i=0;i<8;i++) { int childx=x+dx[i]; int childy=y+dy[i]; if(valid(childx,childy,r,c,visited)) { visited[childx][childy]=true; distance[childx][childy]=d+1; q.add(new Pair(childx,childy)); } } } return distance[endx][endy]; } public static boolean valid(int x,int y,int r,int c,boolean visited[][]) { if(x<0||y<0||x>=r||y>=c) return false; if(visited[x][y]) return false; return true; } public static int knight(int i,int j){ if(i==dest1 && j==dest2) return 0; if(i<1 || j<1) return 0; if(i>8 || j>8) return 0; if(i<1 || j>8) return 0; if(i>8 || j<1) return 0; int min=0; if(i-1>=1 && j-2>=1) min+=1+knight(i-1,j-2); if(i-1>=1 && j+2<=8) min+=1+knight(i-1,j+2); if(i-2>=1 && j-1>=1) min+=1+knight(i-2,j-1); if(i-2>=1 && j+1<=8) min+=1+knight(i-2,j+1); if(i+1<=8 && j-2>=1) min+=1+knight(i+1,j-2); if(i+1<=8 && j+2<=8) min+=1+knight(i+1,j+2); if(i+2<=8 && j-1>=1) min+=1+knight(i+2,j-1); if(i+2<=8 && j+1<=8) min+=1+knight(i+2,j+1); return min; } } class FastScanner { java.io.BufferedReader br = new java.io.BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } class Pair { int x; int y; Pair(int i,int j) { x=i; y=j; } }
import java.util.*; public class Sol { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int a[][]=new int[n][5]; int tot[]=new int[n]; for(int i=0;i<n;i++) { String x = sc.next(); for(int j=0;j<x.length();j++) a[i][x.charAt(j)-'a'] += 1; tot[i]=x.length(); } int max=Integer.MIN_VALUE; for(int i=0;i<5;i++) max=Math.max(max,function(a,n,i,tot)); System.out.println(max); } } static int function(int a[][],int n,int i,int tot[]) { Integer ans[] = new Integer[n]; for(int j=0;j<n;j++) ans[j]=a[j][i]-(tot[j]-a[j][i]); int res=0,j=0; Arrays.sort(ans,Collections.reverseOrder()); while(j<n&&res+ans[j]>0) res+=ans[j++]; return j; } }
1
Plagiarised
079ad09e
20012377
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; 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); EAirConditioners solver = new EAirConditioners(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class EAirConditioners { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(), q = in.nextInt(); int[] a = in.readArray(q); long[] t = in.readLongArray(q); long[] pref = new long[n + 2]; long[] suff = new long[n + 2]; Arrays.fill(pref, (long) 1e14); Arrays.fill(suff, (long) 1e14); for (int i = 0; i < q; ++i) { pref[a[i]] = t[i] - a[i]; suff[a[i]] = t[i] + a[i]; } for (int i = 1; i <= n; ++i) { pref[i] = Math.min(pref[i], pref[i - 1]); } for (int i = n; i >= 1; --i) { suff[i] = Math.min(suff[i], suff[i + 1]); } for (int i = 1; i <= n; ++i) { out.print(Math.min(pref[i] + i, suff[i] - i) + " "); } out.println(); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] readArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } public long[] readLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) array[i] = nextLong(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; 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); EAirConditioners solver = new EAirConditioners(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class EAirConditioners { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(), q = in.nextInt(); int[] a = in.readArray(q); long[] t = in.readLongArray(q); long[] pref = new long[n + 2]; long[] suff = new long[n + 2]; Arrays.fill(pref, (long) 1e14); Arrays.fill(suff, (long) 1e14); for (int i = 0; i < q; ++i) { pref[a[i]] = t[i] - a[i]; suff[a[i]] = t[i] + a[i]; } for (int i = 1; i <= n; ++i) { pref[i] = Math.min(pref[i], pref[i - 1]); } for (int i = n; i >= 1; --i) { suff[i] = Math.min(suff[i], suff[i + 1]); } for (int i = 1; i <= n; ++i) { out.print(Math.min(pref[i] + i, suff[i] - i) + " "); } out.println(); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] readArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } public long[] readLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) array[i] = nextLong(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
1
Plagiarised
141effef
9cea10af
// package codeforces; import java.util.*; public class ArmChairs { static int[]arr; static ArrayList<Integer>a; static ArrayList<Integer>b; static int dp[][]; public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=scn.nextInt(); } dp=new int[n+1][n+1]; a =new ArrayList<>(); b =new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]==0) { a.add(i); }else{ b.add(i); } } System.out.println(solve(0,0)); } public static int solve(int i,int j) { if(i==b.size()) { return 0; } if(j==a.size()) { return 100000000; } if(dp[i][j]!=0) { return dp[i][j]; } int x=Math.abs(a.get(j)-b.get(i))+solve(i+1,j+1); int y=solve(i,j+1); return dp[i][j]=Math.min(x, y); } }
//package currentContest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class P4 { static int dp[][]=new int[5000+1][5000+1]; public static void main(String[] args) { // TODO Auto-generated method stub FastReader sc=new FastReader(); int t=1; //t=sc.nextInt(); StringBuilder s=new StringBuilder(); while(t--!=0) { int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++) { P4.dp[i][j]=-1; } } ArrayList<Integer> one=new ArrayList<>(); ArrayList<Integer> zero=new ArrayList<>(); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); if(a[i]==0) { zero.add(i); }else { one.add(i); } } Collections.sort(zero); Collections.sort(one); long ans=sol(0,0,zero.size(),one.size(),a,zero,one); System.out.println(ans); } //System.out.println(s); } private static long sol(int i, int j, int n, int m,int a[], ArrayList<Integer> zero, ArrayList<Integer> one) { //System.out.println(i+" "+j); // TODO Auto-generated method stub if(j==m) { return 0; } int av=n-i; int rem=m-j; if(av<rem) { return Integer.MAX_VALUE-1; } if(dp[i][j]!=-1) { return dp[i][j]; } long ans1=sol(i+1,j,n,m,a, zero, one); long ans2=Math.abs(zero.get(i)-one.get(j))+sol(i+1,j+1,n,m,a, zero, one); dp[i][j]=(int) Math.min(ans1, ans2); return dp[i][j]; } 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; } } }
0
Non-plagiarised
018c9543
d1dbc56a
import java.io.*; import java.util.*; public class Main { public static int n; public static int a[] = new int[12]; public static int b[] = new int[12]; public static boolean f(int p) { if (p > n) return false; for (int i = 0; i < p; i++) { b[p] = a[p] + b[i]; for (int j = 0; j < p; j++) { if (b[j] == b[p]) return true; } if (f(p + 1)) { return true; } // b[p] = b[i] - a[p]; // for (int j = 0; j < p; j++) { // if (b[j] == b[p]) // return true; // } // if (f(p + 1)) { // return true; // } } return false; } public static void main(String[] args) throws Exception { int T = r.readInt(); for (int t = 0; t < T; t++) { n = r.readInt(); for (int i = 1; i <= n; i++) { a[i] = r.readInt(); } boolean ans = false; if (n == 1) { ans = a[1] == 0; } else { b[0] = 1; ans = f(1); } if (ans) { System.out.println("YES"); } else { System.out.println("NO"); } } } static public InputReader r = new InputReader(System.in); static public OutputWriter w = new OutputWriter(System.out); static public class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] readIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = this.readInt(); } return array; } public long[] readLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = this.readLong(); } return array; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static public 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(); } } } // 1 // 10 // 1 2 4 8 16 32 64 128 256 512
import java.util.*; import java.io.*; public class D { static ArrayList<Integer> set = new ArrayList<Integer>(); static int[] a; static int test; public static void main(String[] args) throws IOException{ Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int q=0;q<t;q++) { int n = in.nextInt(); a = new int[n]; for(int i=0;i<n;i++) { a[i] = Math.abs(in.nextInt()); } boolean yes = false; for(int i=0;i<n;i++) { if(a[i]==0) yes = true; int[] b = new int[n-1]; int index=0; for(int j=0;j<n;j++) { if(j!=i) { b[index] = a[j]; index++; } } test = a[i]; set = new ArrayList<Integer>(); if(b.length!=0) { if(subset(b,0)) yes = true; } } if(yes) { System.out.println("YES"); }else { System.out.println("NO"); } } } static boolean subset(int[] a,int i) { // System.out.println(set); if(i>=a.length) { if(binary(new int[set.size()],0)) return true; return false; } set.add(a[i]); if(subset(a,i+1))return true; set.remove(set.size()-1); if(subset(a,i+1))return true; return false; } static boolean binary(int[] b,int i) { if(i==b.length) { int sum=0; for(int j=0;j<b.length;j++) { if(b[j]==0) { sum+= set.get(j); }else { sum-= set.get(j); } } // System.out.println(sum+" "+test); if(sum==test) { return true; }else { return false; } } b[i] = 0; if(binary(b,i+1))return true; b[i] = 1; if(binary(b,i+1))return true; return false; } } //Is only possible if you can use at least 1 number in b for at least 2 numbers in a
0
Non-plagiarised
2f8c3bf3
3c667d4f
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int k=sc.nextInt(); int idx[]=new int[k]; for(int i=0;i<k;i++){ idx[i]=sc.nextInt(); } long arr[]=new long[n]; Arrays.fill(arr,Integer.MAX_VALUE); for(int i=0;i<k;i++){ long temp=sc.nextLong(); arr[idx[i]-1]=temp; } long left[]=new long[n]; long right[]=new long[n]; Arrays.fill(left,Integer.MAX_VALUE); Arrays.fill(right,Integer.MAX_VALUE); left[0]=arr[0]; for(int i=1;i<n;i++){ left[i]=Math.min(left[i-1]+1,arr[i]); } right[arr.length-1]=arr[arr.length-1]; for(int i=n-2;i>=0;i--){ right[i]=Math.min(right[i+1]+1,arr[i]); } for(int i=0;i<n;i++){ // System.out.print(left[i]+"--"+right[i]+"\\"); System.out.print(Math.min(left[i],right[i])+" "); } System.out.println(); } } }
import java.util.*; public class j { public static void main(String args[]) { Scanner in=new Scanner(System.in); int n=in.nextInt(); while(n-->0) { int len=in.nextInt(); int t=in.nextInt(); int pos[]=new int[t]; int temp[]=new int[t]; for(int i=0;i<t;i++) pos[i]=in.nextInt(); for(int i=0;i<t;i++) temp[i]=in.nextInt(); long range[]=new long[len]; Arrays.fill(range,Long.MAX_VALUE-10000); for(int i=0;i<t;i++) range[pos[i]-1]=temp[i]; for(int i=1;i<len;i++) { range[i]=Math.min(range[i],1+range[i-1]); } for(int i=len-2;i>=0;i--) { range[i]=Math.min(range[i+1]+1,range[i]); } for(int i=0;i<len;i++) { System.out.print(range[i]+" "); }System.out.println(); } } }
0
Non-plagiarised
3e6def38
e7dce35b
import java.io.*; import java.util.*; public class D_Java { public static final int MOD = 998244353; public static int mul(int a, int b) { return (int)((long)a * (long)b % MOD); } int[] f; int[] rf; public int C(int n, int k) { return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n-k], rf[k])); } public static int pow(int a, int n) { int res = 1; while (n != 0) { if ((n & 1) == 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } static void shuffleArray(int[] a) { Random rnd = new Random(); for (int i = a.length-1; i > 0; i--) { int index = rnd.nextInt(i + 1); int tmp = a[index]; a[index] = a[i]; a[i] = tmp; } } public static int inv(int a) { return pow(a, MOD-2); } public void doIt() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tok.nextToken()); int k = Integer.parseInt(tok.nextToken()); f = new int[n+42]; rf = new int[n+42]; f[0] = rf[0] = 1; for (int i = 1; i < f.length; ++i) { f[i] = mul(f[i-1], i); rf[i] = mul(rf[i-1], inv(i)); } int[] events = new int[2*n]; for (int i = 0; i < n; ++i) { tok = new StringTokenizer(in.readLine()); int le = Integer.parseInt(tok.nextToken()); int ri = Integer.parseInt(tok.nextToken()); events[i] = le*2; events[i + n] = ri*2 + 1; } shuffleArray(events); Arrays.sort(events); int ans = 0; int balance = 0; for (int r = 0; r < 2*n;) { int l = r; while (r < 2*n && events[l] == events[r]) { ++r; } int added = r - l; if (events[l] % 2 == 0) { // Open event ans += C(balance + added, k); if (ans >= MOD) ans -= MOD; ans += MOD - C(balance, k); if (ans >= MOD) ans -= MOD; balance += added; } else { // Close event balance -= added; } } in.close(); System.out.println(ans); } public static void main(String[] args) throws IOException { (new D_Java()).doIt(); } }
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int n = nextInt(); int k = nextInt(); f = new int[n + 42]; rf = new int[n + 42]; f[0] = 1; rf[0] = 1; for (int i = 1; i < f.length; i++) { f[i] = mul(f[i - 1], i); rf[i] = mul(rf[i - 1], inv(i)); } int[] a = new int[n * 2]; for (int i = 0; i < n; i++) { a[i] = nextInt() * 2; a[i + n] = nextInt() * 2 + 1; } Arrays.sort(a); int ans = 0; int curOpen = 0; for (int r = 0; r < 2 * n;) { int l = r; while (r < 2 * n && a[l] == a[r]) r++; int intersections = r - l; if (a[l] % 2 == 0) { ans += C(curOpen + intersections, k); if (ans >= mod) ans -= mod; ans += mod - C(curOpen, k); if (ans >= mod) ans -= mod; curOpen += intersections; } else { curOpen -= intersections; } } pw.println(ans); pw.close(); } static int mod = 998244353; static int mul(int a, int b) { return (int) ((long) a * (long) b % mod); } static int[] f; static int[] rf; static int C(int n, int k) { return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n - k], rf[k])); } static int pow(int a, int n) { int res = 1; while (n != 0) { if ((n & 1) == 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } static int inv(int a) { return pow(a, mod - 2); } static StringTokenizer st = new StringTokenizer(""); static BufferedReader br; static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } } class Coordinates { int l; int r; public Coordinates(int l, int r) { this.l = l; this.r = r; } } class CoordinatesComparator implements Comparator<Coordinates> { @Override public int compare(Coordinates o1, Coordinates o2) { if (o1.l == o2.l) return Integer.compare(o1.r, o2.r); return Integer.compare(o1.l, o2.l); } }
1
Plagiarised
4da08761
c4ca2ff3
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Solution { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Main solver = new Main(); boolean multipleTC = true; int testCount = multipleTC ? Integer.parseInt(in.next()) : 1; for (int i = 1; i <= testCount; i++) solver.solve(in, out, i); out.close(); } static class Main { PrintWriter out; InputReader in; public void solve(InputReader in, PrintWriter out, int test) { this.out = out; this.in = in; int n = ni(); String[] arr = new String[n]; int[][] freq = new int[n][5]; int[][] rem = new int[n][5]; for(int i = 0; i < n; i++){ arr[i] = n(); for(int j = 0; j < arr[i].length(); j++) freq[i][arr[i].charAt(j) - 'a']++; for(int j = 0; j < 5; j++) rem[i][j] = arr[i].length() - freq[i][j]; } int ans = 0; for(int i = 0; i < 5; i++){ int[] vals = new int[n]; for(int j = 0; j < n; j++) vals[j] = freq[j][i] - rem[j][i]; Arrays.sort(vals); int sum = 0, x = 0; for(int j = n - 1; j >= 0; j--){ if(sum + vals[j] > 0){ x++; sum += vals[j]; } else { break; } } if(x > ans) { ans = x; } } pn(ans); } int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } String n(){ return in.next(); } int ni() { return in.nextInt(); } long nl() { return in.nextLong(); } void pn(long zx) { out.println(zx); } void pn(String sz) { out.println(sz); } void pn(double dx){ out.println(dx); } class Tuple { long x; long y; Tuple(long a, long b) { x = a; y = b; } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new UnknownError(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc=new Scanner(System.in); int t=sc.nextInt(); PrintWriter out=new PrintWriter(System.out); while(t-->0) { int n=sc.nextInt(); int freq[][]=new int[n][5]; int rem[][]=new int[n][5]; for(int i=0;i<n;i++) { String str=sc.next(); for(int j=0;j<str.length();j++) { freq[i][str.charAt(j)-'a']++; } for(int k=0;k<5;k++) { rem[i][k]=str.length()-freq[i][k]; } } int ans=0; for(int i=0;i<5;i++) { int arr[]=new int[n]; for(int j=0;j<n;j++) arr[j]=freq[j][i]-rem[j][i]; Arrays.sort(arr); int total=0; int sum=0; for(int k=n-1;k>=0;k--) { if(sum+arr[k]>0) { sum=sum+arr[k]; total++; } else { break; } } ans=Math.max(ans,total); } out.println(ans); } out.flush(); out.close(); } }
1
Plagiarised
04ed33a5
0c173033
import java.util.Scanner; public class Subsequence { private static Scanner sc = new Scanner(System.in); public static void main(String args[]) { int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int a[] = new int[n]; int b[] = new int[n]; for(int i=0;i<n;i++) { a[i]= sc.nextInt(); } if(n%2==0) { calculateB(a,b,n); } else { calculateB(a,b,n-3); if (a[n - 2] + a[n - 3] != 0) { b[n - 3] = -a[n - 1]; b[n - 2] = -a[n - 1]; b[n - 1] = a[n - 2] + a[n - 3]; } else if (a[n - 2] + a[n - 1] != 0) { b[n - 3] = a[n - 2] + a[n - 1]; b[n - 2] = -a[n - 3]; b[n - 1] = -a[n - 3]; } else { b[n - 3] = -a[n - 2]; b[n - 2] = a[n - 3] + a[n - 1]; b[n - 1] = -a[n - 2]; } } for(int i=0;i<n;i++) { System.out.print(b[i] + " "); } System.out.println(); } } private static void calculateB(int[] a, int[] b, int n) { for(int i=0;i<n-1;i=i+2) { b[i] = -a[i+1]; b[i+1] = a[i]; } } }
import java.io.*; import java.util.*; public class M { static Scanner scanner=new Scanner(System.in); public static void main(String[] args) { int t=scanner.nextInt(); while(t-->0) { int n=scanner.nextInt(); int a[]=new int [n]; int b[]=new int [n]; for(int i=0;i<n;i++)a[i]=scanner.nextInt(); for(int i=0;i+1<n;i+=2) { b[i]=-a[i+1]; b[i+1]=a[i]; } if(n%2==1) { int x=a[n-1],y=a[n-2],z=a[n-3]; if(x+y!=0) { b[n-3]=x+y; b[n-2]=-z; b[n-1]=-z; }else if(y+z!=0) { b[n-1]=y+z; b[n-2]=-x; b[n-3]=-x; }else { b[n-2]=x+z; b[n-1]=-y; b[n-3]=-y; } } StringBuilder sb=new StringBuilder(); for(int i=0;i<n;i++)sb.append(b[i]+" "); System.out.println(sb); } } }
1
Plagiarised
0ee2f8f1
c2b7b017
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int T=sc.nextInt(); while(T-->0){ int n=sc.nextInt(); int arr[]=new int[n]; int min=Integer.MAX_VALUE;int max=Integer.MIN_VALUE; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); min=Math.min(arr[i],min); max=Math.max(arr[i],max); } while(min<=max){ int mid=min+(max-min)/2; if(helper(arr,mid)) min=mid+1; else max=mid-1; } System.out.println(min-1); } } public static boolean helper(int arr[],int min){ int tmp[]=Arrays.copyOf(arr,arr.length); for(int i=arr.length-1;i>=2;i--){ if(tmp[i]<min) return false; int d=(Math.min(arr[i],tmp[i]-min))/3; tmp[i-1]+=d; tmp[i-2]+=d*2; } return tmp[1]>=min && tmp[0]>=min; } }
// Working program with FastReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; // public class Example { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc = new FastReader(); int t= sc.nextInt(); while(t>0){ t--; int ans=Integer.MAX_VALUE; int n=sc.nextInt(); int[] ar= new int[n]; int l=Integer.MAX_VALUE; int h=Integer.MIN_VALUE; for(int i=0;i<n;i++){ ar[i]=sc.nextInt(); l=Math.min(l,ar[i]); h=Math.max(h,ar[i]); } int[] extra; while(l<=h){ int mid=l+(h-l)/2; if(possibleans(ar,mid)){ ans=mid; l=mid+1; }else{ h=mid-1; } } System.out.println(ans); } } private static boolean possibleans(int[] ar, int mid) { int[] extra=new int[ar.length]; for(int i=ar.length-1;i>=2;i--){ if((ar[i]+extra[i]-mid)<0){ return false; } int d=Math.min(ar[i],extra[i]+ar[i]-mid); extra[i-1]=extra[i-1]+d/3; extra[i-2]+=2*(d/3); } int a=ar[0]+extra[0]; int b=ar[1]+extra[1]; return (a>=mid && b>=mid); } private static boolean possible(int a, int b, int c, int mid) { int min=Math.min(a,Math.min(b,c)); if(3*mid<=(c)){ c=c-3*mid; a=a+2*mid; b=b+mid; int min1=Math.min(a,Math.min(b,c)); if(min1>min){ return true; }else{ return false; } }else{ return false; } } }
1
Plagiarised
49e94e7e
f3d7ce08
import java.io.BufferedReader; import java.io.IOException; import java.lang.*; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.File; import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; public class Main { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair ob) { return (int)(first - ob.first); } } static class Tuple implements Comparable<Tuple> { int first, second,third; public Tuple(int first, int second, int third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int[] parent; int[] rank; //Size of the trees is used as the rank public DSU(int n) { parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) //finding through path compression { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public boolean union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return false; //if they are already connected we exit by returning false. // if a's parent is less than b's parent if(rank[a] < rank[b]) { //then move a under b parent[a] = b; } //else if rank of j's parent is less than i's parent else if(rank[a] > rank[b]) { //then move b under a parent[b] = a; } //if both have the same rank. else { //move a under b (it doesnt matter if its the other way around. parent[b] = a; rank[a] = 1 + rank[a]; } return true; } } static class Reader { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) throws IOException { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] longReadArray(int n) throws IOException { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static long lcm(long a, long b) { return (a / LongGCD(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long modPow(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return modPow(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)(n)] * modInverse(fac[(int)(r)], p) % p * modInverse(fac[(int)(n - r)], p) % p) % p; } public static long fact(long n) { long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (long i = 1; i <= n; i++) fac[(int)(i)] = fac[(int)(i - 1)] * i; return fac[(int)(n)]; } public static long nCr(long n, long k) { long ans = 1; for(long i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } //Modular Operations for Addition and Multiplication. public static long perfomMod(long x) { return ((x%M + M)%M); } public static long addMod(long a, long b) { return perfomMod(perfomMod(a)+perfomMod(b)); } public static long subMod(long a, long b) { return perfomMod(perfomMod(a)-perfomMod(b)); } public static long mulMod(long a, long b) { return perfomMod(perfomMod(a)*perfomMod(b)); } public static boolean isPrime(long n) { if(n == 1) { return false; } //check only for sqrt of the number as the divisors //keep repeating so only half of them are required. So,sqrt. for(int i = 2;i*i<=n;i++) { if(n%i == 0) { return false; } } return true; } public static List<Integer> SieveList(int n) { boolean prime[] = new boolean[(int)(n+1)]; Arrays.fill(prime, true); List<Integer> l = new ArrayList<>(); for (int p = 2; p*p<=n; p++) { if (prime[p] == true) { for(int i = p*p; i<=n; i += p) { prime[i] = false; } } } for (int p = 2; p<=n; p++) { if (prime[p] == true) { l.add(p); } } return l; } public static int countDivisors(int x) { int c = 0; for(int i = 1;i*i<=x;i++) { if(x%i == 0) { if(x/i != i) { c+=2; } else { c++; } } } return c; } public static long log2(long n) { long ans = (long)(log(n)/log(2)); return ans; } public static boolean isPow2(long n) { return (n != 0 && ((n & (n-1))) == 0); } public static boolean isSq(int x) { long s = (long)Math.round(Math.sqrt(x)); return s*s==x; } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(long a[], long x) { int l=-1, r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void Sort(long[] a) { List<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); // Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void ssort(char[] a) { List<Character> l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int tt = sc.nextInt(); while(tt-- > 0) { int n = sc.nextInt(); char[] a = sc.next().toCharArray(), b = sc.next().toCharArray(); int c00 = 0, c01 = 0, c10 = 0, c11 = 0; for(int i = 0;i<n;i++) { if(a[i] == '0' && b[i] == '0') { c00++; } else if(a[i] == '0' && b[i] == '1') { c01++; } else if(a[i] == '1' && b[i] == '0') { c10++; } else if(a[i] == '1' && b[i] == '1') { c11++; } } int ans = mod; if(c01 == c10) ans = min(ans, c01 + c10); if(c11 == c00 + 1) ans = min(ans, c11 + c00); fout.println((ans == mod) ? -1 : ans); } fout.close(); } }
import javax.swing.plaf.IconUIResource; import java.lang.reflect.Array; import java.text.CollationElementIterator; import java.util.*; import java.io.*; //Timus judge id- 323935JJ public class Main { //---------------------------------------------------------------------------------------------- public static class Pair implements Comparable<Pair> { int x=0,y=0; int z=0; public Pair(int a, int b) { this.x=a; this.y=b; } public int compareTo(Pair o) { return this.x - o.x; } } public static int mod = (int) (1e9 + 7); static int ans = Integer.MAX_VALUE; public static void main(String hi[]) throws Exception { FastReader sc = new FastReader(); int t =sc.nextInt(); while(t-->0) { int n =sc.nextInt(); String a = sc.nextLine(),b=sc.nextLine(); int count1=0,count2=0,count3=0,count4=0; for(int i=0;i<n;i++) { if(a.charAt(i)=='0'&&b.charAt(i)=='0') count1++; else if(a.charAt(i)=='1'&&b.charAt(i)=='1') count2++; else if(a.charAt(i)=='1'&&b.charAt(i)=='0') count3++; else if(a.charAt(i)=='0'&&b.charAt(i)=='1') count4++; } int ans=Integer.MAX_VALUE; if(count3==count4) ans=Math.min(count3*2,ans); if(count2==count1+1) ans=Math.min(ans,2*count1+1); if(ans==Integer.MAX_VALUE) System.out.println(-1); else System.out.println(ans); } } static int find(int[] a,int x) { if(a[x]==-1) return x; else return find(a,a[x]); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // method to return LCM of two numbers static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcdl(long a, long b) { if (a == 0) return b; return gcdl(b % a, a); } // method to return LCM of two numbers static long lcml(long a, long b) { return (a / gcdl(a, b)) * b; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
0
Non-plagiarised
1f748faf
851fafb6
/* JAI MATA DI */ import java.util.*; import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction; import java.io.*; import java.math.*; import java.sql.Array;; public class Main { static class FR{ BufferedReader br; StringTokenizer st; public FR() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long mod = (long)(1e9 + 7); static void sort(long[] arr ) { ArrayList<Long> al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr ) { ArrayList<Integer> al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static int UB(long[] arr , long find , int l , int r) { while(l<=r) { int m = (l+r)/2; if(arr[m]<find) l = m+1; else r = m-1; } return l; } static int LB(long[] arr , long find,int l ,int r ) { while(l<=r) { int m = (l+r)/2; if(arr[m] > find) r = m-1; else l = m+1; } return r; } static int UB(int[] arr , long find , int l , int r) { while(l<=r) { int m = (l+r)/2; if(arr[m]<find) l = m+1; else r = m-1; } return l; } static int LB(int[] arr , long find,int l ,int r ) { while(l<=r) { int m = (l+r)/2; if(arr[m] > find) r = m-1; else l = m+1; } return r; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i); } static long[][] ncr(int n, int k) { long C[][] = new long[n + 1][k + 1]; int i, j; // Calculate value of Binomial // Coefficient in bottom up manner for (i = 0; i <= n; i++) { for (j = 0; j <= Math.min(i, k); j++) { // Base Cases if (j == 0 || j == i) C[i][j] = 1; // Calculate value using // previously stored values else C[i][j] = (C[i - 1][j - 1] + C[i - 1][j])%mod; } } return C; } static long modInverse(long a, long m) { long g = gcd(a, m); return power(a, m - 2, m); } static long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (int)((p * (long)p) % m); if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); } /* ***************************************************************************************************************************************************/ static FR sc = new FR(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) { int tc = 1; tc = sc.nextInt(); while(tc-->0) { TEST_CASE(); } System.out.println(sb); } static void TEST_CASE() { int n = sc.nextInt(); ; int[] arr = new int[n]; for(int i =0 ; i<n;i++) { arr[i] = sc.nextInt(); } boolean cond = false; Set<Integer> set = new HashSet<>(); set.add(0); for(int i =0 ; i<n ; i++) { ArrayList<Integer> al = new ArrayList<>(set); for(int e:al) { int num = e+arr[i]; if(set.contains(num)) { cond = true; break; } set.add(num); } } if(cond) System.out.println("YES"); else System.out.println("NO"); } }
// package codeforce.cfGR15; import java.io.PrintWriter; import java.util.*; public class D { // MUST SEE BEFORE SUBMISSION // check whether int part would overflow or not, especially when it is a * b!!!! public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); // int t = 1; for (int i = 0; i < t; i++) { solve(sc, pw); } pw.close(); } static void solve(Scanner in, PrintWriter out){ int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } Set<Integer> set = new HashSet<>(); for(int x : arr) { if (x == 0){ out.println("YES"); return; }else if (set.contains(x) || set.contains(-x)){ out.println("YES"); return; } set.add(x); } for (int i = 0; i < n; i++) { if (dfs(0, i, arr, 0)){ out.println("YES"); return; } } out.println("NO"); } static boolean dfs(int idx, int need, int[] arr, int cur){ if (cur == arr[need]) return true; if (idx == arr.length) return false; if (idx == need) return dfs(idx + 1, need, arr, cur); return dfs(idx + 1, need, arr, cur + arr[idx]) | dfs(idx + 1, need, arr, cur - arr[idx]) | dfs(idx + 1, need, arr, cur); } }
0
Non-plagiarised
08cf0478
0f14b12d
import java.util.*; import java.io.*; public class Main { static class Pair { int fi; int se; public Pair(int fi, int se) { this.fi = fi; this.se = se; } } public static void main(final String[] args) { FastScanner sc = new FastScanner(); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int[][] arr = new int[5][n]; for(int i = 0; i < n; i++) { char[] s = sc.next().toCharArray(); int[] cnt = new int[5]; for(int j = 0; j < s.length; j++) { cnt[s[j]-'a']++; } for(int j = 0; j < 5; j++) arr[j][i] = cnt[j]-(s.length-cnt[j]); } int ans = 0; for(int i = 0; i < 5; i++) { Arrays.sort(arr[i]); int maxSum = 0; int words = 0; for(int j = arr[i].length-1; j >=0; j--) { maxSum += arr[i][j]; if(maxSum > 0) words++; } ans = Math.max(ans, words); } System.out.println(ans); } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() {while (st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();}} return st.nextToken();} int 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;} } }
//import jdk.nashorn.internal.parser.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.*; import java.util.*; import javax.management.Query; public class Test{ public static void main(String[] args) throws IOException, InterruptedException{ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); String [] words = new String[n]; int [] occ = new int[5]; int [] occWord = new int [5]; boolean [] found ; for(int i =0;i<n;i++){ words[i] = sc.nextLine(); found = new boolean[5]; for(int j=0 ; j<words[i].length();j++){ occ[words[i].charAt(j)-'a']++; if(!found[words[i].charAt(j)-'a']){ found[words[i].charAt(j)-'a']=true; occWord[words[i].charAt(j)-'a'] ++; } } } int maxRes =0; for(int i =0;i<5;i++){ int maxChar = 'a' +i; PriorityQueue<Pair> pq = new PriorityQueue<>(); for (String word : words){ pq.add(new Pair(word,occOfMaxChar(word, maxChar)-occOfOtherChar(word, maxChar))); } int res = 0; int curr = 0; int maxCharCount = 0; int otherCharCount =0; while(!pq.isEmpty()){ String word = pq.poll().x; maxCharCount +=occOfMaxChar(word, maxChar); otherCharCount += occOfOtherChar(word, maxChar); curr ++; if(maxCharCount >otherCharCount){ res = curr; } } maxRes = Math.max(maxRes, res); } System.out.println(maxRes);} } public static int occOfMaxChar (String s, int maxChar){ int occ = 0; for(int i =0 ;i<s.length();i++){ if(s.charAt(i)==maxChar){ occ++; } } return occ; } public static int occOfOtherChar (String s, int maxChar){ int occ = 0; for(int i =0 ;i<s.length();i++){ if(s.charAt(i)!=maxChar){ occ++; } } return occ; } static int w; static int n; static long [][] memo; static int [] depth ; static long[] values; static ArrayList<Pair> gold ; public static long dp (int idx,int time){ if ( idx == n){ return 0; } if (memo[idx][time] != -1){ return memo[idx][time]; } long take = 0; if (3 * w*depth[idx] <= time){ take = values[idx]+ dp(idx+1, time-3*w*depth[idx]); } long leave = dp(idx+1, time); return memo[idx][time]=Math.max(take, leave); } static class Pair implements Comparable { String x; int y; public Pair (String x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Object o){ Pair p = (Pair) o; return p.y -y; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public boolean hasNext() { // TODO Auto-generated method stub return false; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
0
Non-plagiarised
584b0e9e
d9199dfd
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class D_Round_753_Div3 { public static int MOD = 1000000007; static int[][] dp; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int T = in.nextInt(); for (int z = 0; z < T; z++) { int n = in.nextInt(); int[] data = new int[n]; for (int i = 0; i < n; i++) { data[i] = in.nextInt(); } String line = in.next(); ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for (int i = 0; i < n; i++) { if (line.charAt(i) == 'B') { blue.add(data[i]); } else { red.add(data[i]); } } Collections.sort(blue); Collections.sort(red); int st = 1; boolean ok = true; for (int i : blue) { if (i < st) { ok = false; break; } st++; } if (ok) { for (int i : red) { if (i > st) { ok = false; break; } st++; } } out.println(ok ? "Yes" : "No"); } out.close(); } static int find(int v, int[] u) { if (v == u[v]) { return v; } return u[v] = find(u[v], u); } static int abs(int a) { return a < 0 ? -a : a; } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point { int x; int y; public Point(int start, int end) { this.x = start; this.y = end; } public String toString() { return x + " " + y; } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; data[index] %= MOD; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; result %= MOD; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return (val * val); } else { return (val * ((val * a))); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Scanner; public class Simple{ public static void main(String args[]){ //System.out.println("Hello Java"); Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t>0){ int n = s.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = s.nextInt(); } String str = s.next(); //Arrays.sort(arr); ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for(int i=0;i<n;i++){ if(str.charAt(i)=='R'){ red.add(arr[i]); } else{ blue.add(arr[i]); } } Collections.sort(red); Collections.sort(blue); int start =1; boolean bool =true; for(int i=0;i<blue.size();i++){ if(blue.get(i)<start){ bool = false; break; } start++; } if(!bool){ System.out.println("NO"); } else{ for(int i=0;i<red.size();i++){ if(red.get(i)>start){ bool = false; break; } start++; } if(bool){ System.out.println("YES"); } else{ System.out.println("NO"); } } t--; } s.close(); } }
1
Plagiarised
0c173033
6b83b22e
import java.io.*; import java.util.*; public class M { static Scanner scanner=new Scanner(System.in); public static void main(String[] args) { int t=scanner.nextInt(); while(t-->0) { int n=scanner.nextInt(); int a[]=new int [n]; int b[]=new int [n]; for(int i=0;i<n;i++)a[i]=scanner.nextInt(); for(int i=0;i+1<n;i+=2) { b[i]=-a[i+1]; b[i+1]=a[i]; } if(n%2==1) { int x=a[n-1],y=a[n-2],z=a[n-3]; if(x+y!=0) { b[n-3]=x+y; b[n-2]=-z; b[n-1]=-z; }else if(y+z!=0) { b[n-1]=y+z; b[n-2]=-x; b[n-3]=-x; }else { b[n-2]=x+z; b[n-1]=-y; b[n-3]=-y; } } StringBuilder sb=new StringBuilder(); for(int i=0;i<n;i++)sb.append(b[i]+" "); System.out.println(sb); } } }
import java.util.Scanner; public class Subsequence { private static Scanner sc = new Scanner(System.in); public static void main(String args[]) { int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int a[] = new int[n]; int b[] = new int[n]; for(int i=0;i<n;i++) { a[i]= sc.nextInt(); } if(n%2==0) { calculateB(a,b,n); } else { calculateB(a,b,n-3); if (a[n - 2] + a[n - 3] != 0) { b[n - 3] = -a[n - 1]; b[n - 2] = -a[n - 1]; b[n - 1] = a[n - 2] + a[n - 3]; } else if (a[n - 2] + a[n - 1] != 0) { b[n - 3] = a[n - 2] + a[n - 1]; b[n - 2] = -a[n - 3]; b[n - 1] = -a[n - 3]; } else { b[n - 3] = -a[n - 2]; b[n - 2] = a[n - 3] + a[n - 1]; b[n - 1] = -a[n - 2]; } } for(int i=0;i<n;i++) { System.out.print(b[i] + " "); } System.out.println(); } } private static void calculateB(int[] a, int[] b, int n) { for(int i=0;i<n-1;i=i+2) { b[i] = -a[i+1]; b[i+1] = a[i]; } } }
1
Plagiarised
43ca682a
baebdc56
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class D { static final int mod=998244353; static long[] facts, factInvs; public static void main(String[] args) { precomp(); FastScanner fs=new FastScanner(); int n=fs.nextInt(), k=fs.nextInt(); Seg[] segs=new Seg[n]; for (int i=0; i<n; i++) segs[i]=new Seg(fs.nextInt(), fs.nextInt()); Event[] events=new Event[n*2]; for (int i=0; i<n; i++) { events[2*i]=new Event(segs[i], true); events[2*i+1]=new Event(segs[i], false); } long ans=0; Arrays.sort(events); int counter=0; for (Event e:events) { if (e.start) { counter++; } else { counter--; if (counter+1<k) continue; else ans=add(ans, nCk(counter, k-1)); } } System.out.println(ans); } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static long add(long a, long b) { return (a+b)%mod; } static long mul(long a, long b) { return a*b%mod; } static long exp(long base, long e) { if (e==0) return 1; long half=exp(base, e/2); if (e%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long modInv(long x) { return exp(x, mod-2); } static void precomp() { facts=new long[1_000_000]; factInvs=new long[1_000_000]; factInvs[0]=facts[0]=1; for (int i=1; i<facts.length; i++) facts[i]=mul(facts[i-1], i); factInvs[facts.length-1]=modInv(facts[facts.length-1]); for (int i=facts.length-2; i>=0; i--) factInvs[i]=mul(factInvs[i+1], i+1); } static long nCk(int n, int k) { return mul(facts[n], mul(factInvs[k], factInvs[n-k])); } static class Seg { int l, r; public Seg(int l, int r) { this.l=l; this.r=r; } } static class Event implements Comparable<Event> { boolean start; Seg s; public Event(Seg s, boolean start) { this.s=s; this.start=start; } int x() { if (start) return s.l; return s.r; } public int compareTo(Event o) { if (x()!=o.x()) return Integer.compare(x(), o.x()); if (start==o.start)return 0; if (start) return -1; return 1; } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class D { static final int mod=998244353; static long[] facts, factInvs; public static void main(String[] args) { precomp(); FastScanner fs=new FastScanner(); int n=fs.nextInt(), k=fs.nextInt(); Seg[] segs=new Seg[n]; for (int i=0; i<n; i++) segs[i]=new Seg(fs.nextInt(), fs.nextInt()); Event[] events=new Event[n*2]; for (int i=0; i<n; i++) { events[2*i]=new Event(segs[i], true); events[2*i+1]=new Event(segs[i], false); } long ans=0; Arrays.sort(events); int counter=0; for (Event e:events) { if (e.start) { counter++; } else { counter--; if (counter+1<k) continue; else ans=add(ans, nCk(counter, k-1)); } } System.out.println(ans); } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static long add(long a, long b) { return (a+b)%mod; } static long mul(long a, long b) { return a*b%mod; } static long exp(long base, long e) { if (e==0) return 1; long half=exp(base, e/2); if (e%2==0) return mul(half, half); return mul(half, mul(half, base)); } static long modInv(long x) { return exp(x, mod-2); } static void precomp() { facts=new long[1_000_000]; factInvs=new long[1_000_000]; factInvs[0]=facts[0]=1; for (int i=1; i<facts.length; i++) facts[i]=mul(facts[i-1], i); factInvs[facts.length-1]=modInv(facts[facts.length-1]); for (int i=facts.length-2; i>=0; i--) factInvs[i]=mul(factInvs[i+1], i+1); } static long nCk(int n, int k) { return mul(facts[n], mul(factInvs[k], factInvs[n-k])); } static class Seg { int l, r; public Seg(int l, int r) { this.l=l; this.r=r; } } static class Event implements Comparable<Event> { boolean start; Seg s; public Event(Seg s, boolean start) { this.s=s; this.start=start; } int x() { if (start) return s.l; return s.r; } public int compareTo(Event o) { if (x()!=o.x()) return Integer.compare(x(), o.x()); if (start==o.start)return 0; if (start) return -1; return 1; } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
1
Plagiarised
968c1e7e
a4e1511a
import java.util.*; import java.io.*; public class E_Air_Conditioners{ public static void main(String[] args) { FastScanner s= new FastScanner(); StringBuilder res = new StringBuilder(); int t=s.nextInt(); int p=0; while(p<t){ int n=s.nextInt(); int k=s.nextInt(); int pos[]= new int[k]; int temp[]= new int[k]; int min=Integer.MAX_VALUE; int ans[]= new int[n]; HashMap<Integer,ArrayList<Integer>> map = new HashMap<Integer,ArrayList<Integer>>(); HashMap<Integer,Integer> count1 = new HashMap<Integer,Integer> (); for(int i=0;i<k;i++){ pos[i]=s.nextInt(); } for(int i=0;i<k;i++){ temp[i]=s.nextInt(); ans[pos[i]-1]=temp[i]; min=Math.min(temp[i],min); if(map.containsKey(temp[i])){ map.get(temp[i]).add(pos[i]-1); int a=count1.get(temp[i]); a++; count1.remove(temp[i]); count1.put(temp[i],a); } else{ ArrayList<Integer> obj = new ArrayList<Integer>(); obj.add(pos[i]-1); map.put(temp[i],obj); count1.put(temp[i],1); } } int num=min; while(true){ if(!map.containsKey(num)){ break; } ArrayList<Integer> obj2 = map.get(num); for(int i=0;i<obj2.size();i++){ int index=obj2.get(i); if(ans[index]!=0){ if(ans[index]<num){ if(index+1<n && (ans[index+1]>(num+1)|| ans[index+1]==0)){ ans[index+1]=num+1; if(map.containsKey(num+1)){ map.get(num+1).add(index+1); } else{ ArrayList<Integer> object = new ArrayList<Integer>(); object.add(index+1); map.put(num+1,object); } } if(index-1>=0 && (ans[index-1]>(num+1)|| ans[index-1]==0)){ ans[index-1]=num+1; if(map.containsKey(num+1)){ map.get(num+1).add(index-1); } else{ ArrayList<Integer> object = new ArrayList<Integer>(); object.add(index-1); map.put(num+1,object); } } } else if(ans[index]==num){ if(index+1<n && (ans[index+1]>(num+1)|| ans[index+1]==0)){ ans[index+1]=num+1; if(map.containsKey(num+1)){ map.get(num+1).add(index+1); } else{ ArrayList<Integer> object = new ArrayList<Integer>(); object.add(index+1); map.put(num+1,object); } } if(index-1>=0 && (ans[index-1]>(num+1)|| ans[index-1]==0)){ ans[index-1]=num+1; if(map.containsKey(num+1)){ map.get(num+1).add(index-1); } else{ ArrayList<Integer> object = new ArrayList<Integer>(); object.add(index-1); map.put(num+1,object); } } } } } num++; } for(int i=0;i<ans.length;i++){ res.append(ans[i]+" "); } res.append("\n"); p++; } System.out.println(res); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } }
//package Practice; import java.io.*; import java.util.*; public class codefor { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc=new FastReader(); int T=sc.nextInt(); while(T-->0) { int n=sc.nextInt(),k=sc.nextInt(),i=0; long t[]=new long[n]; int a[]=new int[k]; for(i=0;i<k;i++) a[i]=sc.nextInt()-1; for(i=0;i<k;i++) t[a[i]]=sc.nextLong(); long ans[]=new long[n]; PriorityQueue<Long> pq=new PriorityQueue<>(); for(i=0;i<n;i++) { if(t[i]!=0) pq.add(t[i]-i); if(pq.size()!=0) { ans[i]=pq.peek()+i; } } pq.clear(); for(i=n-1;i>=0;i--) { if(t[i]!=0) pq.add(t[i]+i); if(pq.size()!=0) { long val=pq.peek()-i; if(ans[i]==0) ans[i]=val; else ans[i]=Math.min(val, ans[i]); } } pq.clear(); for(i=0;i<n;i++) System.out.print(ans[i]+" "); System.out.println(); } } }
0
Non-plagiarised
69b2fd22
e6b7a899
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=1; while(T-->0) { int n=input.nextInt(); int a[]=new int[n]; ArrayList<Integer> list=new ArrayList<>(); ArrayList<Integer> space=new ArrayList<>(); for(int i=0;i<n;i++) { a[i]=input.nextInt(); if(a[i]==1) { list.add(i); } else { space.add(i); } } int pre[]=new int[space.size()]; for(int i=0;i<list.size();i++) { if(i==0) { int min=Integer.MAX_VALUE; for(int j=0;j<space.size();j++) { pre[j]=Math.abs(list.get(i)-space.get(j)); min=Math.min(min,pre[j]); pre[j]=min; } } else { int arr[]=new int[space.size()]; for(int j=0;j<i;j++) { arr[j]=Integer.MAX_VALUE; } int min=Integer.MAX_VALUE; for(int j=i;j<space.size();j++) { int v=Math.abs(list.get(i)-space.get(j)); v+=pre[j-1]; arr[j]=v; min=Math.min(min,v); arr[j]=min; } for(int j=0;j<space.size();j++) { pre[j]=arr[j]; } } } out.println(pre[space.size()-1]); } out.close(); } 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; } } }
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.*; public class Main { static final long MOD=1000000007; static final long MOD1=998244353; static long ans=0; //static ArrayList<Integer> ans=new ArrayList<>(); public static void main(String[] args){ PrintWriter out = new PrintWriter(System.out); InputReader sc=new InputReader(System.in); int N = sc.nextInt(); int[] A = sc.nextIntArray(N); ArrayList<Integer> a1 = new ArrayList<Integer>(); ArrayList<Integer> a2 = new ArrayList<Integer>(); for (int i = 0; i < A.length; i++) { if (A[i]==0) { a1.add(i); }else { a2.add(i); } } int[][] dp = new int[a1.size()+1][a2.size()+1]; for (int i = 0; i < dp.length; i++) { Arrays.fill(dp[i], Integer.MAX_VALUE/2); } dp[0][0] = 0; for (int i = 1; i <= a1.size() ; i++) { int pos1 = a1.get(i-1); for (int j = 0; j <= a2.size(); j++) { dp[i][j] = dp[i-1][j]; if (j-1>=0) { int pos2 = a2.get(j-1); dp[i][j] = Math.min(dp[i][j], dp[i-1][j-1] + Math.abs(pos1-pos2)); } } } System.out.println(dp[a1.size()][a2.size()]); } static class InputReader { private InputStream in; private byte[] buffer = new byte[1024]; private int curbuf; private int lenbuf; public InputReader(InputStream in) { this.in = in; this.curbuf = this.lenbuf = 0; } public boolean hasNextByte() { if (curbuf >= lenbuf) { curbuf = 0; try { lenbuf = in.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return false; } return true; } private int readByte() { if (hasNextByte()) return buffer[curbuf++]; else return -1; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private void skip() { while (hasNextByte() && isSpaceChar(buffer[curbuf])) curbuf++; } public boolean hasNext() { skip(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public double[] nextDoubleArray(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[][] nextCharMap(int n, int m) { char[][] map = new char[n][m]; for (int i = 0; i < n; i++) map[i] = next().toCharArray(); return map; } } }
0
Non-plagiarised
5dbd0a07
bdc08e6f
import java.io.*; import java.util.*; import static java.lang.Double.parseDouble; import static java.lang.Integer.compare; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.in; import static java.lang.System.out; import static java.util.Arrays.asList; import static java.util.Collections.max; import static java.util.Collections.min; public class Main { private static final int MOD = (int) (1E9 + 7); private static final int INF = (int) (1E9); static FastScanner scanner = new FastScanner(in); public static void main(String[] args) throws IOException { // Write your solution here StringBuilder answer = new StringBuilder(); int t = 1; t = parseInt(scanner.nextLine()); while (t-- > 0) { answer.append(solve()).append("\n"); } // out.println(answer); } private static Object solve() throws IOException { int n = scanner.nextInt(); int m = scanner.nextInt(); int x = scanner.nextInt(); Integer[] h = new Integer[n]; for (int i = 0; i < n; i++) { h[i] = scanner.nextInt(); } TreeSet<Pair> q = new TreeSet<>(); PriorityQueue<Pair> height = new PriorityQueue<>(); for (int i = 0; i < m; i++) { q.add(new Pair(i + 1,0)); } for (int i = 0; i < n; i++) { height.add(new Pair(i,h[i])); } boolean shift = false; int size = 0; while(!height.isEmpty()){ Pair p = height.poll(); Pair building = (shift) ? q.pollLast() : q.pollFirst(); h[p.index] = building.index; q.add(new Pair(building.index,building.value + p.value)); //out.println("q = " + q); if(++size == n){ size = 0; shift = !shift; } } if(safe(q,x)){ out.println("YES"); StringBuilder stringBuilder = new StringBuilder(); for (Object o : h) stringBuilder.append(o).append(" "); out.println(stringBuilder); } else { out.println("NO"); } return ""; } private static boolean safe(TreeSet<Pair> q, int x) { int last = q.pollFirst().value; while (!q.isEmpty()){ Pair p = q.pollFirst(); if(p.value - last > x) return false; last = p.value; } return true; } private static class Pair implements Comparable<Pair> { int index, value; public Pair(int index, int value) { this.index = index; this.value = value; } public int compareTo(Pair o) { if (value != o.value) return compare(value, o.value); return compare(index, o.index); } @Override public String toString() { return "Pair{" + "index=" + index + ", value=" + value + '}'; } } private static int maxx(Integer... a) { return max(asList(a)); } private static int minn(Integer... a) { return min(asList(a)); } private static long maxx(Long... a) { return max(asList(a)); } private static long minn(Long... a) { return min(asList(a)); } private static int add(int a, int b) { long res = ((long) (a + MOD) % MOD + (b + MOD) % MOD) % MOD; return (int) res; } private static int mul(int a, int b) { long res = ((long) a % MOD * b % MOD) % MOD; return (int) res; } private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return parseInt(next()); } long nextLong() { return parseLong(next()); } double nextDouble() { return parseDouble(next()); } } }
import java.io.*; import java.util.*; import static java.lang.Double.parseDouble; import static java.lang.Integer.compare; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.in; import static java.lang.System.out; import static java.util.Arrays.asList; import static java.util.Collections.max; import static java.util.Collections.min; public class Main { private static final int MOD = (int) (1E9 + 7); private static final int INF = (int) (1E9); static FastScanner scanner = new FastScanner(in); public static void main(String[] args) throws IOException { // Write your solution here StringBuilder answer = new StringBuilder(); int t = 1; t = parseInt(scanner.nextLine()); while (t-- > 0) { answer.append(solve()).append("\n"); } // out.println(answer); } private static Object solve() throws IOException { int n = scanner.nextInt(); int m = scanner.nextInt(); int x = scanner.nextInt(); Integer[] h = new Integer[n]; for (int i = 0; i < n; i++) { h[i] = scanner.nextInt(); } TreeSet<Pair> q = new TreeSet<>(); PriorityQueue<Pair> height = new PriorityQueue<>(); for (int i = 0; i < m; i++) { q.add(new Pair(i + 1,0)); } for (int i = 0; i < n; i++) { height.add(new Pair(i,h[i])); } boolean shift = false; int size = 0; while(!height.isEmpty()){ Pair p = height.poll(); Pair building = (shift) ? q.pollLast() : q.pollFirst(); h[p.index] = building.index; q.add(new Pair(building.index,building.value + p.value)); //out.println("q = " + q); if(++size == n){ size = 0; shift = !shift; } } if(safe(q,x)){ out.println("YES"); StringBuilder stringBuilder = new StringBuilder(); for (Object o : h) stringBuilder.append(o).append(" "); out.println(stringBuilder); } else { out.println("NO"); } return ""; } private static boolean safe(TreeSet<Pair> q, int x) { int last = q.pollFirst().value; while (!q.isEmpty()){ Pair p = q.pollFirst(); if(p.value - last > x) return false; last = p.value; } return true; } private static class Pair implements Comparable<Pair> { int index, value; public Pair(int index, int value) { this.index = index; this.value = value; } public int compareTo(Pair o) { if (value != o.value) return compare(value, o.value); return compare(index, o.index); } @Override public String toString() { return "Pair{" + "index=" + index + ", value=" + value + '}'; } } private static int maxx(Integer... a) { return max(asList(a)); } private static int minn(Integer... a) { return min(asList(a)); } private static long maxx(Long... a) { return max(asList(a)); } private static long minn(Long... a) { return min(asList(a)); } private static int add(int a, int b) { long res = ((long) (a + MOD) % MOD + (b + MOD) % MOD) % MOD; return (int) res; } private static int mul(int a, int b) { long res = ((long) a % MOD * b % MOD) % MOD; return (int) res; } private static int pow(int a, int b) { a %= MOD; int res = 1; while (b > 0) { if ((b & 1) != 0) res = mul(res, a); a = mul(a, a); b >>= 1; } return res; } private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return parseInt(next()); } long nextLong() { return parseLong(next()); } double nextDouble() { return parseDouble(next()); } } }
1
Plagiarised
29bbcd8b
d4779c71
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Stack; import java.util.StringTokenizer; public class D { private static final String INPUT_FILE_PATH = ""; void solve() { int n = in.nextInt(); int[] h = new int[n]; for (int i = 0; i < n; i++) h[i] = in.nextInt(); Stack<Integer> increasing = new Stack(); Stack<Integer> increasingIndices = new Stack(); Stack<Integer> decreasing = new Stack(); Stack<Integer> decreasingIndices = new Stack(); increasing.push(h[0]); increasingIndices.push(0); decreasing.push(h[0]); decreasingIndices.push(0); int[] dp = new int[n]; dp[0] = 0; for (int i = 1; i < n; i++) { dp[i] = dp[i - 1] + 1; while (!increasing.isEmpty() && increasing.peek() > h[i]) { dp[i] = Math.min(dp[i], 1 + dp[increasingIndices.peek()]); increasing.pop(); increasingIndices.pop(); } while (!decreasing.isEmpty() && decreasing.peek() < h[i]) { dp[i] = Math.min(dp[i], 1 + dp[decreasingIndices.peek()]); decreasing.pop(); decreasingIndices.pop(); } if (!increasing.isEmpty()) { dp[i] = Math.min(dp[i], 1 + dp[increasingIndices.peek()]); } if (!decreasing.isEmpty()) { dp[i] = Math.min(dp[i], 1 + dp[decreasingIndices.peek()]); } if (!increasing.isEmpty() && increasing.peek() == h[i]) { increasing.pop(); increasingIndices.pop(); } if (!decreasing.isEmpty() && decreasing.peek() == h[i]) { decreasing.pop(); decreasingIndices.pop(); } increasing.push(h[i]); increasingIndices.push(i); decreasing.push(h[i]); decreasingIndices.push(i); } out.println(dp[n - 1]); } private final InputReader in; private final PrintWriter out; private D(InputReader in, PrintWriter out) { this.in = in; this.out = out; } private static class InputReader { private BufferedReader br; private StringTokenizer st; public InputReader(InputStream inputStream) { this.br = new BufferedReader(new InputStreamReader(inputStream), 32768); this.st = null; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } public static void main(String[] args) throws Exception { InputStream inputStream = INPUT_FILE_PATH.isEmpty() ? System.in : new FileInputStream(new File(INPUT_FILE_PATH)); OutputStream outputStream = System.out; InputReader inputReader = new InputReader(inputStream); PrintWriter printWriter = new PrintWriter(outputStream); new D(inputReader, printWriter).solve(); printWriter.close(); } }
import java.util.ArrayList; import java.util.Scanner; import java.util.Stack; public class D { static Scanner sc = new Scanner(System.in); static int[] height; static int[] dp; public static void main(String[] args) { int n = sc.nextInt(); height = new int[n]; dp = new int[n]; dp[0] = 0; for (int i = 0; i < n; i++) { height[i] = sc.nextInt(); } Stack<Integer> rise = new Stack<Integer>(); Stack<Integer> fail = new Stack<Integer>(); rise.push(0); fail.push(0); for (int i = 1; i < n; i++) { dp[i] = dp[i-1]+1; if (rise.isEmpty()) { rise.push(i); } else if (height[rise.peek()] < height[i]) { rise.push(i); } else { while (!rise.isEmpty() && height[rise.peek()] > height[i]) { rise.pop(); if (!rise.isEmpty()) { dp[i] = Math.min(dp[i], dp[rise.peek()] + 1); } } while (!rise.isEmpty() && height[rise.peek()] == height[i]) { rise.pop(); } rise.push(i); } if (fail.isEmpty()) { fail.push(i); } else if (height[fail.peek()] > height[i]) { fail.push(i); } else { while (!fail.isEmpty() && height[fail.peek()] < height[i]) { fail.pop(); if (!fail.isEmpty()){ dp[i] = Math.min(dp[i], dp[fail.peek()] + 1); } } while (!fail.isEmpty() && height[fail.peek()] == height[i]) { fail.pop(); } fail.push(i); } } System.out.println(dp[n - 1]); } } // a[i] // // // 6 7 6 2 //3 5 6 4 5 6 3
0
Non-plagiarised
24afd00e
279e274a
/* JAI MATA DI */ import java.util.*; import java.io.*; import java.math.BigInteger; import java.sql.Array; public class CP { static class FR{ BufferedReader br; StringTokenizer st; public FR() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int mod = 1000000007; static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } /* ***************************************************************************************************************************************************/ static FR sc = new FR(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) { int tc = sc.nextInt(); while(tc-- > 0) { TEST_CASE(); } System.out.println(sb); } static void TEST_CASE() { int n = sc.nextInt(); lr = new long[n][2]; for(int i =0 ; i < n ; i ++) { lr[i][0] = sc.nextLong(); lr[i][1] = sc.nextLong(); } adj = new ArrayList<ArrayList<Integer>>(); for(int i = 0 ; i <n ; i++) adj.add(new ArrayList<Integer>()); for(int i = 0 ; i<n-1 ; i++) { int u = sc.nextInt()-1 , v = sc.nextInt()-1; adj.get(u).add(v); adj.get(v).add(u); } min = new long[n]; max = new long[n]; dfs(0,-1); sb.append(Math.max(min[0], max[0])).append("\n"); } static long[] min , max , lr[]; static ArrayList<ArrayList<Integer>> adj; static void dfs(int u , int p ) { for(int child:adj.get(u)) { if(child == p) continue; dfs(child , u); } long left = lr[u][0] , right = lr[u][1]; long ansl = 0 , ansr = 0; for(int child:adj.get(u)) { if(child == p) continue; long leftc = lr[child][0] , rightc = lr[child][1]; ansl += Math.max( min[child] + Math.abs(left - leftc) , max[child] +Math.abs(left - rightc) ); } for(int child:adj.get(u)) { if(child == p) continue; long leftc = lr[child][0] , rightc = lr[child][1]; ansr += Math.max( min[child] + Math.abs(right - leftc) , max[child] +Math.abs(right - rightc) ); } min[u] = ansl; max[u] = ansr; } }
import java.io.*; import java.util.*; public class C { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader s = new FastReader(); static PrintWriter out = new PrintWriter(System.out); private static int[] rai(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); } return arr; } private static int[][] rai(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextInt(); } } return arr; } private static long[] ral(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = s.nextLong(); } return arr; } private static long[][] ral(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = s.nextLong(); } } return arr; } private static int ri() { return s.nextInt(); } private static long rl() { return s.nextLong(); } private static String rs() { return s.next(); } static long gcd(long a,long b) { if(b==0) { return a; } return gcd(b,a%b); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } static boolean isPrime(int n) { //check if n is a multiple of 2 if (n % 2 == 0) return false; //if not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static int MOD=1000000007; static long mpow(long base,long pow) { long res=1; while(pow>0) { if(pow%2==1) { res=(res*base)%MOD; } pow>>=1; base=(base*base)%MOD; } return res; } static long[][] dp; static void dfs(List<Integer>[] list, long[] l, long[] r, boolean[] vis,int node) { vis[node] = true; for(int i:list[node]) { if(!vis[i]) { dfs(list,l,r,vis, i); dp[node][0] += Math.max(dp[i][0]+Math.abs(l[node]-l[i]), dp[i][1]+Math.abs(l[node]-r[i])); dp[node][1] += Math.max(dp[i][0]+Math.abs(r[node]-l[i]), dp[i][1]+Math.abs(r[node]-r[i])); } } } public static void main(String[] args) { StringBuilder ans = new StringBuilder(); int t = ri(); // int t = 1; while (t-- > 0) { int n=ri(); dp = new long[n][2]; long[] l = new long[n]; long[] r = new long[n]; for(int i=0;i<n;i++) { l[i] = rl(); r[i] = rl(); } List<Integer>[] list=new ArrayList[n]; for(int i=0;i<n;i++) { list[i] = new ArrayList<>(); } for(int i=0;i<n-1;i++) { int a= ri();int b=ri(); a--;b--; list[a].add(b); list[b].add(a); } boolean[] vis = new boolean[n]; dfs(list,l,r,vis,0); // for(int i=0;i<n;i++) // { // System.out.println(Arrays.toString(dp[i])); // } ans.append(Math.max(dp[0][0],dp[0][1])).append("\n"); } out.print(ans.toString()); out.flush(); } }
0
Non-plagiarised
949502c2
9fc811f7
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class Template { static int mod = 1000000007; public static void main(String[] args) { FastScanner sc = new FastScanner(); int yo = sc.nextInt(); while (yo-- > 0) { int n = sc.nextInt(); int a = sc.nextInt()-1; int b = sc.nextInt()-1; int da = sc.nextInt(); int db = sc.nextInt(); List<List<Integer>> list = new ArrayList<>(); for(int i = 0; i < n; i++) list.add(new ArrayList<>()); for(int i = 0; i < n-1; i++){ int x = sc.nextInt()-1; int y = sc.nextInt()-1; list.get(x).add(y); list.get(y).add(x); } for(int i = 0; i <= n; i++) depth[i] = 0; diam = 0; dfs(a,-1,list); if(2 * da >= min(diam, db) || depth[b] <= da){ out.println("Alice"); } else { out.println("Bob"); } } } static int[] depth = new int[200001]; static int diam = 0; static int dfs(int x, int p, List<List<Integer>> list) { int len = 0; List<Integer> ne = list.get(x); for(int y : ne) { if(y != p) { depth[y] = depth[x] + 1; int cur = 1 + dfs(y, x,list); diam = max(diam, cur + len); len = max(len, cur); } } return len; } public static class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } public static void sort(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for (int x : arr) ls.add(x); Collections.sort(ls); for (int i = 0; i < arr.length; i++) arr[i] = ls.get(i); } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] sieve(int N) { boolean[] sieve = new boolean[N + 1]; for (int i = 2; i <= N; i++) sieve[i] = true; for (int i = 2; i <= N; i++) { if (sieve[i]) { for (int j = 2 * i; j <= N; j += i) { sieve[j] = false; } } } return sieve; } public static long power(long x, long y, long p) { long res = 1L; x = x % p; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y >>= 1; x = (x * x) % p; } return res; } public static void print(int[] arr) { //for debugging only for (int x : arr) out.print(x + " "); out.println(); } static class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] readInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] readLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] readDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } // For Input.txt and Output.txt // FileInputStream in = new FileInputStream("input.txt"); // FileOutputStream out = new FileOutputStream("output.txt"); // PrintWriter pw = new PrintWriter(out); // Scanner sc = new Scanner(in); }
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.util.ArrayList; import java.io.InputStream; 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); DTreeTag solver = new DTreeTag(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class DTreeTag { int diam = 0; public int dfs(ArrayList<Integer> g[], int x, int depth[], int p) { int len = 0; for (int y : g[x]) { if (y != p) { depth[y] = depth[x] + 1; int cur = 1 + dfs(g, y, depth, x); diam = Math.max(diam, cur + len); len = Math.max(len, cur); } } return len; } public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int a = in.nextInt() - 1; int b = in.nextInt() - 1; int da = in.nextInt(); int db = in.nextInt(); int dis[] = new int[n]; ArrayList<Integer> g[] = new ArrayList[n]; for (int i = 0; i < n; i++) g[i] = new ArrayList<>(); for (int i = 0; i < n - 1; i++) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; g[u].add(v); g[v].add(u); } diam = 0; dfs(g, a, dis, -1); int disb = dis[b]; if (2 * da >= Math.min(diam, db) || disb <= da) { out.println("Alice"); } else { out.println("Bob"); } } } 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()); } } }
1
Plagiarised
929b98f0
bf85ab7b
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.*; public class Main { public static long[] ans(List<List<Integer>> edges, long[][] range, int root, boolean[] visited, PrintStream out) { if (visited[root]) { return new long[2]; } visited[root] = true; long[] ans = new long[2]; for (int x : edges.get(root)) { if (!visited[x]) { long[] temp = ans(edges, range, x, visited, out); ans[0] += Math.max(Math.abs(range[root][0] - range[x][0]) + temp[0], Math.abs(range[root][0] - range[x][1]) + temp[1]); ans[1] += Math.max(Math.abs(range[root][1] - range[x][0]) + temp[0], Math.abs(range[root][1] - range[x][1]) + temp[1]); } } return ans; } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintStream out = new PrintStream(System.out); int t = Integer.parseInt(reader.readLine()); while (t-->0) { int n = Integer.parseInt(reader.readLine()); long[][] range = new long[n][2]; for (int i = 0; i < n; i++) { String[] input = reader.readLine().split(" "); range[i][0] = Integer.parseInt(input[0]); range[i][1] = Integer.parseInt(input[1]); } List<List<Integer>> edges = new ArrayList<>(); for (int i = 0; i < n; i++) { edges.add(new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { String[] input = reader.readLine().split(" "); int u = Integer.parseInt(input[0]) - 1, v = Integer.parseInt(input[1]) - 1; edges.get(u).add(v); edges.get(v).add(u); } int root = 0; for (int i = 0; i < n; i++) { if (edges.get(i).size() > 1) { root = i; break; } if (edges.get(i).size() == 1) { root = i; } } long[] ans = ans(edges, range, root, new boolean[n], out); out.println(Math.max(ans[0], ans[1])); } out.close(); } }
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; public class ParsasHumongousTree { public static void main(String args[]) throws IOException { Reader scan = new Reader(); StringBuilder sb = new StringBuilder(); int t = scan.nextInt(); while (t-- > 0) { int n = scan.nextInt(); int[] l = new int[n + 1]; int[] r = new int[n + 1]; for (int i = 1; i <= n; i++) { l[i] = scan.nextInt(); r[i] = scan.nextInt(); } Graph g = new Graph(n); for (int i = 0; i < n - 1; i++) { g.addEdge(scan.nextInt(), scan.nextInt()); } sb.append(g.dfs(l, r) + "\n"); } System.out.println(sb); } } class Graph { ArrayList<Integer>[] node; int n; int c = 0; boolean[] vis; Graph(int s) { n = s + 1; vis = new boolean[n + 1]; node = new ArrayList[n + 1]; for (int i = 0; i < n + 1; i++) { node[i] = new ArrayList<>(); } } void addEdge(int u, int v) { node[u].add(v); node[v].add(u); if (node[u].size() == 1) { c = u; } if (node[v].size() == 1) { c = v; } } void cleanVisArray() { for (int i = 0; i < n + 1; i++) { vis[i] = false; } } long dfs(int[] l, int[] r) { cleanVisArray(); long[][] dp = new long[n][2]; dfsMain(1, dp, l, r); return Math.max(dp[1][0], dp[1][1]); } void dfsMain(int v, long[][] dp, int[] l, int[] r) { vis[v] = true; for (int i : node[v]) { if (!vis[i]) { dfsMain(i, dp, l, r); dp[v][0] += Math.max(Math.abs(l[v] - l[i]) + dp[i][0], Math.abs(l[v] - r[i]) + dp[i][1]); dp[v][1] += Math.max(Math.abs(r[v] - l[i]) + dp[i][0], Math.abs(r[v] - r[i]) + dp[i][1]); } } } } 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(); } }
0
Non-plagiarised
1be078c4
7ae0cb0d
import java.util.*; import java.io.*; public class E_1547 { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int T = sc.nextInt(); while(T-->0) { int n = sc.nextInt(), k = sc.nextInt(); int[] a = sc.nextIntArray(k); int[] t = sc.nextIntArray(k); int[] array = new int[n]; Arrays.fill(array, Integer.MAX_VALUE); for(int i = 0; i < k; i++) array[a[i] - 1] = t[i]; int[] pre = new int[n]; int[] post = new int[n]; int prev = (int)2e9; for(int i = 0; i < n; i++) prev = pre[i] = Math.min(prev + 1, array[i]); prev = (int)2e9; for(int i = n - 1; i >= 0; i--) prev = post[i] = Math.min(prev + 1, array[i]); for(int i = 0; i < n; i++) array[i] = Math.min(pre[i], post[i]); for(int i = 0; i < n; i++) pw.print(array[i] + (i == n - 1 ? "\n" : " ")); } pw.flush(); } public static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public int[] nextIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) array[i] = new Integer(nextInt()); return array; } public long[] nextLongArray(int n) throws IOException { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public double[] nextDoubleArray(int n) throws IOException { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } public static int[] shuffle(int[] a) { int n = a.length; Random rand = new Random(); for (int i = 0; i < n; i++) { int tmpIdx = rand.nextInt(n); int tmp = a[i]; a[i] = a[tmpIdx]; a[tmpIdx] = tmp; } return a; } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class AirConditioners { static FastReader read =new FastReader(); static int INF = (int)(2e9); static int mxn = (int)(3e5 + 10); public static void main(String[] args) { int t = read.nextInt(); while (t-- > 0) solve(); } private static void solve() { int n = read.nextInt(), k = read.nextInt(); int[] a = new int[n]; int[] t = new int[n]; Arrays.fill(t, Integer.MAX_VALUE); for (int i=0;i<k;++i){ a[i] = read.nextInt(); } for (int i=0;i<k;++i){ t[--a[i]] = read.nextInt(); } int[] L = new int[n]; int[] R = new int[n]; int tmp = INF; for (int i=0;i<n;++i){ tmp = Math.min(tmp+1, t[i]); L[i] = tmp; } tmp = INF; for (int i=n-1;i>=0;--i){ tmp = Math.min(tmp+1, t[i]); R[i] = tmp; } for (int i=0;i<n;++i){ int ans = Math.min(L[i], R[i]); System.out.print(ans + " "); } System.out.println(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } String[] strArray() { String[] str = nextLine().split(" "); return str; } int[] intArray(int n) { String[] str = strArray(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(str[i]); } return arr; } } }
0
Non-plagiarised
1dab88fb
a6229ee9
import java.util.*; public class Main { static class Edge{ public int node; public int index; public Edge(int n, int i){ node=n; index=i; } } static Scanner sc=new Scanner(System.in); public static void main(String[] args) { int test=sc.nextInt(); while(test-->0){ solve(); } } static void solve(){ int n=sc.nextInt(); ArrayList<ArrayList<Edge>> graph= new ArrayList<ArrayList<Edge>>(); for(int i=0;i<n;i++){ graph.add(new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { int u = sc.nextInt(); int v = sc.nextInt(); u--; v--; graph.get(u).add(new Edge(v, i)); graph.get(v).add(new Edge(u, i)); } int start = 0; for (int i = 0; i < n; i++) { if (graph.get(i).size() > 2) { System.out.println("-1"); return; } else if (graph.get(i).size() == 1) { start = i; } } int[] weight = new int[n - 1]; int prevNode = -1; int curNode = start; int curWeight = 2; while (true) { ArrayList<Edge> edges = graph.get(curNode); Edge next = edges.get(0); if (next.node == prevNode) { if (edges.size() == 1) { break; } else { next = edges.get(1); } } weight[next.index] = curWeight; prevNode = curNode; curNode = next.node; curWeight = 5 - curWeight; } for (int i = 0; i < n - 1; i++) { System.out.print(weight[i]); System.out.print(" "); } System.out.println(); } }
import java.util.*; import java.io.*; public class hmm { static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static boolean visited[]; static ArrayList<pair>arr[]; static HashMap<String,Integer>hm = new HashMap<>(); public static void dfs(int start, int prime) { visited[start]=true; for(pair x: arr[start]) { if(!visited[(int) x.x]) { x.y = prime==2?3:2; hm.put(start+","+x.x,x.y); hm.put(x.x+","+start, x.y); dfs(x.x,x.y); } } } public static void main(String[] args) throws Exception { int t =sc.nextInt(); while (t-- > 0) { int n =sc.nextInt(); visited= new boolean [n]; arr=new ArrayList[n]; int[]color = new int[n]; ArrayList<pair>lol = new ArrayList<pair>(); for(int i=0;i<n;i++) arr[i]=new ArrayList<pair>(); for(int i=0;i<n-1;i++) { int u = sc.nextInt()-1; int v = sc.nextInt()-1; lol.add(new pair(u,v)); arr[u].add(new pair(v,0)); arr[v].add(new pair(u,0)); } boolean can = true; for(int i=0;i<n;i++) if(arr[i].size()>2){ can = false; } if(!can) pw.println(-1); else { int []hh = new int [] {2,3}; int i=0; visited[0]= true; for(pair x: arr[0]) { hm.put(0+","+x.x,hh[i]); dfs(x.x,hh[i++]); } //dfs(0,3); for(pair a:lol) { int u = a.x; int y = a.y; if(hm.containsKey(u+","+y)) pw.print(hm.get(u+","+y)+" "); else pw.print(hm.get(y+","+u)+" "); } pw.println(); } } pw.close(); } // -------------- stuff ------------------------------ static class pair { int x ; int y; public pair(int n,int c) { x= n; y = c; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } } }
0
Non-plagiarised
6653a758
6bcc5afd
import java.util.*; public class D { public static void main(String[] args) { Scanner sc=new Scanner(System.in); ArrayList<Integer> o=new ArrayList<Integer>(), e=new ArrayList<Integer>(); int n = sc.nextInt(),dp[][]=new int[n+1][n+1]; for(int i=1;i<=n;i++){ int x=sc.nextInt(); if(x==1)o.add(i); else e.add(i); } for(int i=1;i<=o.size();i++){ dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1)); for(int j=i+1;j<=e.size();j++) dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1))); } System.out.println(dp[o.size()][e.size()]); } }
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int a[]=new int[n]; ArrayList<Integer> lt1=new ArrayList<>(); ArrayList<Integer> lt0=new ArrayList<>(); for(int i=0;i<n;i++) { int l=s.nextInt(); if(l==0) lt0.add(i+1); else lt1.add(i+1); } int dp[][]=new int[lt1.size()+1][lt0.size()+1]; for(int i=1;i<=lt1.size();i++) { dp[i][i]=dp[i-1][i-1]+Math.abs(lt0.get(i-1)-lt1.get(i-1)); for(int j=i+1;j<=lt0.size();j++) { dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(lt1.get(i-1)-lt0.get(j-1))); } } System.out.println(dp[lt1.size()][lt0.size()]); } }
1
Plagiarised
312d9460
e7024f3e
//https://codeforces.com/contest/1547/problem/E //E. Air Conditioners import java.util.*; import java.io.*; public class CF_1547_E{ public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); StringBuilder sb = new StringBuilder(); StringTokenizer st, st1; int q = Integer.parseInt(br.readLine()); while(q-->0){ br.readLine(); st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); PriorityQueue<Pair> right_side = new PriorityQueue<Pair>(); int at[] = new int[n+1]; st = new StringTokenizer(br.readLine()); st1 = new StringTokenizer(br.readLine()); for(int i=0;i<k;i++){ int a = Integer.parseInt(st.nextToken()); int t = Integer.parseInt(st1.nextToken()); at[a] = t; right_side.add(new Pair(a, t)); } long left= Integer.MAX_VALUE; for(int i=1;i<=n;i++){ while(right_side.isEmpty()==false && right_side.peek().a<=i){ Pair temp = right_side.poll(); if(temp.t - temp.a <= left) left = temp.t - temp.a; } if(at[i]!=0){ if(at[i]-i <=left) left = at[i] - i; } long ans = left+i; if(!right_side.isEmpty()){ Pair right = right_side.peek(); ans = Math.min(ans, right.t+right.a-i); } sb.append(ans+" "); } sb.append("\n"); } pw.print(sb); pw.flush(); pw.close(); } } class Pair implements Comparable<Pair>{ int a, t; Pair(int a, int t){ this.a = a; this.t = t; } public int compareTo(Pair A){ return (this.a+this.t-A.a-A.t); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { static String solve(int n, int k, int[] a, int[] t) { Pair[] pairs = new Pair[k]; for (int i = 0; i < k; i++) { pairs[i] = new Pair(a[i], t[i]); } Arrays.sort(pairs); int[] ret = new int[n + 1]; Arrays.fill(ret, Integer.MAX_VALUE); int pIdx = 0; int ct = pairs[pIdx].t; ret[pairs[pIdx].a] = ct; for (int i = pairs[pIdx].a + 1; i <= n; i++) { ct++; if (pIdx + 1 < k && pairs[pIdx + 1].a == i) { if (ct > pairs[pIdx + 1].t) { ct = pairs[pIdx + 1].t; } pIdx++; } ret[i] = ct; // System.out.println(Arrays.toString(ret)); } // System.out.println(); pIdx = k - 1; ct = pairs[pIdx].t; for (int i = pairs[pIdx].a - 1; i > 0; i--) { ct++; if (pIdx - 1 >= 0 && pairs[pIdx - 1].a == i) { if (ct > pairs[pIdx - 1].t) { ct = pairs[pIdx - 1].t; } pIdx--; } if (ct < ret[i]) { ret[i] = ct; } // System.out.println(Arrays.toString(ret)); } StringBuilder out = new StringBuilder(); for (int i = 1; i <= n; i++) { out.append(ret[i]).append(" "); } // System.out.println(); // System.out.println(); return out.toString(); } static class Pair implements Comparable<Pair> { int a, t; public Pair(int a, int t) { this.a = a; this.t = t; } @Override public int compareTo(Pair o) { return this.a - o.a; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); StringBuilder out = new StringBuilder(); int T = Integer.parseInt(st.nextToken()); while (T-- > 0) { st = new StringTokenizer(br.readLine()); st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] a = new int[k]; for (int i = 0; i < k; i++) { a[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(br.readLine()); int[] t = new int[k]; for (int i = 0; i < k; i++) { t[i] = Integer.parseInt(st.nextToken()); } out.append(solve(n, k, a, t)).append("\n"); } System.out.println(out); } }
0
Non-plagiarised
6e637812
aa8091b0
//package codeforces; import java.io.PrintWriter; import java.util.*; public class codeforces { public static void main(String[] args) { PrintWriter out=new PrintWriter(System.out); Scanner s=new Scanner(System.in); int t=s.nextInt(); for(int tt=0;tt<t;tt++) { int n=s.nextInt(),m=s.nextInt(),x=s.nextInt(); HashMap<Integer,ArrayList<Integer>> map=new HashMap<>(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=s.nextInt(); if(map.containsKey(a[i])) { ArrayList<Integer> c=map.get(a[i]); c.add(i+1); map.put(a[i], c); }else { ArrayList<Integer> c=new ArrayList<>(); c.add(i+1); map.put(a[i], c); } } sort(a); long ans[]=new long[m]; int c=0; boolean l=true; for(int i=n-1;i>=0;i--) { if(c==m && l) { c=m-1; l=false; }else if(!l && c==-1){ l=true; c=0; } ans[c]+=a[i]; if(l) { c++; }else { c--; } } long min=Integer.MAX_VALUE,max=Integer.MIN_VALUE; for(int i=0;i<m;i++) { min=Math.min(ans[i], min); max=Math.max(ans[i], max); } if(max-min>x) { System.out.println("NO"); }else { System.out.println("YES"); int a1[]=new int[n]; c=0; l=true; for(int i=n-1;i>=0;i--) { if(c==m && l) { c=m-1; l=false; }else if(!l && c==-1){ l=true; c=0; } a1[map.get(a[i]).get(0)-1]=c+1; map.get(a[i]).remove(0); if(l) { c++; }else { c--; } } for(int i=0;i<n;i++) { System.out.print(a1[i]+" "); } System.out.println(); } } out.close(); s.close(); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } }
import java.util.*; import java.io.*; public class AiseHi { static Scanner sc = new Scanner(System.in); static int mod = (int)(1e9+7); public static void main (String[] args) { PrintWriter out = new PrintWriter(System.out); int t = 1; t = sc.nextInt(); z : while(t-->0) { int n = sc.nextInt(); int m = sc.nextInt(); int x = sc.nextInt(); PriorityQueue<twoval> myQueue = new PriorityQueue<>(); for(int i=1;i<=m;i++) { myQueue.add(new twoval(0,i)); } List<twoval> arrayList = new ArrayList<>(); int myArray[] = new int[n]; for(int i=0;i<n;i++) { int aasjd = sc.nextInt(); arrayList.add(new twoval(aasjd,i)); myArray[i] = aasjd; } Collections.sort(arrayList); int ans[] = new int[n]; for(int i=n-1;i>=0;i--) { twoval p = myQueue.poll(); long aasjd = p.myArray; int idx = p.b; aasjd += arrayList.get(i).myArray; ans[arrayList.get(i).b] = idx; myQueue.add(new twoval(aasjd,idx)); } long dasdaknw[] = new long[m]; long min = Long.MAX_VALUE, max = Long.MIN_VALUE; for(int i=0;i<n;i++) { dasdaknw[ans[i]-1] += myArray[i]; } for(int i=0;i<m;i++) { min = Math.min(min, dasdaknw[ans[i]-1]); max = Math.max(max, dasdaknw[ans[i]-1]); } if(max-min>x) { out.write("NO\n"); continue; } out.write("YES\n"); for(int aasjd : ans) out.write(aasjd+" "); out.write("\n"); } out.close(); } static int ceil(int myArray,int b) { return myArray/b + (myArray%b==0?0:1); } static boolean prime[] = new boolean[2000009]; static int fac[] = new int[2000009]; static void sieve() { prime[0] = true; prime[1] = true; int max = 1000000; for(int i=2;i*i<=max;i++) { if(!prime[i]) { for(int j=i*i;j<=max;j+=i) { prime[j] = true; fac[j] = i; } } } } static int gcd(int myArray,int b) { if(b==0) return myArray; return gcd(b,myArray%b); } } class twoval implements Comparable<twoval>{ long myArray; int b; twoval(long aasjd,int r){ this.myArray = aasjd; this.b = r; } public int compareTo(twoval o) { return (int) (this.myArray - o.myArray); } }
0
Non-plagiarised
2b7b2d45
a0b406e6
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class D_1552 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]) { FastReader fs = new FastReader(); int tc = fs.nextInt(); while(tc-- > 0) { boolean flag=false; int n=fs.nextInt(); int[] ar=new int[n]; for(int i=0;i<n;i++) {ar[i]=fs.nextInt();} for(int i=1;i<Math.pow(3, n);i++) { int copy=i; int sum=0; for(int j=0;j<n;j++) { int rem=copy%3; sum+=rem==0?0:rem==1?ar[j]:-ar[j]; copy=copy/3; } if(sum==0) {System.out.println("yes"); flag=true; break;} } if(flag==false) System.out.println("no"); } } }
import java.io.*; import java.util.*; 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); Task solver = new Task(); int tcCnt = in.nextInt(); for (int tc = 1; tc <= tcCnt; tc++) solver.solve(tc, in, out); out.close(); } static class Task { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } for (int mask = 1; mask < (int)(Math.pow(3, n)); mask++) { int copy = mask; int sum = 0; for (int idx = 0; idx < n; idx++) { int digit = copy % 3; sum += digit == 0 ? 0 : digit == 1 ? a[idx] : -a[idx]; copy = copy / 3; } if (sum == 0) { out.println("YES"); return; } } out.println("NO"); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public String nextToken() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(nextLine()); } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public int[] nextIntArray(int size) { int[] arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = nextInt(); } return arr; } } }
1
Plagiarised
26e699de
3dd65549
//package Task1; import java.util.Scanner; public class Menorah { static int MOD9= 1000000000; public static void main(String[] args){ Scanner sc= new Scanner(System.in); int numberTest=sc.nextInt(); while(numberTest-->0){ int n=sc.nextInt(); char[] s=new char[n+5]; char[] t=new char[n+5]; String ss=sc.next(); String tt=sc.next(); s=ss.toCharArray(); t=tt.toCharArray(); int cntax = 0, cntbx = 0, same = 0; int ans=MOD9; for(int i=0; i<n; i++){ if(s[i]=='1')cntax++; if(t[i]=='1')cntbx++; if(s[i]==t[i])same++; } if(same==n){ System.out.println(0); continue; } else if (cntax==0){ System.out.println(-1); continue; } if(cntax==cntbx){ ans=Math.min(ans,n-same); } if(n-cntax+1==cntbx)ans=Math.min(ans,same); if(ans<MOD9) System.out.println(ans); else System.out.println(-1); } } }
import java.util.*; import java.io.*; public class codeforces { public static void main(String[] args) throws Exception { int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); char[] a=sc.next().toCharArray(); char[] b=sc.next().toCharArray(); int e0=0; int e1=0; int o0=0; int o1=0; for(int i=0;i<n;i++) { if(a[i]!=b[i]) { if(a[i]=='1') { e1++; }else { e0++; } }else { if(a[i]=='1') { o1++; }else { o0++; } } } int ans=Integer.MAX_VALUE; if(e1==e0) { ans=Math.min(ans, e1+e0); } if(o1==o0+1) { ans=Math.min(ans, o1+o0); } // pw.println(e0+" "+e1+" "+o0+" "+o1); pw.println(ans==Integer.MAX_VALUE?-1:ans); } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
0
Non-plagiarised
16857116
6e207cbf
import javax.swing.plaf.IconUIResource; import java.lang.reflect.Array; import java.text.CollationElementIterator; import java.util.*; import java.io.*; //Timus judge id- 323935JJ public class Main { //---------------------------------------------------------------------------------------------- public static class Pair implements Comparable<Pair> { int x=0,y=0; int z=0; public Pair(int a, int b) { this.x=a; this.y=b; } public int compareTo(Pair o) { return this.x - o.x; } } public static int mod = (int) (1e9 + 7); static int ans = Integer.MAX_VALUE; public static void main(String hi[]) throws Exception { FastReader sc = new FastReader(); int t =sc.nextInt(); while(t-->0) { int n =sc.nextInt(); String a = sc.nextLine(),b=sc.nextLine(); int count1=0,count2=0,count3=0,count4=0; for(int i=0;i<n;i++) { if(a.charAt(i)=='0'&&b.charAt(i)=='0') count1++; else if(a.charAt(i)=='1'&&b.charAt(i)=='1') count2++; else if(a.charAt(i)=='1'&&b.charAt(i)=='0') count3++; else if(a.charAt(i)=='0'&&b.charAt(i)=='1') count4++; } int ans=Integer.MAX_VALUE; if(count3==count4) ans=Math.min(count3*2,ans); if(count2==count1+1) ans=Math.min(ans,2*count1+1); if(count2==1&&count1==0) ans=1; if(count3==0&&count4==0) ans=0; if(ans==Integer.MAX_VALUE) System.out.println(-1); else System.out.println(ans); } } static int find(int[] a,int x) { if(a[x]==-1) return x; else return find(a,a[x]); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // method to return LCM of two numbers static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcdl(long a, long b) { if (a == 0) return b; return gcdl(b % a, a); } // method to return LCM of two numbers static long lcml(long a, long b) { return (a / gcdl(a, b)) * b; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
import java.util.*; import java.lang.*; import java.io.*; public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int sm, n; while(t > 0) { t--; n = sc.nextInt(); String s1,s2; s1 = sc.next(); s2 = sc.next(); int a[] = new int[4]; a[0] = 0; a[1] = 0; a[2] = 0; a[3] = 0; for(int i = 0 ; i < n ; i++) { if(s1.charAt(i) == '0'&& s2.charAt(i) == '1') a[0]++; else if(s1.charAt(i) == '1'&& s2.charAt(i) == '0') a[1]++; else if(s1.charAt(i) == '1'&& s2.charAt(i) == '1') a[2]++; else a[3]++; } // System.out.println(a[0] + " " + a[1] + " " + a[2] + " " + a[3]); int n1 = Integer.MAX_VALUE, n2 = Integer.MAX_VALUE, n3 = Integer.MAX_VALUE; if (a[0] == a[1]) { n1 = 2*a[0]; } if((a[2] - 1) == a[3]) { // System.out.println(a[3] + 1); n2 = 2*a[3] + 1; } if((a[3] + 1) == a[2]) { // System.out.println(a[2] + 1); n3 = 2*a[2] + 1; } int ans = Math.min(n1, Math.min(n2,n3)); if(ans == Integer.MAX_VALUE) { System.out.println("-1"); } else { System.out.println(ans); } } } }
0
Non-plagiarised
0588b869
9028caf7
import java.util.*; import java.io.*; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static final long mod=(long)1e9+7; public static long pow(long a,int p) { long res=1; while(p>0) { if(p%2==1) { p--; res*=a; res%=mod; } else { a*=a; a%=mod; p/=2; } } return res; } static class Pair { int u,v,w; Pair(int u,int v,int w) { this.u=u; this.v=v; this.w=w; } } /*static class Pair implements Comparable<Pair> { int v,l; Pair(int v,int l) { this.v=v; this.l=l; } public int compareTo(Pair p) { return l-p.l; } }*/ static int gcd(int a,int b) { if(b%a==0) return a; return gcd(b%a,a); } public static void dfs(int u,int dist[],int sub[],int mxv[],int par[],ArrayList<Integer> edge[]) { sub[u]=1; for(int v:edge[u]) { if(dist[v]==-1) { par[v]=u; dist[v]=dist[u]+1; dfs(v,dist,sub,mxv,par,edge); if(sub[v]+1>sub[u]) { sub[u]=sub[v]+1; mxv[u]=v; } } } } public static void main(String args[])throws Exception { FastReader fs=new FastReader(); PrintWriter pw=new PrintWriter(System.out); //int tc=fs.nextInt(); int n=fs.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=fs.nextInt(); ArrayList<Integer> o=new ArrayList<>(); ArrayList<Integer> z=new ArrayList<>(); for(int i=0;i<n;i++) { if(a[i]==1)o.add(i); else z.add(i); } int ans[][]=new int[o.size()+1][z.size()+1]; for(int i=1;i<=o.size();i++) { for(int j=i;j<=z.size();j++) { if(i==j)ans[i][j]=ans[i-1][j-1]+(int)Math.abs(o.get(i-1)-z.get(j-1)); else ans[i][j]=Math.min(ans[i][j-1],ans[i-1][j-1]+(int)Math.abs(o.get(i-1)-z.get(j-1))); } } pw.println(ans[o.size()][z.size()]); pw.flush(); pw.close(); } }
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=1; while(T-->0) { int n=input.nextInt(); int a[]=new int[n]; ArrayList<Integer> list=new ArrayList<>(); ArrayList<Integer> space=new ArrayList<>(); for(int i=0;i<n;i++) { a[i]=input.nextInt(); if(a[i]==1) { list.add(i); } else { space.add(i); } } int pre[]=new int[space.size()]; for(int i=0;i<list.size();i++) { if(i==0) { int min=Integer.MAX_VALUE; for(int j=0;j<space.size();j++) { pre[j]=Math.abs(list.get(i)-space.get(j)); min=Math.min(min,pre[j]); pre[j]=min; } } else { int arr[]=new int[space.size()]; for(int j=0;j<i;j++) { arr[j]=Integer.MAX_VALUE; } int min=Integer.MAX_VALUE; for(int j=i;j<space.size();j++) { int v=Math.abs(list.get(i)-space.get(j)); v+=pre[j-1]; arr[j]=v; min=Math.min(min,v); arr[j]=min; } for(int j=0;j<space.size();j++) { pre[j]=arr[j]; } } } out.println(pre[space.size()-1]); } out.close(); } 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; } } }
0
Non-plagiarised
1097b326
b45d28b2
// package com.company; import java.util.Scanner; public class Main { public static void solution1(){ Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- != 0){ int n, k1, k2, w, b; n = scanner.nextInt(); k1 = scanner.nextInt(); k2 = scanner.nextInt(); w = scanner.nextInt(); b = scanner.nextInt(); if (k1 < k2){ int temp = k1; k1 = k2; k2 = temp; } int white_dominoes = k2 + (k1 - k2) / 2; int black_dominoes = (n - k1) + (k1 - k2) / 2; if (white_dominoes >= w && black_dominoes >= b) { System.out.println("YES"); } else System.out.println("NO"); } scanner.close(); } public static void solution2(){ Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); scanner.nextLine(); while (t-- != 0) { String s; s = scanner.nextLine(); boolean flag = true; int first_ones = -1, last_zeroes = -1; for (int i=0; i<s.length() - 1; i++){ if (s.charAt(i) == '1' && s.charAt(i+1) == '1'){ first_ones = i; break; } } for (int i=s.length() - 1; i>0; i--){ if (s.charAt(i) == '0' && s.charAt(i-1) == '0'){ last_zeroes = i-1; break; } } if (first_ones != -1 && last_zeroes != -1 && first_ones < last_zeroes) flag = false; if (flag) System.out.println("YES"); else System.out.println("NO"); } scanner.close(); } public static void solution3(){ Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- > 0){ int n; n = scanner.nextInt(); int[] costs = new int[n]; for (int i=0; i<n; i++) costs[i] = scanner.nextInt(); //starts with Even index 0 long minEven = costs[0]; long minOdd = costs[1]; long totalEven = minEven; long totalOdd = minOdd; long minCost = minEven * n + minOdd * n; for (int i=2; i<n; i++){ if (i%2 == 1){ minOdd = Math.min(minOdd, costs[i]); totalOdd += costs[i]; } else{ minEven = Math.min(minEven, costs[i]); totalEven += costs[i]; } long this_cost = totalEven - minEven + minEven * (n - (i+2)/2 + 1) + totalOdd - minOdd + minOdd * (n - (i+1)/2 + 1); minCost = Math.min(minCost, this_cost); } System.out.println(minCost); } scanner.close(); } public static long power(int a, int b){ long ans = 1; while (b > 0){ if (b%2 == 1){ ans *= a; } a *= a; b >>= 1; } return ans; } public static void main(String[] args) { solution3(); } }
/* Codeforces Problem 1499C */ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class MinimumGridPath { public static void main(String[] args) throws IOException { FastIO in = new FastIO(System.in); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); long[] c = new long[n]; long[] sums = new long[n]; long[] mins = new long[n]; for (int i = 0; i < n; i++) c[i] = in.nextInt(); sums[0] = mins[0] = c[0]; sums[1] = mins[1] = c[1]; for (int i = 2; i < n; i++) { sums[i] = sums[i-2] + c[i]; mins[i] = Math.min(mins[i-2], c[i]); } long ans = Long.MAX_VALUE; for (int i = 1; i < n; i++) { ans = Math.min(ans, (n - (long) Math.ceil((double) (i+1) / 2)) * mins[i] + (n - (i+1) / 2) * mins[i-1] + sums[i] + sums[i-1]); } System.out.println(ans); } in.close(); } public static class FastIO { private InputStream dis; private byte[] buffer = new byte[1 << 17]; private int pointer = 0; public FastIO(String fileName) throws IOException { dis = new FileInputStream(fileName); } public FastIO(InputStream is) throws IOException { dis = is; } public int nextInt() throws IOException { int ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } public long nextLong() throws IOException { long ret = 0; byte b; do { b = nextByte(); } while (b <= ' '); boolean negative = false; if (b == '-') { negative = true; b = nextByte(); } while (b >= '0' && b <= '9') { ret = 10 * ret + b - '0'; b = nextByte(); } return (negative) ? -ret : ret; } public byte nextByte() throws IOException { if (pointer == buffer.length) { dis.read(buffer, 0, buffer.length); pointer = 0; } return buffer[pointer++]; } public String next() throws IOException { StringBuffer ret = new StringBuffer(); byte b; do { b = nextByte(); } while (b <= ' '); while (b > ' ') { ret.appendCodePoint(b); b = nextByte(); } return ret.toString(); } public void close() throws IOException { dis.close(); } } }
0
Non-plagiarised
8d9871a9
eb6cfca7
import java.util.*; public class Main { static Scanner scan = new Scanner(System.in); static int[] readArray(int[] x) { for(int i=0; i<x.length; ++i) x[i] = scan.nextInt(); return x; } static long[] readArray(long[] x) { for(int i=0; i<x.length; ++i) x[i] = scan.nextLong(); return x; } public static void go() { } public static void main(String[] args) { int t = scan.nextInt(); for(int it=0; it<t; ++it) { int n = scan.nextInt(); long[] aa = readArray(new long[n]); long minEven = aa[0]; long minOdd = aa[1]; long sum = aa[0]+aa[1]; long best = n*minEven + n*minOdd; int numOdd = 1; int numEven = 1; for(int i=2; i<n; ++i) { if(i%2 == 0) { minEven = Math.min(aa[i], minEven); numEven++; }else { minOdd = Math.min(aa[i], minOdd); numOdd++; } sum += aa[i]; long score = sum; score += minEven*(n-numEven); score += minOdd*(n-numOdd); best = Math.min(best, score); } System.out.println(best); } } }
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.List; import java.util.Map.Entry; import java.util.TreeMap; public class Main { public Main() throws FileNotFoundException { // File file = Paths.get("input.txt").toFile(); // if (file.exists()) { // System.setIn(new FileInputStream(file)); // } long t = System.currentTimeMillis(); InputReader reader = new InputReader(); int ttt = reader.nextInt(); for (int tt = 0; tt < ttt; tt++) { int n=reader.nextInt(); long[] s=new long[n]; for(int i=0;i<n;i++) { s[i]=reader.nextLong(); } long smallest1=s[0]; long smallest2=s[1]; long val=n*s[0]+n*s[1]; int left1=n-1; int left2=n-1; long base=s[0]+s[1]; for(int i=2;i<n;i++) { if(i%2==0) { //left1 val=Math.min(val, base+left2*smallest2+left1*s[i]); base+=s[i]; smallest1=Math.min(smallest1, s[i]); left1--; }else { val=Math.min(val, base+left1*smallest1+left2*s[i]); base+=s[i]; smallest2=Math.min(smallest2, s[i]); left2--; //left2 } } System.out.println(val); } } static class InputReader { private byte[] buf = new byte[16384]; 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; } } public static void main(String[] args) throws FileNotFoundException { new Main(); } }
0
Non-plagiarised
11d91f31
eea69e7f
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; public class ArmChairs { static final Random random = new Random(); 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; } int[] readArray(int n, int size) { int[] a = new int[size]; for (int i = 0; i < n; i++) { a[i] = this.nextInt(); } return a; } long[] readLongArray(int n, int size) { long[] a = new long[size]; for (int i = 0; i < n; i++) { a[i] = this.nextLong(); } return a; } } static void ruffleSort(long[] a) { int n = a.length;//shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n); long temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static void ruffleSort(int[] a) { int n = a.length;//shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } public static void main(String[] args) { FastReader fs = new FastReader(); int t = 1; for (int z = 0; z < t; z++) { int n = fs.nextInt(); List<Integer> empty = new ArrayList<>(); List<Integer> chairs = new ArrayList<>(); for(int i = 0; i < n; i++) { int status = fs.nextInt(); if(status == 1) chairs.add(i+1); else empty.add(i+1); } int[][] dp = new int[empty.size() + 1][chairs.size() + 1]; dp[0][0] = 0; for(int i = 1; i <= chairs.size(); i++) dp[0][i] = (int)3e+8; for(int i = 1; i <= empty.size(); i++) { for(int j = 1; j <= chairs.size(); j++) { // Shift jth person to ith chair dp[i][j] = dp[i-1][j-1] + Math.abs(empty.get(i-1) - chairs.get(j-1)); dp[i][j] = Math.min(dp[i][j], dp[i-1][j]); } //System.out.println(i + " " + Arrays.toString(dp[i])); } //System.out.println(empty.size() + " " + chairs.size()); System.out.println(dp[empty.size()][chairs.size()]); } } }
import java.util.*; public class Solution { public static int minMoves(int[] input) { List<Integer> people = new ArrayList<Integer>(); List<Integer> chairs = new ArrayList<Integer>(); for (int i = 0; i < input.length; i++) { if (input[i] == 1) { people.add(i); } else { chairs.add(i); } } int[] memo = new int[chairs.size() + 1]; for (int p = 1; ((!people.isEmpty()) && (p <= people.size())); p++) { int prev = memo[p]; memo[p] = memo[p - 1] + Math.abs(people.get(p - 1) - chairs.get(p - 1)); for (int c = p + 1; c <= chairs.size(); c++) { int tmp = memo[c]; memo[c] = Math.min(memo[c - 1], prev + Math.abs(people.get(p - 1) - chairs.get(c - 1))); prev = tmp; } } return memo[memo.length - 1]; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] input = new int[n]; for (int i = 0; i < n; i++) { input[i] = sc.nextInt(); } System.out.println(Solution.minMoves(input)); } }
0
Non-plagiarised
bf0df1d5
ea899386
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; import java.io.IOException; public class C_Phoenix_and_Towers { public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static class Pair implements Comparable<Pair> { int id, h; public Pair(int id, int h) { this.id = id; this.h = h; } public int compareTo(Pair o) { return this.h - o.h; } } public static void main(String[] args) throws java.lang.Exception { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); int x = sc.nextInt(); int tow[] = new int[n]; int ans[] = new int[n]; PriorityQueue<Pair> pq = new PriorityQueue<>(); for (int i = 0; i < n; i++) { tow[i] = sc.nextInt(); } for (int i = 0; i < m; i++) { ans[i] = i + 1; pq.add(new Pair(i + 1, tow[i])); } for (int i = m; i < n; i++) { Pair p = pq.poll(); p.h = p.h + tow[i]; ans[i] = p.id; pq.add(p); } System.out.println("YES"); for (int i = 0; i < n; i++) { System.out.print(ans[i] + " "); } System.out.println(); } } }
import java.io.*; import java.util.*; public class Codeforces { public static class Tower implements Comparable<Tower>{ int val; int index; public Tower(int ind, int v) { val = v; index = ind; } @Override public int compareTo(Tower o) { return Integer.compare(val, o.val); } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int cases = Integer.parseInt(br.readLine()); while(cases-- > 0) { String[] str = br.readLine().split(" "); int n = Integer.parseInt(str[0]); int m = Integer.parseInt(str[1]); int x = Integer.parseInt(str[2]); int[] h = new int[n]; str = br.readLine().split(" "); for(int i=0; i<n; i++) { h[i] = Integer.parseInt(str[i]); } PriorityQueue<Tower> q = new PriorityQueue<>(m); int[] ans = new int[n]; for(int i=0; i<m; i++) { q.add(new Tower(i, h[i])); ans[i] = i; } for(int i=m; i<n; i++) { Tower lowest = q.poll(); lowest.val += h[i]; ans[i] = lowest.index; q.add(lowest); } System.out.println("YES"); for(int i=0; i<n; i++) { System.out.print((ans[i]+1) + " "); } System.out.println(); } } }
1
Plagiarised
1b372750
fdd41565
import java.io.*; import java.util.*; import java.math.BigInteger; import java.util.InputMismatchException; public class Main { static PrintWriter out; static Reader in; public static void main(String[] args) throws IOException { input_output(); Main solver = new Main(); solver.solve(); out.close(); out.flush(); } static long INF = (long)1e18; static int MAXN = (int)1e5 + 5; static int MOD = (int)1e9 + 7; static int q, t, n, m, k; static double pi = Math.PI; void solve() throws IOException { t = in.nextInt(); while (t --> 0) { n = in.nextInt(); String[] arr = new String[n]; for (int i = 0; i < n; i++) { arr[i] = in.next(); } int ans = 0; for (int i = 0; i < 26; i++) { List<Integer> list = new ArrayList<>(); for (int j = 0; j < n; j++) { int tmp = 0; for (int x = 0; x < arr[j].length(); x++) { if (arr[j].charAt(x)-'a' == i) tmp++; else tmp--; } list.add(tmp); } Collections.sort(list); int sum = 0, tmpAns = 0; for (int j = n-1; j >= 0; j--) { if (sum + list.get(j) > 0) { tmpAns++; sum += list.get(j); } else break; } ans = Math.max(ans, tmpAns); } out.println(ans); } } static class Reader { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public Reader() { this(System.in); } public Reader(InputStream is) { mIs = is; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = mIs.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } double nextDouble() { return Double.parseDouble(next()); } 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 boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static void input_output() throws IOException { File f = new File("in.txt"); if (f.exists() && !f.isDirectory()) { in = new Reader(new FileInputStream("in.txt")); } else in = new Reader(); f = new File("out.txt"); if (f.exists() && !f.isDirectory()) { out = new PrintWriter(new File("out.txt")); } else out = new PrintWriter(System.out); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String args[]) { FastReader s=new FastReader(); int t=s.nextInt(); while(t>0) { Solve solve=new Solve(); t--; int n=s.nextInt(); String str[]=new String[n]; for(int i=0;i<n;i++) str[i]=s.nextLine(); char array[]=new char[]{'a','b','c','d','e'}; int arr[]=new int[n]; int ans=0; for(int i=0;i<5;i++) { Arrays.fill(arr,0); for(int j=0;j<n;j++) { for(int k=0;k<str[j].length();k++) { if(str[j].charAt(k)==array[i]) arr[j]++; else arr[j]--; } } ans=(ans>solve.solve(arr,n))?ans:solve.solve(arr,n); } System.out.println(ans); } } } class Solve{ public int solve(int arr[],int n) { int ans=0; int sum=0; Arrays.sort(arr); for(int i=n-1;i>=0;i--) { if(sum+arr[i]>0) { sum+=arr[i]; ans++; } else break; } return ans; } } 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; } }
1
Plagiarised
46e9aed4
7011024d
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public final class Solution { public static void main(String[] args) throws Exception { Reader sc = new Reader(); BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out)); int n=sc.nextInt(); ArrayList<Integer> fill= new ArrayList<Integer>(); ArrayList<Integer> unfilled= new ArrayList<>(); for(int i=0;i<n;i++){ int x =sc.nextInt(); if(x==1){ fill.add(i); }else{ unfilled.add(i); } } Collections.sort(fill); Collections.sort(unfilled); long[][] dp =new long[fill.size()+1][unfilled.size()+1]; for(int i=0;i<fill.size()+1;i++){ for(int j=0;j<unfilled.size()+1;j++){ dp[i][j]=Integer.MAX_VALUE; } } for(int i=0;i<unfilled.size()+1;i++){ dp[0][i]=0; } // for(int j=0;j<fill.size()+1;j++){ // dp[j][0]=0; // } for(int i=1;i<fill.size()+1;i++){ for(int j=1;j<unfilled.size()+1;j++){ dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(fill.get(i-1)-unfilled.get(j-1))); } } System.out.println(dp[fill.size()][unfilled.size()]); // for(int i=0;i<fill.size()+1;i++){ // for(int j=0;j<unfilled.size()+1;j++) // { // System.out.print(dp[i][j]+" "); // } // System.out.println(); // } } } 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') { if (cnt != 0) { break; } else { continue; } } 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(); } }
import java.util.*; public class D { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); ArrayList<Integer> occupied = new ArrayList<>(); ArrayList<Integer> vacant = new ArrayList<>(); for (int i = 0; i < n; i++) { int x = scanner.nextInt(); if (x == 1) occupied.add(i); else vacant.add(i); } Solution Solution = new Solution(occupied, vacant); // System.out.println(Solution.tabulation()); System.out.println(Solution.memoization()); } } class Solution { ArrayList<Integer> occupied, vacant; int x, y; public Solution(ArrayList<Integer> occupied, ArrayList<Integer> vacant) { this.occupied = occupied; this.vacant = vacant; x = occupied.size(); y = vacant.size(); } int tabulation() { return tabulation(x, y); } int tabulation(int x, int y) { int[][] dp = new int[x+1][y+1]; for (int i = 0; i <= x; i++) { Arrays.fill(dp[i], Integer.MAX_VALUE/2); } for (int i = 0; i <= x; i++) { dp[i][0] = 0; } for (int i = 0; i <= y; i++) { dp[0][i] = 0; } for (int i = 1; i <= x; i++) { for (int j = 1; j <= y; j++) { if(i == j) { dp[i][j] = dp[i-1][j-1] + Math.abs(occupied.get(i-1) - vacant.get(j-1)); } else { dp[i][j] = Math.min(dp[i][j-1], dp[i-1][j-1] + Math.abs(occupied.get(i-1) - vacant.get(j-1))); } } } return dp[x][y]; } int memoization() { int[][] dp = new int[x][y]; for (int i = 0; i < x; i++) { Arrays.fill(dp[i], -1); } return memoization(dp, x-1, y-1); } int memoization(int[][] dp, int n, int m) { if(n < 0) { return 0; } if(m < n) { return Integer.MAX_VALUE; } if(dp[n][m] != -1) { return dp[n][m]; } int first = memoization(dp, n, m-1); int second = memoization(dp, n-1, m-1) + Math.abs(occupied.get(n) - vacant.get(m)); dp[n][m] = Math.min(first, second); return dp[n][m]; } }
0
Non-plagiarised
2120328e
52cd85f2
/***** ---> :) Vijender Srivastava (: <--- *****/ import java.util.*; import java.lang.*; import java.io.*; public class Main { static FastReader sc =new FastReader(); static PrintWriter out=new PrintWriter(System.out); static int mod=10000007; static StringBuilder sb=new StringBuilder(); /* start */ public static void main(String [] args) { int t = i(); while(t-->0) { int n = i(); int a[] = input(n); char c[] = inputC(); ArrayList<Integer> b = new ArrayList<>(); ArrayList<Integer> r = new ArrayList<>(); for(int i=0;i<n;i++) { if(c[i]=='R') r.add(a[i]); else b.add(a[i]); } Collections.sort(b); Collections.sort(r,Collections.reverseOrder()); boolean is = true; int cnt = 1; for(int i=0;i<b.size();i++) { if(b.get(i)<cnt) { is = false; break; } cnt++; } for(int i=0;i<r.size();i++) { if(r.get(i)>n-i) { is = false; break; } } out.println(is==true?"YES":"NO"); } out.close(); } /* end */ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static long[] putL(long a[]) { long A[]=new long[a.length]; for(int i=0;i<a.length;i++) { A[i]=a[i]; } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x) ; y = y >> 1; x = (x * x); } return res; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } //pair class private static class Pair implements Comparable<Pair> { int first, second; public Pair(int f, int s) { first = f; second = s; } @Override public int compareTo(Pair p) { if (first > p.first) return 1; else if (first < p.first) return -1; else { if (second > p.second) return 1; else if (second < p.second) return -1; else return 0; } } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.StringTokenizer; public class PC3C { static PrintWriter out = new PrintWriter(System.out); static MyFastReaderPC3C in = new MyFastReaderPC3C(); static long mod = (long) (1e9 + 7); public static void main(String[] args) throws Exception { int test = i(); while (test-- > 0) { int n=i(); int[] arr=arrI(n); String s=string(); ArrayList<Integer> lR=new ArrayList<>(); ArrayList<Integer> lB=new ArrayList<>(); for(int i=0;i<n;i++) { if(s.charAt(i)=='R') lR.add(arr[i]); else lB.add(arr[i]); } Collections.sort(lB); Collections.sort(lR,Collections.reverseOrder()); int k=1; boolean st=true; for(int i=0;i<lB.size();i++) { if(lB.get(i)>=k) { k+=1; } else { st=false; break; } } boolean st2=true; k=n; for(int i=0;i<lR.size();i++) { if(lR.get(i)>k) { st2=false; break; } else { k-=1; } } if(st && st2) out.print("YES"); else out.print("NO"); out.print("\n"); out.flush(); } out.close(); } static class pair { long x, y; pair(long ar, long ar2) { x = ar; y = ar2; } } static void sort(long[] a) // check for long { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static class DescendingComparator implements Comparator<Integer> { public int compare(Integer a, Integer b) { return b - a; } } static class AscendingComparator implements Comparator<Integer> { public int compare(Integer a, Integer b) { return a - b; } } static boolean isPalindrome(char X[]) { int l = 0, r = X.length - 1; while (l <= r) { if (X[l] != X[r]) return false; l++; r--; } return true; } static long fact(long N) { long num = 1L; while (N >= 1) { num = ((num % mod) * (N % mod)) % mod; N--; } return num; } static long pow(long a, long b) { long mod = 1000000007; long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) pow = (pow * x) % mod; x = (x * x) % mod; b /= 2; } return pow; } static long toggleBits(long x)// one's complement || Toggle bits { int n = (int) (Math.floor(Math.log(x) / Math.log(2))) + 1; return ((1 << n) - 1) ^ x; } static int countBits(long a) { return (int) (Math.log(a) / Math.log(2) + 1); } static boolean isPrime(long N) { if (N <= 1) return false; if (N <= 3) return true; if (N % 2 == 0 || N % 3 == 0) return false; for (int i = 5; i * i <= N; i = i + 6) if (N % i == 0 || N % (i + 2) == 0) return false; return true; } static long GCD(long a, long b) { if (b == 0) { return a; } else return GCD(b, a % b); } // Debugging Functions Starts static void print(char A[]) { for (char c : A) System.out.print(c + " "); System.out.println(); } static void print(boolean A[]) { for (boolean c : A) System.out.print(c + " "); System.out.println(); } static void print(int A[]) { for (int a : A) System.out.print(a + " "); System.out.println(); } static void print(long A[]) { for (long i : A) System.out.print(i + " "); System.out.println(); } static void print(ArrayList<Integer> A) { for (int a : A) System.out.print(a + " "); System.out.println(); } // Debugging Functions END // ---------------------- // IO FUNCTIONS STARTS static HashMap<Integer, Integer> getHashMap(int A[]) { HashMap<Integer, Integer> mp = new HashMap<>(); for (int a : A) { int f = mp.getOrDefault(a, 0) + 1; mp.put(a, f); } return mp; } public static Map<Character, Integer> mapSortByValue(Map<Character, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Character, Integer>> list = new LinkedList<Map.Entry<Character, Integer>>(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() { public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) { return o1.getValue() - o2.getValue(); } }); // put data from sorted list to hashmap Map<Character, Integer> temp = new LinkedHashMap<Character, Integer>(); for (Map.Entry<Character, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static String string() { return in.nextLine(); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] arrI(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] arrL(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) A[i] = in.nextLong(); return A; } } class MyFastReaderPC3C { BufferedReader br; StringTokenizer st; public MyFastReaderPC3C() { 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; } }
0
Non-plagiarised
13441e8f
2ebeae13
import java.util.*; public class Armchairs { public static int findMinTime(List<Integer> zeros, List<Integer> ones) { if (ones.size() == 0) return 0; int oneSize = ones.size(); int zeroSize = zeros.size(); int [][] time = new int [oneSize + 1][zeroSize + 1]; for (int i=1; i<=oneSize; i++) { time[i][i] = time[i - 1][i - 1] + Math.abs(ones.get(i - 1) - zeros.get(i - 1)); for (int j=i+1; j<=zeroSize; j++) { time[i][j] = Math.min(time[i][j - 1], time[i - 1][j - 1] + Math.abs(ones.get(i - 1) - zeros.get(j - 1))); } } return time[oneSize][zeroSize]; } public static void main (String [] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); List<Integer> zeros = new ArrayList<>(); List<Integer> ones = new ArrayList<>(); for (int i=0; i<n; i++) { int number= sc.nextInt(); if (number == 1) ones.add(i); else zeros.add(i); } System.out.println(findMinTime(zeros, ones)); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class codeforcesD{ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static long dp[][]; public static List<Integer> list1,list0; public static void main(String args[]){ FastReader sc=new FastReader(); int n=sc.nextInt(); list1=new ArrayList<>(); list0=new ArrayList<>(); for(int i=0;i<n;i++){ int a=sc.nextInt(); if(a==0){list0.add(i);} if(a==1){list1.add(i);} } dp=new long[list1.size()][list0.size()]; for(int i=0;i<list1.size();i++){ for(int j=0;j<list0.size();j++){ dp[i][j]=-1; } } System.out.println(check(0,0)); } public static long check(int i,int j){ if(i>=list1.size()){return 0;} if(j>=list0.size()){return Integer.MAX_VALUE;} if(dp[i][j]!=-1){return dp[i][j];} dp[i][j]=Math.min(check(i+1,j+1)+(long)Math.abs(list1.get(i)-list0.get(j)),check(i,j+1)); return dp[i][j]; } }
0
Non-plagiarised
1162c08f
eea69e7f
import java.util.*; public class CodeForces1525C{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); ArrayList<Integer> o=new ArrayList<Integer>(), e=new ArrayList<Integer>(); int n = sc.nextInt(),dp[][]=new int[n+1][n+1]; for(int i=1;i<=n;i++){ int x=sc.nextInt(); if(x==1)o.add(i); else e.add(i); } for(int i=1;i<=o.size();i++){ dp[i][i]=dp[i-1][i-1]+Math.abs(o.get(i-1)-e.get(i-1)); for(int j=i+1;j<=e.size();j++) dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(o.get(i-1)-e.get(j-1))); } System.out.println(dp[o.size()][e.size()]); } }
import java.util.*; public class Solution { public static int minMoves(int[] input) { List<Integer> people = new ArrayList<Integer>(); List<Integer> chairs = new ArrayList<Integer>(); for (int i = 0; i < input.length; i++) { if (input[i] == 1) { people.add(i); } else { chairs.add(i); } } int[] memo = new int[chairs.size() + 1]; for (int p = 1; ((!people.isEmpty()) && (p <= people.size())); p++) { int prev = memo[p]; memo[p] = memo[p - 1] + Math.abs(people.get(p - 1) - chairs.get(p - 1)); for (int c = p + 1; c <= chairs.size(); c++) { int tmp = memo[c]; memo[c] = Math.min(memo[c - 1], prev + Math.abs(people.get(p - 1) - chairs.get(c - 1))); prev = tmp; } } return memo[memo.length - 1]; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] input = new int[n]; for (int i = 0; i < n; i++) { input[i] = sc.nextInt(); } System.out.println(Solution.minMoves(input)); } }
0
Non-plagiarised
c9da41af
f59d9b6e
import java.util.*; import java.lang.*; import java.io.*; public class Main { static { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) {} } void solve() { int n = in.nextInt(); ArrayList<Edge>[] graph = new ArrayList[n + 1]; for (int i = 0; i < n; i++) { graph[i] = new ArrayList<Edge>(); } for (int i = 0; i < n - 1; i++) { int u = in.nextInt(); int v = in.nextInt(); v--; u--; graph[u].add(new Edge(v, i)); graph[v].add(new Edge(u, i)); } int[] res = new int[n - 1]; for (int i = 0; i < n; i++) { if (graph[i].size() > 2) { out.append("-1\n"); return; } } int start = -1; for (int i = 0; i < n; i++) { if (graph[i].size() == 1) { start = i; break; } } int currNode = start; int prevNode = -1; int weight = 2; while (true) { ArrayList<Edge> edges = graph[currNode]; Edge next = edges.get(0); if (next.node == prevNode) { if (edges.size() == 1) { break; } next = edges.get(1); } res[next.index] = weight; weight = 5 - weight; prevNode = currNode; currNode = next.node; } for (int i = 0; i < n - 1; i++) { out.append(res[i] + " "); } out.append("\n"); } public static void main (String[] args) { // Its Not Over Untill I Win - Syed Mizbahuddin Main sol = new Main(); int t = 1; t = in.nextInt(); while (t-- != 0) { sol.solve(); } System.out.print(out); } <T> void println(T[] s) { System.out.println(Arrays.toString(s)); } <T> void println(T s) { System.out.println(s); } void print(int s) { System.out.print(s); } void println(int s) { System.out.println(s); } void println(int[] a) { println(Arrays.toString(a)); } int[] array(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } return a; } int[] array1(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) { a[i] = in.nextInt(); } return a; } static FastReader in; static StringBuffer out; final int MAX; final int MIN; int mod ; Main() { in = new FastReader(); out = new StringBuffer(); MAX = Integer.MAX_VALUE; MIN = Integer.MIN_VALUE; mod = (int)1e9 + 7; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } class Edge { int node, index; Edge(int node, int index) { this.node = node; this.index = index; } }
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef{ public static class Edge{ int node; int index; Edge(int node, int index){ this.node = node; this.index = index; } } static Scanner scn = new Scanner(System.in); public static void main (String[] args) throws java.lang.Exception{ int t = scn.nextInt(); while(t-->0){ solve(); } } public static void solve(){ int n = scn.nextInt(); ArrayList<Edge>[]graph = new ArrayList[n]; for(int i = 0; i < n; i++){ graph[i] = new ArrayList<>(); } for(int i = 0; i < n - 1; i++){ int u = scn.nextInt() - 1; int v = scn.nextInt() - 1; graph[u].add(new Edge(v, i)); graph[v].add(new Edge(u, i)); } int start = 0; for(int i = 0; i < n; i++){ if(graph[i].size() > 2){ System.out.println("-1"); return; }else if(graph[i].size() == 1){ start = i; } } int[]weight = new int[n - 1]; int prevNode = -1, curNode = start, curWeight = 2; while(true){ ArrayList<Edge>edges = graph[curNode]; Edge next = edges.get(0); if(next.node == prevNode){ if(edges.size() == 1){ break; }else{ next = edges.get(1); } } weight[next.index] = curWeight; prevNode = curNode; curNode = next.node; curWeight = 5 - curWeight; } for(int i = 0; i < n - 1; i++){ System.out.print(weight[i]); System.out.print(" "); } System.out.println(); } }
1
Plagiarised
d6fb3b9e
f7a0ea6d
import java.util.*; public class Sol { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int a[][]=new int[n][5]; int tot[]=new int[n]; for(int i=0;i<n;i++) { String x = sc.next(); for(int j=0;j<x.length();j++) a[i][x.charAt(j)-'a'] += 1; tot[i]=x.length(); } int max=Integer.MIN_VALUE; for(int i=0;i<5;i++) max=Math.max(max,function(a,n,i,tot)); System.out.println(max); } } static int function(int a[][],int n,int i,int tot[]) { Integer ans[] = new Integer[n]; for(int j=0;j<n;j++) ans[j]=a[j][i]-(tot[j]-a[j][i]); int res=0,j=0; Arrays.sort(ans,Collections.reverseOrder()); while(j<n&&res+ans[j]>0) res+=ans[j++]; return j; } }
import java.util.*; public class Solution { private static Scanner in = new Scanner(System.in); public static void main(String args[]) { int t = in.nextInt(); while(t-->0) { solution(); } } private static void solution() { int ans=0; int n = in.nextInt(); String s[] = new String[n]; int occurance[][] = new int[n][5]; for(int i=0;i<n;i++) { s[i] = in.next(); for(int j=0;j<s[i].length();j++) { occurance[i][s[i].charAt(j)-'a']++; } } // for(int i=0;i<n;i++) // { // for(int j=0;j<5;j++) // System.out.println(occurance[i][j]); // System.out.println(); // } for(int i=0;i<5;i++) { int arr[] = new int[n]; for(int j=0;j<n;j++) { arr[j] = s[j].length() - (2 * occurance[j][i]); } Arrays.sort(arr); // for(int j=0;j<n;j++) // System.out.println(arr[j]); int temp=0; int count=0; for(int j=0;j<n;j++) { if(temp+arr[j] < 0) { count++; temp += arr[j]; } else break; } ans = Math.max(ans, count); } System.out.println(ans); } }
0
Non-plagiarised
548ffb07
9ab3c0e1
import java.io.*; import java.util.*; public class D_Java { public static final int MOD = 998244353; public static int mul(int a, int b) { return (int)((long)a * (long)b % MOD); } int[] f; int[] rf; public int C(int n, int k) { return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n-k], rf[k])); } public static int pow(int a, int n) { int res = 1; while (n != 0) { if ((n & 1) == 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } static void shuffleArray(int[] a) { Random rnd = new Random(); for (int i = a.length-1; i > 0; i--) { int index = rnd.nextInt(i + 1); int tmp = a[index]; a[index] = a[i]; a[i] = tmp; } } public static int inv(int a) { return pow(a, MOD-2); } public void doIt() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tok.nextToken()); int k = Integer.parseInt(tok.nextToken()); f = new int[n+42]; rf = new int[n+42]; f[0] = rf[0] = 1; for (int i = 1; i < f.length; ++i) { f[i] = mul(f[i-1], i); rf[i] = mul(rf[i-1], inv(i)); } int[] events = new int[2*n]; for (int i = 0; i < n; ++i) { tok = new StringTokenizer(in.readLine()); int le = Integer.parseInt(tok.nextToken()); int ri = Integer.parseInt(tok.nextToken()); events[i] = le*2; events[i + n] = ri*2 + 1; } shuffleArray(events); Arrays.sort(events); int ans = 0; int balance = 0; for (int r = 0; r < 2*n;) { int l = r; while (r < 2*n && events[l] == events[r]) { ++r; } int added = r - l; if (events[l] % 2 == 0) { // Open event ans += C(balance + added, k); if (ans >= MOD) ans -= MOD; ans += MOD - C(balance, k); if (ans >= MOD) ans -= MOD; balance += added; } else { // Close event balance -= added; } } in.close(); System.out.println(ans); } public static void main(String[] args) throws IOException { (new D_Java()).doIt(); } }
//package CF672; /** * @version 1.0 * @Package_Name: CF672 * @Author: Redorain * @Date: 2020/9/25 8:47 */ import java.util.*; public class d { public static Scanner sc = new Scanner(System.in); public static final int MOD = 998244353; int []f; int [] lf; public static int mul(int a, int b) { return (int)((long)a * (long)b % MOD); } public static int ksm(int a, int n) { int ans = 1; while(n > 0) { if((n & 1) == 1) ans = mul(a, ans); a = mul(a, a); n >>= 1; } return ans; } public int C(int n, int k) { return (k < 0 || k > n) ? 0 : mul(f[n], mul(lf[n - k], lf[k])); } public static int inv(int a) { return ksm(a, MOD - 2); } public void solve() { int n = sc.nextInt(); int k = sc.nextInt(); f = new int[n + 42]; lf = new int[n + 42]; f[0] = lf[0] = 1; for(int i = 1; i < f.length; i++) { f[i] = mul(f[i - 1], i); lf[i] = mul(lf[i - 1], inv(i)); } int[] events = new int[2 * n]; for(int i = 0; i < n; i++) { int le = sc.nextInt(); int ri = sc.nextInt(); events[i] = le * 2; events[i + n] = ri * 2 + 1; } Arrays.sort(events); int ans = 0, balance = 0; for(int r = 0; r < 2 * n;) { int l = r; while(r < 2 * n && events[l] == events[r]) ++r; int added = r - l; if(events[l] % 2 == 0) { ans += C(balance + added, k); if(ans >= MOD) ans -= MOD; ans += MOD - C(balance, k); if(ans >= MOD) ans -= MOD; balance += added; } else balance -= added; } sc.close(); System.out.println(ans); } public static void main(String[] args) { (new d()).solve(); } }
1
Plagiarised
4a570de6
6c4ac8d3
import java.io.*; import java.lang.*; import java.util.*; public class c { static class FastScanner { InputStreamReader is; BufferedReader br; StringTokenizer st; public FastScanner() { is = new InputStreamReader(System.in); br = new BufferedReader(is); } String next() throws Exception { while (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int ni() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } int[] readArray(int num) throws Exception { int arr[]=new int[num]; for(int i=0;i<num;i++) arr[i]=ni(); return arr; } String nextLine() throws Exception { return br.readLine(); } } public static boolean power_of_two(int a) { if((a&(a-1))==0) { return true; } return false; } static boolean PS(double x) { if (x >= 0) { double i= Math.sqrt(x); if(i%1!=0) { return false; } return ((i * i) == x); } return false; } public static int[] ia(int n) { int ar[]=new int[n]; return ar; } public static long[] la(int n) { long ar[]=new long[n]; return ar; } static class pair implements Comparable<pair>{ int ht; int id; pair(int ht, int id) { this.ht=ht; this.id=id; } public int compareTo(pair p) { return this.ht-p.ht; } } public static void main(String args[]) throws java.lang.Exception { FastScanner sc=new FastScanner(); int t=sc.ni(); while(t-->0) { int n=sc.ni(); int m=sc.ni(); int x=sc.ni(); int ar[]=ia(n); for(int i=0;i<n;i++) { ar[i]=sc.ni(); } System.out.println("YES"); PriorityQueue<pair> pq=new PriorityQueue<>(); for(int i=0;i<m;i++) { pq.add(new pair(0,i+1)); } int i=0; while(i<n) { pair pp=pq.remove(); pp.ht+=ar[i]; System.out.print(pp.id+" "); pq.add(pp); i++; } System.out.println(); } } }
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.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.io.*; public class ieee1{ public static void main(String[] args) { Scanner scn=new Scanner(System.in); int t=scn.nextInt(); while(t-->0){ HashMap<Integer,Integer> map=new HashMap<>(); int b=scn.nextInt(); int m=scn.nextInt(); int x=scn.nextInt(); int[] arr=new int[b]; PriorityQueue<Node> pq=new PriorityQueue<>(new pqc()); for(int i=0;i<b;i++){ int ele=scn.nextInt(); arr[i]=ele; } for(int i=1;i<=m;i++){ pq.add(new Node(i,0)); } System.out.println("YES"); for(int i=0;i<arr.length;i++){ int ele=arr[i]; Node n=pq.poll(); System.out.print(n.ind+" "); n.data+=ele; pq.add(n); } System.out.println(); } } public static class Node{ int ind; int data; Node(int ind,int data){ this.ind=ind; this.data=data; } } public static class pqc implements Comparator<Node>{ public int compare(Node n1,Node n2){ if(n1.data<n2.data){ return -1; } else if(n1.data>n2.data){ return 1; } else{ return 0; } } } }
0
Non-plagiarised
722e318f
ff1fc018
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.LongStream; public class TaskB { static long mod = 1000000007; static FastScanner scanner; static final StringBuilder result = new StringBuilder(); public static void main(String[] args) { // 2 : 1000000000 scanner = new FastScanner(); int T = scanner.nextInt(); for (int t = 0; t < T; t++) { solve(t + 1); result.append("\n"); } System.out.println(result); } static void solve(int t) { int n = scanner.nextInt(); int[] a = scanner.nextIntArray(n); String s = scanner.nextToken(); List<Integer> blue = new ArrayList<>(); List<Integer> red = new ArrayList<>(); for (int i = 0; i < n; i++) { if (s.charAt(i) == 'B') { blue.add(a[i]); } else { red.add(a[i]); } } Collections.sort(blue); Collections.sort(red); for (int i = 0; i < blue.size(); i++) { if (blue.get(i) < i + 1) { result.append("NO"); return; } } for (int i = 0; i < red.size(); i++) { if (red.get(i) > i + 1 + blue.size()) { result.append("NO"); return; } } result.append("YES"); } static class WithIdx implements Comparable<WithIdx> { int val, idx; public WithIdx(int val, int idx) { this.val = val; this.idx = idx; } @Override public int compareTo(WithIdx o) { if (val == o.val) { return Integer.compare(idx, o.idx); } return Integer.compare(val, o.val); } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } int[] nextIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] nextLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } String[] nextStringArray(int n) { String[] res = new String[n]; for (int i = 0; i < n; i++) res[i] = nextToken(); return res; } } static class PrefixSums { long[] sums; public PrefixSums(long[] sums) { this.sums = sums; } public long sum(int fromInclusive, int toExclusive) { if (fromInclusive > toExclusive) throw new IllegalArgumentException("Wrong value"); return sums[toExclusive] - sums[fromInclusive]; } public static PrefixSums of(int[] ar) { long[] sums = new long[ar.length + 1]; for (int i = 1; i <= ar.length; i++) { sums[i] = sums[i - 1] + ar[i - 1]; } return new PrefixSums(sums); } public static PrefixSums of(long[] ar) { long[] sums = new long[ar.length + 1]; for (int i = 1; i <= ar.length; i++) { sums[i] = sums[i - 1] + ar[i - 1]; } return new PrefixSums(sums); } } static class ADUtils { static void sort(int[] ar) { Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap int a = ar[index]; ar[index] = ar[i]; ar[i] = a; } Arrays.sort(ar); } static void reverse(int[] arr) { int last = arr.length / 2; for (int i = 0; i < last; i++) { int tmp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = tmp; } } static void sort(long[] ar) { Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap long a = ar[index]; ar[index] = ar[i]; ar[i] = a; } Arrays.sort(ar); } } static class MathUtils { static long[] FIRST_PRIMES = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051}; static long[] primes(int to) { long[] all = new long[to + 1]; long[] primes = new long[to + 1]; all[1] = 1; int primesLength = 0; for (int i = 2; i <= to; i++) { if (all[i] == 0) { primes[primesLength++] = i; all[i] = i; } for (int j = 0; j < primesLength && i * primes[j] <= to && all[i] >= primes[j]; j++) { all[(int) (i * primes[j])] = primes[j]; } } return Arrays.copyOf(primes, primesLength); } static long modpow(long b, long e, long m) { long result = 1; while (e > 0) { if ((e & 1) == 1) { /* multiply in this bit's contribution while using modulus to keep * result small */ result = (result * b) % m; } b = (b * b) % m; e >>= 1; } return result; } static long submod(long x, long y, long m) { return (x - y + m) % m; } static long modInverse(long a, long m) { long g = gcdF(a, m); if (g != 1) { throw new IllegalArgumentException("Inverse doesn't exist"); } else { // If a and m are relatively prime, then modulo // inverse is a^(m-2) mode m return modpow(a, m - 2, m); } } static public long gcdF(long a, long b) { while (b != 0) { long na = b; long nb = a % b; a = na; b = nb; } return a; } } } /* 5 3 2 3 8 8 2 8 5 10 1 */
import java.util.*; public class mentor1 { public static boolean solve(int n, String color, int[] arr){ List<Integer> Barr = new ArrayList<Integer>(); List<Integer> Rarr = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { if(color.charAt(i) == 'B')Barr.add(arr[i]); else Rarr.add(arr[i]); } Barr.sort(Comparator.naturalOrder()); Rarr.sort(Comparator.reverseOrder()); for (int i = 0; i < Barr.size(); i++) { if(Barr.get(i)< i + 1)return false; } for (int i = 0; i < Rarr.size(); i++) { int expect = n-i; if(Rarr.get(i) > expect)return false; } return true; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); for (int i = 0; i < n; i++) { int m = input.nextInt(); int[] arr = new int[m]; for(int j = 0;j<m; j++)arr[j] = input.nextInt(); String color = input.next(); if(solve(m,color,arr)) System.out.println("YES"); else System.out.println("NO"); } } }
0
Non-plagiarised
03b3c5af
d74028ea
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; public class TaskA { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); int testCount = 1; testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class Solver { public void fastSort(int[] a){ Random random = new Random(); for(int i=0;i<a.length;i++){ int nextInd = random.nextInt(a.length); int temp = a[nextInd]; a[nextInd] = a[i]; a[i] = temp; } Arrays.sort(a); return; } public Pair[] pairs; ArrayList<ArrayList<Integer>> adj; long[][] dp; public void dfs(int vertex,int parent){ // dp[0][vertex] = dp[1][vertex] = 0; for(int i=0;i<adj.get(vertex).size();i++){ if((int)adj.get(vertex).get(i)!=parent){ int curVer = adj.get(vertex).get(i); dfs(curVer,vertex); dp[0][vertex] += Math.max( (dp[0][curVer] + Math.abs(pairs[curVer].f - pairs[vertex].f) ) , (dp[1][curVer] + Math.abs(pairs[curVer].s - pairs[vertex].f)) ); dp[1][vertex] += Math.max( (dp[0][curVer] + Math.abs(pairs[curVer].f - pairs[vertex].s) ) , (dp[1][curVer] + Math.abs(pairs[curVer].s - pairs[vertex].s)) ); } } } public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); pairs = new Pair[n]; adj = new ArrayList<>(); for(int i=0;i<n;i++){ pairs[i] = new Pair(in.nextInt(),in.nextInt()); adj.add(new ArrayList<>()); } for(int i=0;i<n-1;i++){ int u = in.nextInt(),v = in.nextInt(); adj.get(u-1).add(v-1); adj.get(v-1).add(u-1); } dp = new long[2][n]; dfs(0,-1); // out.println(Arrays.toString(dp[0])); // out.println(Arrays.toString(dp[1])); out.println(Math.max(dp[0][0],dp[1][0])); } } static class Pair implements Comparable<Pair>{ int f; int s; public Pair(int a,int b){ this.f = a; this.s = b; } public int compareTo(Pair other){ if(other.f==this.f){ return (int)(this.s-other.s); } else return this.f-other.f; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(InputStream stream){ br = new BufferedReader(new InputStreamReader(stream)); } public String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } public int nextInt(){ return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } public String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e){ e.printStackTrace(); } return str; } public int[] nextArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
import java.io.*; import java.util.*; public class Main { static class Task { int NN = 200005; int MOD = 1000000007; int INF = 2000000000; long INFINITY = 2000000000000000000L; int [] a; int [] b; List<Integer> [] g; long [][] dp; long rec(int node, int prev, int index) { if(dp[node][index] != -1){ return dp[node][index]; } long ret = 0; int val = index==0?a[node]:b[node]; for(int adj: g[node]) { if(adj == prev) { continue; } ret += Math.max((long)(rec(adj, node, 0) + (long)(Math.abs(val - a[adj]))), (long)(rec(adj, node, 1) + (long)(Math.abs(val - b[adj])))); } return dp[node][index] = ret; } public void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while(t-->0) { int n = in.nextInt(); a = new int[n + 1]; b = new int[n + 1]; g = new ArrayList[n + 1]; dp = new long[n + 1][2]; for(int i=1;i<=n;++i) { a[i] = in.nextInt(); b[i] = in.nextInt(); g[i] = new ArrayList<>(); dp[i][0] = dp[i][1] = -1; } for(int i=1;i<n;++i) { int u = in.nextInt(); int v = in.nextInt(); g[u].add(v); g[v].add(u); } long ans = Math.max(rec(1, -1, 0), rec(1, -1, 1)); out.println(ans); } } } static void prepareIO(boolean isFileIO) { // long t1 = System.currentTimeMillis(); Task solver = new Task(); // Standard IO if (!isFileIO) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); solver.solve(in, out); // out.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0); out.close(); } // File IO else { String IPfilePath = System.getProperty("user.home") + "/Downloads/ip.in"; String OPfilePath = System.getProperty("user.home") + "/Downloads/op.out"; InputReader fin = new InputReader(IPfilePath); PrintWriter fout = null; try { fout = new PrintWriter(new File(OPfilePath)); } catch (FileNotFoundException e) { e.printStackTrace(); } solver.solve(fin, fout); // fout.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0); fout.close(); } } public static void main(String[] args) { prepareIO(false); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public InputReader(String filePath) { File file = new File(filePath); try { reader = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } tokenizer = null; } public String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return str; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
0
Non-plagiarised
4fb09c5f
c57a973e
import java.io.*; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.logging.Logger; import java.util.stream.Collectors; public class Trial { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int q = sc.nextInt(); while (q-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); int[] arr = new int[k]; int[] t = new int[k]; HashMap<Integer, Integer> hm = new HashMap<>(); for (int i = 0; i < k; i++) { arr[i] = sc.nextInt() - 1; } for (int i = 0; i < k; i++) { t[i] = sc.nextInt(); hm.put(arr[i], t[i]); } int[] left = new int[n]; int[] right = new int[n]; left[0] = hm.getOrDefault(0, -1); right[n - 1] = hm.getOrDefault(n - 1, -1); for (int i = 1; i < n; i++) { if (hm.containsKey(i)) { if (left[i - 1] < 0) { left[i] = hm.get(i); } else { left[i] = Math.min(hm.get(i), left[i - 1] + 1); } } else { left[i] = left[i - 1] < 0 ? -1 : left[i - 1] + 1; } } for (int i = n - 2; i >= 0; i--) { if (hm.containsKey(i)) { if (right[i + 1] < 0) { right[i] = hm.get(i); } else { right[i] = Math.min(hm.get(i), right[i + 1] + 1); } } else { right[i] = right[i + 1] < 0 ? -1 : right[i + 1] + 1; } } for (int i = 0; i < n; i++) { if (left[i] < 0) { pw.print(right[i] + " "); } else if (right[i] < 0) { pw.print(left[i] + " "); } else { pw.print(Math.min(left[i], right[i]) + " "); } } pw.println(); } pw.flush(); pw.close(); } // inclusive private static void rotate(int[] arr, int l, int r) { int temp = arr[r]; for (int i = r - 1; i >= l; i--) { arr[i + 1] = arr[i]; } arr[l] = temp; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream System) throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(System)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() { for (long i = 0; i < 3e9; i++) ; } } static class Pair { int a; int b; boolean asc; Pair(int a, int b, boolean asc) { this.a = a; this.b = b; this.asc = asc; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return a == pair.a && b == pair.b && asc == pair.asc; } @Override public int hashCode() { return Objects.hash(a, b, asc); } } }
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Air { public static void main(String[] args) { FastScanner sc = new FastScanner(); int T = sc.nextInt(); for(int tt=0; tt<T;tt++){ int n = sc.nextInt(), k=sc.nextInt(); int [] positions=new int[k], temp=new int[k]; for (int i=0;i<k;i++) positions[i]=sc.nextInt(); for (int i=0;i<k;i++) temp[i]=sc.nextInt(); int[] forced=new int[n]; Arrays.fill(forced, Integer.MAX_VALUE/2); for (int i=0;i<k;i++) forced[positions[i]-1]=temp[i]; for (int i=1;i<n;i++) forced[i]=Math.min(forced[i], forced[i-1]+1); for (int i=n-2;i>=0;i--) forced[i]=Math.min(forced[i], forced[i+1]+1); for (int i=0;i<n;i++) System.out.print(forced[i]+" "); System.out.println(); } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } }
0
Non-plagiarised
9e7551da
f0e13442
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; // نورت الكود يا كبير اتفضل // يا رب Accepted public class InterestingStory { private static int[] freq; private static String[] strs; private static int[] countAlpha(char alpha) { int[] count = new int[strs.length]; for (int i = 0; i < strs.length; i++) for (char c : strs[i].toCharArray()) count[i] += c == alpha ? -1 : 1; return count; } private static int solve(char alpha) { int[] res = countAlpha(alpha); Arrays.sort(res); int freqSum = 0; for (int j : freq) freqSum += j; freqSum -= freq[alpha - 'a']; int k = res.length - 1; //System.out.println(freq[alpha - 'a'] + " " + freqSum); while (k >= 0 && freq[alpha - 'a'] <= freqSum) { //System.out.println(freq[alpha - 'a'] + " " + freqSum); freqSum -= res[k--]; } return k + 1; } public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); freq = new int[7]; strs = new String[n]; for (int i = 0; i < n; i++) strs[i] = in.nextLine(); for (String str : strs) for (char c : str.toCharArray()) freq[c - 'a']++; int max = 0; // int x = solve('d'); // out.println(x); for (char c = 'a'; c < 'f'; c++) max = Math.max(max, solve(c)); // int[] arr = countAlpha('d'); // Arrays.sort(arr); // // for (int i : arr) // out.println(i); out.println(max); } out.close(); } private static class FastReader { BufferedReader br; StringTokenizer st; FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; // نورت الكود يا كبير اتفضل // يا رب Accepted public class InterestingStory { private static int[] freq; private static String[] strs; private static int[] countAlpha(char alpha) { int[] count = new int[strs.length]; for (int i = 0; i < strs.length; i++) for (char c : strs[i].toCharArray()) count[i] += c == alpha ? -1 : 1; return count; } private static int solve(char alpha) { int[] res = countAlpha(alpha); Arrays.sort(res); int freqSum = 0; for (int j : freq) freqSum += j; freqSum -= freq[alpha - 'a']; int k = res.length - 1; while (k >= 0 && freq[alpha - 'a'] <= freqSum) freqSum -= res[k--]; return k + 1; } public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); freq = new int[5]; strs = new String[n]; for (int i = 0; i < n; i++) strs[i] = in.nextLine(); for (String str : strs) for (char c : str.toCharArray()) freq[c - 'a']++; int max = 0; for (char c = 'a'; c < 'f'; c++) max = Math.max(max, solve(c)); out.println(max); } out.close(); } private static class FastReader { BufferedReader br; StringTokenizer st; FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1
Plagiarised
317baeaf
e17c5159
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); StringTokenizer st; int t = Integer.parseInt(br.readLine()); while (t --> 0) { int n = Integer.parseInt(br.readLine()); String a = br.readLine(); String b = br.readLine(); int alit = 0; int blit = 0; int ans = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { if (a.charAt(i) == '1') alit++; if (b.charAt(i) == '1') blit++; } if (alit == blit) { int count = 0; for (int i = 0; i < n; i++) if (a.charAt(i) != b.charAt(i)) count++; ans = Math.min(count, ans); } if (alit == n - blit + 1) { int count = 0; for (int i = 0; i < n; i++) if (a.charAt(i) == b.charAt(i)) count++; ans = Math.min(ans, count); } if (ans == Integer.MAX_VALUE) { pw.println("-1"); } else { pw.println(ans); } } pw.close(); } }
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=Integer.valueOf(sc.nextLine()); while (t-->0){ int n=Integer.valueOf(sc.nextLine()); int ans=100001; String a=sc.nextLine(); String b=sc.nextLine(); HashSet<Integer> listb=new HashSet<>(); ArrayList<Integer> lista=new ArrayList<>(); for (int i=0;i<n;i++){ if(a.charAt(i)=='1') lista.add(i); if(b.charAt(i)=='1') listb.add(i); } int num=0; for (int i=0;i<lista.size();i++){ if(listb.contains(lista.get(i))) num++; } //第一种情况 if(lista.size()==listb.size()){ ans=Math.min(ans,(listb.size()-num)*2); } //第二种情况 n-lista.size() listb.size()-num if(listb.size()-(n-lista.size())==1){ ans=Math.min(ans,(num-1)*2+1); } System.out.println((ans==100001)?-1:ans); } } }
0
Non-plagiarised
6f393cfe
b185d034
import java.io.*; import java.util.*; public class C { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); try{ int t = Integer.parseInt(br.readLine()); while(t-->0){ int n = Integer.parseInt(br.readLine()); int lst[][] = new int[n][5]; for(int i=0; i<n; i++){ String s = br.readLine(); for(int j=0; j<s.length(); j++){ lst[i][s.charAt(j)-'a']++; } } int fans = Integer.MIN_VALUE; for(int i=0; i<5; i++){ int val[] = new int[n]; for(int k=0; k<n; k++){ int sum = 0; for(int j=0; j<5; j++){ if(i==j){ sum += lst[k][j]; }else{ sum -= lst[k][j]; } } val[k] = sum; } Arrays.sort(val); int sum = 0; int ans = 0; for(int x = n-1; x>=0; x--){ sum+=val[x]; if(sum>0){ ans++; }else{ break; } } fans = Math.max(fans, ans); } bw.write(fans+"\n"); } bw.flush(); }catch(Exception e){ return; } } }
import java.io.*; import java.util.*; public class A734C { public static void main(String[] args) { JS scan = new JS(); int t = scan.nextInt(); loop:while(t-->0){ int n = scan.nextInt(); String[] arr= new String[n]; Integer[][] counts = new Integer[5][n]; for(int i = 0;i<5;i++){ for(int j = 0;j<n;j++){ counts[i][j] = 0; } } for(int i =0;i<n;i++){ arr[i] = scan.next(); int[] freq =new int[5]; for(int j = 0;j<arr[i].length();j++){ freq[arr[i].charAt(j)-'a']++; } for(int j = 0;j<5;j++){ counts[j][i] = freq[j]-(arr[i].length()-freq[j]); } } int best = 0; for(int i = 0;i<5;i++){ Arrays.sort(counts[i]); int curr = 0; int extra = 0; for(int j = n-1;j>=0;j--){ extra+=counts[i][j]; if(extra>0)curr++; } best = Math.max(best,curr); } System.out.println(best); } } static class JS { public int BS = 1 << 16; public char NC = (char) 0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public long nextLong() { num = 1; boolean neg = false; if (c == NC) c = nextChar(); for (; (c < '0' || c > '9'); c = nextChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = nextChar()) { res = (res << 3) + (res << 1) + c - '0'; num *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / num; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c > 32) { res.append(c); c = nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c != '\n') { res.append(c); c = nextChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = nextChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
0
Non-plagiarised
9291ca83
d6fb3b9e
import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; public class Practice { static HashMap<String, Integer> map = new HashMap<>(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-->0) { int n = sc.nextInt(); int[][] occurances = new int[5][n]; for(int i=0;i<n;i++){ String s = sc.next(); int[] count = new int[5]; int len = s.length(); for(int j=0;j<s.length();j++){ count[s.charAt(j)-'a']++; } for(int j=0;j<5;j++){ occurances[j][i] = count[j] - (len-count[j]); } } int ans = 0; for(int i=0;i<5;i++){ Arrays.sort(occurances[i]); int tmpAns = 0; int tmpSum=0; for(int j=n-1;j>=0;j--){ tmpSum+=occurances[i][j]; if(tmpSum>0) tmpAns++; else break; } ans = Math.max(ans, tmpAns); } System.out.println(ans); } } }
import java.util.*; public class Sol { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int a[][]=new int[n][5]; int tot[]=new int[n]; for(int i=0;i<n;i++) { String x = sc.next(); for(int j=0;j<x.length();j++) a[i][x.charAt(j)-'a'] += 1; tot[i]=x.length(); } int max=Integer.MIN_VALUE; for(int i=0;i<5;i++) max=Math.max(max,function(a,n,i,tot)); System.out.println(max); } } static int function(int a[][],int n,int i,int tot[]) { Integer ans[] = new Integer[n]; for(int j=0;j<n;j++) ans[j]=a[j][i]-(tot[j]-a[j][i]); int res=0,j=0; Arrays.sort(ans,Collections.reverseOrder()); while(j<n&&res+ans[j]>0) res+=ans[j++]; return j; } }
0
Non-plagiarised
b9595381
c4ca2ff3
import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main{ static int dest1; static int dest2; public static void main(String args[]){ FastScanner in = new FastScanner(); int test=in.nextInt(); while(test-->0){ int n=in.nextInt(); int count[][]=new int[n][5]; int total[]=new int[n]; String words[]=new String[n]; for(int i=0;i<n;i++){ words[i]=in.next(); for(int j=0;j<words[i].length();j++) count[i][words[i].charAt(j)-'a']++; total[i]=words[i].length(); } int max=Integer.MIN_VALUE; for(int i=0;i<5;i++){ Integer ans[]=new Integer[n]; for(int j=0;j<n;j++){ ans[j]=count[j][i]-(total[j]-count[j][i]); } Arrays.sort(ans,Collections.reverseOrder()); int j=0; int r=0; while(j<n && r+ans[j]>0){ r+=ans[j]; j++; } max=Math.max(j,max); } System.out.println(max); } } public static int solve(int start[], int end[], int n) { int distance[][]=new int[n][n]; int r=n; int c=n; boolean visited[][]=new boolean[n][n]; int startx=start[0]; int starty=start[1]; int endx=end[0]; int endy=end[1]; Pair tmp=new Pair(startx,starty); Queue<Pair> q=new LinkedList<>(); q.add(tmp); visited[startx][starty]=true; distance[startx][starty]=0; int dx[]={-2,-1,1,2,2,1,-1,-2}; int dy[]={1,2,2,1,-1,-2,-2,-1}; while(!q.isEmpty()) { Pair cell=q.poll(); int x=cell.x; int y=cell.y; int d=distance[x][y]; for(int i=0;i<8;i++) { int childx=x+dx[i]; int childy=y+dy[i]; if(valid(childx,childy,r,c,visited)) { visited[childx][childy]=true; distance[childx][childy]=d+1; q.add(new Pair(childx,childy)); } } } return distance[endx][endy]; } public static boolean valid(int x,int y,int r,int c,boolean visited[][]) { if(x<0||y<0||x>=r||y>=c) return false; if(visited[x][y]) return false; return true; } public static int knight(int i,int j){ if(i==dest1 && j==dest2) return 0; if(i<1 || j<1) return 0; if(i>8 || j>8) return 0; if(i<1 || j>8) return 0; if(i>8 || j<1) return 0; int min=0; if(i-1>=1 && j-2>=1) min+=1+knight(i-1,j-2); if(i-1>=1 && j+2<=8) min+=1+knight(i-1,j+2); if(i-2>=1 && j-1>=1) min+=1+knight(i-2,j-1); if(i-2>=1 && j+1<=8) min+=1+knight(i-2,j+1); if(i+1<=8 && j-2>=1) min+=1+knight(i+1,j-2); if(i+1<=8 && j+2<=8) min+=1+knight(i+1,j+2); if(i+2<=8 && j-1>=1) min+=1+knight(i+2,j-1); if(i+2<=8 && j+1<=8) min+=1+knight(i+2,j+1); return min; } } class FastScanner { java.io.BufferedReader br = new java.io.BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } class Pair { int x; int y; Pair(int i,int j) { x=i; y=j; } }
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc=new Scanner(System.in); int t=sc.nextInt(); PrintWriter out=new PrintWriter(System.out); while(t-->0) { int n=sc.nextInt(); int freq[][]=new int[n][5]; int rem[][]=new int[n][5]; for(int i=0;i<n;i++) { String str=sc.next(); for(int j=0;j<str.length();j++) { freq[i][str.charAt(j)-'a']++; } for(int k=0;k<5;k++) { rem[i][k]=str.length()-freq[i][k]; } } int ans=0; for(int i=0;i<5;i++) { int arr[]=new int[n]; for(int j=0;j<n;j++) arr[j]=freq[j][i]-rem[j][i]; Arrays.sort(arr); int total=0; int sum=0; for(int k=n-1;k>=0;k--) { if(sum+arr[k]>0) { sum=sum+arr[k]; total++; } else { break; } } ans=Math.max(ans,total); } out.println(ans); } out.flush(); out.close(); } }
0
Non-plagiarised
54e71b9b
fd16f5b0
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.InputMismatchException; public class C { private static int n, m, k, x, y; private static int[][] a, b; private static long ans; private static int[][] id; private static long[][] dp; private static int idf; private static String s, t; private static HashMap<Integer, ArrayList<Integer>> g; // CONSTANTS private static final int MOD = (int) 1e9 + 7; private static final int[] dx = { -1, 1, 0, 0 }; private static final int[] dy = { 0, 0, -1, 1 }; private static final int MAX = Integer.MAX_VALUE; private static final int MIN = Integer.MIN_VALUE; private static final long MAXLONG = (long) 1e18; private static final long MINLONG = -(long) 1e18; public static void main(String[] args) { int testCases = in.nextInt(); id = new int[(int)1e5+10][2]; dp = new long[(int)1e5+10][2]; for (int z = 1; z <= testCases; z++) { initCase(z); n = ini(); a = ina2d(n, 2); g = intree(n); println(Math.max(dfs(0, -1, 0), dfs(0, -1, 1))); } //Ax, A*(B-x) out.flush(); out.close(); } private static long dfs(int u, int p, int f) { if (id[u][f]==idf) { return dp[u][f]; } long ans = 0; for(int v: g.get(u)) { if (v==p) continue; ans += Math.max(Math.abs(a[v][1]-a[u][f])+dfs(v, u, 1), Math.abs(a[v][0]-a[u][f])+dfs(v, u, 0)); } id[u][f] = idf; dp[u][f] = ans; return ans; } // INIT private static void initCase(int z) { idf = z; ans = 0; } // PRINT ANSWER private static void printAns(Object o) { out.println(o); } private static void printAns(Object o, int testCaseNo) { out.println("Case #" + testCaseNo + ": " + o); } private static void printArray(Object[] a) { for (int i = 0; i < a.length; i++) { out.print(a[i] + " "); } out.println(); } // SORT SHORTCUTS - QUICK SORT TO MERGE SORT private static void sort(int[] a) { int n = a.length; Integer[] b = new Integer[n]; for (int i = 0; i < n; i++) { b[i] = a[i]; } Arrays.sort(b); for (int i = 0; i < n; i++) { a[i] = b[i]; } } private static void sort(long[] a) { int n = a.length; Long[] b = new Long[n]; for (int i = 0; i < n; i++) { b[i] = a[i]; } Arrays.sort(b); for (int i = 0; i < n; i++) { a[i] = b[i]; } } // INPUT SHORTCUTS private static int[] ina(int n) { int[] temp = new int[n]; for (int i = 0; i < n; i++) { temp[i] = in.nextInt(); } return temp; } private static int[][] ina2d(int n, int m) { int[][] temp = new int[n][m]; for (int i = 0; i < n; i++) { temp[i] = ina(m); } return temp; } private static char[][] ina2dChar(int n, int m) { char[][] temp = new char[n][]; for (int i = 0; i < n; i++) { temp[i] = ins().toCharArray(); } return temp; } private static int ini() { return in.nextInt(); } private static long inl() { return in.nextLong(); } private static double ind() { return Double.parseDouble(ins()); } private static String ins() { return in.readString(); } // PRINT SHORTCUTS private static void println(Object... o) { for (Object x : o) { out.write(x + ""); } out.write("\n"); } private static void pd(Object... o) { for (Object x : o) { out.write(x + ""); } out.flush(); out.write("\n"); } private static void print(Object... o) { for (Object x : o) { out.write(x + ""); } } // GRAPH SHORTCUTS private static HashMap<Integer, ArrayList<Integer>> intree(int n) { HashMap<Integer, ArrayList<Integer>> g = new HashMap<>(); for (int i = 0; i < n; i++) { g.put(i, new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { int u = ini() - 1; int v = ini() - 1; g.get(u).add(v); g.get(v).add(u); } return g; } private static HashMap<Integer, ArrayList<Integer>> ingraph(int n, int m) { HashMap<Integer, ArrayList<Integer>> g = new HashMap<>(); for (int i = 0; i < n; i++) { g.put(i, new ArrayList<>()); } for (int i = 0; i < m; i++) { int u = ini() - 1; int v = ini() - 1; g.get(u).add(v); g.get(v).add(u); } return g; } private static HashMap<Integer, ArrayList<Integer>> indirectedgraph(int n, int m) { HashMap<Integer, ArrayList<Integer>> g = new HashMap<>(); for (int i = 0; i < n; i++) { g.put(i, new ArrayList<>()); } for (int i = 0; i < m; i++) { int u = ini() - 1; int v = ini() - 1; g.get(u).add(v); } return g; } private static HashMap<Integer, ArrayList<Edge>> inweightedgraph(int n, int m) { HashMap<Integer, ArrayList<Edge>> g = new HashMap<>(); for (int i = 0; i < n; i++) { g.put(i, new ArrayList<>()); } for (int i = 0; i < m; i++) { int u = ini() - 1; int v = ini() - 1; int w = ini(); Edge edge = new Edge(u, v, w); g.get(u).add(edge); g.get(v).add(edge); } return g; } private static class Edge implements Comparable<Edge> { private int u, v; private long w; public Edge(int a, int b, long c) { u = a; v = b; w = c; } public int other(int x) { return (x == u ? v : u); } public int compareTo(Edge edge) { return Long.compare(w, edge.w); } } private static class Pair { private int u, v; public Pair(int a, int b) { u = a; v = b; } public int hashCode() { return u + v + u * v; } public boolean equals(Object object) { Pair pair = (Pair) object; return u == pair.u && v == pair.v; } } private static class Triplet { private int u, v, w; public Triplet(int a, int b, int c) { u = a; v = b; w = c; } public int hashCode() { return u + v + w + u * v + v * w + u * w + u * v * w; } public boolean equals(Object object) { Triplet triplet = (Triplet) object; return u == triplet.u && v == triplet.v && v == triplet.w; } } private static class Node implements Comparable<Node> { private int u; private long dist; public Node(int a, long b) { u = a; dist = b; } public int compareTo(Node node) { return Long.compare(dist, node.dist); } } // MATHS AND NUMBER THEORY SHORTCUTS private static int gcd(int a, int b) { // O(log(min(a,b))) if (b == 0) return a; return gcd(b, a % b); } private static long modExp(long a, long b) { if (b == 0) return 1; a %= MOD; long exp = modExp(a, b / 2); if (b % 2 == 0) { return (exp * exp) % MOD; } else { return (a * ((exp * exp) % MOD)) % MOD; } } private static long mul(int a, int b) { return a * 1L * b; } // Segment Tree private static class SegmentTree<T extends Comparable<T>> { private int n, m; private T[] a; private T[] seg; private T NULLVALUE; public SegmentTree(int n, T NULLVALUE) { this.NULLVALUE = NULLVALUE; this.n = n; m = 4 * n; seg = (T[]) new Object[m]; } public SegmentTree(T[] a, int n, T NULLVALUE) { this.NULLVALUE = NULLVALUE; this.a = a; this.n = n; m = 4 * n; seg = (T[]) new Object[m]; construct(0, n - 1, 0); } private void update(int pos) { // Range Sum // seg[pos] = seg[2*pos+1]+seg[2*pos+2]; // Range Min if (seg[2 * pos + 1].compareTo(seg[2 * pos + 2]) <= 0) { seg[pos] = seg[2 * pos + 1]; } else { seg[pos] = seg[2 * pos + 2]; } } private T optimum(T leftValue, T rightValue) { // Range Sum // return leftValue+rightValue; // Range Min if (leftValue.compareTo(rightValue) <= 0) { return leftValue; } else { return rightValue; } } public void construct(int low, int high, int pos) { if (low == high) { seg[pos] = a[low]; return; } int mid = (low + high) / 2; construct(low, mid, 2 * pos + 1); construct(mid + 1, high, 2 * pos + 2); update(pos); } public void add(int index, T value) { add(index, value, 0, n - 1, 0); } private void add(int index, T value, int low, int high, int pos) { if (low == high) { seg[pos] = value; return; } int mid = (low + high) / 2; if (index <= mid) { add(index, value, low, mid, 2 * pos + 1); } else { add(index, value, mid + 1, high, 2 * pos + 2); } update(pos); } public T get(int qlow, int qhigh) { return get(qlow, qhigh, 0, n - 1, 0); } public T get(int qlow, int qhigh, int low, int high, int pos) { if (qlow > low || low > qhigh) { return NULLVALUE; } else if (qlow <= low || qhigh >= high) { return seg[pos]; } else { int mid = (low + high) / 2; T leftValue = get(qlow, qhigh, low, mid, 2 * pos + 1); T rightValue = get(qlow, qhigh, mid + 1, high, 2 * pos + 2); return optimum(leftValue, rightValue); } } } // DSU private static class DSU { private int[] id; private int[] size; private int n; public DSU(int n) { this.n = n; id = new int[n]; for (int i = 0; i < n; i++) { id[i] = i; } size = new int[n]; Arrays.fill(size, 1); } private int root(int u) { while (u != id[u]) { id[u] = id[id[u]]; u = id[u]; } return u; } public boolean connected(int u, int v) { return root(u) == root(v); } public void union(int u, int v) { int p = root(u); int q = root(v); if (size[p] >= size[q]) { id[q] = p; size[p] += size[q]; } else { id[p] = q; size[q] += size[p]; } } } // KMP private static int countSearch(String s, String p) { int n = s.length(); int m = p.length(); int[] b = backTable(p); int j = 0; int count = 0; for (int i = 0; i < n; i++) { if (j == m) { j = b[j - 1]; count++; } while (j != 0 && s.charAt(i) != p.charAt(j)) { j = b[j - 1]; } if (s.charAt(i) == p.charAt(j)) { j++; } } if (j == m) count++; return count; } private static int[] backTable(String p) { int m = p.length(); int j = 0; int[] b = new int[m]; for (int i = 1; i < m; i++) { while (j != 0 && p.charAt(i) != p.charAt(j)) { j = b[j - 1]; } if (p.charAt(i) == p.charAt(j)) { b[i] = ++j; } } return b; } private static int[][] countMap(String s) { int n = s.length(); int[][] map = new int[n][26]; for (int i = 0; i < n; i++) { for (int j = 0; j < 26; j++) { map[i][j] = (i - 1 >= 0 ? map[i - 1][j] : 0); } map[i][s.charAt(i) - 'a']++; } return map; } private static int[][] rightOcc(String s) { int n = s.length(); int[][] right = new int[n][26]; for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < 26; j++) { right[i][j] = (i + 1 < n ? right[i + 1][j] : -1); } right[i][s.charAt(i) - 'a'] = i; } return right; } private static int[][] leftOcc(String s) { int n = s.length(); int[][] left = new int[n][26]; for (int i = 0; i < n; i++) { for (int j = 0; j < 26; j++) { left[i][j] = (i - 1 >= 0 ? left[i - 1][j] : -1); } left[i][s.charAt(i) - 'a'] = i; } return left; } // LCA private static class LCA { private HashMap<Integer, ArrayList<Integer>> g; private int[] level; private int[] a; private int[][] P; private int n, m; private int[] xor; public LCA(HashMap<Integer, ArrayList<Integer>> g, int[] a) { this.g = g; this.a = a; n = g.size(); m = (int) (Math.log(n) / Math.log(2)) + 5; P = new int[n][m]; xor = new int[n]; level = new int[n]; preprocess(); } private void preprocess() { dfs(0, -1); for (int j = 1; j < m; j++) { for (int i = 0; i < n; i++) { if (P[i][j - 1] != -1) { P[i][j] = P[P[i][j - 1]][j - 1]; } } } } private void dfs(int u, int p) { P[u][0] = p; xor[u] = a[u] ^ (p == -1 ? 0 : xor[p]); level[u] = (p == -1 ? 0 : level[p] + 1); for (int v : g.get(u)) { if (v == p) continue; dfs(v, u); } } public int lca(int u, int v) { if (level[v] > level[u]) { int temp = v; v = u; u = temp; } for (int j = m; j >= 0; j--) { if (level[u] - (1 << j) < level[v]) { continue; } else { u = P[u][j]; } } if (u == v) return u; for (int j = m - 1; j >= 0; j--) { if (P[u][j] == -1 || P[u][j] == P[v][j]) { continue; } else { u = P[u][j]; v = P[v][j]; } } return P[u][0]; } private int xor(int u, int v) { int l = lca(u, v); return xor[u] ^ xor[v] ^ a[l]; } } // Geometry private static class Line { private int a, b, c; public Line(int x1, int y1, int x2, int y2) { a = y2 - y1; b = x1 - x2; c = a * x1 + b * y1; int g = gcd(a, gcd(b, c)); a /= g; b /= g; c /= g; } public int hashCode() { return a + b + c + a * b + b * c + c * a + a * b * c; } public boolean equals(Object o) { Line line = (Line) o; return a == line.a && b == line.b && c == line.c; } } // FAST INPUT OUTPUT LIBRARY private static InputReader in = new InputReader(System.in); private static PrintWriter out = new PrintWriter(System.out); private static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
/* * * CREATED BY : NAITIK V * * */ import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc=new FastReader(); static long dp[][]; static int mod=1000000007; static int max; public static void main(String[] args) { PrintWriter out=new PrintWriter(System.out); //StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); outer :while (ttt-- > 0) { int n=i(); int B[][]=new int[n][2]; for(int i=0;i<n;i++) { B[i][0]=i(); B[i][1]=i(); } ArrayList<Integer> A[]=new ArrayList[n+1]; for(int i=0;i<A.length;i++) { A[i]=new ArrayList<Integer>(); } int m=n-1; dp=new long[n+1][2]; for(int i=0;i<=n;i++) Arrays.fill(dp[i],-1); for(int i=0;i<m;i++) { int a=i(); int b=i(); A[a].add(b); A[b].add(a); } System.out.println(Math.max(dfs(A, 1, -1, 0, B), dfs(A, 1, -1, 1, B))); } //System.out.println(sb.toString()); out.close(); //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 //CHECK FOR N=1 //CHECK FOR N=1 } private static long dfs(ArrayList<Integer> [] A, int i,int par,int pv,int B[][]) { long ans=0; long res=B[i-1][pv]; if(dp[i][pv]!=-1) return dp[i][pv]; for(int child : A[i]) { if(child!=par) { long op1=dfs(A, child, i, 0, B)+Math.abs(B[child-1][0]-res); long op2=dfs(A, child, i, 1, B)+Math.abs(B[child-1][1]-res); ans+=Math.max(op1, op2); } } return dp[i][pv]=ans; } static class Pair implements Comparable<Pair> { int x; int y; Pair(int x,int y){ this.x=x; this.y=y; } @Override public int compareTo(Pair o) { if(this.x>o.x) return 1; else if(this.x<o.x) return -1; else { if(this.y>o.y) return 1; else if(this.y<o.y) return -1; else return 0; } } /* FOR TREE MAP PAIR USE */ // public int compareTo(Pair o) { // if (x > o.x) { // return 1; // } // if (x < o.x) { // return -1; // } // if (y > o.y) { // return 1; // } // if (y < o.y) { // return -1; // } // return 0; // } } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void reverse(long A[]) { int n=A.length; long B[]=new long[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void reverse(int A[]) { int n=A.length; int B[]=new int[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static int[][] input(int n,int m){ int A[][]=new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { A[i][j]=i(); } } return A; } static char[][] charinput(int n,int m){ char A[][]=new char[n][m]; for(int i=0;i<n;i++) { String s=s(); for(int j=0;j<m;j++) { A[i][j]=s.charAt(j); } } return A; } static int find(int A[],int a) { if(A[a]==a) return a; return A[a]=find(A, A[a]); } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long [] prefix(long A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] prefix(int A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] suffix(long A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static long [] suffix(int A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long power(long x, long y, long p) { if(y==0) return 1; if(x==0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static HashMap<Integer,Integer> hash(int A[]){ HashMap<Integer,Integer> map=new HashMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Integer,Integer> tree(int A[]){ TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static 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; } } }
0
Non-plagiarised
2470b521
2da4b3fe
// package com.company; import com.sun.security.jgss.GSSUtil; import javax.swing.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; //ankit singh public class Main { static int inf=Integer.MAX_VALUE/2; public static void main(String[] args) { Scanner input=new Scanner(System.in); int nn=input.nextInt(); int a[]=new int[nn]; ArrayList<Integer> one=new ArrayList<>(); ArrayList<Integer> zero=new ArrayList<>(); for (int i = 0; i <nn ; i++) { a[i]=input.nextInt(); if(a[i]==1){ one.add(i); }else zero.add(i); } if (one.size()==0) { System.out.println(0); return; } int n=one.size(); int m=zero.size(); int dp[][]=new int[n+1][m+1]; int ans=0; for (int i = 1; i <=n ; i++) { dp[i][0]=inf; for (int j = 1; j <=m ; j++) { dp[i][j]=inf; //take the current dp[i][j]=Math.min(dp[i][j],dp[i-1][j-1]+Math.abs(one.get(i-1)-zero.get(j-1))); //take previos dp[i][j]=Math.min(dp[i][j],dp[i][j-1]); } } ans=inf; for (int i = 1; i <=m ; i++) { ans=Math.min(ans,dp[n][i]); } System.out.println(ans); } }
import java.util.*; import java.lang.*; import java.io.*; public class Codeforces { public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main (String[] args) throws java.lang.Exception { // your code goes here FastReader sc=new FastReader(); int n=sc.nextInt(); int a[]=new int[n]; ArrayList<Integer> arr0=new ArrayList<>(); ArrayList<Integer> arr1=new ArrayList<>(); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); if(a[i]==0) arr0.add(i); else arr1.add(i); } n=arr0.size(); int m=arr1.size(); int dp[][]=new int[m+1][n+1]; for(int i=0;i<=n;i++) { dp[0][i]=0; } for(int i=1;i<=m;i++) { dp[i][i]=dp[i-1][i-1]+Math.abs(arr0.get(i-1)-arr1.get(i-1)); for(int j=i+1;j<=n;j++) { dp[i][j]=Math.min(dp[i-1][j-1]+Math.abs(arr0.get(j-1)-arr1.get(i-1)),dp[i][j-1]); } } System.out.println(dp[m][n]); } }
0
Non-plagiarised
18e2441c
24b20554
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.Map; import java.util.HashMap; public class cf1515 { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } static class Task { public void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- != 0) { int n = in.nextInt(); int m = in.nextInt(); int x = in.nextInt(); TreeMap<Integer, ArrayList<Integer>> map = new TreeMap<>(); for (int i = 0; i < n; i++) { int j = in.nextInt(); if (!map.containsKey(j)) { map.put(j, new ArrayList<Integer>()); } map.get(j).add(i); } out.println("YES"); int[] ans = new int[n]; int sta = 0; for (int s : map.keySet()) { for (int i = 0; i < map.get(s).size(); i++) { ans[map.get(s).get(i)] = (sta++) % m + 1; } } for(int i=0;i<n;i++) { out.print(ans[i]+" "); } out.println(); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreElements()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
import java.util.*; import java.lang.*; public class Codeforces { static Scanner sr=new Scanner(System.in); public static void main(String[] args) throws java.lang.Exception { StringBuilder ans = new StringBuilder(""); int T = sr.nextInt(); while (T-- > 0) { int n=sr.nextInt(); int m=sr.nextInt(); int x=sr.nextInt(); TreeMap<Integer,ArrayList<Integer>>h=new TreeMap<>(); for(int i=0;i<n;i++) { int a=sr.nextInt(); if(!h.containsKey(a)) h.put(a,new ArrayList<>()); h.get(a).add(i); } ans.append("YES"); ans.append('\n'); int an[]=new int[n]; int q=0; for(int z:h.keySet()) { for(int i=0;i<h.get(z).size();i++) { an[h.get(z).get(i)]=(q++)%m+1; } } for(int i=0;i<n;i++) ans.append(an[i]+" "); ans.append('\n'); } System.out.println(ans); } }
1
Plagiarised
18e2441c
792863db
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.Map; import java.util.HashMap; public class cf1515 { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } static class Task { public void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- != 0) { int n = in.nextInt(); int m = in.nextInt(); int x = in.nextInt(); TreeMap<Integer, ArrayList<Integer>> map = new TreeMap<>(); for (int i = 0; i < n; i++) { int j = in.nextInt(); if (!map.containsKey(j)) { map.put(j, new ArrayList<Integer>()); } map.get(j).add(i); } out.println("YES"); int[] ans = new int[n]; int sta = 0; for (int s : map.keySet()) { for (int i = 0; i < map.get(s).size(); i++) { ans[map.get(s).get(i)] = (sta++) % m + 1; } } for(int i=0;i<n;i++) { out.print(ans[i]+" "); } out.println(); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreElements()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
//package practice; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int gcd(int a,int b) { if(b<=0) { return a; } return gcd(b,a%b); } static int digSum(int n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } static Set<Long> set=new HashSet<>(); static void sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for(int i = 2; i <= n; i++) { if(prime[i] == true) { set.add((long)i); } } } static boolean isPrime(long x){ if(x <= 1)return false; if(x == 2)return true; if(x%2 == 0)return false; for(long i = 3; i*i <= x; i+= 2) if(x%i == 0) return false; return true; } static int power(int x, int y, int p) { int res = 1; // Initialize result //Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) == 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static boolean palin(char ch[]) { int i=0;int j=ch.length-1; while(i<j) { if(ch[i]!=ch[j]) return false; i++; j--; } return true; } static int highestDivisor(int n) { if ((n & 1) == 0) return n / 2; int i = 3; while (i * i <= n) { if (n % i == 0) { return n / i; } i = i + 2; } return 1; } static boolean isPowerOfTwo(int n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static boolean isPerfectSquare(int x) { if (x >= 0) { int sr = (int)Math.sqrt(x); return ((sr * sr) == x); } return false; } static boolean solve(int l,String s) { int cnt=0; int n=s.length(); for(int i=0;i<n;i++) { if(s.charAt(i)=='1') cnt++; int per= (cnt *100)/(i+1); if(per>=50) return true; } return false; } public static void main(String[] args) throws IOException { FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int m=sc.nextInt(); int x=sc.nextInt(); int a[]=new int[n]; int b[]=new int[n]; int freq[]=new int[100002]; Arrays.fill(freq, 0); for(int i=0;i<n;i++) { a[i]=b[i]=sc.nextInt(); } Arrays.sort(b); int temp=1; Map<Integer,List<Integer>> map=new HashMap<>(); for(int i=0;i<n;i++) { if(map.get(b[i])==null) { List<Integer> lis=new ArrayList<Integer>(); lis.add(temp); map.put(b[i], lis); } else { List<Integer> lis=map.get(b[i]); lis.add(temp); map.put(b[i], lis); } temp++; if(temp==m+1) { temp=1; } } // for(Map.Entry<Integer,List<Integer>> set: map.entrySet()) { // System.out.println(set.getKey()+" "+set.getValue()); // } StringBuilder sb=new StringBuilder(); List<Integer> res=new ArrayList<Integer>(); for(int i=0;i<n;i++) { List<Integer> lis=map.get(a[i]); res.add(lis.get(freq[a[i]])); // sb.append(lis.get(freq[a[i]])+" "); // System.out.println(lis); freq[a[i]]++; } boolean flag=true; int arr[]=new int[m+1]; for(int i=0;i<n;i++) { arr[res.get(i)] += a[i]; } for(int i=1;i<=m-1;i++) { long cal= Math.abs(arr[i+1]-arr[i]); if(cal >x) { flag=false; break; } if(!flag) break; } if(!flag) { System.out.println("NO"); } else { System.out.println("YES"); for(int i=0;i<res.size();i++) { System.out.print(res.get(i)+" "); } System.out.println(); } } } }
0
Non-plagiarised
3368f340
a8f7c8b7
//package Codeforces; import java.io.*; import java.util.*; public class Menorah { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); while (t-->0){ int n = sc.nextInt(); char[] a = sc.next().toCharArray(); char[] b = sc.next().toCharArray(); int a1=0, b1=0; for(int i=0;i<n;i++){ if(a[i]=='1') a1++; if(b[i]=='1') b1++; } int min = 100000000; if(a1==b1){ int c = 0; for(int i=0;i<n;i++){ if(a[i]!=b[i]) c++; } min = Math.min(min, c); } if(b1==(n-a1+1)){ int ind = -1; for(int i=0;i<n;i++){ if(a[i]==b[i] && a[i]=='1'){ ind = i; break; } } int c = 0; for(int i=0;i<n;i++){ if(i==ind) continue; if(a[i]==b[i]) c++; } min = Math.min(min, c + 1); } if(min == 100000000) sb.append("-1\n"); else sb.append(min).append("\n"); } System.out.println(sb); sc.close(); } }
import java.util.Scanner; public class Menorah { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); char initial[] = sc.next().toCharArray(); char desired[] = sc.next().toCharArray(); int lit1 = 0, lit2 = 0; int ans = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { if (initial[i] == '1') { ++lit1; } if (desired[i] == '1') { ++lit2; } } if (lit1 == lit2) { int count = 0; for (int i = 0; i < n; i++) { if (initial[i] != desired[i]) { ++count; } } ans = Math.min(count, ans); } if (lit2 == (n - lit1 + 1)) { int count = 0; for (int i = 0; i < n; i++) { if (initial[i] == desired[i]) { ++count; } } ans = Math.min(ans, count); } if (ans == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(ans); } } } }
1
Plagiarised
bdfe8110
e6a6e318
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; public class E { public static void main(String[] args) { FastScanner fs=new FastScanner(); int T=fs.nextInt(); PrintWriter out=new PrintWriter(System.out); for (int tt=0; tt<T; tt++) { int n=fs.nextInt(), k=fs.nextInt(); int[] positions=fs.readArray(k), temps=fs.readArray(k); int[] forced=new int[n]; Arrays.fill(forced, Integer.MAX_VALUE/2); for (int i=0; i<k; i++) forced[positions[i]-1]=temps[i]; for (int i=1; i<n; i++) forced[i]=Math.min(forced[i], forced[i-1]+1); for (int i=n-2; i>=0; i--) forced[i]=Math.min(forced[i], forced[i+1]+1); for (int i=0; i<n; i++) out.print(forced[i]+" "); out.println(); } out.close(); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
//package codeforces; import java.io.PrintWriter; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class solution { public static void main(String args[]) throws java.lang.Exception{ FastScanner s=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int t=s.nextInt(); for(int tt=0;tt<t;tt++) { int n=s.nextInt(), k=s.nextInt(); int[] a=s.readArray(k), temp=s.readArray(k); long[] ans=new long[n]; Arrays.fill(ans, Integer.MAX_VALUE); for (int i=0; i<k; i++) { ans[a[i]-1]=temp[i]; } for (int i=1; i<n; i++) { ans[i]=Math.min(ans[i],ans[i-1]+1); } for (int i=n-2; i>=0; i--) { ans[i]=Math.min(ans[i],ans[i+1]+1); } for (int i=0; i<n; i++) { out.print(ans[i]+" "); } out.println(); } out.close(); } static void sort(long [] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sort(int [] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static int gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); } static void sortcol(int a[][],int c) { Arrays.sort(a, (x, y) -> { if(x[c]>y[c]) { return 1; }else { return -1; } }); } public static void printb(boolean ans) { if(ans) { System.out.println("Yes"); }else { System.out.println("No"); } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } double nextDouble() { return Double.parseDouble(next()); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static class Pair implements Comparable<Pair>{ int a , b; Pair(int x , int y){ a=x; b=y; } public int compareTo(Pair o) { return a != o.a ? a - o.a : b - o.b; } } }
1
Plagiarised
52cd85f2
ff34fab2
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.StringTokenizer; public class PC3C { static PrintWriter out = new PrintWriter(System.out); static MyFastReaderPC3C in = new MyFastReaderPC3C(); static long mod = (long) (1e9 + 7); public static void main(String[] args) throws Exception { int test = i(); while (test-- > 0) { int n=i(); int[] arr=arrI(n); String s=string(); ArrayList<Integer> lR=new ArrayList<>(); ArrayList<Integer> lB=new ArrayList<>(); for(int i=0;i<n;i++) { if(s.charAt(i)=='R') lR.add(arr[i]); else lB.add(arr[i]); } Collections.sort(lB); Collections.sort(lR,Collections.reverseOrder()); int k=1; boolean st=true; for(int i=0;i<lB.size();i++) { if(lB.get(i)>=k) { k+=1; } else { st=false; break; } } boolean st2=true; k=n; for(int i=0;i<lR.size();i++) { if(lR.get(i)>k) { st2=false; break; } else { k-=1; } } if(st && st2) out.print("YES"); else out.print("NO"); out.print("\n"); out.flush(); } out.close(); } static class pair { long x, y; pair(long ar, long ar2) { x = ar; y = ar2; } } static void sort(long[] a) // check for long { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static class DescendingComparator implements Comparator<Integer> { public int compare(Integer a, Integer b) { return b - a; } } static class AscendingComparator implements Comparator<Integer> { public int compare(Integer a, Integer b) { return a - b; } } static boolean isPalindrome(char X[]) { int l = 0, r = X.length - 1; while (l <= r) { if (X[l] != X[r]) return false; l++; r--; } return true; } static long fact(long N) { long num = 1L; while (N >= 1) { num = ((num % mod) * (N % mod)) % mod; N--; } return num; } static long pow(long a, long b) { long mod = 1000000007; long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) pow = (pow * x) % mod; x = (x * x) % mod; b /= 2; } return pow; } static long toggleBits(long x)// one's complement || Toggle bits { int n = (int) (Math.floor(Math.log(x) / Math.log(2))) + 1; return ((1 << n) - 1) ^ x; } static int countBits(long a) { return (int) (Math.log(a) / Math.log(2) + 1); } static boolean isPrime(long N) { if (N <= 1) return false; if (N <= 3) return true; if (N % 2 == 0 || N % 3 == 0) return false; for (int i = 5; i * i <= N; i = i + 6) if (N % i == 0 || N % (i + 2) == 0) return false; return true; } static long GCD(long a, long b) { if (b == 0) { return a; } else return GCD(b, a % b); } // Debugging Functions Starts static void print(char A[]) { for (char c : A) System.out.print(c + " "); System.out.println(); } static void print(boolean A[]) { for (boolean c : A) System.out.print(c + " "); System.out.println(); } static void print(int A[]) { for (int a : A) System.out.print(a + " "); System.out.println(); } static void print(long A[]) { for (long i : A) System.out.print(i + " "); System.out.println(); } static void print(ArrayList<Integer> A) { for (int a : A) System.out.print(a + " "); System.out.println(); } // Debugging Functions END // ---------------------- // IO FUNCTIONS STARTS static HashMap<Integer, Integer> getHashMap(int A[]) { HashMap<Integer, Integer> mp = new HashMap<>(); for (int a : A) { int f = mp.getOrDefault(a, 0) + 1; mp.put(a, f); } return mp; } public static Map<Character, Integer> mapSortByValue(Map<Character, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Character, Integer>> list = new LinkedList<Map.Entry<Character, Integer>>(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() { public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) { return o1.getValue() - o2.getValue(); } }); // put data from sorted list to hashmap Map<Character, Integer> temp = new LinkedHashMap<Character, Integer>(); for (Map.Entry<Character, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static String string() { return in.nextLine(); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] arrI(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] arrL(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) A[i] = in.nextLong(); return A; } } class MyFastReaderPC3C { BufferedReader br; StringTokenizer st; public MyFastReaderPC3C() { 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; } }
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.io.*; public class Div2 { private static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static String solution(int [] arr, int n, String st) { ArrayList<Integer> red = new ArrayList<>(); ArrayList<Integer> blue = new ArrayList<>(); for(int i = 0; i<n; i++) { if(st.charAt(i)=='R') red.add(arr[i]); else blue.add(arr[i]); } Collections.sort(red); Collections.sort(blue); int cb = 1; for(int j = 0; j<blue.size(); j++) { if(blue.get(j)<cb) return "NO"; cb++; } int cr = n; for(int j = red.size()-1; j>=0; j--) { if(red.get(j)>cr) return "NO"; cr--; } return "YES"; } private static PrintWriter out = new PrintWriter(System.out); public static void main (String[] args) { MyScanner s = new MyScanner(); int t = s.nextInt(); for(int j = 0; j<t ; j++) { int n = s.nextInt(); int[] arr = new int[n]; for(int i =0; i<n; i++) arr[i] = s.nextInt(); String st = s.next(); out.println(solution(arr,n, st)); } out.flush(); out.close(); } }
0
Non-plagiarised
6c4ac8d3
829d2024
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.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.io.*; public class ieee1{ public static void main(String[] args) { Scanner scn=new Scanner(System.in); int t=scn.nextInt(); while(t-->0){ HashMap<Integer,Integer> map=new HashMap<>(); int b=scn.nextInt(); int m=scn.nextInt(); int x=scn.nextInt(); int[] arr=new int[b]; PriorityQueue<Node> pq=new PriorityQueue<>(new pqc()); for(int i=0;i<b;i++){ int ele=scn.nextInt(); arr[i]=ele; } for(int i=1;i<=m;i++){ pq.add(new Node(i,0)); } System.out.println("YES"); for(int i=0;i<arr.length;i++){ int ele=arr[i]; Node n=pq.poll(); System.out.print(n.ind+" "); n.data+=ele; pq.add(n); } System.out.println(); } } public static class Node{ int ind; int data; Node(int ind,int data){ this.ind=ind; this.data=data; } } public static class pqc implements Comparator<Node>{ public int compare(Node n1,Node n2){ if(n1.data<n2.data){ return -1; } else if(n1.data>n2.data){ return 1; } else{ return 0; } } } }
import java.util.*; import java.io.*; public class Main{ public static class Element implements Comparable<Element>{ public int key; public int value; Element(int k, int v) { key=k; value=v; } @Override public int compareTo(Element o) { if(this.value<o.value) return -1; if(this.value>o.value) return 1; return 0; } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int m=sc.nextInt(); int x=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;++i) arr[i]=sc.nextInt(); PriorityQueue<Element> pq=new PriorityQueue<>(); for(int i=1;i<=m;++i) { pq.add(new Element(i,0)); } System.out.println("YES"); for(int j=0;j<n;j++) { Element cur = pq.poll(); System.out.print(cur.key+" "); cur.value+= arr[j]; pq.add(cur); } System.out.println(); } } }
1
Plagiarised
0cedec8a
ac8acb97
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 */ 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); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, Scanner in, PrintWriter out) { int T = in.nextInt(); while (T-- > 0) { solveOne(in, out); } } private void solveOne(Scanner in, PrintWriter out) { int N = in.nextInt(); int nums[] = L.readIntArray(N, in); int min[] = new int[]{Integer.MAX_VALUE, Integer.MAX_VALUE}; int rem[] = new int[]{N, N}; long sum = 0; long ans = Long.MAX_VALUE; for (int idx = 0; idx < N; idx++) { min[idx % 2] = Math.min(min[idx % 2], nums[idx]); rem[idx % 2]--; sum += nums[idx]; long cur = sum + rem[0] * (long) min[0] + rem[1] * (long) min[1]; ans = Math.min(ans, cur); } out.println(ans); } } static class L { public static int[] readIntArray(int size, Scanner in) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = in.nextInt(); } return array; } } }
import java.util.Scanner; public class C1499 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); // int[] arr = new int[n]; long[] mn = { Long.MAX_VALUE, Long.MAX_VALUE }; long[] rem = { n, n }; long sum = 0; long ans = Long.MAX_VALUE; for (int i = 0; i < n; i++) { int temp = in.nextInt(); mn[i % 2] = Math.min(mn[i % 2], temp); rem[i % 2]--; sum += temp; if (i > 0) { long cur = sum + rem[0] * mn[0] + rem[1] * mn[1]; ans = Math.min(ans, cur); } } System.out.println(ans); // int a = Integer.MAX_VALUE; // int aIndex = -1; // int b = Integer.MAX_VALUE; // int bIndex = -1; // // for (int i = 0; i < n; i++) { // arr[i] = in.nextInt(); // if (i % 2 == 0) { // if (arr[i] < a) { // a = arr[i]; // aIndex = i; // } // } else { // if (arr[i] < b) { // b = arr[i]; // bIndex = i; // } // } // } // int sum = 0; // for (int i = 0; i < Math.max(bIndex, aIndex) + 1; i++) { // if (i % 2 == 0) { // if (i == aIndex) { // if (aIndex < bIndex) { // sum += (n - (i / 2) - ((bIndex - aIndex) / 2)) * arr[i]; // } else { // sum += (n - (i / 2)) * arr[i]; // } // } else { // sum += arr[i]; // } // } else { // if (i == bIndex) { // if (bIndex < aIndex) { // sum += (n - (i / 2) - ((aIndex - bIndex) / 2)) * arr[i]; // } else { // sum += (n - (i / 2)) * arr[i]; // } // } else { // sum += arr[i]; // } // } // // } // System.out.println(sum); // for (int i = 0; i < n; i++) { // arr[i] = in.nextInt(); // if (arr[i] < a) { // a = arr[i]; // aIndex = i; // // } // // } // if (aIndex == 0) { // // for (int i = 1; i < n; i++) { // if (arr[i] < b) { // b = arr[i]; // bIndex = i; // } // } // System.out.println(aIndex + " " + bIndex); // System.out.println(a + " " + b); // int sum = 0; // for (int i = 1; i < bIndex; i++) { // sum += arr[i]; // } // sum += b * (n - bIndex + 1); // sum += a * n; // System.out.println(sum); // } else { // int b = Integer.MAX_VALUE; // int bIndex = -1; // for (int i = 0; i < aIndex; i++) { // if (arr[i] < b) { // b = arr[i]; // bIndex = i; // } // } // System.out.println(aIndex + " " + bIndex); // System.out.println(a + " " + b); // int sum = 0; // for (int i = 0; i < bIndex; i++) { // sum += arr[i]; // } // sum += b * (n - bIndex); // sum += a * n; // System.out.println(sum); // } } } }
1
Plagiarised
11c2ab99
fadc1365
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { FastScanner fs=new FastScanner(); int T=fs.nextInt(); PrintWriter out=new PrintWriter(System.out); for (int tt=0; tt<T; tt++) { int n=fs.nextInt(), k=fs.nextInt(); int[] positions=fs.readArray(k), temps=fs.readArray(k); int[] forced=new int[n]; Arrays.fill(forced, Integer.MAX_VALUE/2); for (int i=0; i<k; i++) forced[positions[i]-1]=temps[i]; for (int i=1; i<n; i++) forced[i]=Math.min(forced[i], forced[i-1]+1); for (int i=n-2; i>=0; i--) forced[i]=Math.min(forced[i], forced[i+1]+1); for (int i=0; i<n; i++) out.print(forced[i]+" "); out.println(); } out.close(); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
import java.io.PrintWriter; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class a{ public static void main(String args[]) throws java.lang.Exception{ FastScanner s=new FastScanner(); PrintWriter out=new PrintWriter(System.out); int t=s.nextInt(); for(int tt=0;tt<t;tt++) { int n=s.nextInt(),k=s.nextInt(); int pos[]=s.readArray(k); int temp[]=s.readArray(k); long ans[]=new long[n]; Arrays.fill(ans,Integer.MAX_VALUE); for(int i=0;i<k;i++){ ans[pos[i]-1]=temp[i]; } for(int i=1;i<n;i++){ ans[i]=Math.min(ans[i-1]+1,ans[i]); } for(int i=n-2;i>=0;i--){ ans[i]=Math.min(ans[i],ans[i+1]+1); } for(int i=0;i<n;i++){ out.print(ans[i]+" "); } out.println(); } out.close(); } static boolean isPrime(int n){ if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.sqrt(n); i += 2){ if (n % i == 0) return false; } return true; } static void sort(long [] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sort(char [] a) { ArrayList<Character> l=new ArrayList<>(); for (char i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sort(int [] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static int gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); } static void sortcol(int a[][],int c) { Arrays.sort(a, (x, y) -> { if(x[c]>y[c]) { return 1; }else { return -1; } }); } public static void printb(boolean ans) { if(ans) { System.out.println("Yes"); }else { System.out.println("No"); } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } double nextDouble() { return Double.parseDouble(next()); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static class Pair implements Comparable<Pair>{ int a , b; Pair(int x , int y){ a=x; b=y; } public int compareTo(Pair o) { return a != o.a ? a - o.a : b - o.b; } } }
1
Plagiarised
a4d6775d
eea69e7f
import java.io.*; import java.util.*; public class ArmChairs { public static int solution(int n, int[] arr) { ArrayList<Integer> one = new ArrayList<Integer>(); ArrayList<Integer> zero = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { if (arr[i] == 1) { one.add(i); } else { zero.add(i); } } int[][] dp = new int[one.size() + 1][zero.size() + 1]; for (int i = 1; i <= one.size(); i++) { dp[i][i] = dp[i - 1][i - 1] + Math.abs(one.get(i - 1) - zero.get(i - 1)); for (int j = i + 1; j <= zero.size(); j++) { dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j - 1] + Math.abs(one.get(i - 1) - zero.get(j - 1))); } } return dp[one.size()][zero.size()]; } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split(" "); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(s[i]); } log.write(Integer.toString(solution(n, arr)) + "\n"); log.flush(); } }
import java.util.*; public class Solution { public static int minMoves(int[] input) { List<Integer> people = new ArrayList<Integer>(); List<Integer> chairs = new ArrayList<Integer>(); for (int i = 0; i < input.length; i++) { if (input[i] == 1) { people.add(i); } else { chairs.add(i); } } int[] memo = new int[chairs.size() + 1]; for (int p = 1; ((!people.isEmpty()) && (p <= people.size())); p++) { int prev = memo[p]; memo[p] = memo[p - 1] + Math.abs(people.get(p - 1) - chairs.get(p - 1)); for (int c = p + 1; c <= chairs.size(); c++) { int tmp = memo[c]; memo[c] = Math.min(memo[c - 1], prev + Math.abs(people.get(p - 1) - chairs.get(c - 1))); prev = tmp; } } return memo[memo.length - 1]; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] input = new int[n]; for (int i = 0; i < n; i++) { input[i] = sc.nextInt(); } System.out.println(Solution.minMoves(input)); } }
0
Non-plagiarised
31f29772
b728ba1d
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.PriorityQueue; import java.util.StringTokenizer; import java.io.*; public class PhoenixTow { private static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair p) { return ((Integer)this.x).compareTo(p.x); } } public static void solution(int[] arr, int n, int m, int x) { ArrayList<Pair> list = new ArrayList<>(); for(int i = 0; i<n; i++) { list.add(new Pair(arr[i], i)); } Collections.sort(list); long[] sum = new long[m]; int[] ans = new int[n]; int k = 1; for(int i = 0; i<list.size(); i++) { if(k<m) { if(sum[k-1]+list.get(i).x - sum[k]>x) {out.println("NO"); return; } } sum[k-1]+=list.get(i).x; ans[list.get(i).y] = k; k++; if(k==(m+1)) k=1; } out.println("YES"); for(int i = 0; i<n; i++) out.print(ans[i]+" "); out.println(); } private static PrintWriter out = new PrintWriter(System.out); public static void main (String[] args) { MyScanner s = new MyScanner(); int t = s.nextInt(); for(int j = 0; j<t ; j++) { int n = s.nextInt(); int m = s.nextInt(); int x = s.nextInt(); int[] arr = new int[n]; for(int i =0; i<n; i++) arr[i] = s.nextInt(); solution(arr,n,m,x); } out.flush(); out.close(); } }
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.Objects; import java.util.Collections; import java.io.InputStream; 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); CPhoenixAndTowers solver = new CPhoenixAndTowers(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class CPhoenixAndTowers { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(), m = in.nextInt(), k = in.nextInt(); ArrayList<Pair<Integer, Integer>> a = new ArrayList<>(); for (int i = 0; i < n; ++i) { a.add(new Pair<>(in.nextInt(), i)); } Collections.sort(a); int[] ans = new int[n]; int[] sum = new int[m]; int j = 1; for (int i = 0; i < n; ++i) { ans[a.get(i).y] = j; sum[j - 1] += a.get(i).x; j++; if (j == m + 1) j = 1; } for (int i = 1; i < m; ++i) { if (Math.abs(sum[i - 1] - sum[i]) > k) { out.println("NO"); } } out.println("YES"); for (int e : ans) { out.print(e + " "); } out.println(); } } static class Pair<U, V> implements Comparable<Pair<U, V>> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } public int compareTo(Pair<U, V> o) { int value = ((Comparable<U>) x).compareTo(o.x); if (value != 0) return value; return ((Comparable<V>) y).compareTo(o.y); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return x.equals(pair.x) && y.equals(pair.y); } public int hashCode() { return Objects.hash(x, y); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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); } } }
0
Non-plagiarised
49e94e7e
da5cf40b
import java.io.BufferedReader; import java.io.IOException; import java.lang.*; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.File; import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; public class Main { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair ob) { return (int)(first - ob.first); } } static class Tuple implements Comparable<Tuple> { int first, second,third; public Tuple(int first, int second, int third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int[] parent; int[] rank; //Size of the trees is used as the rank public DSU(int n) { parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) //finding through path compression { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public boolean union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return false; //if they are already connected we exit by returning false. // if a's parent is less than b's parent if(rank[a] < rank[b]) { //then move a under b parent[a] = b; } //else if rank of j's parent is less than i's parent else if(rank[a] > rank[b]) { //then move b under a parent[b] = a; } //if both have the same rank. else { //move a under b (it doesnt matter if its the other way around. parent[b] = a; rank[a] = 1 + rank[a]; } return true; } } static class Reader { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) throws IOException { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] longReadArray(int n) throws IOException { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static long lcm(long a, long b) { return (a / LongGCD(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long modPow(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return modPow(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)(n)] * modInverse(fac[(int)(r)], p) % p * modInverse(fac[(int)(n - r)], p) % p) % p; } public static long fact(long n) { long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (long i = 1; i <= n; i++) fac[(int)(i)] = fac[(int)(i - 1)] * i; return fac[(int)(n)]; } public static long nCr(long n, long k) { long ans = 1; for(long i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } //Modular Operations for Addition and Multiplication. public static long perfomMod(long x) { return ((x%M + M)%M); } public static long addMod(long a, long b) { return perfomMod(perfomMod(a)+perfomMod(b)); } public static long subMod(long a, long b) { return perfomMod(perfomMod(a)-perfomMod(b)); } public static long mulMod(long a, long b) { return perfomMod(perfomMod(a)*perfomMod(b)); } public static boolean isPrime(long n) { if(n == 1) { return false; } //check only for sqrt of the number as the divisors //keep repeating so only half of them are required. So,sqrt. for(int i = 2;i*i<=n;i++) { if(n%i == 0) { return false; } } return true; } public static List<Integer> SieveList(int n) { boolean prime[] = new boolean[(int)(n+1)]; Arrays.fill(prime, true); List<Integer> l = new ArrayList<>(); for (int p = 2; p*p<=n; p++) { if (prime[p] == true) { for(int i = p*p; i<=n; i += p) { prime[i] = false; } } } for (int p = 2; p<=n; p++) { if (prime[p] == true) { l.add(p); } } return l; } public static int countDivisors(int x) { int c = 0; for(int i = 1;i*i<=x;i++) { if(x%i == 0) { if(x/i != i) { c+=2; } else { c++; } } } return c; } public static long log2(long n) { long ans = (long)(log(n)/log(2)); return ans; } public static boolean isPow2(long n) { return (n != 0 && ((n & (n-1))) == 0); } public static boolean isSq(int x) { long s = (long)Math.round(Math.sqrt(x)); return s*s==x; } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(long a[], long x) { int l=-1, r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void Sort(long[] a) { List<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); // Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void ssort(char[] a) { List<Character> l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int tt = sc.nextInt(); while(tt-- > 0) { int n = sc.nextInt(); char[] a = sc.next().toCharArray(), b = sc.next().toCharArray(); int c00 = 0, c01 = 0, c10 = 0, c11 = 0; for(int i = 0;i<n;i++) { if(a[i] == '0' && b[i] == '0') { c00++; } else if(a[i] == '0' && b[i] == '1') { c01++; } else if(a[i] == '1' && b[i] == '0') { c10++; } else if(a[i] == '1' && b[i] == '1') { c11++; } } int ans = mod; if(c01 == c10) ans = min(ans, c01 + c10); if(c11 == c00 + 1) ans = min(ans, c11 + c00); fout.println((ans == mod) ? -1 : ans); } fout.close(); } }
/***** ---> :) Vijender Srivastava (: <--- *****/ import java.util.*; import java.lang.*; import java.io.*; public class Main { static FastReader sc =new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=(long)1e9+7; /* start */ public static void main(String [] args) { // int testcases = 1; int testcases = i(); while(testcases-->0) { solve(); } out.flush(); out.close(); } static void solve() { int n = i(); char c[] = inputC(); char d[] = inputC(); int x01=0,x10=0,x00=0,x11=0; for(int i=0;i<n;i++) { if(c[i]=='0'&&d[i]=='0')x00++; if(c[i]=='0'&&d[i]=='1')x01++; if(c[i]=='1'&&d[i]=='0')x10++; if(c[i]=='1'&&d[i]=='1')x11++; } int ans = Integer.MAX_VALUE; if(x01==0 && x10==0) { pl(0); return ; } if(x11==x00+1) { ans = min(x11+x00,ans); } if(x01==x10) { ans = min(x01+x10,ans); } if(ans == Integer.MAX_VALUE){ ans = -1; } pl(ans); } /* end */ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void p(Object o) { out.print(o); } static void pl(Object o) { out.println(o); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static char[] inputC() { String s = sc.nextLine(); return s.toCharArray(); } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static long[] putL(long a[]) { long A[]=new long[a.length]; for(int i=0;i<a.length;i++) { A[i]=a[i]; } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x) ; y = y >> 1; x = (x * x); } return res; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long[] sort(long a[]) { ArrayList<Long> arr = new ArrayList<>(); for(long i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } static int[] sort(int a[]) { ArrayList<Integer> arr = new ArrayList<>(); for(Integer i : a) { arr.add(i); } Collections.sort(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } //pair class private static class Pair implements Comparable<Pair> { long first, second; public Pair(long f, long s) { first = f; second = s; } @Override public int compareTo(Pair p) { if (first > p.first) return 1; else if (first < p.first) return -1; else { if (second > p.second) return 1; else if (second < p.second) return -1; else return 0; } } } }
0
Non-plagiarised
a8f7c8b7
f229aa7f
import java.util.Scanner; public class Menorah { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); char initial[] = sc.next().toCharArray(); char desired[] = sc.next().toCharArray(); int lit1 = 0, lit2 = 0; int ans = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { if (initial[i] == '1') { ++lit1; } if (desired[i] == '1') { ++lit2; } } if (lit1 == lit2) { int count = 0; for (int i = 0; i < n; i++) { if (initial[i] != desired[i]) { ++count; } } ans = Math.min(count, ans); } if (lit2 == (n - lit1 + 1)) { int count = 0; for (int i = 0; i < n; i++) { if (initial[i] == desired[i]) { ++count; } } ans = Math.min(ans, count); } if (ans == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(ans); } } } }
import java.util.*; import java.io.*; import java.math.*; public class cf { static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); char[] a = sc.next().toCharArray(); char[] b = sc.next().toCharArray(); int x = 0, y = 0, lit = 0,lit2 = 0; for (int i = 0; i < n; i++) { if (a[i] == '1') lit++; if (b[i] == '1') lit2++; if (a[i] == b[i]) x++; else y++; } if(lit == lit2 || n - lit + 1 == lit2) { if (lit == lit2 && n - lit + 1 == lit2) { pw.println(Math.min(x,y)); }else if(lit == lit2) { pw.println(y); }else { pw.println(x); } }else { pw.println(-1); } } pw.close(); } public static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) return this.z - other.z; return this.y - other.y; } else { return this.x - other.x; } } } public static class pair implements Comparable<pair> { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Integer(x).hashCode() * 31 + new Integer(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
0
Non-plagiarised
065e0cbd
9b449b4f
import java.util.*; import java.io.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static int ask(int i){ FastReader sc = new FastReader(); System.out.println("? " + (i+1)); System.out.flush(); int x = sc.nextInt(); return x - 1; } public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); int inf = 1000000007; while(t-->0){ int n = sc.nextInt(); int ans[] = new int[n]; for(int i=0;i<n;i++){ if(ans[i] == 0){ ArrayList<Integer> cycle = new ArrayList<Integer>(); int x = ask(i), y = ask(i); cycle.add(y); while(y != x){ y = ask(i); cycle.add(y); } for(int j=0;j<cycle.size();j++){ ans[cycle.get(j)] = cycle.get((j+1)%cycle.size()) + 1; } } } System.out.print("! "); for(int i=0;i<n;i++) System.out.print(ans[i] + " "); System.out.println(); } } }
/* "Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up." */ import java.util.*; import java.lang.*; import java.io.*; public class Solution { public static void main(String[] args) throws java.lang.Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new FastReader(); int test = sc.nextInt(); for (int t = 0; t < test; t++) { solve(); } out.close(); } private static void solve() { int n = sc.nextInt(); int[] permutation = new int[n]; Arrays.fill(permutation, -1); // there may be multiple cycles in the given permutation for (int i = 0; i < n; i++) { if (permutation[i] == -1) { // ith permutation value is not found yet // so we find all values of permutation having i in their cycle // for that we always ask(i) so that we can get all values in that cycle List<Integer> cycle = new ArrayList<>(); int startCycleValue = ask(i + 1); int currValueAt = ask(i + 1); cycle.add(currValueAt); while (currValueAt != startCycleValue) { currValueAt = ask(i + 1); cycle.add(currValueAt); } int m = cycle.size(); for (int j = 0; j < m; j++) { permutation[cycle.get(j)] = cycle.get((j + 1) % m); } } } out.println("! "); for (int i = 0; i < n; i++) { out.print((permutation[i] + 1) + " "); } out.println(); out.flush(); } private static int ask(int i) { out.println("? " + i + " "); out.flush(); int value = sc.nextInt(); return value - 1; } public static FastReader sc; public static PrintWriter out; static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException end) { end.printStackTrace(); } } return str.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 end) { end.printStackTrace(); } return str; } } }
0
Non-plagiarised
53d782a0
5769b7b3
import java.io.*; import java.util.*; public class B { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(); long k = sc.nl(); Long[] arr = new Long[n]; for(int i = 0; i < n; i++) arr[i] = sc.nl(); Arrays.sort(arr); long sum = 0; for(int i = 0; i < n; i++) { sum += arr[i]; } if(sum <= k) { w.p(0); return; } long cont = 0; long min = sum-k; for(int i = n-1; i >= 0; i--) { cont += arr[i]; long psum = sum-cont; if(psum <= k) { long extra = k-psum; if(arr[0]*(n-i)<=extra) { min = Math.min(min, n-i); continue; } if(i == 0) { long q = k/n; long ans = arr[0]-q+n-1; min = Math.min(min, ans); continue; } extra += arr[0]; long q = extra/(n-i+1); long ans = arr[0]-q+n-i; min = Math.min(min, ans); } else { long toRem = psum-k-arr[0]; long q = (long)Math.ceil(toRem/(n-i+1.0)); long ans = q+arr[0]+n-i; min = Math.min(min, ans); } } w.p(min); } }
import java.io.*; import java.util.*; public class B { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(); long k = sc.nl(); Long[] arr = new Long[n]; for(int i = 0; i < n; i++) arr[i] = sc.nl(); Arrays.sort(arr); long sum = 0; for(int i = 0; i < n; i++) { sum += arr[i]; } if(sum <= k) { w.p(0); return; } long cont = 0; long min = sum-k; for(int i = n-1; i >= 0; i--) { cont += arr[i]; long psum = sum-cont; if(psum <= k) { long extra = k-psum; if(arr[0]*(n-i)<=extra) { min = Math.min(min, n-i); continue; } if(i == 0) { long q = k/n; long ans = arr[0]-q+n-1; min = Math.min(min, ans); continue; } extra += arr[0]; long q = extra/(n-i+1); long ans = arr[0]-q+n-i; min = Math.min(min, ans); } else { long toRem = psum-k-arr[0]; long q = (long)Math.ceil(toRem/(n-i+1.0)); long ans = q+arr[0]+n-i; min = Math.min(min, ans); } } w.p(min); } }
1
Plagiarised
35eb27da
64ce7b1e
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); sc.nextLine(); int a[][] = new int[26][26]; int b[][][] = new int[26][26][26]; boolean bl = false; String arr[] = new String[n]; for(int i = 0 ; i < n ; i++) { arr[i] = sc.nextLine(); if(arr[i].length() == 1) { bl = true; } else if(arr[i].length() == 2) { //a[arr[i].charAt(0)-'a'][arr[i].charAt(1)-'a'] = 1; if(arr[i].charAt(0)==arr[i].charAt(1)) bl = true; } else { //b[arr[i].charAt(0)-'a'][arr[i].charAt(1)-'a'][arr[i].charAt(2)-'a'] = 1; if(arr[i].charAt(0) == arr[i].charAt(2)) bl = true; } } if(bl) System.out.println("YES"); else { for(int i = 0; i < n ; i++) { if(arr[i].length() == 2) { int p1 = arr[i].charAt(0)-'a'; int p2 = arr[i].charAt(1)-'a'; if(a[p2][p1] == 1) bl = true; for(int j = 0; j < 26 ; j++) { if(b[p2][p1][j] == 1) bl = true; } a[p1][p2] = 1; } else { int p1 = arr[i].charAt(0)-'a'; int p2 = arr[i].charAt(1)-'a'; int p3 = arr[i].charAt(2)-'a'; if(a[p3][p2] == 1) bl = true; if(b[p3][p2][p1] == 1) bl = true; b[p1][p2][p3] = 1; } } if(bl) System.out.println("YES"); else System.out.println("NO"); } } } }
import java.io.*; import java.util.*; public class new1{ static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static void main(String[] args) throws IOException{ BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); FastReader s = new FastReader(); int t = s.nextInt(); for(int z = 0; z < t; z++) { int n = s.nextInt(); Set<String> st = new HashSet<String>(); String[] arr = new String[n]; boolean pos = false; for(int i = 0; i < n; i++) { String str = s.next(); st.add(str); arr[i] = str; if(str.length() == 1) pos = true; } if(pos) { System.out.println("YES"); continue; } for(int i = 0; i < n; i++) { String str = arr[i]; st.remove(str); if(str.charAt(0) == str.charAt(str.length() - 1)) pos = true; if(str.length() == 3) { String str1 = Character.toString(str.charAt(1)) + Character.toString(str.charAt(0)); if(st.contains(str1)) pos = true; String str2 = Character.toString(str.charAt(2)) + str1; if(st.contains(str2)) pos = true; } else { String str1 = Character.toString(str.charAt(1)) + Character.toString(str.charAt(0)); if(st.contains(str1)) pos = true;; for(int j = 0; j < 26; j++) { char ch = (char) ((int)'a' + j); String str2 = Character.toString(ch) + str1; if(st.contains(str2)) pos = true;; } } } //System.out.println(st.toString()); if(pos) System.out.println("YES"); else System.out.println("NO"); //System.out.println(st.contains("ba")); } } } 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(); } 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; }}
0
Non-plagiarised
12309af3
390d2f23
import java.util.*; import java.lang.*; import java.io.*; public class MyAnswer { public static void main(String[] args)throws IOException{ FastScanner scan = new FastScanner(); //SuperFastScanner scan = new SuperFastScanner(); PrintWriter out = new PrintWriter(System.out); StringBuilder result = new StringBuilder(); int t = scan.nextInt(); while (t-- > 0){ int n = scan.nextInt(); int arr[] = scan.nextIntArray(n); if(n%2==0){ int j = n/2; for(int i = j;i<n;i++){ int val = arr[i] * -1; result.append(val + " "); } for(int i = 0;i<j;i++){ int val = arr[i] ; result.append(val + " "); } }else{ int j = (n-3)/2; for(int i = j;i<n-3;i++){ int val = arr[i] * -1; result.append(val + " "); } for(int i = 0;i<j;i++){ int val = arr[i] ; result.append(val + " "); } if(arr[n-3]+arr[n-2] !=0 ){ int sum = arr[n-3] + arr[n-2]; sum *=-1; result.append(arr[n-1] + " "); result.append(arr[n-1] + " "); result.append(sum+ " "); }else if(arr[n-1]+arr[n-2] !=0 ){ int sum = arr[n-1] + arr[n-2]; sum *=-1; result.append(sum+ " "); result.append(arr[n-3] + " "); result.append(arr[n-3]+" "); }else{ int sum = arr[n-1] + arr[n-3]; sum *=-1; result.append(arr[n-2] + " "); result.append(sum+ " "); result.append(arr[n-2]+ " "); } } result.append("\n"); } out.println(result); out.flush(); } public static int solve(String str,char ch){ int ans = 0; int l = str.length(); int i = 0,j = l-1; while(i<=j){ char a = str.charAt(i); char b = str.charAt(j); if(a==b){ i++; j--; }else{ if(a==ch){ i++; ans++; }else if(b==ch){ j--; ans++; }else{ return 10000000; } } } return ans; } static class FastScanner{ //10^5 -- .15 sec && 4*10^6 ---> .86 sec BufferedReader br; StringTokenizer st; public FastScanner(){ br = new BufferedReader(new InputStreamReader(System.in)); } public String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } public int nextInt(){ return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } public String nextLine(){ String str = ""; try{ str = br.readLine(); }catch (IOException e){ e.printStackTrace(); } return str; } 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; } public double[] nextDoubleArray(int n){ double arr[] = new double[n]; for(int i = 0;i<n;i++){ arr[i] = nextDouble(); } return arr; } public String[] nextStringArray(int n){ String arr[] = new String[n]; for(int i = 0;i<n;i++){ arr[i] = next(); } return arr; } } }
//package oj; import java.io.*; public class Main { static BufferedReader BR = new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer IN = new StreamTokenizer(BR); static PrintWriter OUT = new PrintWriter(new BufferedOutputStream(System.out)); static int nextInt() throws IOException { IN.nextToken(); return (int) IN.nval; } static double nextDouble() throws IOException { IN.nextToken(); return IN.nval; } static String nextStr() throws IOException { IN.nextToken(); return IN.sval; } static String nextLine() throws IOException { return BR.readLine(); } static final int MAXN = 1000000; public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static int lcm(int a, int b) { return a / gcd(a, b) * b; } public static void main(String[] args) throws IOException { // Scanner sc = new Scanner(System.in); int T = nextInt(); while(T-- > 0) { int N = nextInt(); int[] a = new int[N]; for (int i = 0; i < N; i++) { a[i] = nextInt(); } if (N % 2 == 0) { for (int i = 0; i < N; i+=2) { OUT.print(a[i+1] + " "); OUT.print(-1 * a[i] + " "); } } else{ for (int i = 0; i < N-3; i+=2) { OUT.print(a[i+1] + " "); OUT.print(-1 * a[i] + " "); } if(a[N-3] + a[N-2] != 0) { OUT.print(-a[N-1] + " " + -a[N-1] + " " + (a[N-3] + a[N-2])); } else if(a[N-2] + a[N-1] != 0) { OUT.print((a[N-2] + a[N-1]) + " " + -a[N-3] + " " + -a[N-3]); } else { OUT.print(-a[N-2] + " " + (a[N-1] + a[N-3]) + " " + -a[N-2]); } } OUT.println(); } OUT.flush(); } }
0
Non-plagiarised
12c1cc56
7b71234c
import java.io.*; import java.util.*; public class Codeforces { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); int n=Integer.parseInt(bu.readLine()); String s[]=bu.readLine().split(" "); ArrayList<Integer> z=new ArrayList<>(),o=new ArrayList<>(); long dp[][]=new long[n+1][n+1]; int i,j,a; for(i=0;i<n;i++) { a=Integer.parseInt(s[i]); if(a==0) z.add(i); else o.add(i); } for(i=1;i<=o.size();i++) { long min=dp[i-1][i-1]; for(j=i;j<=z.size();j++) { dp[i][j]=min+Math.abs(z.get(j-1)-o.get(i-1)); min=Math.min(min,dp[i-1][j]); } } long ans=Long.MAX_VALUE; for(i=o.size();i<=z.size();i++) ans=Math.min(ans,dp[o.size()][i]); System.out.print(ans); } }
import java.io.*; import java.util.*; public class Codeforces { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); int n=Integer.parseInt(bu.readLine()); String s[]=bu.readLine().split(" "); ArrayList<Integer> z=new ArrayList<>(),o=new ArrayList<>(); long dp[][]=new long[n+1][n+1]; int i,j,a; for(i=0;i<n;i++) { a=Integer.parseInt(s[i]); if(a==0) z.add(i); else o.add(i); } for(i=1;i<=o.size();i++) { long min=dp[i-1][i-1]; for(j=i;j<=z.size();j++) { dp[i][j]=min+Math.abs(z.get(j-1)-o.get(i-1)); min=Math.min(min,dp[i-1][j]); } } long ans=Long.MAX_VALUE; for(i=o.size();i<=z.size();i++) ans=Math.min(ans,dp[o.size()][i]); System.out.print(ans); } }
1
Plagiarised
b56ebada
b95a75a7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class First { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); //int a = 1; int t; t = in.nextInt(); //t = 1; while (t > 0) { //out.print("Case #"+(a++)+": "); solver.call(in, out); t--; } out.close(); } static class TaskA { public void call(InputReader in, PrintWriter out) { int n; n = in.nextInt(); int[] arr = new int[n]; Integer[] lower = new Integer[n]; Integer[] higher = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } TreeSet<Integer> set = new TreeSet<>(); for (int i = 0; i < n; i++) { set.add(arr[i]); lower[i] = set.lower(arr[i]); higher[i] = set.higher(arr[i]); } for (int i = 1; i < n; i++) { if(arr[i]>arr[i-1]){ if(higher[i-1] != null && higher[i-1]<arr[i]){ out.println("NO"); return; } } else{ if(lower[i-1] != null && lower[i-1]>arr[i]){ out.println("NO"); return; } } } out.println("YES"); } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class answer implements Comparable<answer>{ int a; int b; public answer(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return o.a - this.a; } } static class answer1 implements Comparable<answer1>{ int a, b, c; public answer1(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } @Override public int compareTo(answer1 o) { return this.a - o.a; } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static final Random random=new Random(); static void shuffleSort(int[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
import java.util.*; import java.io.*; public class Main{ public static class Node { int val; Node left; Node right; Node(int val) { this.val=val; } } public static void main(String[] args) throws java.io.IOException { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;++i) { arr[i]=sc.nextInt(); } Node head=new Node(arr[0]); boolean cond=false; for(int i=1;i<n;i++) { int cur=head.val; //System.out.println(cur); if(arr[i]>cur) { if(head.right==null) { Node next=new Node(arr[i]); head.right=next; next.left=head; head=head.right; } else if(head.right.val<arr[i]) { cond=true; break; } else if(head.right.val==arr[i]) { head=head.right; } else { Node next=new Node(arr[i]); next.right=head.right; head.right.left=next; head.right=next; next.left=head; head=next; } } else if(arr[i]<cur) { if(head.left==null) { Node next=new Node(arr[i]); head.left=next; next.right=head; head=head.left; } else if(head.left.val>arr[i]) { cond=true; break; } else if(head.left.val==arr[i]) { head=head.left; } else { Node next=new Node(arr[i]); next.left=head.left; head.left.right=next; head.left=next; next.right=head; head=next; } } } if(cond) System.out.println("NO"); else System.out.println("YES"); } } } // 1 0 1 1 0 0 1
0
Non-plagiarised
bdf7bfb2
d92c5342
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0){ int n = sc.nextInt(); String[] s = new String[n]; for(int i=0; i<n; i++) s[i] = sc.next(); int MAX = 0; for(char c = 'a'; c <= 'e'; c++){ PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); //Big comes in top; for(int i=0; i<n; ++i) { int curChar = 0; int otherChar = 0; for(int j=0; j<s[i].length(); j++) { if(s[i].charAt(j) == c) curChar++; else otherChar++; } int diff = curChar - otherChar; pq.add(diff); } int cur = 0; int numberOfWords = 0; while(!pq.isEmpty()){ if(cur + pq.peek() > 0){ cur += pq.poll(); numberOfWords++; }else{ break; } } MAX = Math.max(MAX, numberOfWords); } pw.println(MAX); } pw.close(); } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader f) { br = new BufferedReader(f); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] nextIntArr(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(next()); } return arr; } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.Array; import java.util.*; public class Main{ static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] nextArray(long n) { long[] a = new long[(int) n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } static class FastWriter extends PrintWriter { FastWriter(){ super(System.out); } 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(); } } static int MOD=1000003; public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(); FastWriter out = new FastWriter(); int t=in.nextInt(); //int t=1; while (t-->0){ int n=in.nextInt(); String[] ar=new String[n]; for (int i = 0; i < n; i++) { ar[i]=in.next(); } int ans=0; for(char ch='a';ch<='e';ch++){ int[] res=new int[n]; for (int i = 0; i < n; i++) { String ss=ar[i]; for (int j = 0; j < ss.length(); j++) { if(ss.charAt(j)==ch){ res[i]++; }else { res[i]--; } } } Arrays.sort(res);int max=0,nn=0; for (int i = n-1; i>=0; i--) { max+=res[i]; if(max>0){ nn++; }else { break; } } ans=Math.max(ans,nn); } out.println(ans); } out.close(); } //Arrays.sort(a, (o1, o2) -> (o1[0] - o2[0])); static int totalPrimeFactors(int n) { int cnt=0; while (n%2==0) { cnt++; n /= 2; } int num= (int) Math.sqrt(n); for (int i = 3; i <= num; i+= 2) { while (n%i == 0) { cnt++; n /= i; num=(int)Math.sqrt(n); } } if (n > 2){ cnt++; } return cnt; } static char get(int i){ return (char)(i+'a'); } static boolean distinct(int num){ HashSet<Integer> set=new HashSet<>(); int len=String.valueOf(num).length(); while (num!=0){ set.add(num%10); num/=10; } return set.size()==len; } static int maxSubArraySum(int a[],int st,int en) { int max_s = Integer.MIN_VALUE, max_e = 0,ind=0,ind1=1; for (int i = st; i <= en; i++) { max_e+= a[i]; if (max_s < max_e){ max_s = max_e; ind=ind1; } if (max_e < 0){ max_e = 0; ind1=i+1; } } if(st==0){ return max_s; } if(ind==1){ return a[0]+2*max_s; }else { return 2*max_s+maxSubArraySum(a,0,ind-1); } //return max_s; } static void segmentedSieve(ArrayList<Integer> res,int low, int high) { if(low < 2){ low = 2; } ArrayList<Integer> prime = new ArrayList<>(); sS2(high, prime); boolean[] mark = new boolean[high - low + 1]; Arrays.fill(mark, true); for (int i = 0; i < prime.size(); i++) { int loLim = (low / prime.get(i)) * prime.get(i); if (loLim < low){ loLim += prime.get(i); } if (loLim == prime.get(i)){ loLim += prime.get(i); } for (int j = loLim; j <= high; j += prime.get(i)) { mark[j - low] = false; } } for (int i = low; i <= high; i++) { if (mark[i - low]){ System.out.println(i); res.add(i); } } } static void sS2(int limit, ArrayList<Integer> prime) { int bound = (int)Math.sqrt(limit); boolean[] mark = new boolean[bound + 1]; for (int i = 0; i <= bound; i++){ mark[i] = true; } for (int i = 2; i * i <= bound; i++) { if (mark[i]) { for (int j = i * i; j <= bound; j += i){ mark[j] = false; } } } for (int i = 2; i <= bound; i++) { if (mark[i]){ prime.add(i); } } } static boolean[] sieveOfEratosthenes(int n){ boolean[] prime = new boolean[n + 1]; Arrays.fill(prime,true); prime[0]=false;prime[1]=false; for (int p = 2; p * p <= n; p++) { if(prime[p]){ for (int i = p * p; i <= n; i += p){ prime[i] = false; } } } return prime; } static int highestPowerOf2(int n) { if (n < 1){ return 0; } int res = 1; for (int i = 0; i < 8 * Integer.BYTES; i++) { int curr = 1 << i; if (curr > n){ break; } res = curr; } return res; } static int reduceFraction(int x, int y) { int d= gcd(x, y); return x/d+y/d; } static boolean subset(int[] ar,int n,int sum){ if(sum==0){ return true; } if(n<0||sum<0){ return false; } return subset(ar,n-1,sum)||subset(ar,n-1,sum-ar[n]); } static boolean isPrime(int n){ if(n<=1) return false; for(int i = 2;i<=Math.sqrt(n);i++){ if (n % i == 0) return false; } return true; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } static int lcm(int a, int b) { return (a * b) / gcd(a, b); } static boolean isPowerOfTwo(long n) { if(n==0){ return false; } while (n%2==0){ n/=2; } return n==1; } static boolean isPerfectSquare(int x){ if (x >= 0) { int sr = (int)Math.sqrt(x); return ((sr * sr) == x); } return false; } static int lower_bound(int[] arr, int x) { int low_limit = 0, high_limit = arr.length, mid; while (low_limit < high_limit) { mid = (low_limit + high_limit) / 2; if (arr[mid] >= x){ high_limit = mid; }else{ low_limit = mid + 1; } } return low_limit; } static int upper_bound(int[] arr, int x) { int low_limit = 0, high_limit = arr.length, mid; while (low_limit < high_limit) { mid = (low_limit + high_limit) / 2; if (arr[mid] > x){ high_limit = mid; }else{ low_limit = mid + 1; } } return low_limit; } static int binarySearch(int[] ar,int n,int num){ int low=0,high=n-1; while (low<=high){ int mid=(low+high)/2; if(ar[mid]==num){ return mid; }else if(ar[mid]>num){ high=mid-1; }else { low=mid+1; } } return -1; } static int fib(int n) { long[][] F = new long[][]{{1,1},{1,0}}; if (n == 0){ return 0; } power(F, n-1); return (int)F[0][0]; } static void multiply(long F[][], long M[][]) { long x = (F[0][0]*M[0][0])%1000000007 + (F[0][1]*M[1][0])%1000000007; long y = (F[0][0]*M[0][1])%1000000007 + (F[0][1]*M[1][1])%1000000007; long z = (F[1][0]*M[0][0])%1000000007 + (F[1][1]*M[1][0])%1000000007; long w = (F[1][0]*M[0][1])%1000000007 + (F[1][1]*M[1][1])%1000000007; F[0][0] = x%1000000007; F[0][1] = y%1000000007; F[1][0] = z%1000000007; F[1][1] = w%1000000007; } static void power(long F[][], int n) { if( n == 0 || n == 1){ return; } long M[][] = new long[][]{{1,1},{1,0}}; power(F, n/2); multiply(F, F); if (n%2 != 0){ multiply(F, M); } } static int binaryExponentiation(int x,int n){ int ans=1; while (n>0){ if(n%2!=0){ ans=(ans*x)%MOD; } x=(x*x)%MOD; n/=2; } return ans; } static void bfs(ArrayList<Integer>[] ar,int node){ boolean[] visited=new boolean[ar.length]; Queue<Integer> queue=new LinkedList<>(); visited[node]=true; queue.add(node); while(!queue.isEmpty()){ int nn=queue.poll(); System.out.print(nn+" "); for(int n : ar[nn]) { if (!visited[n]) { visited[n] = true; queue.add(n); } } } System.out.println(); } static void dfs(ArrayList<Integer>[] ar,int node){ boolean[] visited=new boolean[ar.length]; dfsUtil(ar,node,visited); System.out.println(); } static void dfsUtil(ArrayList<Integer>[] ar,int node,boolean[] visited){ visited[node]=true; //System.out.print(node+" "); for(Integer n:ar[node]){ if(!visited[n]){ dfsUtil(ar,n,visited); } } } }
0
Non-plagiarised
69b8ffb2
ba468e1f
import java.util.*; //CODE FORCES public class anshulvmc { public static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static int gcd(int a, int b) { if (b==0) return a; return gcd(b, a%b); } public static void google(int t) { System.out.println("Case #"+t+": "); } // public static void gt(int[][] arr,int k) { // int n = arr.length+1; // k = Math.min(k,n+1); // // Node[] nodes = new Node[n]; // for(int i=0;i<n;i++) nodes[i] = new Node(); // for(int i=0;i<n-1;i++) { // int a = arr[i][0]; // int b = arr[i][1]; // System.out.println(a+" "+b); // nodes[a].adj.add(nodes[b]); // nodes[b].adj.add(nodes[a]); // } // // ArrayDeque<Node> bfs = new ArrayDeque<>(); // for(Node nn:nodes) { // if(nn.adj.size()<2) { // bfs.addLast(nn); // nn.dist=0; // } // } // // while(bfs.size()>0) { // Node nn = bfs.removeFirst(); // for(Node a : nn.adj) { // if(a.dist!=-1) continue; // a.usedDegree++; // if(a.adj.size() - a.usedDegree <= 1) { // a.dist = nn.dist+1; // bfs.addLast(a); // } // } // } // // int[] cs = new int[n+1]; // for(Node nn:nodes) { // cs[nn.dist]++; // } // for(int i=1;i<cs.length;i++) cs[i]+=cs[i-1]; // System.out.println(n-cs[k-1]); // } public static class Node{ ArrayList<Node> adj = new ArrayList<>(); int dist = -1; int usedDegree = 0; } public static void cat_mice(int dest,int[] arr) { sort(arr); int time = dest; int timeleft = dest-1; int counter=0; for(int i=arr.length-1;i>=0;i--) { int val = arr[i]; int takes = time - val; if(takes <= timeleft) { timeleft -= takes; counter++; } } System.out.println(counter); } public static void minex(int n,int[] arr) { sort(arr); int ans=arr[0]; for(int i=0;i<arr.length-1;i++) { ans = Math.max(ans,arr[i+1] - arr[i]); } System.out.println(ans); } public static void func(long start,long n) { long x = start; if(n==0){ System.out.println(x); return; } long k=n-1; long c=k/4; long rem=k%4; long ans=x; if(x%2 == 0) { ans -= 1; ans -= (c * 4); if(rem == 1) { ans += n; } else if(rem == 2) { ans += n + n-1; } else if(rem == 3){ ans += (n-2) + (n-1) - n; } } else { ans += 1; ans += (c * 4); if(rem == 1) { ans -= n; } else if(rem == 2) { ans -= n + n-1; } else if(rem == 3) { ans -= n-2 + n-1 - n; } } System.out.println(ans); } public static boolean redblue(int[] num, String chnum) { ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for(int i=0;i<chnum.length();i++) { char ch = chnum.charAt(i); if(ch == 'B') { blue.add(num[i]); } else { red.add(num[i]); } } Collections.sort(blue); Collections.sort(red); // System.out.println(blue); // System.out.println(red); for(int i=0;i<blue.size();i++) { if(blue.get(i) >= i+1) { } else { return false; } } for(int i=0;i<red.size();i++) { if(red.get(i) > i+1 + blue.size()) { // System.out.println(red.get(i)+" "+(i+1 + blue.size())); return false; } } return true; } public static void main(String args[]) { Scanner scn = new Scanner(System.in); int test = scn.nextInt(); for(int i=0;i<test;i++) { int size = scn.nextInt(); int[] arr = new int[size]; for(int j=0;j<size;j++) { arr[j] = scn.nextInt(); } String str = scn.next(); boolean f = redblue(arr,str); if(f) { System.out.println("YES"); } else { System.out.println("NO"); } } // int n = 1; // int[] dp = new int[2]; // System.out.println(fact(n,dp)); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Codeforces { public static void main(String[] args) { FastReader fastReader = new FastReader(); int t = fastReader.nextInt(); while (t-- > 0) { int n = fastReader.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = fastReader.nextInt(); } ArrayList<Integer> b = new ArrayList<>(); ArrayList<Integer> r = new ArrayList<>(); char c[] = fastReader.next().toCharArray(); for (int i = 0; i < n; i++) { if (c[i] == 'B') { b.add(a[i]); } else { r.add(a[i]); } } Collections.sort(b); Collections.sort(r); int sizeb = b.size(); boolean isValid = true; for (int i = 1 , j = 0; i <=sizeb; i++ , j++){ if (b.get(j) < i){ isValid =false; } } for (int i = sizeb+1 , j = 0; i <=n && j < r.size(); i++ , j++){ if (r.get(j) > i){ isValid =false; } } if (isValid){ System.out.println("YES"); }else{ System.out.println("NO"); } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
0
Non-plagiarised
4323e2bd
ebd8ff55
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; //import javafx.util.*; public final class B { static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<ArrayList<Integer>> g; static long mod=(long)(1e9+7); static int D1[],D2[],par[]; static boolean set[]; static int value[]; static long INF=Long.MAX_VALUE; static int dp[][]; static int N,M; static int A[][],B[][]; static int s=1; public static void main(String args[])throws IOException { int N=i(); int A[]=input(N); ArrayList<Integer> one=new ArrayList<Integer>(); ArrayList<Integer> zero=new ArrayList<Integer>(); for(int i=1; i<=N; i++) { if(A[i-1]==1)one.add(i); else zero.add(i); } int sum[][]=new int[N+5][N+5]; for(int i=1; i<=one.size(); i++) { for(int j=1; j<=zero.size(); j++) { sum[i][j]=Math.abs(one.get(i-1)-zero.get(j-1)); } //print(sum[i]); } dp=new int[N+5][N+5]; //for(int d[]:dp)Arrays.fill(d, Integer.MAX_VALUE); Arrays.fill(dp[0], 0); for(int i=1; i<=one.size(); i++) { for(int j=i; j<=zero.size(); j++) { if(i==j) { dp[i][j]=dp[i-1][j-1]+sum[i][j]; } else { dp[i][j]=Math.min(dp[i][j-1], dp[i-1][j-1]+sum[i][j]); } } } System.out.println(dp[one.size()][zero.size()]); // f(0,0,one,zero,0); //for(int d[]:dp)print(d); } static int f(int i,int j,ArrayList<Integer> one,ArrayList<Integer> zero, int s) { if(i==one.size())return s; if(j==zero.size())return Integer.MAX_VALUE; int a=one.get(i),b=zero.get(j); if(dp[a][b]==-1) { int min=Integer.MAX_VALUE; for(int t=j; t<zero.size(); t++) { min=Math.min(min, f(i+1,t+1,one,zero,s+Math.abs(b-a))); } dp[a][b]=min; } a=one.get(i);b=zero.get(j); return dp[a][b]; } static boolean isSorted(int A[]) { int N=A.length; for(int i=0; i<N-1; i++) { if(A[i]>A[i+1])return false; } return true; } static int f1(int x,ArrayList<Integer> A) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; int a=A.get(m); if(a<x)l=m; else r=m; } return l; } static int ask(int t,int i,int j,int x) { System.out.println("? "+t+" "+i+" "+j+" "+x); return i(); } static int ask(int a) { System.out.println("? 1 "+a); return i(); } static int f(int st,int end,int d) { if(st>end)return 0; int a=0,b=0,c=0; if(d>1)a=f(st+d-1,end,d-1); b=f(st+d,end,d); c=f(st+d+1,end,d+1); return value[st]+max(a,b,c); } static int max(int a,int b,int c) { return Math.max(a, Math.max(c, b)); } static int value(char X[],char Y[]) { int c=0; for(int i=0; i<7; i++) { if(Y[i]=='1' && X[i]=='0')return -1; if(X[i]=='1' && Y[i]=='0')c++; } return c; } static boolean isValid(int i,int j) { if(i<0 || i>=N)return false; if(j<0 || j>=M)return false; return true; } static long fact(long N) { long num=1L; while(N>=1) { num=((num%mod)*(N%mod))%mod; N--; } return num; } static boolean reverse(long A[],int l,int r) { while(l<r) { long t=A[l]; A[l]=A[r]; A[r]=t; l++; r--; } if(isSorted(A))return true; else return false; } static boolean isSorted(long A[]) { for(int i=1; i<A.length; i++)if(A[i]<A[i-1])return false; return true; } static boolean isPalindrome(char X[],int l,int r) { while(l<r) { if(X[l]!=X[r])return false; l++; r--; } return true; } static long min(long a,long b,long c) { return Math.min(a, Math.min(c, b)); } static void print(int a) { System.out.println("! "+a); } static int ask(int a,int b) { System.out.println("? "+a+" "+b); return i(); } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { a=find(a); b=find(b); if(a!=b) { par[a]+=par[b]; //transfers the size par[b]=a; //changes the parent } } static void swap(char A[],int a,int b) { char ch=A[a]; A[a]=A[b]; A[b]=ch; } static void sort(long[] a) //check for long { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void setGraph(int N) { g=new ArrayList<ArrayList<Integer>>(); for(int i=0; i<=N; i++) { g.add(new ArrayList<Integer>()); } } static long pow(long a,long b) { long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } //Debugging Functions Starts static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } //Debugging Functions END //---------------------- //IO FUNCTIONS STARTS static HashMap<Integer,Integer> Hash(int A[]) { HashMap<Integer,Integer> mp=new HashMap<>(); for(int a:A) { int f=mp.getOrDefault(a,0)+1; mp.put(a, f); } return mp; } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } //IO FUNCTIONS END } class node implements Comparable<node> { int l,r,index; node(int l,int r,int index) { this.l=l; this.r=r; this.index=index; } public int compareTo(node X) { return X.r-this.r; } } class node1 implements Comparable<node1> { int l,r,index; node1(int l,int r,int index) { this.l=l; this.r=r; this.index=index; } public int compareTo(node1 X) { if(this.l==X.l) return X.r-this.r; return this.l-X.l; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader 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()); } //gey double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
import java.io.*; import java.util.*; public class Armchairs { static final int INF = 1000000000; public static void main(String[] args) { InputReader reader = new InputReader(System.in); PrintWriter writer = new PrintWriter(System.out, false); int N = reader.nextInt(); int[] A = new int[N]; for (int i = 0; i < N; i++) { A[i] = reader.nextInt(); } List<Integer> occupied = new ArrayList<>(); for (int i = 0; i < N; i++) { if (A[i] == 1) occupied.add(i); } int K = occupied.size(); int[][] dp = new int[N + 1][K + 1]; for (int[] row : dp) Arrays.fill(row, INF); dp[0][0] = 0; for (int i = 0; i < N; i++) { for (int j = 0; j <= K; j++) { int x = j < K ? occupied.get(j) : 0; dp[i + 1][j] = Math.min(dp[i + 1][j], dp[i][j]); if (j + 1 <= K && A[i] == 0) { dp[i + 1][j + 1] = Math.min(dp[i + 1][j + 1], dp[i][j] + Math.abs(i - x)); } } } writer.println(dp[N][K]); writer.close(); System.exit(0); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
0
Non-plagiarised
01c915a2
4e5ee0f7
/* TO LEARN 1-segment trees 2-euler tour */ /* TO SOLVE uva 1103 codeforces 722 kavi on pairing duty */ /* bit manipulation shit 1-Computer Systems: A Programmer's Perspective 2-hacker's delight 3-(02-03-bits-ints) 4-machine-basics 5-Bits Manipulation tutorialspoint */ /* TO WATCH 1-what is bitmasking by kartik arora youtube */ import java.util.*; import java.math.*; import java.io.*; import java.util.stream.Collectors; public class A{ static InputStream inputStream = System.in; static FastScanner scan=new FastScanner(); public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); static final long p=29; static final long mod=(long)1e9+9; static int n,m; static String spotty[],plain[]; static boolean check(int l) { Map<Long,Set<Pair>>hashes=new HashMap<Long,Set<Pair>>(); HashSet<Long>hashes2=new HashSet<Long>(); long pow_p[]=new long[m]; pow_p[0]=1; for(int i=1;i<m;i++) pow_p[i]=(pow_p[i-1]*p); for(int i=0;i<n;i++) { String s=plain[i]; long h[]=new long[m+1]; for(int j=0;j<m;j++) { h[j+1]=(h[j]+(s.charAt(j)-'a'+1)*pow_p[j]); } for(int j=0;j<=m-l;j++) { long cur_h=(h[j+l]-h[j]); cur_h=(cur_h*pow_p[m-j-1]); if(hashes.containsKey(cur_h)) hashes.get(cur_h).add(new Pair(j,j+l)); else { hashes.put(cur_h,new HashSet()); hashes.get(cur_h).add(new Pair(j,j+l)); } //map.getOrDefault(cur_h,new ArrayList()).add(new Pair(j,j+1)); //hashes.put(cur_h,); // out.println("FIR "+cur_h); } } for(int i=0;i<n;i++) { String s=spotty[i]; long h[]=new long[m+1]; for(int j=0;j<m;j++) { h[j+1]=(h[j]+(s.charAt(j)-'a'+1)*pow_p[j]); } for(int j=0;j<=m-l;j++) { long cur_h=(h[j+l]-h[j]); cur_h=(cur_h*pow_p[m-j-1]); if(hashes.containsKey(cur_h)){ if(!hashes.get(cur_h).contains(new Pair(j,j+l))) return true; } else{ out.println(j+" "+i); return true; } } } return false; } public static void main(String[] args) throws Exception { ///scan=new FastScanner("cownomics.in"); //out = new PrintWriter("cownomics.out"); /* currently doing 1-digit dp 2-ds like fenwick and interval tree and sparse table */ /* READING 1-Everything About Dynamic Programming codeforces 2-DYNAMIC PROGRAMMING: FROM NOVICE TO ADVANCED topcoder 3-Introduction to DP with Bitmasking codefoces 4-Bit Manipulation hackerearth 5-read more about mobious and inculsion-exclusion */ int tt=1; tt=scan.nextInt(); int T=tt; /*Map<Integer,Pair>map=new HashMap<Integer,Pair>(); for(int i=1;i<=333333334;i++) { map.put(i+2*i,new Pair(i,2*i)); map.put(i+(2*(i+1)),new Pair(i,i+1)); map.put(i+1+(2*i),new Pair(i+1,i)); }*/ outer:while(tt-->0) { int n=scan.nextInt(); int cnt[][]=new int[n][5]; String arr[]=new String[n]; int sumlens=0; for(int i=0;i<n;i++) { String s=scan.next(); sumlens+=s.length(); arr[i]=s; for(int j=0;j<s.length();j++) cnt[i][s.charAt(j)-'a']++; } int res=0; for(int to=0;to<5;to++) { ArrayList<Pair>tmp=new ArrayList<Pair>(); int sumall=0; for(int i=0;i<n;i++) sumall+=cnt[i][to]; for(int i=0;i<n;i++) { int sum=0; for(int j=0;j<5;j++) { if(j!=to) { sum+=cnt[i][j]; } } tmp.add(new Pair(arr[i].length()-sum,sum)); } Collections.sort(tmp); int THIS=sumall,THAT=sumlens-sumall; //if(to==3) // out.println(tmp); for(int i=0;i<tmp.size();i++) { if(THIS>THAT) { // if(to==3) // out.println("AHA "+(n-i)); res=Math.max(res,n-i); break; } THIS-=tmp.get(i).x; THAT-=tmp.get(i).y; } } out.println(res); } out.close(); } static class special{ boolean bool; int n; //int id; special(boolean bool,int n) { this.bool=bool; this.n=n; } @Override public int hashCode() { int hash = 7; hash = 31 * hash + (int) n; return hash; } @Override public boolean equals(Object o){ if (o == this) return true; if (o.getClass() != getClass()) return false; special t = (special)o; return t.bool == bool && t.n == n; } } static long binexp(long a,long n) { if(n==0) return 1; long res=binexp(a,n/2); if(n%2==1) return res*res*a; else return res*res; } static long powMod(long base, long exp, long mod) { if (base == 0 || base == 1) return base; if (exp == 0) return 1; if (exp == 1) return (base % mod+mod)%mod; long R = (powMod(base, exp/2, mod) % mod+mod)%mod; R *= R; R %= mod; if ((exp & 1) == 1) { return (base * R % mod+mod)%mod; } else return (R %mod+mod)%mod; } static double dis(double x1,double y1,double x2,double y2) { return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } static long mod(long x,long y) { if(x<0) x=x+(-x/y+1)*y; return x%y; } public static long pow(long b, long e) { long r = 1; while (e > 0) { if (e % 2 == 1) r = r * b ; b = b * b; e >>= 1; } return r; } private static void sort(long[] arr) { List<Long> list = new ArrayList<>(); for (long object : arr) list.add(object); Collections.sort(list); //Collections.reverse(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } private static void sort2(long[] arr) { List<Long> list = new ArrayList<>(); for (Long object : arr) list.add(object); Collections.sort(list); Collections.reverse(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } static class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } static class Pair implements Comparable<Pair>{ public long x, y,z; public Pair(long x1, long y1,long z1) { x=x1; y=y1; z=z1; } public Pair(long x1, long y1) { x=x1; y=y1; } @Override public int hashCode() { return (int)(x + 31 * y); } public String toString() { return x + " " + y+" "+z; } @Override public boolean equals(Object o){ if (o == this) return true; if (o.getClass() != getClass()) return false; Pair t = (Pair)o; return t.x == x && t.y == y&&t.z==z; } public int compareTo(Pair o) { return (int)((o.y-o.x)-(y-x)); } } }
import java.io.*; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.util.Collections; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.*; public class Main1 { public static void main(String[] args) throws IOException { // try { // Scanner in = new Scanner(System.in) ; FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt() ; while (t-- > 0){ int n = in.nextInt() ; int dp[][] = new int[n][5] ; String tt[] = new String[n] ; for (int i = 0; i <n ; i++) { String s= in.next() ; tt[i] = s ; for (int j = 0; j <s.length() ; j++) { dp[i][s.charAt(j)-'a']++ ; } } int max = 0 ; for (int i = 0; i <5 ; i++) { ArrayList<Integer>list = new ArrayList<>() ; for (int j = 0; j <n ; j++) { list.add(dp[j][i] - (tt[j].length()-dp[j][i]) ); } list.sort(Collections.reverseOrder()); int ans = 0 ; int sum = 0 ; for (int curr : list){ sum+= curr ; if (sum > 0){ ans++ ; max = max(max , ans) ; } else break; } } System.out.println(max); } out.flush(); out.close(); // } catch (Exception e) { // return; // } } static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } static void sort(int ar[]) { int n = ar.length; ArrayList<Integer> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static void sort1(long ar[]) { int n = ar.length; ArrayList<Long> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static long ncr(long n, long r, long mod) { if (r == 0) return 1; long val = ncr(n - 1, r - 1, mod); val = (n * val) % mod; val = (val * modInverse(r, mod)) % mod; return val; } static long fast_pow(long base, long n, long M) { if (n == 0) return 1; if (n == 1) return base % M; long halfn = fast_pow(base, n / 2, M); if (n % 2 == 0) return (halfn * halfn) % M; else return (((halfn * halfn) % M) * base) % M; } static long modInverse(long n, long M) { return fast_pow(n, M - 2, M); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
0
Non-plagiarised
1c90c367
e52863b8
/* /$$$$$ /$$$$$$ /$$ /$$ /$$$$$$ |__ $$ /$$__ $$ |$$ |$$ /$$__ $$ | $$| $$ \ $$| $$|$$| $$ \ $$ | $$| $$$$$$$$| $$ / $$/| $$$$$$$$ / $$ | $$| $$__ $$ \ $$ $$/ | $$__ $$ | $$ | $$| $$ | $$ \ $$$/ | $$ | $$ | $$$$$$/| $$ | $$ \ $/ | $$ | $$ \______/ |__/ |__/ \_/ |__/ |__/ /$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$$$ /$$$$$$$ | $$__ $$| $$__ $$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$$ /$$$| $$$ /$$$| $$_____/| $$__ $$ | $$ \ $$| $$ \ $$| $$ \ $$| $$ \__/| $$ \ $$| $$ \ $$| $$$$ /$$$$| $$$$ /$$$$| $$ | $$ \ $$ | $$$$$$$/| $$$$$$$/| $$ | $$| $$ /$$$$| $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$ $$/$$ $$| $$$$$ | $$$$$$$/ | $$____/ | $$__ $$| $$ | $$| $$|_ $$| $$__ $$| $$__ $$| $$ $$$| $$| $$ $$$| $$| $$__/ | $$__ $$ | $$ | $$ \ $$| $$ | $$| $$ \ $$| $$ \ $$| $$ | $$| $$\ $ | $$| $$\ $ | $$| $$ | $$ \ $$ | $$ | $$ | $$| $$$$$$/| $$$$$$/| $$ | $$| $$ | $$| $$ \/ | $$| $$ \/ | $$| $$$$$$$$| $$ | $$ |__/ |__/ |__/ \______/ \______/ |__/ |__/|__/ |__/|__/ |__/|__/ |__/|________/|__/ |__/ */ import java.io.BufferedReader; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; import java.io.*; public class abc { static PrintWriter pw; static long x = 1, y = 1; /* * static long inv[]=new long[1000001]; static long dp[]=new long[1000001]; */ /// MAIN FUNCTION/// public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); pw = new PrintWriter(System.out); // use arraylist as it uses the concept of dynamic table(amortized analysis) // Arrays.stream(array).forEach(a -> Arrays.fill(a, 0)); /* List<Integer> l1 = new ArrayList<Integer>(); */ // Random rand = new Random(); int tst = sc.nextInt(); while(tst-->0) { int n=sc.nextInt(); int app[]=new int[n]; int h[]=new int[n]; for(int i=0;i<n;i++) { app[i]=sc.nextInt(); } for(int i=0;i<n;i++) { h[i]=sc.nextInt(); } long man = 0; long last = app[n - 1] - h[n - 1] + 1; int end = n-1; for (int i = n-2; i >=0; i--) { if(app[i]>=last) { last = Math.min(last,app[i] - h[i] + 1); } else { long s = app[end]-last+1; man += (s*(s+1))/2; end = i; last = app[i] - h[i] + 1;; } } long s = app[end]-last+1; man += (s*(s+1))/2; pw.println(man); } pw.flush(); } public static long eculidean_gcd(long a, long b) { if (a == 0) { x = 0; y = 1; return b; } long ans = eculidean_gcd(b % a, a); long x1 = x; x = y - (b / a) * x; y = x1; return ans; } public static int sum(int n) { int sum = 1; if (n == 0) return 0; while (n != 0) { sum = sum * n; n = n - 1; } return sum; } public static boolean isLsbOne(int n) { if ((n & 1) != 0) return true; return false; } public static pair helper(int arr[], int start, int end, int k, pair dp[][]) { if (start >= end) { if (start == end) return (new pair(arr[start], 0)); else return (new pair(0, 0)); } if (dp[start][end].x != -1 && dp[start][end].y != -1) { return dp[start][end]; } pair ans = new pair(0, Integer.MAX_VALUE); for (int i = start; i < end; i++) { pair x1 = helper(arr, start, i, k, dp); pair x2 = helper(arr, i + 1, end, k, dp); long tip = k * (x1.x + x2.x) + x1.y + x2.y; if (tip < ans.y) ans = new pair(x1.x + x2.x, tip); } return dp[start][end] = ans; } public static void debugger() { Random rand = new Random(); int tst = (int) (Math.abs(rand.nextInt()) % 2 + 1); pw.println(tst); while (tst-- > 0) { int n = (int) (Math.abs(rand.nextInt()) % 5 + 1); pw.println(n); for (int i = 0; i < n; i++) { pw.print((int) (Math.abs(rand.nextInt()) % 6 + 1) + " "); } pw.println(); } } static int UpperBound(long a[], long x) {// x is the key or target value int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } static int LowerBound(long a[], long x) { // x is the target value or key int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } static void recursion(int n) { if (n == 1) { pw.print(n + " "); return; } // pw.print(n+" "); gives us n to 1 recursion(n - 1); // pw.print(n+" "); gives us 1 to n } // ch.charAt(i)+"" converts into a char sequence 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; } /* CREATED BY ME */ 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; } } public static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public static boolean isPrime(long n) { if (n == 2) return true; long i = 2; while (i * i <= n) { if (n % i == 0) return false; i++; } return true; } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static double max(double x, double y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int abs(int x) { return Math.abs(x); } static long abs(long x) { return Math.abs(x); } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } public static class pair { long x; long y; public pair(long a, long b) { x = a; y = b; } } public static class Comp implements Comparator<pair> { public int compare(pair a, pair b) { long ans = a.x - b.x; if (ans > 0) return 1; if (ans < 0) return -1; return 0; } } // modular exponentiation public static long fastExpo(long a, int n, int mod) { if (n == 0) return 1; else { if ((n & 1) == 1) { long x = fastExpo(a, n / 2, mod); return (((a * x) % mod) * x) % mod; } else { long x = fastExpo(a, n / 2, mod); return (((x % mod) * (x % mod)) % mod) % mod; } } } public static long modInverse(long n, int p) { return fastExpo(n, p - 2, p); } /* * public static void extract(ArrayList<Integer> ar, int k, int d) { int c = 0; * for (int i = 1; i < k; i++) { int x = 0; boolean dm = false; while (x > 0) { * long dig = x % 10; x = x / 10; if (dig == d) { dm = true; break; } } if (dm) * ar.add(i); } } */ public static int[] prefixfuntion(String s) { int n = s.length(); int z[] = new int[n]; for (int i = 1; i < n; i++) { int j = z[i - 1]; while (j > 0 && s.charAt(i) != s.charAt(j)) j = z[j - 1]; if (s.charAt(i) == s.charAt(j)) j++; z[i] = j; } return z; } // counts the set(1) bit of a number public static long countSetBitsUtil(long x) { if (x <= 0) return 0; return (x % 2 == 0 ? 0 : 1) + countSetBitsUtil(x / 2); } //tells whether a particular index has which bit of a number public static int getIthBitsUtil(int x, int y) { return (x & (1 << y)) != 0 ? 1 : 0; } public static void swaper(long x, long y) { x = x ^ y; y = y ^ x; x = x ^ y; } public static double decimalPlaces(double sum) { DecimalFormat df = new DecimalFormat("#.00"); String angleFormated = df.format(sum); double fin = Double.parseDouble(angleFormated); return fin; } //use collections.swap for swapping static boolean isSubSequence(String str1, String str2, int m, int n) { int j = 0; for (int i = 0; i < n && j < m; i++) if (str1.charAt(j) == str2.charAt(i)) j++; return (j == m); } static long sum(long n) { long s2 = 0, max = -1, min = 10; while (n > 0) { s2 = (n % 10); min = min(s2, min); max = max(s2, max); n = n / 10; } return max * min; } static long pow(long base, long power) { if (power == 0) { return 1; } long result = pow(base, power / 2); if (power % 2 == 1) { return result * result * base; } return result * result; } // return the hash value of a string static long compute_hash(String s) { long val = 0; long p = 31; long mod = (long) (1000000007); long pow = 1; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); val = (val + (int) (ch - 'a' + 1) * pow) % mod; pow = (pow * p) % mod; } return val; } }
import java.util.*; import java.io.*; public class q3{ static FastScanner fs = new FastScanner(); static PrintWriter pw = new PrintWriter(System.out); static int[] sec; static int[] health; static int[] pp; public static void main(String[] args){ int T = fs.nextInt(); for (int tt=0;tt<T;tt++){ int n = fs.nextInt(); sec = new int[n]; health = new int[n]; for (int i=0;i<n;i++) sec[i] = fs.nextInt(); for (int i=0;i<n;i++) health[i] = fs.nextInt(); pp = new int[]{sec[n-1]-health[n-1]+1, sec[n-1], health[n-1]}; long sum = 0; for (int i=n-2;i>=0;i--){ int left = sec[i] - health[i] + 1, right = sec[i], top = health[i]; if (right < pp[0]){ sum += ((long)pp[2] + 1) * (long)pp[2] / 2; pp = new int[]{left, right, top}; } else if (left < pp[0]){ pp[0] = left; pp[2] = top + (pp[1] - right); } } sum += ((long)pp[2] + 1) * (long)pp[2] / 2; pw.println(sum); } pw.close(); } // ----------input function---------- static void sort(int[] a){ ArrayList<Integer> L = new ArrayList<>(); for (int i : a) L.add(i); Collections.sort(L); for (int i = 0; i < a.length; i++) a[i] = L.get(i); } static class FastScanner{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next(){ while (!st.hasMoreTokens()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } int[] readArray(int n){ int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong(){ return Long.parseLong(next()); } } }
0
Non-plagiarised
4e5ee0f7
d6fb3b9e
import java.io.*; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.util.Collections; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.*; public class Main1 { public static void main(String[] args) throws IOException { // try { // Scanner in = new Scanner(System.in) ; FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt() ; while (t-- > 0){ int n = in.nextInt() ; int dp[][] = new int[n][5] ; String tt[] = new String[n] ; for (int i = 0; i <n ; i++) { String s= in.next() ; tt[i] = s ; for (int j = 0; j <s.length() ; j++) { dp[i][s.charAt(j)-'a']++ ; } } int max = 0 ; for (int i = 0; i <5 ; i++) { ArrayList<Integer>list = new ArrayList<>() ; for (int j = 0; j <n ; j++) { list.add(dp[j][i] - (tt[j].length()-dp[j][i]) ); } list.sort(Collections.reverseOrder()); int ans = 0 ; int sum = 0 ; for (int curr : list){ sum+= curr ; if (sum > 0){ ans++ ; max = max(max , ans) ; } else break; } } System.out.println(max); } out.flush(); out.close(); // } catch (Exception e) { // return; // } } static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } static void sort(int ar[]) { int n = ar.length; ArrayList<Integer> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static void sort1(long ar[]) { int n = ar.length; ArrayList<Long> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static long ncr(long n, long r, long mod) { if (r == 0) return 1; long val = ncr(n - 1, r - 1, mod); val = (n * val) % mod; val = (val * modInverse(r, mod)) % mod; return val; } static long fast_pow(long base, long n, long M) { if (n == 0) return 1; if (n == 1) return base % M; long halfn = fast_pow(base, n / 2, M); if (n % 2 == 0) return (halfn * halfn) % M; else return (((halfn * halfn) % M) * base) % M; } static long modInverse(long n, long M) { return fast_pow(n, M - 2, M); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
import java.util.*; public class Sol { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int a[][]=new int[n][5]; int tot[]=new int[n]; for(int i=0;i<n;i++) { String x = sc.next(); for(int j=0;j<x.length();j++) a[i][x.charAt(j)-'a'] += 1; tot[i]=x.length(); } int max=Integer.MIN_VALUE; for(int i=0;i<5;i++) max=Math.max(max,function(a,n,i,tot)); System.out.println(max); } } static int function(int a[][],int n,int i,int tot[]) { Integer ans[] = new Integer[n]; for(int j=0;j<n;j++) ans[j]=a[j][i]-(tot[j]-a[j][i]); int res=0,j=0; Arrays.sort(ans,Collections.reverseOrder()); while(j<n&&res+ans[j]>0) res+=ans[j++]; return j; } }
0
Non-plagiarised
b185d034
ccc8ef27
import java.io.*; import java.util.*; public class A734C { public static void main(String[] args) { JS scan = new JS(); int t = scan.nextInt(); loop:while(t-->0){ int n = scan.nextInt(); String[] arr= new String[n]; Integer[][] counts = new Integer[5][n]; for(int i = 0;i<5;i++){ for(int j = 0;j<n;j++){ counts[i][j] = 0; } } for(int i =0;i<n;i++){ arr[i] = scan.next(); int[] freq =new int[5]; for(int j = 0;j<arr[i].length();j++){ freq[arr[i].charAt(j)-'a']++; } for(int j = 0;j<5;j++){ counts[j][i] = freq[j]-(arr[i].length()-freq[j]); } } int best = 0; for(int i = 0;i<5;i++){ Arrays.sort(counts[i]); int curr = 0; int extra = 0; for(int j = n-1;j>=0;j--){ extra+=counts[i][j]; if(extra>0)curr++; } best = Math.max(best,curr); } System.out.println(best); } } static class JS { public int BS = 1 << 16; public char NC = (char) 0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public long nextLong() { num = 1; boolean neg = false; if (c == NC) c = nextChar(); for (; (c < '0' || c > '9'); c = nextChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = nextChar()) { res = (res << 3) + (res << 1) + c - '0'; num *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / num; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c > 32) { res.append(c); c = nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c != '\n') { res.append(c); c = nextChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = nextChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
import java.util.*; public class Sol { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int a[][]=new int[n][5]; int tot[]=new int[n]; for(int i=0;i<n;i++) { String x = sc.next(); for(int j=0;j<x.length();j++) a[i][x.charAt(j)-'a'] += 1; tot[i]=x.length(); } int max=Integer.MIN_VALUE; for(int i=0;i<5;i++) max=Math.max(max,function(a,n,i,tot)); System.out.println(max); } } static int function(int a[][],int n,int i,int tot[]) { Integer ans[] = new Integer[n]; for(int j=0;j<n;j++) ans[j]=a[j][i]-(tot[j]-a[j][i]); int res=0,j=0; Arrays.sort(ans,Collections.reverseOrder()); while(j<n&&res+ans[j]>0) res+=ans[j++]; return j; } }
0
Non-plagiarised
5a81e159
d82fa7e3
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.StringTokenizer; public class C_CF { public static void main(String[] args) { FastScanner57 fs = new FastScanner57(); PrintWriter pw = new PrintWriter(System.out); int t = fs.ni(); //int t = 1; for (int tc = 0; tc < t; tc++) { int n = fs.ni(); long[][] lr = new long[n][2]; for (int i = 0; i < n; i++) { lr[i][0] = fs.nl(); lr[i][1] = fs.nl(); } List<List<Integer>> list = new ArrayList(); for (int i = 0; i < n;i ++) { List<Integer> temp = new ArrayList(); list.add(temp); } for (int i = 0; i < n-1; i++) { int a = fs.ni()-1, b = fs.ni()-1; list.get(a).add(b); list.get(b).add(a); } Long[][] dp = new Long[n][2]; pw.println(recur(0,0,-1,new long[] {0,0},dp,lr,list)); } pw.close(); } // 0 -> left was chosen // 1 -> right was chosen public static long recur(int ind, int p,int prev, long[] v, Long[][] dp, long[][] lr,List<List<Integer>> list) { long last = v[0]; long ls = 0L; long rs = 0L; if (p==1) { last = v[1]; } if (ind!=0) ls += (long)Math.abs(last-lr[ind][0]); if (ind!=0) rs += (long)Math.abs(last-lr[ind][1]); if (dp[ind][p]!=null) return dp[ind][p]; long[] cur = lr[ind]; List<Integer> temp = list.get(ind); for (int val : temp) { if (prev==val) continue; ls += recur(val,0,ind,cur,dp,lr,list); rs += recur(val,1,ind,cur,dp,lr,list); } return dp[ind][p] = Math.max(ls,rs); } public static void sort(long[] a) { List<Long> list = new ArrayList(); for (int i = 0; i < a.length; i++) { list.add(a[i]); } Collections.sort(list); for (int i = 0; i < a.length; i++) { a[i] = list.get(i); } } public static long gcd(long n1, long n2) { if (n2 == 0) { return n1; } return gcd(n2, n1 % n2); } } class UnionFind16 { int[] id; public UnionFind16(int size) { id = new int[size]; for (int i = 0; i < size; i++) { id[i] = i; } } public int find(int p) { int root = p; while (root != id[root]) { root = id[root]; } while (p != root) { int next = id[p]; id[p] = root; p = next; } return root; } public void union(int p, int q) { int a = find(p), b = find(q); if (a == b) { return; } id[b] = a; } } class FastScanner57 { BufferedReader br; StringTokenizer st; public FastScanner57() { br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } int[] intArray(int N) { int[] ret = new int[N]; for (int i = 0; i < N; i++) { ret[i] = ni(); } return ret; } long nl() { return Long.parseLong(next()); } long[] longArray(int N) { long[] ret = new long[N]; for (int i = 0; i < N; i++) { ret[i] = nl(); } return ret; } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; 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); CParsasHumongousTree solver = new CParsasHumongousTree(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class CParsasHumongousTree { int n; long[][] dp; int[] l; int[] r; ArrayList<Integer>[] a; public void solve(int testNumber, FastReader in, PrintWriter out) { n = in.nextInt(); l = new int[n]; r = new int[n]; dp = new long[n][2]; a = new ArrayList[n]; for (int i = 0; i < n; ++i) { a[i] = new ArrayList<>(); } for (int i = 0; i < n; ++i) { l[i] = in.nextInt(); r[i] = in.nextInt(); } for (int i = 0; i < n - 1; ++i) { int u = in.nextInt() - 1, v = in.nextInt() - 1; a[u].add(v); a[v].add(u); } dfs(0, -1); out.println(Math.max(dp[0][0], dp[0][1])); } void dfs(int u, int p) { dp[u][0] = dp[u][1] = 0; for (int v : a[u]) { if (v != p) { dfs(v, u); dp[u][0] += Math.max(dp[v][0] + Math.abs(l[u] - l[v]), dp[v][1] + Math.abs(l[u] - r[v])); dp[u][1] += Math.max(dp[v][0] + Math.abs(r[u] - l[v]), dp[v][1] + Math.abs(r[u] - r[v])); } } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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); } } }
0
Non-plagiarised
1ea771ea
ee270b2a
import java.io.*; import java.util.*; public class CODECHEF { static class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } static long MOD=1000000000; static class Pair{ long a; int b; Pair(long i,int j){ a=i; b=j; } } static long[] solve(int[] pos,long[] arr,int n,int k){ long[] ans=new long[n]; long[] left=new long[n]; long[] right=new long[n]; long min=Integer.MAX_VALUE; for(int i=0;i<n;i++){ min=Math.min(min+1,arr[i]); left[i]=min; } min=Integer.MAX_VALUE; for(int i=n-1;i>=0;i--){ min=Math.min(min+1,arr[i]); right[i]=min; } for(int i=0;i<n;i++){ ans[i]=Math.min(left[i],right[i]); } return ans; } public static void main(String[] args) throws java.lang.Exception { FastReader fs=new FastReader(System.in); // StringBuilder sb=new StringBuilder(); // PrintWriter out=new PrintWriter(System.out); int t=fs.nextInt(); while (t-->0){ int n=fs.nextInt(); int k=fs.nextInt(); int[] pos=new int[k]; for(int i=0;i<k;i++) pos[i]=fs.nextInt()-1; long[] temp=new long[n]; int ptr=0; Arrays.fill(temp,Integer.MAX_VALUE); for(int i=0;i<k;i++) temp[pos[ptr++]]=fs.nextLong(); long[] ans=solve(pos,temp,n,k); for(int i=0;i<n;i++) System.out.print(ans[i]+" "); System.out.println(); } //out.close; } }
import java.util.*; public class D{ private static Scanner scanner = new Scanner(System.in); public static void main(String[] args){ int q = scanner.nextInt(); while(q-- > 0){ int n = scanner.nextInt(), k = scanner.nextInt(); int[] a = new int[k]; for(int i=0;i<k;i++){ a[i] = scanner.nextInt(); } int[] t = new int[k]; for(int j=0;j<k;j++){ t[j] = scanner.nextInt(); } long[] L = new long[n]; long[] R = new long[n]; for(int i=0;i<n;i++){ L[i] = Integer.MAX_VALUE; R[i] = Integer.MAX_VALUE; } for(int i=0;i<k;i++){ L[a[i]-1] = t[i]; R[a[i]-1] = t[i]; } long min = Integer.MAX_VALUE; for(int i=0;i<n;i++){ L[i] = Math.min(min+1,L[i]); min = L[i]; } min = Integer.MAX_VALUE; for(int i=n-1;i>=0;i--){ R[i] = Math.min(min+1,R[i]); min = R[i]; } for(int i=0;i<n;i++){ System.out.print(Math.min(L[i],R[i]) + " "); } System.out.println(); } } }
1
Plagiarised
7814575b
b55888de
import java.io.*; import java.util.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; public class C { static int fval(String s, char x){ int fx = 0, oth = 0; for(int i = 0; i<s.length(); i++){ if(x == s.charAt(i)) fx++; else oth++; } return fx-oth; } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); //int tst = 1; int tst = Integer.parseInt(br.readLine()); long mod = (long)1e9+7; while(tst-->0){ int n = Integer.parseInt(br.readLine()); ArrayList<String> lst = new ArrayList<>(); for(int i = 0; i<n; i++){ lst.add(br.readLine()); } int ans = 0; for(int i = 0; i<5; i++){ char x = (char)(97+i); ArrayList<Integer> vals = new ArrayList<>(); for(int j = 0; j<n; j++){ vals.add(fval(lst.get(j), x)); } Collections.sort(vals, Collections.reverseOrder()); int pt = -1, sum = 0; while(pt+1<n && sum+vals.get(pt+1)>0){ sum+=vals.get(++pt); } ans = max(ans, pt+1); } sb.append(ans).append('\n'); } System.out.println(sb); } } /** * min deletions * for each letter calc max len of words * Its desirable to add someting that has more of x and less of other letters -> f(x, s)=>fx-sum(fz) */
import java.lang.reflect.Array; import java.util.*; public class Rough { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-->0) { int n = sc.nextInt(); sc.nextLine(); String s[] = new String[n]; int f[][] = new int[n][5]; for (int i = 0; i < n; i++) { s[i] = sc.nextLine(); for (int j = 0; j < s[i].length(); j++) { f[i][s[i].charAt(j)-'a']++; } } int ans = 0; for ( int i = 0; i < 5; i++) { ArrayList<Integer> al = new ArrayList<>(); for (int j = 0; j < n; j++) { int o = 0; for (int k = 0; k < 5; k++) { if(k != i) o+=f[j][k]; } al.add(f[j][i]-o); } Collections.sort(al,Collections.reverseOrder()); int max = 0; int x = 0; for (int j = 0; j < n; j++) { x+=al.get(j); if(x<=0)break; max++; } ans = Math.max(max,ans); } System.out.println(ans); } sc.close(); } }
0
Non-plagiarised