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
9291ca83
b185d034
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.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; } } } }
1
Plagiarised
80881cae
f8e7b886
//package Codeforces; import java.io.*; import java.util.*; public class CP { static Scanner sc=new Scanner(System.in); public static void main(String[] args) throws IOException, CloneNotSupportedException { int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k[]=new int[n]; int h[]=new int[n]; for(int i=0;i<n;i++) k[i]=sc.nextInt(); for(int i=0;i<n;i++) h[i]=sc.nextInt(); ArrayList<Pair> interval=new ArrayList<Pair>(); ArrayList<Pair> act=new ArrayList<Pair>(); for(int i=0;i<n;i++) interval.add(new Pair(k[i]-h[i]+1,k[i])); Collections.sort(interval); // out.println(interval); act.add(interval.get(0).clone()); for(int i=1;i<n;i++) { Pair p=act.get(act.size()-1); if(p.y<interval.get(i).x) act.add(interval.get(i).clone()); else p.y=Math.max(p.y, interval.get(i).y); } // out.println(act); long mana=0; for(Pair p: act) { long x=p.y-p.x+1; mana+=(x*(x+1))/2; } System.out.println(mana); } } static class Pair implements Cloneable, Comparable<Pair> { int x,y; Pair(int a,int b) { this.x=a; this.y=b; } // @Override // public boolean equals(Object obj) // { // if(obj instanceof Pair) // { // Pair p=(Pair)obj; // return p.x==this.x && p.y==this.y; // } // return false; // } // @Override // public int hashCode() // { // return Math.abs(x)+500*Math.abs(y); // } // @Override // public String toString() // { // return "("+x+" "+y+")"; // } @Override protected Pair clone() throws CloneNotSupportedException { return new Pair(this.x,this.y); } @Override public int compareTo(Pair a) { long t=(this.x-a.x); if(t!=0) return t>0?1:-1; else return (int)(this.y-a.y); } // public void swap() // { // this.y=this.y+this.x; // this.x=this.y-this.x; // this.y=this.y-this.x; // } } }
import java.util.*; import java.io.*; import java.time.*; import static java.lang.Math.*; @SuppressWarnings("unused") public class C { static boolean DEBUG = false; static Reader fs; static PrintWriter pw; static void solve() { int n = fs.nextInt(), k[] = fs.readArray(n), h[] = fs.readArray(n); int prev_h = h[0], prev_k = k[0]; // int ans = 0; ArrayList<pair> intervals = new ArrayList<>(); for (int i = 0; i < n; i++) { int start = k[i] - h[i] + 1; int end = k[i]; intervals.add(new pair(start, end)); } // pw.println(intervals); Collections.sort(intervals); ArrayList<pair> merged = new ArrayList<>(); merge(intervals, merged); long ans = 0; for(int i = 0 ; i < merged.size() ; i++) { ans += sum(merged.get(i).len()); } pw.println(ans); } static void merge(ArrayList<pair>a1, ArrayList<pair>a2) { int n = a1.size(); int index = 0; for(int i =1 ; i < n ; i++) { if(a1.get(index).s >= a1.get(i).f) { a1.get(index).s = max(a1.get(index).s, a1.get(i).s); } else { index++; a1.set(index, a1.get(i)); } } for(int i = 0 ; i <= index ; i++) { a2.add(a1.get(i)); } // pw.println(a1); } // int index = 0; // Stores index of last element // // in output array (modified arr[]) // // // Traverse all input Intervals // for (int i=1; i<arr.length; i++) // { // // If this is not first Interval and overlaps // // with the previous one // if (arr[index].end >= arr[i].start) // { // // Merge previous and current Intervals // arr[index].end = Math.max(arr[index].end, arr[i].end); // } // else { // index++; // arr[index] = arr[i]; // } // } static boolean overlapping(pair p1, pair p2) { if((p2.f >= p1.f && p1.s >= p2.f) || (p2.s >= p1.f && p1.s >= p2.s)) { return true; } return false; } static pair merge(pair p1, pair p2) { return new pair(min(p1.f, p2.f), max(p1.s, p2.s)); } static long sum(long n) { return (n * (n + 1) / 2); } static class pair implements Comparable<pair>{ int f, s; pair(int f, int s) { this.f = f; this.s = s; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("{" + f + "," + s + "}"); return sb.toString(); } @Override public int compareTo(pair o) { return f - o.f; } public int len() { return s - f + 1; } } public static void main(String[] args) throws IOException { Instant start = Instant.now(); if (args.length == 2) { System.setIn(new FileInputStream(new File("D:\\program\\javaCPEclipse\\CodeForces\\src\\input.txt"))); // System.setOut(new PrintStream(new File("output.txt"))); System.setErr(new PrintStream(new File("D:\\program\\javaCPEclipse\\CodeForces\\src\\error.txt"))); DEBUG = true; } fs = new Reader(); pw = new PrintWriter(System.out); int t = fs.nextInt(); while (t-- > 0) { solve(); } Instant end = Instant.now(); if (DEBUG) { pw.println(Duration.between(start, end)); } pw.close(); } static void sort(int a[]) { ArrayList<Integer> l = new ArrayList<Integer>(); for (int x : a) l.add(x); Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } public static void print(long a, long b, long c, PrintWriter pw) { pw.println(a + " " + b + " " + c); return; } static class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[][] read2Array(int n, int m) { int a[][] = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextInt(); } } return a; } } }
0
Non-plagiarised
c1fef98f
fadc1365
import java.util.*; import java.io.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class Test{ static FastReader scan; static void solve(){ scan.nextLine(); int n=scan.nextInt(); int k=scan.nextInt(); int []arr=new int[n+1]; int []block=new int[k]; int []temp=new int[k]; for(int i=0;i<k;i++){ block[i]=scan.nextInt(); } for(int i=0;i<k;i++){ temp[i]=scan.nextInt(); } for(int i=0;i<k;i++){ arr[block[i]]=temp[i]; } int []dp=new int[n+1]; int prev=Integer.MAX_VALUE-1000; for(int i=1;i<=n;i++){ if(arr[i]==0){ if(prev==Integer.MAX_VALUE-1000){ dp[i]=prev; } else{ prev=prev+1; dp[i]=prev; } } else{ prev=Math.min(prev+1,arr[i]); dp[i]=prev; } } prev=Integer.MAX_VALUE-1000; for(int i=n;i>=1;i--){ if(arr[i]==0){ if(prev==Integer.MAX_VALUE-1000){ dp[i]=Math.min(prev,dp[i]); } else{ prev=prev+1; dp[i]=Math.min(prev,dp[i]); } } else{ prev=Math.min(prev+1,arr[i]); dp[i]=Math.min(prev,dp[i]); } } for(int i=1;i<=n;i++){ System.out.print(dp[i]+" "); } System.out.println(); } public static void main (String[] args) throws java.lang.Exception{ scan=new FastReader(); int t=scan.nextInt(); while(t-->0){ solve(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Pair implements Comparable<Pair>{ long wt; long idx; Pair(long x,long y){ this.wt=x; this.idx=y; } @Override public int compareTo(Pair x){ return (int)(this.wt-x.wt); } public String toString(){ return "( "+wt+" "+idx+" )"; } } static void printLong(long []arr){ for(long x:arr)System.out.print(x+" "); } static void printInt(int []arr){ for(int x:arr)System.out.print(x+" "); } static void scanInt(int []arr){ for(int i=0;i<arr.length;i++){ arr[i]=scan.nextInt(); } } static void scanLong(long []arr){ for(int i=0;i<arr.length;i++){ arr[i]=scan.nextLong(); } } static long gcd(long a, long b){ if (b == 0) return a; return gcd(b, a % b); } static long power(long x, long y, long mod){ long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0){ if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } static long add(long a,long b,long mod){ a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod; } static long sub(long a, long b,long mod){ a = a % mod; b = b % mod; return (((a - b) % mod) + mod) % mod; } static long mul(long a, long b,long mod){ a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod; } static long mminvprime(long a, long b,long mod) { return power(a, b - 2,mod); } }
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; } } }
0
Non-plagiarised
9be0602e
c4ca2ff3
import java.io.*; import java.util.*; public class Codeforce { 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 mod = (int) (1e9 + 7); public static class pair implements Comparator<pair> { int x; int y; public pair() { } public pair(int x, int y) { this.x = x; this.y = y; } @Override public int compare(pair o1, pair o2) { return o1.y - o2.y; } } public static long modularpow(long a, long b) { long res = 1; if (b == 0) return res; else { while (b > 0) { if (b % 2 == 1) { res *= a; res %= mod; } a = a * a; a %= mod; b /= 2; } return res % mod; } } public static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } public static String binary(int a) { String s1 = ""; while (a > 0) { s1 = a % 2 + s1; a /= 2; } return s1; } public static int Lower_Bound(long a[], int l, int r, long k) { while (r - l > 1) { int mid = l + (r - l) / 2; if (a[mid] <= k) l = mid; else r = mid; } return l; } public static int Upper_Bound(int a[], int l, int r, int k) { while (r - l > 1) { int mid = (l + r) / 2; if (a[mid] <= k) l = mid; else r = mid; } return l + 1; } public static int fun(int ch,int a[][],int tot[],int n){ ArrayList<Integer>ar=new ArrayList<>(); for(int i=0;i<n;i++){ ar.add((a[i][ch]-(tot[i]-a[i][ch]))); } Collections.sort(ar,Collections.reverseOrder()); int cou=0; int ans=0; for(int i:ar){ cou+=i; if(cou>0){ ans++; } else break; } return ans; } public static void main(String arg[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int n=Integer.parseInt(br.readLine()); int a[][]=new int[n][5]; int tot[]=new int[n]; for(int i=0;i<n;i++){ String s=br.readLine(); tot[i]+=s.length(); for(int j=0;j<s.length();j++){ a[i][s.charAt(j)-'a']++; } } int ans=0; for(int i=0;i<5;i++){ ans=Math.max(ans,fun(i,a,tot,n)); } sb.append(ans+"\n"); } System.out.println(sb); } }
/* 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
11c2ab99
28c2d81a
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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class D { public static void main(String[] args) { FastScanner fs = new FastScanner(); int cases = fs.nextInt(); while(cases-->0){ 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++) System.out.print(forced[i] + " "); System.out.println(); } } //----------------------------------------------------------------------------------// 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()); } char nextChar(){ return next().charAt(0); } 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 Debug{ public static void debug(long a){ System.out.println("--> "+a); } public static void debug(long a, long b){ System.out.println("--> "+ a +" + " + b); } public static void debug(char a, char b){ System.out.println("--> "+ a +" + " + b); } public static void debug(int[] array){ System.out.print("Array--> "); System.out.println(Arrays.toString(array)); } public static void debug(char[] array){ System.out.print("Array--> "); System.out.println(Arrays.toString(array)); } public static void debug(HashMap<Integer,Integer> map){ System.out.print("Map--> "+map.toString()); } } }
1
Plagiarised
086f0f90
4a570de6
import java.io.*; import java.util.*; import java.util.concurrent.ConcurrentHashMap; public class Main { public static void main(String[] args) { FastScanner sc = new FastScanner(); int t=sc.nextInt(); while (t-->=1) { int n = sc.nextInt(),m=sc.nextInt(), x = sc.nextInt(); int a[] = sc.readArray(n); PriorityQueue<Pair> pq=new PriorityQueue<>(); for (int i=0;i<m;i++){ pq.add(new Pair(0,i+1)); } int ans[]= new int[n]; System.out.println("YES"); for (int i=0;i<n;i++){ Pair e=pq.peek(); pq.remove(e); e.a+=a[i]; pq.add(e); ans[i]=e.b; // System.out.print((e.b)%m+1+" "); } printArray(ans); System.out.println(); } } // out.flush(); //------------------------------------if------------------------------------------------------------------------------------------------------------------------------------------------- //sieve static void primeSieve(int a[]){ //mark all odd number as prime for (int i=3;i<a.length;i+=2){ a[i]=1; } for (long i=3;i<a.length;i+=2){ //if the number is marked then it is prime if (a[(int) i]==1){ //mark all muliple of the number as not prime for (long j=i*i;j<a.length;j+=i){ a[(int)j]=0; } } } a[2]=1; a[1]=a[0]=0; } 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 String sortString(String s) { char temp[] = s.toCharArray(); Arrays.sort(temp); return new String(temp); } static class Pair implements Comparable<Pair> { int a; int b; public Pair(int a, int b) { this.a = a; this.b = b; } // to sort first part // public int compareTo(Pair other) { // if (this.a == other.a) return other.b > this.b ? -1 : 1; // else if (this.a > other.a) return 1; // else return -1; // } // public int compareTo(Pair other) { // if (this.b == other.b) return 0; // if (this.b < other.b) return 1; // else return -1; // } //sort on the basis of first part only public int compareTo(Pair other) { if (this.a == other.a) return 0; else if (this.a > other.a) return 1; else return -1; } } static int[] frequency(String s){ int fre[]= new int[26]; for (int i=0;i<s.length();i++){ fre[s.charAt(i)-'a']++; } return fre; } static long LOWERBOUND(long a[],long k){ int s=0,e=a.length-1; long ans=-1; while (s<=e){ int mid=(s+e)/2; if (a[mid]<=k){ ans=mid; s=mid+1; } else { e=mid-1; } } return ans; } static int mod =(int)(1e9+7); static long mod(long x) { return ((x % mod + mod) % mod); } static long add(long x, long y) { return mod(mod(x) + mod(y)); } static long mul(long x, long y) { return mod(mod(x) * mod(y)); } //Fast Power(logN) static int fastPower(long a,long n){ int ans=1; while (n>0){ long lastBit=n&1; if (lastBit==1){ ans=(int)mul(ans,a); } a=(int)mul(a,a); n>>=1; } return ans; } static int[] find(int n, int start, int diff) { int a[] = new int[n]; a[0] = start; for (int i = 1; i < n; i++) a[i] = a[i - 1] + diff; return a; } static void swap(int a, int b) { int c = a; a = b; b = c; } static void printArray(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } static boolean sorted(int a[]) { int n = a.length; boolean flag = true; for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) flag = false; } if (flag) return true; else return false; } public static int findlog(long n) { if (n == 0) return 0; if (n == 1) return 0; if (n == 2) return 1; double num = Math.log(n); double den = Math.log(2); if (den == 0) return 0; return (int) (num / den); } public static long gcd(long a, long b) { if (b % a == 0) return a; return gcd(b % a, a); } public static int gcdInt(int a, int b) { if (b % a == 0) return a; return gcdInt(b % a, a); } static void sortReverse(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); // Collections.sort.(l); Collections.sort(l, Collections.reverseOrder()); 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[] readArrayLong(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()); } } }
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(); } } }
0
Non-plagiarised
3ff25f43
a195911e
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class EDU121C { public static void main(String[] args) { JS scan = new JS(); int t = scan.nextInt(); while(t-->0){ int n = scan.nextInt(); long[] k = new long[n]; long[] h = new long[n]; for(int i =0;i<n;i++){ k[i] = scan.nextInt(); } for(int i = 0;i<n;i++){ h[i] = scan.nextInt(); } long ans = 0; long lastD = 0; long lastHP = 0; for(int i = 0;i<n;i++){ long dist = k[i]-lastD; long lo = h[i]; long hi = lastHP+dist; long hpComingOut = 0; while(lo<=hi){ long mid = (lo+hi)/2; boolean check = false; for(int j = i+1;j<n;j++){ if(k[j]-k[i]<h[j]-mid){ check = true; break; } } if(check){ //we're in trouble lo = mid+1; }else{ hi = mid-1; hpComingOut = mid; } } if(hpComingOut>dist){ ans+=summ(dist+lastHP)-summ(lastHP); lastHP+=dist; }else{ long diff = dist-hpComingOut; ans+=summ(hpComingOut); lastHP = hpComingOut; } lastD = k[i]; } System.out.println(ans); } } static long summ(long a){ return (a*(a+1))/2; } 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.*; import java.io.*; import java.math.*; public class Main { static FastReader sc=new FastReader(); static int dp[]; static boolean v[]; // static int mod=998244353;; static int mod=1000000007; static int max; static int bit[]; //static long fact[]; // static long A[]; static HashMap<Integer,Integer> map; //static StringBuffer sb=new StringBuffer(""); //static HashMap<Integer,Integer> map; static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) { // StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); outer :while (ttt-- > 0) { int n=i(); long A[]=inputL(n); long B[]=inputL(n); long C[]=new long[n]; for(int i=0;i<n;i++) { C[i]=A[i]-B[i]+1; } long min=C[n-1]; long ans=0; long last=A[n-1]; for(int i=n-1;i>=0;i--) { if(C[i]>min) { continue; } if(A[i]<min) { long y=last-min+1; ans+=y*(y+1)/2; last=A[i]; min=C[i]; continue; } min=C[i]; } long y=last-min+1; ans+=y*(y+1)/2; System.out.println(ans); } //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; int z; Pair(int x,int y){ this.x=x; this.y=y; // this.z=z; } @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; // } } static int find(int A[],int a) { if(A[a]==a) return a; return A[a]=find(A, A[a]); } //static int find(int A[],int a) { // if(A[a]==a) // return a; // return find(A, A[a]); //} //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 nextPowerOf2(int n) { n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } static int highestPowerof2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static long highestPowerof2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } 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 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 void print(int A[]) { for(int i : A) { out.print(i+" "); } 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 HashMap<Long,Integer> hash(long A[]){ HashMap<Long,Integer> map=new HashMap<Long, Integer>(); for(long 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 TreeMap<Long,Integer> tree(long A[]){ TreeMap<Long,Integer> map=new TreeMap<Long, Integer>(); for(long 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
5f3a196a
d76e3b9d
import java.io.*; import java.util.*; public class MonstersAndSpells { public static PrintWriter out; public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(br.readLine()); out=new PrintWriter(System.out); int t=Integer.parseInt(st.nextToken()); while(t-->0) { st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken());//monsters int time[]=new int[n];//time int health[]=new int[n];//health st=new StringTokenizer(br.readLine()); for(int i=0;i<n;i++) { time[i]=Integer.parseInt(st.nextToken()); } st=new StringTokenizer(br.readLine()); for(int i=0;i<n;i++) { health[i]=Integer.parseInt(st.nextToken()); } State a[]=new State[n]; for(int i=0;i<n;i++) { a[i]=new State(time[i]-health[i], time[i]); } Arrays.sort(a); long ans=0; for(int i=0;i<n;i++) { int j=i+1; int max=a[i].time; while(j<n&&a[j].x<max) { max=Math.max(max, time[j]); j++; } ans+=((long)(max-a[i].x)*(long)(max-a[i].x+1))/2; i=j-1; } out.println(ans); } out.close(); } static class State implements Comparable<State>{ int x, time; public State(int x, int time) { this.x=x;this.time=time; } public int compareTo(State o) { return x-o.x; } } }
import java.io.*; import java.util.*; public class c { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int numcases = Integer.parseInt(in.readLine()); for(int casenum = 0; casenum < numcases; casenum++){ int n = Integer.parseInt(in.readLine()); long[] t = new long[n]; long[] h = new long[n]; StringTokenizer tokenizer = new StringTokenizer(in.readLine()); for(int i = 0; i < n; i++){ t[i] = Integer.parseInt(tokenizer.nextToken()); } tokenizer = new StringTokenizer(in.readLine()); for(int i = 0; i < n; i++){ h[i] = Integer.parseInt(tokenizer.nextToken()); } long mana = 0; int index = 0; while(index < n){ long start = t[index] - h[index]; long end = t[index]; for(int i = index+1; i < n; i++){ if(t[i] - h[i] < start){ start = t[i] - h[i]; end = t[i]; index = i; } else if(t[i] - end < h[i]){ end = t[i]; index = i; } } mana += (end - start + 1) * (end - start) / 2; index++; } System.out.println(mana); } in.close(); out.close(); } }
0
Non-plagiarised
b55888de
d3a0b8d2
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(); } }
import java.io.File; import java.io.IOException; import java.util.*; public class C { public static void main(String[] args){ Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0){ int n = in.nextInt(); int[][] cnt = new int[n][5]; int[] len = new int[n]; for (int i = 0; i < n; i++){ String s = in.next(); len[i] = s.length(); for (char c : s.toCharArray()) cnt[i][c-'a']++; } int max = 0; for (int i = 0; i < 5; i++){ int[] diff = new int[n]; for (int j = 0; j < n; j++) diff[j] = cnt[j][i] - (len[j] - cnt[j][i]); Arrays.sort(diff); int j = n-2, sum = diff[n-1]; while (j>=0 && sum > 0){ max = Math.max(max, n - 1 - j); sum += diff[j]; j--; } if (sum > 0) max = Math.max(max, n); } System.out.println(max); } } }
0
Non-plagiarised
3afcc566
90dc2b20
//package codeforces.globalround18; import java.io.*; import java.util.*; import static java.lang.Math.*; //Think through the entire logic before jump into coding! //If you are out of ideas, take a guess! It is better than doing nothing! //Read both C and D, it is possible that D is easier than C for you! //Be aware of integer overflow! //If you find an answer and want to return immediately, don't forget to flush before return! public class C { static InputReader in; static PrintWriter out; public static void main(String[] args) { //initReaderPrinter(true); initReaderPrinter(false); solve(in.nextInt()); //solve(1); } static void solve(int testCnt) { for (int testNumber = 0; testNumber < testCnt; testNumber++) { int n = in.nextInt(); char[] a = in.next().toCharArray(), b = in.next().toCharArray(); int match0 = 0, match1 = 0, mismatch10 = 0, mismatch01 = 0; for(int i = 0; i < n; i++) { if(a[i] == b[i]) { if(a[i] == '0') match0++; else match1++; } else { if(a[i] == '0') mismatch01++; else mismatch10++; } } if(mismatch01 + mismatch10 == 0) out.println(0); else { if(match1 - match0 == 1 && mismatch01 == mismatch10) { out.println(min(match0 + match1, mismatch01 + mismatch10)); } else if(match1 - match0 == 1) { out.println(match0 + match1); } else if(mismatch01 == mismatch10) { out.println(mismatch01 + mismatch10); } else out.println(-1); } } out.close(); } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream("src/input.in")); out = new PrintWriter(new FileOutputStream("src/output.out")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitive(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitiveOneIndexed(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } Long[] nextLongArray(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitive(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitiveOneIndexed(int n) { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } String[] nextStringArray(int n) { String[] g = new String[n]; for (int i = 0; i < n; i++) g[i] = next(); return g; } List<Integer>[] readGraphOneIndexed(int n, int m) { List<Integer>[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); adj[u].add(v); adj[v].add(u); } return adj; } List<Integer>[] readGraphZeroIndexed(int n, int m) { List<Integer>[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; adj[u].add(v); adj[v].add(u); } return adj; } /* A more efficient way of building a graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]] = end2[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]] = end1[i]; idxForEachNode[end2[i]]++; } return adj; } } }
import java.util.*; import java.io.*; public class C1615{ static FastScanner fs = null; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); while (t-->0) { int n = fs.nextInt(); String a = fs.next(); String b = fs.next(); char ch1[] = a.toCharArray(); char ch2[] = b.toCharArray(); int c00 = 0; int c01 = 0; int c10 = 0; int c11 = 0; for(int i=0;i<n;i++){ if(ch1[i]=='0'){ if(ch2[i]=='0'){ c00+=1; } else{ c01+=1; } } else{ if(ch2[i]=='0'){ c10+=1; } else{ c11+=1; } } } int ans = -1; if((c11-c00)==1 || c10==c01){ int s1 = (int)1e7; int s2 = (int)1e7; if((c11-c00)==1){ s1 = c11+c00; } if(c10==c01) s2 = c10+c01; ans = Math.min(s1,s2); } out.println(ans); } 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()); } } }
0
Non-plagiarised
ba8800f5
d8e4eb5e
import java.io.*; import java.util.*; import java.text.*; import java.math.*; public class Main { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int ar[] = new int[n]; for(int i = 0; i < n; i++){ ar[i] = Integer.parseInt(st.nextToken()); } ArrayList<Integer> ones = new ArrayList<Integer>(); ArrayList<Integer> zeroes = new ArrayList<Integer>(); for(int i = 0; i < n; i++){ if(ar[i] == 1) ones.add(i); else zeroes.add(i); } int r = ones.size(); int c = zeroes.size(); int time[][] = new int[r][c]; System.out.println(calculateTime(time, r, c, 0, 0, ones, zeroes)); } public static int calculateTime(int time[][], int r, int c, int currR, int currC, ArrayList<Integer> ones, ArrayList<Integer> zeroes){ // System.out.println(currR + " " + currC); if(currR == r) return 0; if(currC == c) return (int)1e9; if(time[currR][currC] != 0) return time[currR][currC]; return time[currR][currC] = Math.min((calculateTime(time, r, c, currR + 1, currC + 1, ones, zeroes) + Math.abs(ones.get(currR) - zeroes.get(currC))), calculateTime(time, r, c, currR, currC + 1, ones, zeroes)); } }
import java.io.*; import java.util.*; public class E { public static void main(String[] args) throws NumberFormatException, IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int cnt = n; boolean[] non = new boolean[n]; StringTokenizer st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++) { if(Integer.parseInt(st.nextToken()) == 0) { non[i] = true; cnt--; } } int x = 0; int y = 0; int[] location = new int[cnt]; int[] rlocation = new int[n-cnt]; for(int i = 0; i < n; i++) { if(!non[i]) { location[x] = i; x++; }else{ rlocation[y] = i; y++; } } int[][] dp = new int[(n-cnt)+1][cnt+1]; Arrays.fill(dp[0], 100000000); dp[0][0] = 0; for(int i = 0; i < n-cnt; i++) { //System.out.println("HIT"); if(i < (n-cnt)) Arrays.fill(dp[i+1], 100000000); for(int j = 0; j < cnt; j++) { if(i < (n-cnt)) { dp[i+1][j] = Math.min(dp[i+1][j], dp[i][j]); dp[i+1][j+1] = Math.min(dp[i+1][j+1], dp[i][j] + Math.abs(rlocation[i] - location[j])); //System.out.println(dp[i+1][j+1] + " " + dp[i][j] + " " + j + " " + rlocation[i] + " " + location[j]); } } } int min = Integer.MAX_VALUE; for(int i = 0; i < (n-cnt)+1; i++) { min = Math.min(dp[i][cnt], min); } System.out.println(min); } }
0
Non-plagiarised
7686c854
edce3e39
import java.util.*; import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.IntToLongFunction; import java.lang.*; import java.io.*; import java.math.*; public final class CF { public static void main(String[]args)throws IOException { FastReader ob=new FastReader(); int t=ob.nextInt(); StringBuffer sb=new StringBuffer(); while(t-->0) { int n=ob.nextInt(); PriorityQueue<Long> a=new PriorityQueue<>(); PriorityQueue<Long> b=new PriorityQueue<>(); long ans=Long.MAX_VALUE; long sum=0; for(int i=0;i<n;i++) { long x=ob.nextInt(); if(i%2==0) a.add(x); else b.add(x); sum+=x; if(i!=0) ans=Math.min(ans,sum+(a.peek()*(n-a.size()))+(b.peek()*(n-b.size()))); } sb.append(ans+"\n"); } System.out.println(sb); } } class Pair implements Comparable<Pair> { int x,y; Pair(int x,int y) { this.x=x; this.y=y; } @Override public int compareTo(Pair a) { return a.y-y; } } 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 String nextLine() { String s=""; try { s=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class CF { private static FS sc = new FS(); private static class FS { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } private static class extra { static int[] intArr(int size) { int[] a = new int[size]; for(int i = 0; i < size; i++) a[i] = sc.nextInt(); return a; } static long[] longArr(int size) { long[] a = new long[size]; for(int i = 0; i < size; i++) a[i] = sc.nextLong(); return a; } static long intSum(int[] a) { long sum = 0; for(int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } static long longSum(long[] a) { long sum = 0; for(int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } } static long count = 0; public static void main(String[] args) { int t = sc.nextInt(); // int t = 1; StringBuilder ret = new StringBuilder(); while(t-- > 0) { int n = sc.nextInt(); int[] a = extra.intArr(n); PriorityQueue<Integer> c = new PriorityQueue<>(); PriorityQueue<Integer> d = new PriorityQueue<>(); long sum = 0; long min = Long.MAX_VALUE; for(int i = 0; i < n; i++) { sum += a[i]; if(i%2 == 0) c.add(a[i]); else d.add(a[i]); if(i != 0) { min = Math.min(min, sum + (n - c.size())*1L*c.peek() + (n-d.size())*1L*d.peek()); } } ret.append(min + "\n"); } System.out.println(ret); } }
1
Plagiarised
268eb6c6
3cf63146
import java.util.*; import java.io.*; public class Test { // Fast IO class static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { boolean env = System.getProperty("ONLINE_JUDGE") != null; // env=true; if (!env) { try { br = new BufferedReader(new FileReader("src/input.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } } else br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader sc = new FastReader(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int k = sc.nextInt(); int[] a = new int[k]; int[] temp = new int[n]; for (int i = 0; i < k; i++) a[i] = sc.nextInt(); for (int i = 0; i < k; i++) temp[a[i] - 1] = sc.nextInt(); int[] left = new int[n]; int[] right = new int[n]; Arrays.fill(left, Integer.MAX_VALUE - 1); Arrays.fill(right, Integer.MAX_VALUE - 1); if (temp[0] > 0) left[0] = temp[0]; for (int i = 1; i < n; i++) { left[i] = Math.min(left[i - 1] + 1, left[i]); if (temp[i] > 0) left[i] = Math.min(left[i], temp[i]); } if (temp[n - 1] > 0) right[n - 1] = temp[n - 1]; for (int i = n - 2; i >= 0; i--) { right[i] = Math.min(right[i + 1] + 1, right[i]); if (temp[i] > 0) right[i] = Math.min(right[i], temp[i]); } for (int i = 0; i < n; i++) out.print(Math.min(left[i], right[i]) + " "); out.println(); } out.flush(); out.close(); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.time.LocalDateTime; import java.util.*; public class B { static int tree[]; static int up[]; static int n; static int mod =1000000007; public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); int t=fs.nextInt(); outer: while(t-->0) { n=fs.nextInt(); int k=fs.nextInt(); int ac[]=fs.readArray(k); int temp[]=fs.readArray(k); int arr[]=new int[n]; Arrays.fill(arr, Integer.MAX_VALUE/2); for(int i=0;i<k;i++) { int ind = ac[i]-1; arr[ind]=temp[i]; } int ans[]=new int[n]; int left[]=new int[n]; Arrays.fill(left, Integer.MAX_VALUE/2); // int right[]=new int[n]; left[0]=arr[0]; // Arrays.fill(right, Integer.MAX_VALUE/2); // right[n-1]=arr[n-1]; for(int i=1;i<n;i++) { left[i]=Math.min(arr[i], left[i-1]+1); } ans[n-1]=Math.min(left[n-1],arr[n-1]); for(int i=n-2;i>=0;i--) { ans[i]=Math.min(Math.min(arr[i],left[i]),ans[i+1]+1); } // for(int i=0;i<n;i++) ans[i]=Math.min(left[i], right[i]); for(int i=0;i<n;i++) out.print(ans[i]+" "); out.println(); } out.close(); } static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } static long nck(int n,int k) { if(k>n) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long pow(long a,long b) { long res=1; while(b!=0) { if((b&1)!=0) { res*=a; res%=mod; } a*=a; a%=mod; b=b>>1; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n); int temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort Arrays.sort(a); } // Use this to input code since it is faster than a Scanner 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
43b85d63
a37923d1
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.PriorityQueue; import java.util.Map.Entry; import java.util.Random; import java.util.TreeSet; public final class CF_724_D2_D2 { static boolean verb=true; static void log(Object X){if (verb) System.err.println(X);} static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}} static void logWln(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");}} static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}} static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}} static void logWln(Object X){if (verb) System.err.print(X);} static void info(Object o){ System.out.println(o);} static void output(Object o){outputWln(""+o+"\n"); } static void outputFlush(Object o){try {out.write(""+ o+"\n");out.flush();} catch (Exception e) {}} static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}} static void logBin(int[] tm) {for (int x:tm) logWln(bin(8,x)+" ");log("");} static String bin(int L,int x) { String s=Integer.toBinaryString(x); while (s.length()<L) s="0"+s; return s; } static long powerMod(long b,long e,long m){ long x=1; while (e>0) { if (e%2==1) x=(b*x)%m; b=(b*b)%m; e=e/2; } return x; } static long mod=1000000007; // Global vars static BufferedWriter out; static InputReader reader; static int pgcd(int a,int b){ if (a<b) return pgcd(b,a); while (b!=0){ int c=b; b=a%b; a=c; } return a; } static class Node { Node left; Node right; int nl; int nr; int v; public Node(Node left, Node right, int v) { this.left = left; this.right = right; this.v = v; nl=0; nr=0; } public String toString() { return ""+v; } } static int PX=Integer.MAX_VALUE; static int NX=Integer.MIN_VALUE; static void process() throws Exception { out = new BufferedWriter(new OutputStreamWriter(System.out)); reader = new InputReader(System.in); int T=reader.readInt(); int CX=26; for (int t=0;t<T;t++) { int n=reader.readInt(); int[] b=new int[n]; for (int i=0;i<n;i++) { b[i]=reader.readInt(); } TreeSet<Integer> ts=new TreeSet<Integer>(); ts.add(b[0]); ts.add(NX); ts.add(PX); int core=b[0]; boolean ok=true; for (int i=1;i<n;i++) { int x=b[i]; if (x==core) { //ok } else if (x<core) { int y=ts.lower(core); if (y>x) { ok=false; //log("fail 1"); break; } //log("core:"+core+" x:"+x+" y:"+y); core=x; ts.add(x); } else { int y=ts.higher(core); if (y<x) { ok=false; //log("fail 2"); break; } //log("core:"+core+" x:"+x+" y:"+y); core=x; ts.add(x); } } if (ok) output("YES"); else output("NO"); } try { out.close(); } catch (Exception Ex) { } } public static void main(String[] args) throws Exception { process(); } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read(buf); if (numChars <= 0) { return -1; } } return buf[curChar++]; } public final String readString() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.append((char) c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public final String readString(int L) throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(L); do { res.append((char) c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public final int readInt() throws IOException { int c = read(); boolean neg = false; while (isSpaceChar(c)) { c = read(); } char d = (char) c; // log("d:"+d); if (d == '-') { neg = true; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); // log("res:"+res); if (neg) return -res; return res; } public final long readLong() throws IOException { int c = read(); boolean neg = false; while (isSpaceChar(c)) { c = read(); } char d = (char) c; // log("d:"+d); if (d == '-') { neg = true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); // log("res:"+res); if (neg) return -res; return res; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
import java.util.Scanner; import java.util.TreeSet; public class D { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int test = scn.nextInt(); for (int t = 0; t < test; t++) { int n = scn.nextInt(); int[] b = new int[n]; TreeSet<Integer> ts = new TreeSet<>(); int lastAdded = -1; boolean ans = true; for (int i = 0; i < n; i++) { b[i] = scn.nextInt(); } for (int val : b) { if (t == 4) { int x = 2; } if (val > lastAdded) { Integer between = ts.higher(lastAdded); if (between != null && between < val) { ans = false; break; } } if (val < lastAdded) { Integer between = ts.lower(lastAdded); if (between != null && between > val) { ans = false; break; } } ts.add(val); lastAdded = val; } if (ans) System.out.println("YES"); else System.out.println("NO"); } } boolean between(int mid, int a, int b) { return a >= mid && mid >= b || a <= mid && mid <= b; } }
0
Non-plagiarised
ac7187d8
b7a6c59c
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); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class MinimumGridPath { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); int cases = Integer.parseInt(line); for(int i = 0; i<cases; i++) { int n = Integer.parseInt(br.readLine()); long[] segments = new long[n]; Scanner sc = new Scanner(br.readLine()); for(int k = 0; k<n; k++) { segments[k] = sc.nextLong(); } long minHor = segments[0], minVer = Long.MAX_VALUE; long sumHor = segments[0], sumVer = 0; long best = Long.MAX_VALUE; for(int k = 1; k<n; k++) { if(k % 2 == 1) { minVer = Long.min(minVer, segments[k]); sumVer += segments[k]; }else { minHor = Long.min(minHor, segments[k]); sumHor += segments[k]; } long cost = sumVer + sumHor + minVer * (n - (k+1)/2) + minHor * (n- (1+k/2)); best = Long.min(best, cost); } System.out.println(best); } } }
0
Non-plagiarised
402aff07
f229aa7f
import java.util.*; public class Main { public static void main(String args[]) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0){ int n = s.nextInt(); String a = s.next(); String b = s.next(); int a1 = 0, b1 = 0; for (char c: a.toCharArray()){ if (c == '1') a1++; } for (char c: b.toCharArray()){ if (c == '1') b1++; } int ans = Integer.MAX_VALUE; int res = 0; for (int i = 0; i < n; i++) { if (a.charAt(i) != b.charAt(i)) res++; } if (a1 == b1) ans = Math.min(ans, res); if (b1 == n-a1+1) ans = Math.min(ans, n-res); 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
9399204d
b434c275
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; public final class A { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<Integer> g[]; static long mod=(long)1e9+7,INF=Long.MAX_VALUE; static boolean set[],col[]; static int par[],tot[],partial[]; static int D[],P[][]; static long dp[][],sum=0,max=0,size[]; // static node1 seg[]; //static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)}; public static void main(String args[])throws IOException { int T=i(); outer:while(T-->0) { int N=i(); int size[]=new int[N]; PriorityQueue<node1> q[]=new PriorityQueue[6]; for(int i=0; i<5; i++)q[i]=new PriorityQueue<node1>(); for(int i=0; i<N; i++) { char X[]=in.next().toCharArray(); int s=X.length; size[i]=s; int f[]=new int[5]; for(char x:X)f[x-'a']++; for(int j=0; j<5; j++)q[j].add(new node1(f[j],i,s)); } int max=0; for(int i=0; i<5; i++) { PriorityQueue<node1> q_new=new PriorityQueue<>(); q_new=q[i]; int c=0; long f=0; while(q_new.size()>0) { node1 x=q_new.remove(); // System.out.println(x.f+" "+x.size+" "+x.a); f+=x.a; if(f>0) { c++; max=Math.max(max, c); } else break; } } out.println(max); } out.close(); } static int OR(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("OR "+i+" "+j); return i(); } static int AND(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("AND "+i+" "+j); return i(); } static int XOR(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("XOR "+i+" "+j); return i(); } static boolean f1(int l,char X[]) { int i=0; for(; l<X.length; l++) { if(X[i]!=X[l])return false; i++; } return true; } static int f(int a) { for(int i=a+1; a>0; a++) { if(GCD(i,a)==1)return i; } return 0; } static int min(char X[],char str[],int N) { int s=0; for(int i=0; i<N; i++) { int it=i%3; if(X[i]!=str[it])s++; // ans.append(str[it]); } return s; } static char f(int i,char X[]) { int a=0,b=0,c=0; for(; i<X.length; i+=3) { if(X[i]=='R')a++; if(X[i]=='B')b++; if(X[i]=='G')c++; } if(a>=b && a>=c)return 'R'; if(b>=a && b>=c)return 'B'; return 'G'; } static void f1(int n,int p,long sum,int N) { for(int c:g[n]) { if(c==p)continue; long s=sum+N-2*size[c]; f1(c,n,s,N); max=Math.max(max, s); } } static long f(int i,int j,ArrayList<Integer> A) { if(i+1==A.size()) { return j; } int moves=1+A.get(i); if(j==1)return 1+f(i+1,moves,A); if(j>0 && dp[i][j-1]==0)f(i,j-1,A); return dp[i][j]=dp[i][j-1]+f(i+1,j+moves,A); } // static void build(int v,int tl,int tr,long A[]) // { // if(tl==tr) // { // seg[v]=new node1(A[tl],A[tr],1,true); // return ; // } // int tm=(tl+tr)/2; // build(2*v,tl,tm,A); // build(2*v+1,tm+1,tr,A); // seg[v]=merge(seg[2*v],seg[2*v+1]); // } // static node1 ask(int v,int tl,int tr,int l,int r) // { // if(l>r)return new node1(0,0,0,false);//verify true or false // if(tl==l && tr==r)return seg[v]; // int tm=(tl+tr)/2; // node1 a=ask(v*2,tl,tm,l,Math.min(tm, r)); // node1 b=ask(v*2+1,tm+1,tr,Math.max(tm+1, l),r); // return merge(a,b); // } // static node1 merge(node1 a,node1 b) // { // long s=0; // long l1=a.L,r1=a.R,c1=a.cnt; // long l2=b.L,r2=b.R,c2=b.cnt; // long g=GCD(l2,r1); s=c1+c2; // if(g==1) // { // s--; // g=(l2*r1)/g; // if(c1==1) // { // l1=g; // } // if(c2==1)r2=g; // return new node1(l1,r2,s,true); // } // return new node1(l1,r2,s,a.leaf^b.leaf); // } static long f(long l,long r,long a,long b,long N) { while(r-l>1) { long m=(l+r)/2; long x=m*b; if(N<x) { r=m; continue; } if((N-x)%a==0)r=m; else l=m; } return r; } static long f1(long a,long A[],long bits[],long sum) { long s=A.length; s=mul(s,a); s=(s+(sum%mod))%mod; long p=1L; for(long x:bits) { if((a&p)!=0)s=((s-mul(x,p))+mod)%mod; p<<=1; } return s; } static long f2(long a,long A[],long bits[]) { long s=0; long p=1L; for(long x:bits) { if((a&p)!=0) { s=(s+mul(p,x))%mod; } p<<=1; } return s; } static long f(long x1,long y1,long x2,long y2) { return Math.abs(x1-x2)+Math.abs(y1-y2); } static long f(long x,long max,long s) { long l=-1,r=(x/s)+1; while(r-l>1) { long m=(l+r)/2; if(x-m*s>max)l=m; else r=m; } return l+1; } static int f(long A[],long x) { int l=-1,r=A.length; while(r-l>1) { int m=(l+r)/2; if(A[m]>=x)r=m; else l=m; } return r; } static int bin(int x,ArrayList<Integer> A) { int l=0,r=A.size()-1; while(l<=r) { int m=(l+r)/2; int a=A.get(m); if(a==x)return m; if(a<x)l=m+1; else r=m-1; } return 0; } static int left(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 right(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 r; } static boolean equal(long A[],long B[]) { for(int i=0; i<A.length; i++)if(A[i]!=B[i])return false; return true; } static int max(int a ,int b,int c,int d) { a=Math.max(a, b); c=Math.max(c,d); return Math.max(a, c); } static int min(int a ,int b,int c,int d) { a=Math.min(a, b); c=Math.min(c,d); return Math.min(a, c); } 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 long mul(long a, long b) { return ( a %mod * 1L * b%mod )%mod; } static void swap(int A[],int a,int b) { int t=A[a]; A[a]=A[b]; A[b]=t; } 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]; par[b]=a; } } static boolean isSorted(int A[]) { for(int i=1; i<A.length; i++) { if(A[i]<A[i-1])return false; } return true; } static boolean isDivisible(StringBuilder X,int i,long num) { long r=0; for(; i<X.length(); i++) { r=r*10+(X.charAt(i)-'0'); r=r%num; } return r==0; } static int lower_Bound(int A[],int low,int high, int x) { if (low > high) if (x >= A[high]) return A[high]; int mid = (low + high) / 2; if (A[mid] == x) return A[mid]; if (mid > 0 && A[mid - 1] <= x && x < A[mid]) return A[mid - 1]; if (x < A[mid]) return lower_Bound( A, low, mid - 1, x); return lower_Bound(A, mid + 1, high, x); } static String f(String A) { String X=""; for(int i=A.length()-1; i>=0; i--) { int c=A.charAt(i)-'0'; X+=(c+1)%2; } return X; } 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 String swap(String X,int i,int j) { char ch[]=X.toCharArray(); char a=ch[i]; ch[i]=ch[j]; ch[j]=a; return new String(ch); } static int sD(long n) { if (n % 2 == 0 ) return 2; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0 ) return i; } return (int)n; } static void setGraph(int N) { tot=new int[N+1]; partial=new int[N+1]; D=new int[N+1]; P=new int[N+1][(int)(Math.log(N)+10)]; set=new boolean[N+1]; g=new ArrayList[N+1]; for(int i=0; i<=N; i++) { g[i]=new ArrayList<>(); D[i]=Integer.MAX_VALUE; //D2[i]=INF; } } 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 long fact(long N) { long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } 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 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(); } 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; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } } class node1 implements Comparable<node1> { int index,f,size; long a; node1(int f,int i,int size) { this.f=f; this.index=i; this.size=size; a=2*f-size; } public int compareTo(node1 x) { if(this.a==x.a)return 0; else if(this.a<x.a)return 1; else return -1; } } //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()); } 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.*; import java.math.*; import java.math.BigInteger; public final class A { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<Integer> g[]; static long mod=(long)1e9+7,INF=Long.MAX_VALUE; static boolean set[],col[]; static int par[],tot[],partial[]; static int D[],P[][]; static long dp[][],sum=0,max=0,size[]; // static node1 seg[]; //static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)}; public static void main(String args[])throws IOException { int T=i(); outer:while(T-->0) { int N=i(); int size[]=new int[N]; PriorityQueue<node1> q[]=new PriorityQueue[26]; for(int i=0; i<26; i++)q[i]=new PriorityQueue<node1>(); for(int i=0; i<N; i++) { char X[]=in.next().toCharArray(); int s=X.length; size[i]=s; int f[]=new int[26]; for(char x:X)f[x-'a']++; for(int j=0; j<26; j++)q[j].add(new node1(f[j],i,s)); } int max=0; for(int i=0; i<26; i++) { PriorityQueue<node1> q_new=new PriorityQueue<>(); q_new=q[i]; int c=0; long f=0; while(q_new.size()>0) { node1 x=q_new.remove(); // System.out.println(x.f+" "+x.size+" "+x.a); f+=x.a; if(f>0) { c++; max=Math.max(max, c); } else break; } } out.println(max); } out.close(); } static int OR(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("OR "+i+" "+j); return i(); } static int AND(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("AND "+i+" "+j); return i(); } static int XOR(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("XOR "+i+" "+j); return i(); } static boolean f1(int l,char X[]) { int i=0; for(; l<X.length; l++) { if(X[i]!=X[l])return false; i++; } return true; } static int f(int a) { for(int i=a+1; a>0; a++) { if(GCD(i,a)==1)return i; } return 0; } static int min(char X[],char str[],int N) { int s=0; for(int i=0; i<N; i++) { int it=i%3; if(X[i]!=str[it])s++; // ans.append(str[it]); } return s; } static char f(int i,char X[]) { int a=0,b=0,c=0; for(; i<X.length; i+=3) { if(X[i]=='R')a++; if(X[i]=='B')b++; if(X[i]=='G')c++; } if(a>=b && a>=c)return 'R'; if(b>=a && b>=c)return 'B'; return 'G'; } static void f1(int n,int p,long sum,int N) { for(int c:g[n]) { if(c==p)continue; long s=sum+N-2*size[c]; f1(c,n,s,N); max=Math.max(max, s); } } static long f(int i,int j,ArrayList<Integer> A) { if(i+1==A.size()) { return j; } int moves=1+A.get(i); if(j==1)return 1+f(i+1,moves,A); if(j>0 && dp[i][j-1]==0)f(i,j-1,A); return dp[i][j]=dp[i][j-1]+f(i+1,j+moves,A); } // static void build(int v,int tl,int tr,long A[]) // { // if(tl==tr) // { // seg[v]=new node1(A[tl],A[tr],1,true); // return ; // } // int tm=(tl+tr)/2; // build(2*v,tl,tm,A); // build(2*v+1,tm+1,tr,A); // seg[v]=merge(seg[2*v],seg[2*v+1]); // } // static node1 ask(int v,int tl,int tr,int l,int r) // { // if(l>r)return new node1(0,0,0,false);//verify true or false // if(tl==l && tr==r)return seg[v]; // int tm=(tl+tr)/2; // node1 a=ask(v*2,tl,tm,l,Math.min(tm, r)); // node1 b=ask(v*2+1,tm+1,tr,Math.max(tm+1, l),r); // return merge(a,b); // } // static node1 merge(node1 a,node1 b) // { // long s=0; // long l1=a.L,r1=a.R,c1=a.cnt; // long l2=b.L,r2=b.R,c2=b.cnt; // long g=GCD(l2,r1); s=c1+c2; // if(g==1) // { // s--; // g=(l2*r1)/g; // if(c1==1) // { // l1=g; // } // if(c2==1)r2=g; // return new node1(l1,r2,s,true); // } // return new node1(l1,r2,s,a.leaf^b.leaf); // } static long f(long l,long r,long a,long b,long N) { while(r-l>1) { long m=(l+r)/2; long x=m*b; if(N<x) { r=m; continue; } if((N-x)%a==0)r=m; else l=m; } return r; } static long f1(long a,long A[],long bits[],long sum) { long s=A.length; s=mul(s,a); s=(s+(sum%mod))%mod; long p=1L; for(long x:bits) { if((a&p)!=0)s=((s-mul(x,p))+mod)%mod; p<<=1; } return s; } static long f2(long a,long A[],long bits[]) { long s=0; long p=1L; for(long x:bits) { if((a&p)!=0) { s=(s+mul(p,x))%mod; } p<<=1; } return s; } static long f(long x1,long y1,long x2,long y2) { return Math.abs(x1-x2)+Math.abs(y1-y2); } static long f(long x,long max,long s) { long l=-1,r=(x/s)+1; while(r-l>1) { long m=(l+r)/2; if(x-m*s>max)l=m; else r=m; } return l+1; } static int f(long A[],long x) { int l=-1,r=A.length; while(r-l>1) { int m=(l+r)/2; if(A[m]>=x)r=m; else l=m; } return r; } static int bin(int x,ArrayList<Integer> A) { int l=0,r=A.size()-1; while(l<=r) { int m=(l+r)/2; int a=A.get(m); if(a==x)return m; if(a<x)l=m+1; else r=m-1; } return 0; } static int left(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 right(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 r; } static boolean equal(long A[],long B[]) { for(int i=0; i<A.length; i++)if(A[i]!=B[i])return false; return true; } static int max(int a ,int b,int c,int d) { a=Math.max(a, b); c=Math.max(c,d); return Math.max(a, c); } static int min(int a ,int b,int c,int d) { a=Math.min(a, b); c=Math.min(c,d); return Math.min(a, c); } 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 long mul(long a, long b) { return ( a %mod * 1L * b%mod )%mod; } static void swap(int A[],int a,int b) { int t=A[a]; A[a]=A[b]; A[b]=t; } 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]; par[b]=a; } } static boolean isSorted(int A[]) { for(int i=1; i<A.length; i++) { if(A[i]<A[i-1])return false; } return true; } static boolean isDivisible(StringBuilder X,int i,long num) { long r=0; for(; i<X.length(); i++) { r=r*10+(X.charAt(i)-'0'); r=r%num; } return r==0; } static int lower_Bound(int A[],int low,int high, int x) { if (low > high) if (x >= A[high]) return A[high]; int mid = (low + high) / 2; if (A[mid] == x) return A[mid]; if (mid > 0 && A[mid - 1] <= x && x < A[mid]) return A[mid - 1]; if (x < A[mid]) return lower_Bound( A, low, mid - 1, x); return lower_Bound(A, mid + 1, high, x); } static String f(String A) { String X=""; for(int i=A.length()-1; i>=0; i--) { int c=A.charAt(i)-'0'; X+=(c+1)%2; } return X; } 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 String swap(String X,int i,int j) { char ch[]=X.toCharArray(); char a=ch[i]; ch[i]=ch[j]; ch[j]=a; return new String(ch); } static int sD(long n) { if (n % 2 == 0 ) return 2; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0 ) return i; } return (int)n; } static void setGraph(int N) { tot=new int[N+1]; partial=new int[N+1]; D=new int[N+1]; P=new int[N+1][(int)(Math.log(N)+10)]; set=new boolean[N+1]; g=new ArrayList[N+1]; for(int i=0; i<=N; i++) { g[i]=new ArrayList<>(); D[i]=Integer.MAX_VALUE; //D2[i]=INF; } } 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 long fact(long N) { long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } 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 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(); } 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; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } } class node1 implements Comparable<node1> { int index,f,size; long a; node1(int f,int i,int size) { this.f=f; this.index=i; this.size=size; a=2*f-size; } public int compareTo(node1 x) { if(this.a==x.a)return 0; else if(this.a<x.a)return 1; else return -1; } } //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()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
1
Plagiarised
a3e272af
f7fc2e94
import java.io.*; import java.util.*; public class Asd { static PrintWriter w = new PrintWriter(System.out); static FastScanner s = new FastScanner(); static boolean sd = false; public static void main(String[] args) { int t = s.nextInt(); //int t=1; while (t-- > 0) { solve(); } w.close(); } public static class Student { public int i1; public int value; // A parameterized student constructor public Student(int i1,int i2) { this.i1 = i1; this.value=i2; } public int getkey() { return i1; } public int getValue() { return value; } } static class StudentComparator implements Comparator<Student>{ // Overriding compare()method of Comparator // for descending order of cgpa @Override public int compare(Student s1, Student s2) { if (s1.i1 < s2.i1) return -1; else if (s1.i1 >s2.i1) return 1; return 0; } } /* Function to print all the permutations of the string static String swap(String str, int i, int j) { char ch; char[] array = str.toCharArray(); ch = array[i]; array[i] = array[j]; array[j] = ch; return String.valueOf(array); } static void permute(String str,int low,int high) { if(low == high) list.add(Long.parseLong(str)); int i; for(i = low; i<=high; i++){ str = swap(str,low,i); permute(str, low+1,high); str = swap(str,low,i); } } use permute(str2,0,str2.length()-1); to perform combinations */ public static void solve() { int n=s.nextInt(); int m=s.nextInt(); int x=s.nextInt(); int arr[]=new int[n];int res[]=new int[n]; for(int i=0;i<n;i++) arr[i]=s.nextInt(); PriorityQueue<Student> pq=new PriorityQueue<Student>(new StudentComparator()); for(int i=0;i<m;i++){ pq.add(new Student(arr[i],i));res[i]=i;} for(int i=m;i<n;i++) { Student s1=pq.poll(); int k2=s1.getkey()+arr[i]; int v2=s1.getValue();res[i]=v2; pq.add(new Student(k2,v2)); } w.println("YES"); for(int i=0;i<n;i++) w.print(res[i]+1+" "); w.println(); } static void call(ArrayList<Integer> t, ArrayList<Integer> m) { if (t.size() == 0 && m.size() == 0) { sd = true; return; } t.remove(0); t.remove(t.size() - 1); call(t, new ArrayList<Integer>(m.subList(0, m.size() - 1))); call(t, new ArrayList<Integer>(m.subList(1, m.size()))); } static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static int noofdivisors(int n) { //it counts no of divisors of every number upto number n int arr[] = new int[n + 1]; for (int i = 1; i <= (n); i++) { for (int j = i; j <= (n); j = j + i) { arr[j]++; } } return arr[0]; } static char[] reverse(char arr[]) { char[] b = new char[arr.length]; int j = arr.length; for (int i = 0; i < arr.length; i++) { b[j - 1] = arr[i]; j = j - 1; } return b; } static long factorial(int n) { long su = 1; for (int i = 1; i <= n; i++) { su *= (long) i; } return su; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } long[] readArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
//package Practice; import java.io.*; import java.util.*; public class codefor { static class height implements Comparable<height> { int h,index; height(int hi,int i) { h=hi; index=i; } public int compareTo(height a) { return this.h-a.h; } public String toString() { return "("+this.h+","+this.index+") "; } } 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(),m=sc.nextInt(),x=sc.nextInt(),i; ArrayList<height> arr=new ArrayList<>(); int ans[]=new int[n]; PriorityQueue<height> tower=new PriorityQueue<height>(); for(i=0;i<n;i++) arr.add(new height(sc.nextInt(),i)); if(n<m) System.out.println("NO"); else { System.out.println("YES"); Collections.sort(arr,Collections.reverseOrder()); for(i=0;i<m;i++) tower.add(new height(0,i+1)); for(i=0;i<n;i++) { //System.out.print(tower); height t=tower.poll(); t.h=t.h+arr.get(i).h; //System.out.println(" "+t); tower.add(t); ans[arr.get(i).index]=t.index; } for(i=0;i<n;i++) System.out.print(ans[i]+" "); System.out.println(); } } } }
0
Non-plagiarised
734a94be
cb87df79
import java.io.*; import java.math.*; import java.util.*; public class test { static class Pair{ long x; long y; Pair(long x,long y){ this.x = x; this.y = y; } } static class Compare { void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.x!=p2.x) { return (int)(p1.x - p2.x); } else { return (int)(p1.y - p2.y); } } }); // for (int i = 0; i < n; i++) { // System.out.print(arr[i].x + " " + arr[i].y + " "); // } // System.out.println(); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { 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 solve(int dp[][] , ArrayList<Integer> one , ArrayList<Integer> zero , int i,int j) { int n=one.size(); int m=zero.size(); if(i>=n) return 0; else if(j>=m) return Integer.MAX_VALUE; else if(dp[i][j]!=-1) return dp[i][j]; int val1 = Math.abs(one.get(i)-zero.get(j))+solve(dp, one ,zero ,i+1,j+1); int val2 = solve(dp , one , zero , i,j+1); dp[i][j]= Math.min(val1,val2); return dp[i][j]; } public static void main(String args[]) throws Exception { Scanner sc = new Scanner(); StringBuffer res = new StringBuffer(); int tc = 1; while(tc-->0) { int n = sc.nextInt(); ArrayList<Integer> one = new ArrayList<>(); ArrayList<Integer> zero = new ArrayList<>(); for(int i=0;i<n;i++) { int x = sc.nextInt(); if(x==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(zero.get(i-1)-one.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))); } } System.out.println(dp[one.size()][zero.size()]); } System.out.println(res); } }
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
2120328e
6de04ee2
/***** ---> :) 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.util.*; public class Solution{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); char[] c=sc.next().toCharArray(); Vector<Integer> l=new Vector<>(), r=new Vector<>(); for(int i=0;i<n;i++) (c[i] == 'B' ? l : r).add(a[i]); Collections.sort(l); Collections.sort(r,Collections.reverseOrder()); boolean ok = true; for(int i=0;i<l.size();i++) if (l.get(i) < i + 1) ok = false; for(int i=0;i<r.size();i++) if (r.get(i) > n - i) ok = false; System.out.print((ok ? "YES" : "NO")+'\n'); } } }
1
Plagiarised
2a655afe
692a4496
import java.io.*; import java.util.*; public class c { public static BufferedReader in; public static void main(String[] args) throws Exception { in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int numcases = Integer.parseInt(in.readLine()); for (int casenum = 0; casenum < numcases; casenum++) { int n = Integer.parseInt(in.readLine()); int[] p = new int[n]; Arrays.fill(p, -1); for (int i = 0; i < n; i++) { if (p[i] != -1) continue; ArrayList<Integer> cycle = new ArrayList<>(); cycle.add(query(i)); while (cycle.size() == 1 || !cycle.get(0).equals(cycle.get(cycle.size() - 1))) { cycle.add(query(i)); } cycle.remove(cycle.size() - 1); for (int j = 0; j < cycle.size() - 1; j++) { p[cycle.get(j)] = cycle.get(j + 1); } p[cycle.get(cycle.size() - 1)] = cycle.get(0); // System.out.println(Arrays.toString(p)); } StringBuilder b = new StringBuilder(); b.append("! "); for (int i = 0; i < n; i++) { b.append((p[i] + 1) + (i < n - 1 ? " " : "\n")); } System.out.print(b); System.out.flush(); } in.close(); out.close(); } public static int query(int i) throws Exception { System.out.println("? " + (i + 1)); System.out.flush(); int k = Integer.parseInt(in.readLine()); return k - 1; } }
import java.io.*; import java.util.*; public class c { public static BufferedReader in; public static void main(String[] args) throws Exception { in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int numcases = Integer.parseInt(in.readLine()); for (int casenum = 0; casenum < numcases; casenum++) { int n = Integer.parseInt(in.readLine()); int[] p = new int[n]; Arrays.fill(p, -1); for (int i = 0; i < n; i++) { if (p[i] != -1) continue; ArrayList<Integer> cycle = new ArrayList<>(); cycle.add(query(i)); while (cycle.size() == 1 || !cycle.get(0).equals(cycle.get(cycle.size() - 1))) { cycle.add(query(i)); } cycle.remove(cycle.size() - 1); for(int j = 0; j < cycle.size() - 1; j++){ p[cycle.get(j)] = cycle.get(j + 1); } p[cycle.get(cycle.size() - 1)] = cycle.get(0); //System.out.println(Arrays.toString(p)); } StringBuilder b = new StringBuilder(); b.append("! "); for (int i = 0; i < n; i++) { b.append((p[i] + 1) + (i < n - 1 ? " " : "\n")); } System.out.print(b); System.out.flush(); } in.close(); out.close(); } public static int query(int i) throws Exception { System.out.println("? " + (i + 1)); System.out.flush(); int k = Integer.parseInt(in.readLine()); return k - 1; } }
1
Plagiarised
643e22cc
bd7281dc
import java.io.*; import java.util.*; public class C { public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws IOException { readInput(); out.close(); } static class Tower implements Comparable<Tower>{ int i, w; public Tower(int a, int b) { i=a; w=b; } public int compareTo(Tower o) {return w - o.w;} } static class Block implements Comparable<Block>{ int i, w; public Block(int a, int b) { i=a; w=b; } public int compareTo(Block o) {return o.w-w;} } static PriorityQueue<Tower> towers; static PriorityQueue<Block> blocks; static int[] par; static void solve() { while (!blocks.isEmpty()) { Block h = blocks.poll(); Tower t = towers.poll(); par[h.i] = t.i; t.w += h.w; towers.add(t); } List<Tower> res = new ArrayList<Tower>(towers); Collections.sort(res); boolean works = true; for (int i =1 ; i < m; i++) { if (Math.abs(res.get(i-1).w - res.get(i).w) > x) works = false; } if (works) { out.println("YES"); for (int i = 0; i < n; i++) out.print(par[i] + 1 + " "); out.println(); } else out.println("NO"); } static int n,m,x; public static void readInput() throws IOException { // br = new BufferedReader(new FileReader(".in")); // out = new PrintWriter(new FileWriter(".out")); int t = Integer.parseInt(br.readLine()); while (t-->0) { StringTokenizer st= new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); x = Integer.parseInt(st.nextToken()); towers = new PriorityQueue<Tower>(); blocks = new PriorityQueue<Block>(); par = new int[n]; st = new StringTokenizer(br.readLine()); for (int i= 0 ; i < n; i++) { blocks.add(new Block(i,Integer.parseInt(st.nextToken()))); } for (int i =0 ; i < m; i++) { towers.add(new Tower(i, 0)); } solve(); } } }
/* bts songs to dance to: I need U Run ON Filter I'm fine */ 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 x1515C { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int M = Integer.parseInt(st.nextToken()); int X = Integer.parseInt(st.nextToken()); int[] arr = readArr(N, infile, st); ArrayList<Unit> ls = new ArrayList<Unit>(); for(int i=0; i < N; i++) ls.add(new Unit(arr[i], i)); Collections.sort(ls); int[] res = new int[N]; PriorityQueue<Unit> pq = new PriorityQueue<Unit>(Comparator.reverseOrder()); for(int i=1; i <= M; i++) pq.add(new Unit(0, i)); for(Unit add: ls) { Unit tower = pq.poll(); tower.height += add.height; res[add.id] = tower.id; pq.add(tower); } int min = pq.poll().height; int max = min; while(pq.size() > 0) max = max(max, pq.poll().height); if(max-min > X) sb.append("NO\n"); else { sb.append("YES\n"); for(int x: res) sb.append(x+" "); sb.append("\n"); } } System.out.print(sb); } public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[N]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } } class Unit implements Comparable<Unit> { public int id; public int height; public Unit(int h, int i) { height = h; id = i; } public int compareTo(Unit oth) { return oth.height-height; } }
0
Non-plagiarised
04ed33a5
6b83b22e
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.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
26e699de
e99c14b9
//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.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
692a4496
f7006f16
import java.io.*; import java.util.*; public class c { public static BufferedReader in; public static void main(String[] args) throws Exception { in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int numcases = Integer.parseInt(in.readLine()); for (int casenum = 0; casenum < numcases; casenum++) { int n = Integer.parseInt(in.readLine()); int[] p = new int[n]; Arrays.fill(p, -1); for (int i = 0; i < n; i++) { if (p[i] != -1) continue; ArrayList<Integer> cycle = new ArrayList<>(); cycle.add(query(i)); while (cycle.size() == 1 || !cycle.get(0).equals(cycle.get(cycle.size() - 1))) { cycle.add(query(i)); } cycle.remove(cycle.size() - 1); for(int j = 0; j < cycle.size() - 1; j++){ p[cycle.get(j)] = cycle.get(j + 1); } p[cycle.get(cycle.size() - 1)] = cycle.get(0); //System.out.println(Arrays.toString(p)); } StringBuilder b = new StringBuilder(); b.append("! "); for (int i = 0; i < n; i++) { b.append((p[i] + 1) + (i < n - 1 ? " " : "\n")); } System.out.print(b); System.out.flush(); } in.close(); out.close(); } public static int query(int i) throws Exception { System.out.println("? " + (i + 1)); System.out.flush(); int k = Integer.parseInt(in.readLine()); return k - 1; } }
import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class C { private static StringTokenizer st; private static BufferedReader br; private static PrintWriter out; static boolean[]used; static int[]p; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int t = nextInt(); while (t --> 0) { int n = nextInt(); used = new boolean[n+1]; p = new int[n+1]; for (int i = 1; i <= n; i++) { if (!used[i]) { go(i); } } System.out.print("! "); for (int i = 1; i <= n; i++) { System.out.print(p[i]+" "); } System.out.println(); } out.close(); } private static void go(int i) throws IOException { List<Integer> inCycle = new ArrayList<>(); int first = -1; while (true) { System.out.println("? " + i); int x = nextInt(); used[x] = true; if (x == first) { break; } if (first == -1) { first = x; } inCycle.add(x); } List<Integer> numbers = new ArrayList<>(); for (int j = 0; j < inCycle.size(); j++) { if (inCycle.get(j) == i) { for (int k = j; k < inCycle.size(); k++) { numbers.add(inCycle.get(k)); } for (int k = 0; k < j; k++) { numbers.add(inCycle.get(k)); } break; } } if (numbers.size() == 1) { p[numbers.get(0)] = numbers.get(0); } else { for (int j = 0; j < numbers.size()-1; j++) { p[numbers.get(j)] = numbers.get(j+1); } p[numbers.get(numbers.size()-1)] = numbers.get(0); } } private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static double nextDouble() throws IOException { return Double.parseDouble(next()); } private static String next() throws IOException { while (st==null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } }
0
Non-plagiarised
26e699de
f229aa7f
//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.*; 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
853acff9
929b98f0
import java.lang.*; import java.util.*; import java.io.*; public class C { final static int mod = (int)(1e9 + 7); static LinkedList<Integer>[] adj; static int arr[][] ; static long dp[][] ; public static void main(String[] args) { FastReader fs = new FastReader(); int testcase = 1; testcase = fs.nextInt(); //int i = 1; while (testcase-- > 0) { //System.out.print("Case #" + Integer.toString(i) + ": "); solve(fs); //i++; } } public static void solve(FastReader fs) { int n = fs.nextInt(); arr = new int[n + 1][2]; adj = new LinkedList[n + 1]; dp = new long[n + 1][2]; for (int i = 0; i < adj.length; i++) { adj[i] = new LinkedList<Integer>(); } for (int i = 1; i < arr.length; i++) { arr[i][0] = fs.nextInt(); arr[i][1] = fs.nextInt(); } for (int i = 0; i < n - 1; i++) { int u = fs.nextInt(); int v = fs.nextInt(); adj[u].add(v); adj[v].add(u); } dfs(1, -1); System.out.println(Math.max(dp[1][0], dp[1][1])); } static void dfs(int source , int parent) { for (int val : adj[source]) { if (val == parent) continue; dfs(val, source); dp[source][0] += Math.max(dp[val][0] + Math.abs(arr[source][0] - arr[val][0]), dp[val][1] + Math.abs(arr[source][0] - arr[val][1])); dp[source][1] += Math.max(dp[val][0] + Math.abs(arr[source][1] - arr[val][0]), dp[val][1] + Math.abs(arr[source][1] - arr[val][1])); } } //IO operation 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.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(); } }
0
Non-plagiarised
2c7a0000
4201fdf7
import java.io.*; import java.util.*; public class Codeforces { 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) { int n = Integer.parseInt(br.readLine()); String[] str = br.readLine().split(" "); int[] a = new int[n]; int k = 0; ArrayList<Integer> pos = new ArrayList<>(); for(int i=0; i<n; i++) { a[i] = Integer.parseInt(str[i]); if(a[i] == 1) { k++; pos.add(i); } } int[][] dp = new int[n+1][k+1]; for(int i=0; i<=n; i++) { Arrays.fill(dp[i], Integer.MAX_VALUE); } dp[0][0] = 0; for(int i=0; i<n; i++) { for(int j=0; j<=k; j++) { if(dp[i][j] == Integer.MAX_VALUE) { continue; } dp[i+1][j] = Math.min(dp[i+1][j], dp[i][j]); if(j < k && a[i] == 0) { dp[i+1][j+1] = Math.min(dp[i+1][j+1], dp[i][j]+Math.abs(pos.get(j)-i)); } } } System.out.println(dp[n][k]); //} } }
//https://codeforces.com/contest/1525/problem/D //D. Armchairs import java.util.*; import java.io.*; public class CF_1525_D{ 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; int n = Integer.parseInt(br.readLine()); int a[] = new int[n]; ArrayList<Integer> pos = new ArrayList<Integer>(); st = new StringTokenizer(br.readLine()); for(int i=0;i<n;i++){ a[i] = Integer.parseInt(st.nextToken()); if(a[i]==1) pos.add(i); } int z = pos.size(); int dp[][] = new int[n+1][z+1]; for(int i=0;i<=n;i++) Arrays.fill(dp[i], Integer.MAX_VALUE); dp[0][0] = 0; for(int i=0;i<n;i++){ for(int j=0;j<=z;j++){ if(dp[i][j] == Integer.MAX_VALUE) continue; dp[i+1][j] = Math.min(dp[i+1][j], dp[i][j]); if(j<z && a[i]==0) dp[i+1][j+1] = Math.min(dp[i+1][j+1], dp[i][j]+Math.abs(pos.get(j)-i)); } } pw.print(dp[n][z]); pw.flush(); pw.close(); } }
1
Plagiarised
80407703
99e059b8
import java.util.Scanner; import java.util.Arrays; public class B { public static int[] sort(int[] arr,int low,int high){ int[] sortedArray; if (low==high) { sortedArray=new int[]{arr[low]}; return sortedArray; } int mid=(low+high)/2; int[] arr1=sort(arr,low,mid); int[] arr2=sort(arr,mid+1,high); sortedArray=merge(arr1,low,mid,arr2,mid+1,high); return sortedArray; } public static int[] merge(int[] arr1,int low1,int high1,int[] arr2,int low2,int high2){ int size1=high1-low1+1; int size2=high2-low2+1; int[] arr=new int[size1+size2]; int pointer=0; low1=0;low2=0; while (low1<size1 && low2<size2){ if (arr1[low1] <= arr2[low2]) { arr[pointer++]=arr1[low1++]; } else{ arr[pointer++]=arr2[low2++]; } } while(low1<size1) arr[pointer++]=arr1[low1++]; while(low2<size2) arr[pointer++]=arr2[low2++]; return arr; } public static void main(String[] args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-- > 0){ int n=sc.nextInt(); long k=sc.nextLong(); int[] arr=new int[n]; long sum=0; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); sum+=arr[i]; } if (sum<=k){ System.out.println(0); continue; } long s[]=new long[n]; arr=sort(arr,0,n-1); s[n-1]=arr[n-1]-arr[0]; long ans=sum-k; for(int i=n-2;i>=0;i--) s[i]=s[i+1]+arr[i]-arr[0]; for(int i=n-1;i>=1;i--) { long caseAns=(sum-k-s[i]+n-i)/(n-i+1); caseAns=Math.max(caseAns,0); caseAns+=n-i; ans=Math.min(caseAns,ans); } System.out.println(ans); } sc.close(); } }
// हर हर महादेव import java.util.*; import java.lang.*; import java.io.*; import java.math.BigInteger; import java.text.DecimalFormat; public final class Solution { static int inf = Integer.MAX_VALUE; static long mod = 1000000000 + 7; static void ne(Scanner sc, BufferedWriter op) throws Exception { int n=sc.nextInt(); long k=sc.nextLong(); long[] arr= new long[n]; long sum=0; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); sum+=arr[i]; } if(sum<=k){ op.write("0\n"); return; } sort(arr); long min=sum-k; long ss=0; int cc=0; for(int i=n-1;i>=1;i--){ sum-=arr[i]; sum+=arr[0]; cc++; if(sum<=k){ min=Math.min(min,cc); break; } } long val=Math.abs(arr[0]-(k/n))+(n-1); min=Math.min(val,min); for(int i=1;i<n;i++){ ss+=arr[i]; long no=((k-ss)/(long)(n-i)); if(k-ss<0 && (k-ss)%(n-i)!=0){ no--; } long count=(n-i-1)+Math.abs(arr[0]-no); min=Math.min(count,min); } op.write(min+"\n"); } public static void main(String[] args) throws Exception { BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out)); // Reader sc = new Reader(); Scanner sc= new Scanner(System.in); int t = sc.nextInt(); while (t-->0){ ne(sc, op); } // ne(sc,op); op.flush(); } static void print(Object o) { System.out.println(String.valueOf(o)); } static int[] toIntArr(String s){ int[] val= new int[s.length()]; for(int i=0;i<s.length();i++){ val[i]=s.charAt(i)-'a'; } return val; } static void sort(int[] arr){ ArrayList<Integer> list= new ArrayList<>(); for(int i=0;i<arr.length;i++){ list.add(arr[i]); } Collections.sort(list); for(int i=0;i<arr.length;i++){ arr[i]=list.get(i); } } static void sort(long[] arr){ ArrayList<Long> list= new ArrayList<>(); for(int i=0;i<arr.length;i++){ list.add(arr[i]); } Collections.sort(list); for(int i=0;i<arr.length;i++){ arr[i]=list.get(i); } } } // return -1 to put no ahed in array class pair { int xx; int yy; pair(int xx, int yy ) { this.xx = xx; this.yy = yy; } } class sortY implements Comparator<pair> { public int compare(pair p1, pair p2) { if (p1.yy > p2.yy) { return 1; } else if (p1.yy == p2.yy) { if (p1.xx > p2.xx) { return 1; } else if (p1.xx < p2.xx) { return -1; } return 0; } return -1; } } class sortX implements Comparator<pair> { public int compare(pair p1, pair p2) { if (p1.xx > p2.xx) { return 1; } else if (p1.xx == p2.xx) { if (p1.yy > p2.yy) { return 1; } else if (p1.yy < p2.yy) { return -1; } return 0; } return -1; } } class debug { static void print1d(long[] arr) { System.out.println(); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } static void print1d(int[] arr) { System.out.println(); for (int i = 0; i < arr.length; i++) { System.out.println(i+" = "+arr[i]); } } static void print1d(boolean[] arr) { System.out.println(); for (int i = 0; i < arr.length; i++) { System.out.println(i + "= " + arr[i]); } } static void print2d(int[][] arr) { System.out.println(); int n = arr.length; int n2 = arr[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < n2; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } static void print2d(boolean[][] arr) { System.out.println(); int n = arr.length; int n2 = arr[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < n2; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } static void print2d(long[][] arr) { System.out.println(); int n = arr.length; int n2 = arr[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < n2; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } static void printPair(ArrayList<pair> list) { if(list.size()==0){ System.out.println("empty list"); return; } System.out.println(); for(int i=0;i<list.size();i++){ System.out.println(list.get(i).xx+"-"+list.get(i).yy); } } } 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(); } } class MultiTreeSet<E> { TreeMap<E, Integer> freqTreeMap = new TreeMap<E, Integer>(); int size; public MultiTreeSet() {} public MultiTreeSet(Collection<? extends E> c) { for(E element : c) add(element); } public int size() { return size; } public void add(E element) { Integer freq = freqTreeMap.get(element); if(freq==null) freqTreeMap.put(element, 1); else freqTreeMap.put(element,freq+1); ++size; } public void remove(E element) { Integer freq = freqTreeMap.get(element); if(freq!=null) { if(freq==1) freqTreeMap.remove(element); else freqTreeMap.put(element, freq-1); --size; } } public int get(E element) { Integer freq = freqTreeMap.get(element); if(freq==null) return 0; return freq; } public boolean contains(E element) { return get(element)>0; } public boolean isEmpty() { return size==0; } public E first() { return freqTreeMap.firstKey(); } public E last() { return freqTreeMap.lastKey(); } public E ceiling(E element) { return freqTreeMap.ceilingKey(element); } public E floor(E element) { return freqTreeMap.floorKey(element); } public E higher(E element) { return freqTreeMap.higherKey(element); } public E lower(E element) { return freqTreeMap.lowerKey(element); } }
0
Non-plagiarised
28d8c381
417833c3
import java.io.*; import java.sql.SQLOutput; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int numCases = Integer.parseInt(br.readLine()); for (int i = 0; i < numCases; i++) { int length = Integer.parseInt(br.readLine()); int found = 0; int[] ret = new int[length + 1]; for (int j = 1; j <= length; j++) { if (found == length - 1) { break; } if (ret[j] == 0) { System.out.println("? " + j); System.out.flush(); int start = Integer.parseInt(br.readLine()); int lastNum = start; boolean cont = true; while (cont) { System.out.println("? " + j); System.out.flush(); int num = Integer.parseInt(br.readLine()); ret[lastNum] = num; found++; lastNum = num; if (num == start) cont = false; } } } for (int j = 0; j <= length; j++) if (ret[j] == 0) ret[j] = j; System.out.print("! "); for (int j = 1; j <= length; j++) System.out.print(ret[j] + " "); System.out.println(); } br.close(); } }
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; public final class B { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<Integer> g[]; static long mod=(long)998244353,INF=Long.MAX_VALUE; // static boolean set[]; static int par[],partial[]; static int Days[],P[][]; static int sum=0,size[]; static int seg[],col[]; // static ArrayList<Long> A; static char X[][]; static boolean set[][]; static int D[],min[],A[]; static long dp[][]; // static HashSet<Integer> visited,imposters; // static HashSet<Integer> set; // static node1 seg[]; //static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)}; public static void main(String args[])throws IOException { /* * star,rope,TPST * BS,LST,MS,MQ */ int T=i(); outer:while(T-->0) { int N=i(); int f[]=new int[N+5]; int ask=ask(1); HashSet<Integer> set=new HashSet<>(); int cnt=0; for(int i=1; i<=N; i++) { if(cnt+1==N) { set=new HashSet<>(); for( i=0; i<=N; i++)set.add(i); for( i=1; i<=N; i++)set.remove(f[i]); int a=-1; for(int v:set) { a=v; } for(i=1; i<=N; i++) { if(f[i]==0)f[i]=a; } break; } if(f[i]==0) { int last=0; set=new HashSet<>(); while(true) { int a=ask(i); f[last]=a; if(set.contains(a)) { break; } last=a; set.add(a); } cnt+=set.size(); } } for(int i=1; i<=N; i++) { ans.append(f[i]+" "); } System.out.println("! "+ans); ans=new StringBuilder(); } out.println(ans); out.close(); } static int ask(int a) { System.out.println("? "+a); // out.flush(); return i(); } static boolean f(String A,String B) { char X[]=A.toCharArray(),Y[]=B.toCharArray(); if(X[X.length-1]=='0' && A.length()==B.length()) { return A.equals(B); } else if(X[X.length-1]=='0') { for(int i=0; i<Y.length-X.length; i++) { boolean f=true; for(int j=0; j<X.length; j++) { if(X[j]!=Y[i+j]) { f=false; } } if(f) { for(int j=0; j<i; j++) { if(Y[j]=='0')f=false; } for(int j=i+X.length; j<Y.length; j++) { if(Y[j]=='0')f=false; } if(f) { // System.out.println(B.substring(i+X.length)); return true; } } } return false; } else { for(int i=0; i<=Y.length-X.length; i++) { boolean f=true; for(int j=0; j<X.length; j++) { if(X[j]!=Y[i+j]) { f=false; } } if(f) { for(int j=0; j<i; j++) { if(Y[j]=='0')f=false; } for(int j=i+X.length; j<Y.length; j++) { if(Y[j]=='0')f=false; } if(f) { // System.out.println(B.substring(i+X.length)); return true; } } } return false; } } static String reverse (String X) { StringBuilder sb=new StringBuilder(X); return sb.reverse().toString(); } static String binary(long x) { String str=""; long p=1L; while(p<=x) { if((p&x)!=0)str="1"+str; else str="0"+str; p<<=1; } return str; } static boolean can_loose(Trie root,boolean t) { if(root==null)return true; //never hits int cnt=0; for(Trie x:root.A) { if(x!=null) { cnt++; if(!can_loose(x,t^true)) { //System.out.println("TRUE FOR--> "+x.ch+" "+t); return true; } } } if(cnt==0)return true; return false; } static boolean f(Trie root) { if(root==null)return false; // int cnt=0; for(Trie x:root.A) { if(x!=null) { // cnt++; if(!f(x)) { return true; } } } return false; } static void insert(String X,Trie head) { Trie root=head; for(char x:X.toCharArray()) { int a=x-'a'; if(root.A[a]==null)root.A[a]=new Trie(); root=root.A[a]; root.ch=x; } root.ends=true; } static boolean search(String X,Trie head) { Trie root=head; for(char x:X.toCharArray()) { int a=x-'a'; if(root.A[a]==null)return false; root=root.A[a]; } return root.ends; } static boolean starts_with(String X,Trie head) { Trie root=head; for(char x:X.toCharArray()) { int a=x-'a'; if(root.A[a]==null)return false; root=root.A[a]; } return true; } static int lower(ArrayList<Integer>A,int i) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; if(A.get(m)>i)r=m; else l=m; } return r; } static int upper(ArrayList<Integer>A,int i) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; if(A.get(m)<i)l=m; else l=m; } return l; } static String f() { StringBuilder X=new StringBuilder(i()+"");int p=i(); while(p-->0)X.append("0"); return X.toString(); } static long f(int i,int g,int x,int left) { if(x<=1)return left; if(i<0 || g==1)return left; // System.out.println(i+" "+g); if(dp[i][g]==0) { int next=min[i]; int new_gcd=(int)GCD(g,x); //System.out.println(" factor--> "+x+" gcd--> "+g+" new--> "+new_gcd); long s=0; int c=0; for(int j=next+1; j<=i; j++) { if(A[i]%new_gcd==0) { s+=new_gcd; c--; } } long a=0,b=0; a=f(i,g,x-1,left); b=s+f(next,new_gcd,x-1,left-c); dp[i][g]=Math.max(a, b); } return dp[i][g]; } static boolean f(int A[],int B[],int x) { int c=0; int N=A.length; int l=0,r=x-1; for(int i=0; i<N; i++) { if(A[i]>=r && B[i]>=l) { r--; l++; c++; } if(c>=x)return true; } return false; } static boolean isPalindrome(int A[]) { int i=0,j=A.length-1; while(i<j) { if(A[i]!=A[j])return false; i++; j--; } return true; } static boolean isPalindrome(int A[],int x) { int i=0,j=A.length-1; while(i<j) { if(A[i]!=A[j]) { if(A[i]==x)i++; else if(A[j]==x)j--; else return false; } else { i++; j--; } } return true; } static long fact[]; static double nCr(int a,int b) { double x=fact[a], y=fact[b]*fact[a-b]; return x/y; } static int f(ArrayList<Integer> A,int x) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; if(A.get(m)<x)l=m; else r=m; } return r; } static boolean f(long a,long b,long x) { if(b==0) { return (x==a || x==b); } if(a<x)return false; else { if(a%b==x%b)return true; return f(b,a%b,x); } } static void dfs(int n,int p) { if(p!=-1)D[n]=D[p]+1; for(int c:g[n]) { if(c!=p) dfs(c,n); } } static long f(long a,long A[]) { int l=-1,r=A.length; while(r-l>1) { int m=(l+r)/2; if(A[m]<=a)l=m; else r=m; } return A[l]; } static void build(int v,int tl,int tr,int A[]) { if(tl==tr) { seg[v]=A[tl]; return; } int tm=(tl+tr)/2; build(v*2,tl,tm,A); build(v*2+1,tm+1,tr,A); seg[v]=Math.min(seg[v*2], seg[v*2+1]); } static void update(int v,int tl,int tr,int index,int x) { if(tl==tr && tl==index) { seg[v]=x; return; } int tm=(tl+tr)/2; if(index<=tm)update(v*2,tl,tm,index,x); else update(v*2+1,tm+1,tr,index,x); seg[v]=Math.min(seg[v*2], seg[v*2+1]); } static int ask(int v,int tl,int tr,int l,int r) { // System.out.println(v); // if(v>100)return 0; if(l>r)return Integer.MAX_VALUE; if(tl==l && tr==r)return seg[v]; int tm=(tl+tr)/2; int a=ask(v*2,tl,tm,l,Math.min(tm, r)); // System.out.println("for--> "+(v)+" tm--> "+(tm+1)+" tr--> "+tr+" l--> "+Math.max(l, tm+1)+" r--> "+r); int b=ask(v*2+1,tm+1,tr,Math.max(l, tm+1),r); return Math.min(a, b); } 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]; par[b]=a; } } static int ask(int a,int b) { System.out.println("? "+a+" "+b); return i(); } static long and(int i,int j) { System.out.println("and "+i+" "+j); return l(); } static long or(int i,int j) { System.out.println("or "+i+" "+j); return l(); } static boolean is_Sorted(int A[]) { int N=A.length; for(int i=1; i<=N; i++)if(A[i-1]!=i)return false; return true; } static boolean f(StringBuilder sb,String Y,String order) { StringBuilder res=new StringBuilder(sb.toString()); HashSet<Character> set=new HashSet<>(); for(char ch:order.toCharArray()) { set.add(ch); for(int i=0; i<sb.length(); i++) { char x=sb.charAt(i); if(set.contains(x))continue; res.append(x); } } String str=res.toString(); return str.equals(Y); } static boolean all_Zero(int f[]) { for(int a:f)if(a!=0)return false; return true; } static long form(int a,int l) { long x=0; while(l-->0) { x*=10; x+=a; } return x; } static int count(String X) { HashSet<Integer> set=new HashSet<>(); for(char x:X.toCharArray())set.add(x-'0'); return set.size(); } // static void build(int v,int tl,int tr,long A[]) // { // if(tl==tr) // { // seg[v]=A[tl]; // } // else // { // int tm=(tl+tr)/2; // build(v*2,tl,tm,A); // build(v*2+1,tm+1,tr,A); // seg[v]=Math.min(seg[v*2], seg[v*2+1]); // } // } static int [] sub(int A[],int B[]) { int N=A.length; int f[]=new int[N]; for(int i=N-1; i>=0; i--) { if(B[i]<A[i]) { B[i]+=26; B[i-1]-=1; } f[i]=B[i]-A[i]; } for(int i=0; i<N; i++) { if(f[i]%2!=0)f[i+1]+=26; f[i]/=2; } return f; } static int max(int a ,int b,int c,int d) { a=Math.max(a, b); c=Math.max(c,d); return Math.max(a, c); } static int min(int a ,int b,int c,int d) { a=Math.min(a, b); c=Math.min(c,d); return Math.min(a, c); } 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 long mul(long a, long b) { return ( a %mod * 1L * b%mod )%mod; } static void swap(int A[],int a,int b) { int t=A[a]; A[a]=A[b]; A[b]=t; } static boolean isSorted(int A[]) { for(int i=1; i<A.length; i++) { if(A[i]<A[i-1])return false; } return true; } static boolean isDivisible(StringBuilder X,int i,long num) { long r=0; for(; i<X.length(); i++) { r=r*10+(X.charAt(i)-'0'); r=r%num; } return r==0; } static int lower_Bound(int A[],int low,int high, int x) { if (low > high) if (x >= A[high]) return A[high]; int mid = (low + high) / 2; if (A[mid] == x) return A[mid]; if (mid > 0 && A[mid - 1] <= x && x < A[mid]) return A[mid - 1]; if (x < A[mid]) return lower_Bound( A, low, mid - 1, x); return lower_Bound(A, mid + 1, high, x); } static String f(String A) { String X=""; for(int i=A.length()-1; i>=0; i--) { int c=A.charAt(i)-'0'; X+=(c+1)%2; } return X; } 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 String swap(String X,int i,int j) { char ch[]=X.toCharArray(); char a=ch[i]; ch[i]=ch[j]; ch[j]=a; return new String(ch); } static int sD(long n) { if (n % 2 == 0 ) return 2; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0 ) return i; } return (int)n; } static void setGraph(int N) { // tot=new int[N+1]; partial=new int[N+1]; Days=new int[N+1]; P=new int[N+1][(int)(Math.log(N)+10)]; // set=new boolean[N+1]; g=new ArrayList[N+1]; D=new int[N+1]; for(int i=0; i<=N; i++) { g[i]=new ArrayList<>(); Days[i]=-1; D[i]=Integer.MAX_VALUE; //D2[i]=INF; } } static long pow(long a,long b) { 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 long fact(long N) { long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } 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 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(boolean A[][]) { for(boolean a[]:A)print(a); } static void print(long A[][]) { for(long a[]:A)print(a); } static void print(int A[][]) { for(int a[]:A)print(a); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } 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; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b); } } class Trie { char ch; Trie A[]; boolean ends; Trie() { ch='*'; A=new Trie[26]; ends=false; } } //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()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
0
Non-plagiarised
4f7af821
8f6421f3
import java.util.*; public class contestA { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); int inf = 1000300300; while (t-->0){ int n = scanner.nextInt(); int k = scanner.nextInt(); int[] c = new int[n]; Arrays.fill(c,inf); int[] a = new int[k]; int[] b = new int[k]; for(int i=0;i<k;++i) a[i] = scanner.nextInt() - 1; for(int i=0;i<k;++i) b[i] = scanner.nextInt(); for(int i=0;i<k;++i) c[a[i]] = b[i]; for(int i=1;i<n;++i){ c[i] = Math.min(c[i],c[i-1]+1); } for(int i=n-2;i>=0;--i){ c[i] = Math.min(c[i],c[i+1]+1); } for(int i=0;i<n;++i) System.out.print(c[i]+" "); System.out.println(); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Arrays; public class Main{ public static void main (String[] args){ FastReader s = new FastReader(); int t=1;t=s.ni(); for(int test=1;test<=t;test++){ int n=s.ni(),k=s.ni(); int position[]=s.readArray(k),temp[]=s.readArray(k); int ans[]=new int[n]; Arrays.fill(ans,Integer.MAX_VALUE/2); for(int i=0;i<k;i++){ ans[position[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++) System.out.print(ans[i]+" "); System.out.println(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in));} int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } int[] readArray(int n){ int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] =Integer.parseInt(next()); return a; } String next(){ while (st == null || !st.hasMoreElements()) {try {st = new StringTokenizer(br.readLine());} catch (IOException e){e.printStackTrace();}}return st.nextToken();} String nextLine(){String str = "";try {str = br.readLine();}catch (IOException e) {e.printStackTrace();}return str;} } }
1
Plagiarised
a7e7f371
f8e7b886
import java.io.*; import java.util.*; public class cp { static int mod=(int)1e9+7; // static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static int[] sp; static int size=(int)1e6; static int[] arInt; static long[] arLong; public static void main(String[] args) throws IOException { long tc=sc.nextLong(); // Scanner sc=new Scanner(System.in); // int tc=1; // primeSet=new HashSet<>(); // sieveOfEratosthenes((int)1e6+5); while(tc-->0) { int n=sc.nextInt(); int k[]=new int[n]; int h[]=new int[n]; for(int i=0;i<n;i++) k[i]=sc.nextInt(); for(int i=0;i<n;i++) h[i]=sc.nextInt(); ArrayList<Pair> interval=new ArrayList<Pair>(); ArrayList<Pair> act=new ArrayList<Pair>(); for(int i=0;i<n;i++) interval.add(new Pair(k[i]-h[i]+1,k[i])); Collections.sort(interval); // out.println(interval); act.add(interval.get(0)); for(int i=1;i<n;i++) { Pair p=act.get(act.size()-1); if(p.y<interval.get(i).x) act.add(interval.get(i)); else p.y=Math.max(p.y, interval.get(i).y); } // out.println(act); long mana=0; for(Pair p: act) { long x=p.y-p.x+1; mana+=(x*(x+1))/2; } out.println(mana); // int n=sc.nextInt(); // long days[]=new long[n]; // long power[]=new long[n]; // for (int i = 0; i < power.length; i++) { // days[i]=sc.nextLong(); // } // for (int i = 0; i < power.length; i++) { // power[i]=sc.nextLong(); // // } // // long ans=0; // for(int i=0;i<n;i++) // { // if(i==0) // { // ans+=power[i]*(power[i]+1L)/2L; // continue; // } // // long temp=power[i]*(power[i]+1)/2L; // long temp2=(power[i-1]+days[i]-days[i-1])*(power[i-1]+days[i]-days[i-1]+1L)/2L; // temp2-=power[i-1]*(power[i-1]+1L)/2L; // ans+=Math.min(temp, temp2); //// if(days[i]-days[i-1]<=power[i]) //// { //// ans+=power[i]*(power[i]+1)/2; //// } //// else { //// ans+=power[i]*(power[i]+1)/2; //// ans-=power[i-1]*(power[i-1]+1)/2; //// } // // // } // // out.println(ans); } out.flush(); out.close(); System.gc(); } /* ...SOLUTION ENDS HERE...........SOLUTION ENDS HERE... */ static int util(char a,char b) { int A=a-'0'; int B=b-'0'; return A+B; } static boolean check(int x,int[] rich,int[] poor) { int cnt=0; for(int i=0;i<rich.length;i++) { if(x-1-rich[i]<=cnt && cnt<=poor[i]) cnt++; } return cnt>=x; } static void arrInt(int n) throws IOException { arInt=new int[n]; for (int i = 0; i < arInt.length; i++) { arInt[i]=sc.nextInt(); } } static void arrLong(int n) throws IOException { arLong=new long[n]; for (int i = 0; i < arLong.length; i++) { arLong[i]=sc.nextLong(); } } static ArrayList<Integer> add(int id,int c) { ArrayList<Integer> newArr=new ArrayList<>(); for(int i=0;i<id;i++) newArr.add(arInt[i]); newArr.add(c); for(int i=id;i<arInt.length;i++) { newArr.add(arInt[i]); } return newArr; } // function to find last index <= y static int upper(ArrayList<Integer> arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) <= y) l = mid + 1; else h = mid - 1; } return h; } static int lower(ArrayList<Integer> arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) >= x) h = mid - 1; else l = mid + 1; } return l; } static int N = 501; // Array to store inverse of 1 to N static long[] factorialNumInverse = new long[N + 1]; // Array to precompute inverse of 1! to N! static long[] naturalNumInverse = new long[N + 1]; // Array to store factorial of first N numbers static long[] fact = new long[N + 1]; // Function to precompute inverse of numbers public static void InverseofNumber(int p) { naturalNumInverse[0] = naturalNumInverse[1] = 1; for(int i = 2; i <= N; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse[0] = factorialNumInverse[1] = 1; // Precompute inverse of natural numbers for(int i = 2; i <= N; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to N public static void factorial(int p) { fact[0] = 1; // Precompute factorials for(int i = 1; i <= N; i++) { fact[i] = (fact[i - 1] * (long)i) % p; } } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R, int p) { // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p; return ans; } static String tr(String s) { int now = 0; while (now + 1 < s.length() && s.charAt(now)== '0') ++now; return s.substring(now); } static ArrayList<Integer> ans; static void dfs(int node,Graph gg,int cnt,int k,ArrayList<Integer> temp) { if(cnt==k) return; for(Integer each:gg.list[node]) { if(each==0) { temp.add(each); ans=new ArrayList<>(temp); temp.remove(temp.size()-1); continue; } temp.add(each); dfs(each,gg,cnt+1,k,temp); temp.remove(temp.size()-1); } return; } static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop 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 ArrayList<Integer> commDiv(int a, int b) { // find gcd of a, b int n = gcd(a, b); // Count divisors of n. ArrayList<Integer> Div=new ArrayList<>(); for (int i = 1; i <= Math.sqrt(n); i++) { // if 'i' is factor of n if (n % i == 0) { // check if divisors are equal if (n / i == i) Div.add(i); else { Div.add(i); Div.add(n/i); } } } return Div; } static HashSet<Integer> factors(int x) { HashSet<Integer> a=new HashSet<Integer>(); for(int i=2;i*i<=x;i++) { if(x%i==0) { a.add(i); a.add(x/i); } } return a; } static class Node { int vertex; HashSet<Node> adj; boolean rem; Node(int ver) { vertex=ver; rem=false; adj=new HashSet<Node>(); } @Override public String toString() { return vertex+" "; } } static class Tuple{ int a; int b; int c; public Tuple(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } } //function to find prime factors of n static HashMap<Long,Long> findFactors(long n2) { HashMap<Long,Long> ans=new HashMap<>(); if(n2%2==0) { ans.put(2L, 0L); // cnt++; while((n2&1)==0) { n2=n2>>1; ans.put(2L, ans.get(2L)+1); // } } for(long i=3;i*i<=n2;i+=2) { if(n2%i==0) { ans.put((long)i, 0L); // cnt++; while(n2%i==0) { n2=n2/i; ans.put((long)i, ans.get((long)i)+1); } } } if(n2!=1) { ans.put(n2, ans.getOrDefault(n2, (long) 0)+1); } return ans; } //fenwick tree implementaion static class fwt { int n; long BITree[]; fwt(int n) { this.n=n; BITree=new long[n+1]; } fwt(int arr[], int n) { this.n=n; BITree=new long[n+1]; for(int i = 0; i < n; i++) updateBIT(n, i, arr[i]); } long getSum(int index) { long sum = 0; index = index + 1; while(index>0) { sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(int n, int index,int val) { index = index + 1; while(index <= n) { BITree[index] += val; index += index & (-index); } } void print() { for(int i=0;i<n;i++) out.print(getSum(i)+" "); out.println(); } } class sparseTable{ int n; long[][]dp; int log2[]; int P; void buildTable(long[] arr) { n=arr.length; P=(int)Math.floor(Math.log(n)/Math.log(2)); log2=new int[n+1]; log2[0]=log2[1]=0; for(int i=2;i<=n;i++) { log2[i]=log2[i/2]+1; } dp=new long[P+1][n]; for(int i=0;i<n;i++) { dp[0][i]=arr[i]; } for(int p=1;p<=P;p++) { for(int i=0;i+(1<<p)<=n;i++) { long left=dp[p-1][i]; long right=dp[p-1][i+(1<<(p-1))]; dp[p][i]=Math.max(left, right); } } } long maxQuery(int l,int r) { int len=r-l+1; int p=(int)Math.floor(log2[len]); long left=dp[p][l]; long right=dp[p][r-(1<<p)+1]; return Math.max(left, right); } } //Function to find number of set bits static int setBitNumber(long n) { if (n == 0) return 0; int msb = 0; n = n / 2; while (n != 0) { n = n / 2; msb++; } return msb; } static int getFirstSetBitPos(long n) { return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1; } static ArrayList<Integer> primes; static HashSet<Integer> primeSet; static boolean prime[]; static void sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. prime= new boolean[n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p 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) primeSet.add(i); } } static long mod(long a, long b) { long c = a % b; return (c < 0) ? c + b : c; } static void swap(long arr[],int i,int j) { long temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } static boolean util(int a,int b,int c) { if(b>a)util(b, a, c); while(c>=a) { c-=a; if(c%b==0) return true; } return (c%b==0); } static void flag(boolean flag) { out.println(flag ? "YES" : "NO"); out.flush(); } static void print(int a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print(long a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print_int(ArrayList<Integer> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void print_long(ArrayList<Long> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void printYesNo(boolean condition) { if (condition) { out.println("YES"); } else { out.println("NO"); } } static int LowerBound(int a[], int 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 int lowerIndex(int arr[], int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] >= x) h = mid - 1; else l = mid + 1; } return l; } // function to find last index <= y static int upperIndex(int arr[], int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int upperIndex(long arr[], int n, long y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int UpperBound(int a[], int 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 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 class DisjointUnionSets { int[] rank, parent; int n; // Constructor public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } // Creates n sets with single item in each void makeSet() { for (int i = 0; i < n; i++) parent[i] = i; } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } // Unites the set that includes x and the set // that includes x void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else // if ranks are the same { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } // if(xRoot!=yRoot) // parent[y]=x; } int connectedComponents() { int cnt=0; for(int i=0;i<n;i++) { if(parent[i]==i) cnt++; } return cnt; } } static class Graph { int v; ArrayList<Integer> list[]; Graph(int v) { this.v=v; list=new ArrayList[v+1]; for(int i=1;i<=v;i++) list[i]=new ArrayList<Integer>(); } void addEdge(int a, int b) { this.list[a].add(b); } } // static class GraphMap{ // Map<String,ArrayList<String>> graph; // GraphMap() { // // TODO Auto-generated constructor stub // graph=new HashMap<String,ArrayList<String>>(); // // } // void addEdge(String a,String b) // { // if(graph.containsKey(a)) // this.graph.get(a).add(b); // else { // this.graph.put(a, new ArrayList<>()); // this.graph.get(a).add(b); // } // } // } // static void dfsMap(GraphMap g,HashSet<String> vis,String src,int ok) // { // vis.add(src); // // if(g.graph.get(src)!=null) // { // for(String each:g.graph.get(src)) // { // if(!vis.contains(each)) // { // dfsMap(g, vis, each, ok+1); // } // } // } // // cnt=Math.max(cnt, ok); // } static double sum[]; static long cnt; // static void DFS(Graph g, boolean[] visited, int u) // { // visited[u]=true; // // for(int i=0;i<g.list[u].size();i++) // { // int v=g.list[u].get(i); // // if(!visited[v]) // { // cnt1=cnt1*2; // DFS(g, visited, v); // // } // // } // // // } 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) { // TODO Auto-generated method stub return this.x-o.x; } } static long sum_array(int a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static long sum_array(long a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } 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 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 reverse_array(int a[]) { int n=a.length; int i,t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static void reverse_array(long a[]) { int n=a.length; int i; long t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } // 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 res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static int power(int x, int y) { int res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static long gcd(long a, long b) { if (a == 0) return b; //cnt+=a/b; return gcd(b%a,a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } 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(); } 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 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(); } } static PrintWriter out=new PrintWriter(System.out); static int int_max=Integer.MAX_VALUE; static int int_min=Integer.MIN_VALUE; static long long_max=Long.MAX_VALUE; static long long_min=Long.MIN_VALUE; }
import java.util.*; import java.io.*; import java.time.*; import static java.lang.Math.*; @SuppressWarnings("unused") public class C { static boolean DEBUG = false; static Reader fs; static PrintWriter pw; static void solve() { int n = fs.nextInt(), k[] = fs.readArray(n), h[] = fs.readArray(n); int prev_h = h[0], prev_k = k[0]; // int ans = 0; ArrayList<pair> intervals = new ArrayList<>(); for (int i = 0; i < n; i++) { int start = k[i] - h[i] + 1; int end = k[i]; intervals.add(new pair(start, end)); } // pw.println(intervals); Collections.sort(intervals); ArrayList<pair> merged = new ArrayList<>(); merge(intervals, merged); long ans = 0; for(int i = 0 ; i < merged.size() ; i++) { ans += sum(merged.get(i).len()); } pw.println(ans); } static void merge(ArrayList<pair>a1, ArrayList<pair>a2) { int n = a1.size(); int index = 0; for(int i =1 ; i < n ; i++) { if(a1.get(index).s >= a1.get(i).f) { a1.get(index).s = max(a1.get(index).s, a1.get(i).s); } else { index++; a1.set(index, a1.get(i)); } } for(int i = 0 ; i <= index ; i++) { a2.add(a1.get(i)); } // pw.println(a1); } // int index = 0; // Stores index of last element // // in output array (modified arr[]) // // // Traverse all input Intervals // for (int i=1; i<arr.length; i++) // { // // If this is not first Interval and overlaps // // with the previous one // if (arr[index].end >= arr[i].start) // { // // Merge previous and current Intervals // arr[index].end = Math.max(arr[index].end, arr[i].end); // } // else { // index++; // arr[index] = arr[i]; // } // } static boolean overlapping(pair p1, pair p2) { if((p2.f >= p1.f && p1.s >= p2.f) || (p2.s >= p1.f && p1.s >= p2.s)) { return true; } return false; } static pair merge(pair p1, pair p2) { return new pair(min(p1.f, p2.f), max(p1.s, p2.s)); } static long sum(long n) { return (n * (n + 1) / 2); } static class pair implements Comparable<pair>{ int f, s; pair(int f, int s) { this.f = f; this.s = s; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("{" + f + "," + s + "}"); return sb.toString(); } @Override public int compareTo(pair o) { return f - o.f; } public int len() { return s - f + 1; } } public static void main(String[] args) throws IOException { Instant start = Instant.now(); if (args.length == 2) { System.setIn(new FileInputStream(new File("D:\\program\\javaCPEclipse\\CodeForces\\src\\input.txt"))); // System.setOut(new PrintStream(new File("output.txt"))); System.setErr(new PrintStream(new File("D:\\program\\javaCPEclipse\\CodeForces\\src\\error.txt"))); DEBUG = true; } fs = new Reader(); pw = new PrintWriter(System.out); int t = fs.nextInt(); while (t-- > 0) { solve(); } Instant end = Instant.now(); if (DEBUG) { pw.println(Duration.between(start, end)); } pw.close(); } static void sort(int a[]) { ArrayList<Integer> l = new ArrayList<Integer>(); for (int x : a) l.add(x); Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } public static void print(long a, long b, long c, PrintWriter pw) { pw.println(a + " " + b + " " + c); return; } static class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[][] read2Array(int n, int m) { int a[][] = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = nextInt(); } } return a; } } }
0
Non-plagiarised
810cf242
968c1e7e
import java.util.*; import java.io.*; public class E1547{ static int inf = 2*(int)Math.pow(10,9); static FastScanner fs = null; public static void main(String[] args) { fs = new FastScanner(); 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(); } int time[] = new int[n]; Arrays.fill(time,inf); for(int i=0;i<k;i++){ time[pos[i]-1] = fs.nextInt(); } for(int i=0;i<n;i++){ if(i==0){ continue; } else{ time[i] = Math.min(time[i],time[i-1]+1); } } for(int i=n-2;i>=0;i--){ time[i] = Math.min(time[i],1+time[i+1]); } for(int i=0;i<n;i++){ System.out.print(time[i]+" "); } System.out.println(); } } 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.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()); } } }
0
Non-plagiarised
8e990c75
96b3758b
import java.util.Scanner; public class Armchairs { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); int[] chairs = new int[num]; int numOne = 0; for (int i = 0; i < num; i++) { chairs[i] = scanner.nextInt(); if (chairs[i] == 1) numOne++; } if (numOne == 0) { System.out.println(0); return; } int one = 0; int zero = 0; int[] ones = new int[numOne]; int[] zeros = new int[num - numOne]; for (int i = 0; i < num; i++) { if (chairs[i] == 0) zeros[zero++] = i; else ones[one++] = i; } long[][] nums = new long[numOne][num - numOne]; for (int c = 0; c < num - numOne; c++) nums[0][c] = Math.abs(ones[0] - zeros[c]); for (int r = 1; r < numOne; r++) { long min = nums[r - 1][r - 1]; for (int c = r; c < num - numOne; c++) { min = Math.min(nums[r - 1][c - 1], min); nums[r][c] = min + Math.abs(ones[r] - zeros[c]); } } Long result = Long.MAX_VALUE; for (long min: nums[numOne - 1]) { if (min > 0) result = Math.min(result, min); } System.out.println(result); } }
import java.io.*; import java.util.*; public class D { static int[][] dp; static int solve(int i,int j,ArrayList<Integer> A,ArrayList<Integer> B) { if(i==A.size()) { return 0; } if(j==B.size()) { return 1000000009; } if(dp[i][j]!=-1) { return dp[i][j]; } int ans=1000000009; int a=A.get(i); int b=B.get(j); ans=Math.min(ans, Math.abs(a-b)+solve(i+1,j+1,A,B)); ans=Math.min(ans, solve(i,j+1,A,B)); return dp[i][j]=ans; } public static void main(String[] args){ FastReader sc=new FastReader(); int t=1; while(t-->0) { int n=sc.nextInt(); int[] a=new int[n]; ArrayList<Integer> A=new ArrayList<>(); ArrayList<Integer> B=new ArrayList<>(); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); if(a[i]==1) { A.add(i); } else { B.add(i); } } dp=new int[5010][5010]; for(int i=0;i<5010;i++) { Arrays.fill(dp[i], -1); } System.out.println(solve(0,0,A,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
2d1553b5
656d0f78
// Main Code at the Bottom import java.util.*; import java.io.*; public class Interactive{ static long MOD=(long)1e9+7; //debug static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static Scanner sc = new Scanner(System.in); //Global variables and functions static long query(int l, int r) { System.out.println("? " + l + " " + r); return sc.nextLong(); } static int query(int i) { System.out.println("? " + i); return sc.nextInt(); } //Main function(The main code starts from here) public static void main (String[] args) throws java.lang.Exception { int test=1; test=sc.nextInt(); while(test-->0) { int n = sc.nextInt(); int ans[] = new int[n]; for(int i = 1; i <= n; i++) { if(ans[i - 1] != 0) continue; HashSet<Integer> set = new HashSet<>(); ArrayList<Integer> arr = new ArrayList<>(); while(true) { int x = query(i); if(set.contains(x)) break; set.add(x); arr.add(x - 1); } for(int j = 0; j < arr.size(); j++) { ans[arr.get(j)] = arr.get((j + 1) % arr.size()) + 1; } } System.out.print("! "); for(int x: ans) System.out.print(x+" "); System.out.println(); } } }
import java.awt.Container; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { FastScanner input = new FastScanner(); int tc = input.nextInt(); work: while (tc-- > 0) { int n = input.nextInt(); int ans[] = new int[n]; for (int i = 1; i <=n; i++) { if(ans[i-1]!=0) { // System.out.println("NO"); continue ; } HashSet<Integer> set = new HashSet<>(); ArrayList<Integer> a = new ArrayList<>(); while(true) { System.out.println("? "+i); int value = input.nextInt(); if(set.contains(value)) { break; } a.add(value-1); set.add(value); } for (int j = 0; j <a.size(); j++) { ans[a.get(j)] = a.get((j+1)%a.size())+1; } // System.out.println(a); } System.out.print("! "); for (int an : ans) { System.out.print(an+" "); } System.out.println(""); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
1
Plagiarised
31cdf5fe
f0801d53
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while(t-- > 0) { int n = scanner.nextInt(); int[] time = new int[n]; int[] h = new int[n]; for(int i = 0; i < n; i++) { time[i] = scanner.nextInt(); } for(int j = 0; j < n; j++) { h[j] = scanner.nextInt(); } long prevMTime = time[n-1]; long prevMHealth = h[n-1]; long manaRequired = 0; long currMTime = time[n-1]; long currMHealth = h[n-1]; long spellCastingTime = prevMTime - prevMHealth + 1; for(int i = n-2; i >= 0; i--) { currMHealth = h[i]; currMTime = time[i]; //spellCastingTime = prevMTime - prevMHealth + 1; if(spellCastingTime <= currMTime && currMTime - spellCastingTime + 1 < currMHealth) { spellCastingTime = currMTime - currMHealth + 1; } else if(spellCastingTime > currMTime) { long hm = (prevMTime - spellCastingTime) + 1; manaRequired += (hm * (hm+1)) / 2; prevMTime =currMTime; prevMHealth = currMHealth; spellCastingTime = prevMTime - prevMHealth + 1; } } long hm = (prevMTime - spellCastingTime) + 1; manaRequired += (hm * (hm+1)) / 2; System.out.println(manaRequired); } } }
import java.util.*; import java.io.*; public class hmm { static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws Exception { int t =sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int []k = sc.nextIntArray(n); int h[]=sc.nextIntArray(n); long mana = 0; pair cur = new pair(k[n-1]-h[n-1]+1,k[n-1]); for(int i=n-1;i>=0;i--) { int s = k[i]; int start = s-h[i]+1; if(s>=cur.x) { cur.x = Math.min(start, cur.x); } else { long x = cur.y - cur.x +1; mana += x*(x+1)/2; cur.x = start; cur.y = s; } } long x = cur.y - cur.x +1; mana += x*(x+1)/2; pw.println(mana); } 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
abd16ff0
db1ef8b3
import java.util.*; public class Solve{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); long ans=Long.MAX_VALUE; long pre=0; PriorityQueue<Long> epq=new PriorityQueue<>(); PriorityQueue<Long> opq=new PriorityQueue<>(); for(int i=0;i<n;i++){ long a=sc.nextInt(); if(i%2==0)opq.add(a); else epq.add(a); pre+=a; if(i>0) ans=Math.min(ans,pre+opq.peek()*(n-opq.size())+epq.peek()*(n-epq.size())); } System.out.println(ans); } } }
import java.util.*; import java.io.*; import java.math.*; /** * * @Har_Har_Mahadev */ public class C { private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353; private static int N = 0; public static void process() throws IOException { int n = sc.nextInt(); PriorityQueue<Integer> odd = new PriorityQueue<Integer>(); PriorityQueue<Integer> even = new PriorityQueue<Integer>(); int arr[] = sc.readArray(n); long min = INF; long sumOdd = 0; long sumEven = 0; for (int i = 0; i < n; i++) { int x = arr[i]; if (i % 2 == 0) { even.add(x); sumEven += x; } else { odd.add(x); sumOdd += x; } if (i > 0) { long ans = 0; ans += 1l * even.peek() * (n - (even.size())); ans += 1l * odd.peek() * (n - (odd.size())); ans += sumOdd + sumEven; min = Math.min(ans, min); } } System.out.println(min); } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= static FastScanner sc; static PrintWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new PrintWriter(System.out); } else { sc = new FastScanner(100); out = new PrintWriter("output.txt"); } int t = 1; t = sc.nextInt(); while (t-- > 0) { process(); } out.flush(); out.close(); } static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Integer.compare(this.x, o.x); } // @Override // public boolean equals(Object o) { // if (this == o) return true; // if (!(o instanceof Pair)) return false; // Pair key = (Pair) o; // return x == key.x && y == key.y; // } // // @Override // public int hashCode() { // int result = x; // result = 31 * result + y; // return result; // } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static void println(Object o) { out.println(o); } static void println() { out.println(); } static void print(Object o) { out.print(o); } static void pflush(Object o) { out.println(o); out.flush(); } 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 int max(int x, int 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 int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static int lower_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(System.in)); } FastScanner(int a) throws FileNotFoundException { br = new BufferedReader(new FileReader("input.txt")); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) throws IOException { int[] A = new int[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextInt(); } return A; } long[] readArrayLong(int n) throws IOException { long[] A = new long[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextLong(); } return A; } } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } }
0
Non-plagiarised
2560850b
76791c9f
//package notassigning; import java.util.*; import java.io.*; public class notassigning { public static void main(String[] args) throws IOException { BufferedReader fin = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(fin.readLine()); StringBuilder fout = new StringBuilder(); while(t-- > 0) { int n = Integer.parseInt(fin.readLine()); boolean isValid = true; int start = -1; ArrayList<ArrayList<Integer>> c = new ArrayList<ArrayList<Integer>>(); for(int i = 0; i < n; i++) { c.add(new ArrayList<Integer>()); } HashMap<ArrayList<Integer>, Integer> order = new HashMap<ArrayList<Integer>, Integer>(); for(int i = 0; i < n - 1; i++) { StringTokenizer st = new StringTokenizer(fin.readLine()); int a = Integer.parseInt(st.nextToken()) - 1; int b = Integer.parseInt(st.nextToken()) - 1; order.put(new ArrayList<Integer>(Arrays.asList(a, b)), i); c.get(a).add(b); c.get(b).add(a); if((c.get(a).size() == 3 || c.get(b).size() == 3) && isValid) { isValid = false; } } if(!isValid) { fout.append("-1\n"); continue; } //locate the start of the chain for(int i = 0; i < n; i++) { if(c.get(i).size() == 1) { start = i; break; } } int[] ans = new int[n - 1]; int cur = start; int next = c.get(start).get(0); int prime = 2; while(true) { int index = 0; if(order.containsKey(new ArrayList<Integer>(Arrays.asList(cur, next)))) { index = order.get(new ArrayList<Integer>(Arrays.asList(cur, next))); } else { index = order.get(new ArrayList<Integer>(Arrays.asList(next, cur))); } ans[index] = prime; if(c.get(next).size() == 1) { break; } prime = prime == 3? 2 : 3; int prev = cur; cur = next; next = c.get(cur).get(0) == prev? c.get(cur).get(1) : c.get(cur).get(0); } for(int i : ans) { fout.append(i).append(" "); } fout.append("\n"); } System.out.print(fout); } }
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); //int a = 1; int t; t = in.nextInt(); //t = 1; while (t > 0) { //out.print("Case #"+(a++)+": "); solver.call(in,out); t--; } out.close(); } static class TaskA { Map<Integer, ArrayList<Integer>> ans; Map<Integer,Boolean> visited; Map<Integer, List<Integer>> map; public void call(InputReader in, PrintWriter out) { int n = in.nextInt(); answer[] arr = new answer[n]; visited = new HashMap<>(); ans = new HashMap<>(); int u, v; map = new HashMap<>(); for (int i = 0; i < n - 1; i++) { arr[i] = new answer(in.nextInt(), in.nextInt()); u = arr[i].a; v = arr[i].b; map.putIfAbsent(u, new ArrayList<>()); map.get(u).add(v); map.putIfAbsent(v, new ArrayList<>()); map.get(v).add(u); } int a = 0; for(Integer i : map.keySet()){ if(map.get(i).size() > 2){ out.println(-1); return; } if(map.get(i).size()==1){ a = i; } } dfs(a, -1, 0); int[] ans1 = new int[n - 1]; for(int i = 0; i < n-1; i++){ u = arr[i].a; v = arr[i].b; if(ans.getOrDefault(u, null)!=null && ans.get(u).get(0)==v){ if(ans.get(u).get(1)==0){ ans1[i] = 2; } else{ ans1[i] = 5; } } else{ if(ans.get(v).get(1)==0){ ans1[i] = 2; } else{ ans1[i] = 5; } } } for(Integer i : ans1){ out.print(i+" "); } out.println(); } public void dfs(int child, int par, int c){ if(par!=-1){ ans.putIfAbsent(par, new ArrayList<>()); ans.get(par).add(child); ans.get(par).add(c); } visited.put(child, true); for(Integer i : map.get(child)){ if(!visited.getOrDefault(i, false)){ dfs(i, child, c^1); } } } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class answer implements Comparable<answer>{ int a, b; public answer(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return Integer.compare(this.a, o.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 (o.a - this.a); } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (Long i:a) l.add(i); l.sort(Collections.reverseOrder()); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static final Random random=new Random(); static void shuffleSort(int[] a) { int n = a.length; for (int i=0; i<n; i++) { int oi= random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
0
Non-plagiarised
81fb6415
b3d4c5ca
import java.math.BigInteger; import java.util.*; import java.io.*; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; public class CodeForces { public void run() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); next : while (t-- > 0) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); long k = Long.parseLong(st.nextToken()); Long[] a = new Long[n]; st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { a[i] = Long.parseLong(st.nextToken()); } Arrays.sort(a); long ans = Long.MAX_VALUE; long[] lsum = new long[n + 1]; for (int i = 0; i < n; i++) { lsum[i + 1] = lsum[i] + a[i]; } for (long y = 0; y < n; y++) { long x = 0; if ((k - lsum[n - (int)y] + a[0]) >= 0) { x = (k - lsum[n - (int)y] + a[0]) / (y + 1); } else { if ((k - lsum[n - (int)y] + a[0]) % (y + 1) == 0) { x = (k - lsum[n - (int)y] + a[0]) / (y + 1); } else { x = (k - lsum[n - (int)y] + a[0]) / (y + 1) - 1; } } x = a[0] - x; ans = Math.min(ans, Math.max(0, x) + y); } System.out.println(ans); } } public static void main(String[] args) throws Exception { new CodeForces().run(); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static long floor(long a, long b) { long res = a / b; while(res * b > a) res--; return res; } 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) { StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); long k = Long.parseLong(st.nextToken()); st = new StringTokenizer(br.readLine()); Long[] p = new Long[n]; for(int i = 0 ;i<n;i++) { p[i] = Long.parseLong(st.nextToken()); } Arrays.sort(p); long[] sums = new long[n+1]; for(int i=0;i<n;i++) sums[i+1] = sums[i] + p[i]; long ans = Long.MAX_VALUE; for(int y=0;y<n;y++) { long x = p[0] - floor(k - sums[n-y] + p[0], y+1); ans = Math.min(Math.max(x, 0) + y, ans); } System.out.println(ans); } } }
0
Non-plagiarised
51151974
7d12d33c
import java.io.*; import java.util.*; public class Solution{ public static void main (String[] args) throws java.lang.Exception { FastReader sc = new FastReader(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int testCase = sc.nextInt(); while (testCase-->0){ int n = sc.nextInt(); String[] strArr = new String[n]; for(int i=0; i<n; i++) { strArr[i]=sc.nextLine(); } int[] total = new int[5]; ArrayList<int[]> al = new ArrayList<>(); for(int i=0; i<n; i++){ int[] arr= new int[5]; for(int j=0; j<strArr[i].length(); j++){ arr[strArr[i].charAt(j)-'a']++; } for(int j=0; j<5; j++){ total[j]+=arr[j]; } al.add(arr); } int ans=0; for(int i=0; i<5; i++){ ArrayList<Integer> all = new ArrayList<>(); for(int j=0; j<n; j++){ all.add(strArr[j].length()-2*al.get(j)[i]); } java.util.Collections.sort(all); int c=0, d=0; for(int j=0; j<n; j++){ c+=all.get(j); if(c<0) d=j+1; } ans = Math.max(ans,d); } System.out.println(ans); } } // Fast Reader Class 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 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]; for(int i =0;i<n;i++){ words[i] = sc.nextLine(); } 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
3850468c
403e3270
import java.io.*; import java.util.*; public class solution { static long cr[][]=new long[1001][1001]; //static double EPS = 1e-7; static long mod=1000000007; public static void main(String[] args) { FScanner sc = new FScanner(); //Arrays.fill(prime, true); //sieve(); //ncr(); int t=sc.nextInt(); StringBuilder sb = new StringBuilder(); while(t-->0) { int n=sc.nextInt(); long arr[]=new long[n]; long dp[]=new long[n]; long sum=0; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); dp[i]=arr[i]; } long l=0;long r=1000000000; long ans=0; while(l<=r) { long mid=(l+r)/2; dp=Arrays.copyOf(arr, n); if(solve(arr,dp,mid,n) ) { ans=mid; l=mid+1; } else r=mid-1; } sb.append(ans); sb.append("\n"); } System.out.println(sb.toString()); } public static boolean solve(long arr[],long dp[],long sum,int n) { for(int i=n-1;i>1;i--) { if(dp[i]<=sum) continue; long val=Math.min(arr[i],dp[i]-sum)/3; dp[i-1]+=val; dp[i-2]+=2*val; } long min=Integer.MAX_VALUE; for(int i=0;i<n;i++) { min=Math.min(min,dp[i]); } if(min>=sum) return true; else return false; } public static long gcd(long a,long b) { return b==0 ? a:gcd(b,a%b); } /* public static void sieve() { prime[0]=prime[1]=false; int n=1000000; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } */ public static void ncr() { cr[0][0]=1; for(int i=1;i<=1000;i++) { cr[i][0]=1; for(int j=1;j<i;j++) { cr[i][j]=(cr[i-1][j-1]+cr[i-1][j])%mod; } cr[i][i]=1; } } } class pair //implements Comparable<pair> { int a;int b; pair(int a,int b) { this.b=b; this.a=a; } } class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(next()); } double nextDouble(){ return Double.parseDouble(next()); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.text.DecimalFormat; import java.util.*; //import sun.nio.fs.RegistryFileTypeDetector; public class Codeforces { static int mod= 998244353; public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); int t=fs.nextInt(); outer:while(t-->0) { int n=fs.nextInt(); int arr[]=fs.readArray(n); int l=0, r=1000000000; while(l<r) { int mid=(l+r+1)/2; if(check(arr,mid)) { l=mid; } else r=mid-1; } out.println(l); // System.out.println(check(arr,13)); } out.close(); } static boolean check(int arr[],int min) { int n=arr.length; int brr[]=new int[n]; for(int i=n-1;i>1;i--) { if(brr[i]+arr[i]<min) return false; int cur= Math.min(arr[i], arr[i]+brr[i]-min); // System.out.println(cur+" "); int d=cur/3; // System.out.println(d); brr[i-1]+=d; brr[i-2]+=2*d; } return (arr[0]+brr[0]>=min)&&(arr[1]+brr[1]>=min); } static long pow(long a,long b) { if(b<0) return 1; long res=1; while(b!=0) { if((b&1)!=0) { res*=a; res%=mod; } a*=a; a%=mod; b=b>>1; } return res; } static int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } static long nck(int n,int k) { if(k>n) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { // return fact[(int)n]; long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n); int temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort Arrays.sort(a); } // Use this to input code since it is faster than a Scanner 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()); } long[] lreadArray(int n) { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } 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
6bcc5afd
734a94be
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.io.*; import java.math.*; import java.util.*; public class test { static class Pair{ long x; long y; Pair(long x,long y){ this.x = x; this.y = y; } } static class Compare { void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.x!=p2.x) { return (int)(p1.x - p2.x); } else { return (int)(p1.y - p2.y); } } }); // for (int i = 0; i < n; i++) { // System.out.print(arr[i].x + " " + arr[i].y + " "); // } // System.out.println(); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { 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 solve(int dp[][] , ArrayList<Integer> one , ArrayList<Integer> zero , int i,int j) { int n=one.size(); int m=zero.size(); if(i>=n) return 0; else if(j>=m) return Integer.MAX_VALUE; else if(dp[i][j]!=-1) return dp[i][j]; int val1 = Math.abs(one.get(i)-zero.get(j))+solve(dp, one ,zero ,i+1,j+1); int val2 = solve(dp , one , zero , i,j+1); dp[i][j]= Math.min(val1,val2); return dp[i][j]; } public static void main(String args[]) throws Exception { Scanner sc = new Scanner(); StringBuffer res = new StringBuffer(); int tc = 1; while(tc-->0) { int n = sc.nextInt(); ArrayList<Integer> one = new ArrayList<>(); ArrayList<Integer> zero = new ArrayList<>(); for(int i=0;i<n;i++) { int x = sc.nextInt(); if(x==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(zero.get(i-1)-one.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))); } } System.out.println(dp[one.size()][zero.size()]); } System.out.println(res); } }
1
Plagiarised
9069684f
fffc11ee
import java.io.*; import java.util.*; public class Test { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static boolean[] vis; static ArrayList<Integer>[] adj; static int[] l, r; static long[][] dp; @SuppressWarnings("unchecked") public static void main(String[] args) throws IOException { int T = readInt(); for (int t = 0; t < T; t++) { int n = readInt(); l = new int[n + 1]; r = new int[n + 1]; for (int i = 1; i <= n; i++) { l[i] = readInt(); r[i] = readInt(); } adj = new ArrayList[n + 1]; for (int i = 1; i <= n; i++) adj[i] = new ArrayList<>(); for (int i = 0; i < n - 1; i++) { int u = readInt(), v = readInt(); adj[u].add(v); adj[v].add(u); } dp = new long[n + 1][2]; vis = new boolean[n + 1]; vis[1] = true; dfs(1); System.out.println(Math.max(dp[1][0], dp[1][1])); } } static void dfs(int u) { for (int x : adj[u]) { if (!vis[x]) { vis[x] = true; dfs(x); dp[u][0] += Math.max(dp[x][0] + Math.abs(l[u] - l[x]), dp[x][1] + Math.abs(l[u] - r[x])); dp[u][1] += Math.max(dp[x][0] + Math.abs(r[u] - l[x]), dp[x][1] + Math.abs(r[u] - r[x])); } } } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } static int readInt() throws IOException { return Integer.parseInt(next()); } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; public class C { private static final boolean TEST_MODE = true; private static Node[] tree; private static long[] lWt, rWt; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = getInt(br); for (int t=0; t<T; t++) { int N = getInt(br); tree = new Node[N+1]; for (int v = 1; v <= N; v++) { String s = br.readLine(); tree[v] = new Node(s); } for (int e=1; e<=N-1; e++) { int[] arr = getIntArray(2, br); int u = arr[0]; int v = arr[1]; tree[u].addNbr(v); tree[v].addNbr(u); } rootTheTree(); lWt = new long[N + 1]; rWt = new long[N + 1]; Arrays.fill(lWt, -1); Arrays.fill(rWt, -1); getMaxBeauty(); } } private static void rootTheTree() { // Arbitrarily pick the root as #1. Node root = tree[1]; root.parent = 0; Queue<Integer> q = new LinkedList<>(); q.add(1); // Constraint: Every node that is in the queue has parent already assigned. while (!q.isEmpty()) { int currId = q.poll(); Node currNode = tree[currId]; // Add all neighbours as children. for (int nbrId : currNode.nbrs) { if (nbrId == currNode.parent) { continue; } tree[nbrId].parent = currId; currNode.addChild(nbrId); q.add(nbrId); } // currNode.nbrs.clear(); } } private static void getMaxBeauty() throws Exception { Stack<Integer> stack = new Stack<>(); stack.add(1); while (!stack.isEmpty()) { int currId = stack.pop(); Node curr = tree[currId]; // Is leaf ? if (curr.children.size() == 0) { lWt[currId] = 0; rWt[currId] = 0; continue; } // Are all children processed ? boolean allChildrenProcessed = (lWt[curr.children.get(0)] >= 0); if (allChildrenProcessed) { lWt[currId] = 0; rWt[currId] = 0; // Compute lwt and rwt maximized against each child. for (int childId : curr.children) { Node childNode = tree[childId]; long ll = Math.abs(curr.left - childNode.left) + lWt[childId]; long lr = Math.abs(curr.left - childNode.right) + rWt[childId]; lWt[currId] += Math.max(ll, lr); long rl = Math.abs(curr.right - childNode.left) + lWt[childId]; long rr = Math.abs(curr.right - childNode.right) + rWt[childId]; rWt[currId] += Math.max(rl, rr); } } else { // Add all unvisited children. stack.add(currId); stack.addAll(curr.children); } } long res = Math.max(lWt[1], rWt[1]); System.out.println(res); } private static int getLeftId(int nodeId) { return tree[nodeId].left; } private static int getRightId(int nodeId) { return tree[nodeId].right; } private static Integer getInt(BufferedReader br) throws Exception { return Integer.parseInt(br.readLine()); } private static Long getLong(BufferedReader br) throws Exception { return Long.parseLong(br.readLine()); } private static int[] getIntArray(int N, BufferedReader br) throws Exception { String s = br.readLine(); String[] tokens = s.split(" "); int[] result = new int[N]; for (int i=0; i<N; i++) { result[i] = Integer.parseInt(tokens[i]); } return result; } private static long[] getLongArray(int N, BufferedReader br) throws Exception { String s = br.readLine(); String[] tokens = s.split(" "); long[] result = new long[N+1]; for (int i=0; i<N; i++) { result[i+1] = Long.parseLong(tokens[i]); } return result; } private static void printArray(String message, Object[] arr) { if (!TEST_MODE) { return; } System.out.print(message + " : "); for (int i=0; i<arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } } class Node { int parent = -1; List<Integer> nbrs = new ArrayList<>(); List<Integer> children = new ArrayList<>(); int left, right; public Node(int l, int r) { this.left = l; this.right = r; } public Node(String s) { String[] tokens = s.split(" "); this.left = Integer.parseInt(tokens[0]); this.right = Integer.parseInt(tokens[1]); } public void addNbr(int nbr) { nbrs.add(nbr); } public void addChild(int child) { children.add(child); } }
0
Non-plagiarised
54d7c21e
ad4c7a20
import java.io.*; import java.util.*; public class cp { static int mod=(int)1e9+7; // static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static int[] sp; static int size=(int)1e6; static int[] arInt; static long[] arLong; public static void main(String[] args) throws IOException { long tc=sc.nextLong(); // Scanner sc=new Scanner(System.in); // int tc=1; // primeSet=new HashSet<>(); // sieveOfEratosthenes((int)1e6+5); while(tc-->0) { int n=sc.nextInt(); int k[]=new int[n]; int h[]=new int[n]; for(int i=0;i<n;i++) k[i]=sc.nextInt(); for(int i=0;i<n;i++) h[i]=sc.nextInt(); ArrayList<Pair> interval=new ArrayList<Pair>(); ArrayList<Pair> act=new ArrayList<Pair>(); for(int i=0;i<n;i++) interval.add(new Pair(k[i]-h[i]+1,k[i])); Collections.sort(interval); // out.println(interval); act.add(interval.get(0)); for(int i=1;i<n;i++) { Pair p=act.get(act.size()-1); if(p.y<interval.get(i).x) act.add(interval.get(i)); else p.y=Math.max(p.y, interval.get(i).y); } // out.println(act); long mana=0; for(Pair p: act) { long x=p.y-p.x+1; mana+=(x*(x+1))/2; } out.println(mana); // int n=sc.nextInt(); // long days[]=new long[n]; // long power[]=new long[n]; // for (int i = 0; i < power.length; i++) { // days[i]=sc.nextLong(); // } // for (int i = 0; i < power.length; i++) { // power[i]=sc.nextLong(); // // } // // long ans=0; // for(int i=0;i<n;i++) // { // if(i==0) // { // ans+=power[i]*(power[i]+1L)/2L; // continue; // } // // long temp=power[i]*(power[i]+1)/2L; // long temp2=(power[i-1]+days[i]-days[i-1])*(power[i-1]+days[i]-days[i-1]+1L)/2L; // temp2-=power[i-1]*(power[i-1]+1L)/2L; // ans+=Math.min(temp, temp2); //// if(days[i]-days[i-1]<=power[i]) //// { //// ans+=power[i]*(power[i]+1)/2; //// } //// else { //// ans+=power[i]*(power[i]+1)/2; //// ans-=power[i-1]*(power[i-1]+1)/2; //// } // // // } // // out.println(ans); } out.flush(); out.close(); System.gc(); } /* ...SOLUTION ENDS HERE...........SOLUTION ENDS HERE... */ static int util(char a,char b) { int A=a-'0'; int B=b-'0'; return A+B; } static boolean check(int x,int[] rich,int[] poor) { int cnt=0; for(int i=0;i<rich.length;i++) { if(x-1-rich[i]<=cnt && cnt<=poor[i]) cnt++; } return cnt>=x; } static void arrInt(int n) throws IOException { arInt=new int[n]; for (int i = 0; i < arInt.length; i++) { arInt[i]=sc.nextInt(); } } static void arrLong(int n) throws IOException { arLong=new long[n]; for (int i = 0; i < arLong.length; i++) { arLong[i]=sc.nextLong(); } } static ArrayList<Integer> add(int id,int c) { ArrayList<Integer> newArr=new ArrayList<>(); for(int i=0;i<id;i++) newArr.add(arInt[i]); newArr.add(c); for(int i=id;i<arInt.length;i++) { newArr.add(arInt[i]); } return newArr; } // function to find last index <= y static int upper(ArrayList<Integer> arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) <= y) l = mid + 1; else h = mid - 1; } return h; } static int lower(ArrayList<Integer> arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) >= x) h = mid - 1; else l = mid + 1; } return l; } static int N = 501; // Array to store inverse of 1 to N static long[] factorialNumInverse = new long[N + 1]; // Array to precompute inverse of 1! to N! static long[] naturalNumInverse = new long[N + 1]; // Array to store factorial of first N numbers static long[] fact = new long[N + 1]; // Function to precompute inverse of numbers public static void InverseofNumber(int p) { naturalNumInverse[0] = naturalNumInverse[1] = 1; for(int i = 2; i <= N; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse[0] = factorialNumInverse[1] = 1; // Precompute inverse of natural numbers for(int i = 2; i <= N; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to N public static void factorial(int p) { fact[0] = 1; // Precompute factorials for(int i = 1; i <= N; i++) { fact[i] = (fact[i - 1] * (long)i) % p; } } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R, int p) { // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p; return ans; } static String tr(String s) { int now = 0; while (now + 1 < s.length() && s.charAt(now)== '0') ++now; return s.substring(now); } static ArrayList<Integer> ans; static void dfs(int node,Graph gg,int cnt,int k,ArrayList<Integer> temp) { if(cnt==k) return; for(Integer each:gg.list[node]) { if(each==0) { temp.add(each); ans=new ArrayList<>(temp); temp.remove(temp.size()-1); continue; } temp.add(each); dfs(each,gg,cnt+1,k,temp); temp.remove(temp.size()-1); } return; } static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop 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 ArrayList<Integer> commDiv(int a, int b) { // find gcd of a, b int n = gcd(a, b); // Count divisors of n. ArrayList<Integer> Div=new ArrayList<>(); for (int i = 1; i <= Math.sqrt(n); i++) { // if 'i' is factor of n if (n % i == 0) { // check if divisors are equal if (n / i == i) Div.add(i); else { Div.add(i); Div.add(n/i); } } } return Div; } static HashSet<Integer> factors(int x) { HashSet<Integer> a=new HashSet<Integer>(); for(int i=2;i*i<=x;i++) { if(x%i==0) { a.add(i); a.add(x/i); } } return a; } static class Node { int vertex; HashSet<Node> adj; boolean rem; Node(int ver) { vertex=ver; rem=false; adj=new HashSet<Node>(); } @Override public String toString() { return vertex+" "; } } static class Tuple{ int a; int b; int c; public Tuple(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } } //function to find prime factors of n static HashMap<Long,Long> findFactors(long n2) { HashMap<Long,Long> ans=new HashMap<>(); if(n2%2==0) { ans.put(2L, 0L); // cnt++; while((n2&1)==0) { n2=n2>>1; ans.put(2L, ans.get(2L)+1); // } } for(long i=3;i*i<=n2;i+=2) { if(n2%i==0) { ans.put((long)i, 0L); // cnt++; while(n2%i==0) { n2=n2/i; ans.put((long)i, ans.get((long)i)+1); } } } if(n2!=1) { ans.put(n2, ans.getOrDefault(n2, (long) 0)+1); } return ans; } //fenwick tree implementaion static class fwt { int n; long BITree[]; fwt(int n) { this.n=n; BITree=new long[n+1]; } fwt(int arr[], int n) { this.n=n; BITree=new long[n+1]; for(int i = 0; i < n; i++) updateBIT(n, i, arr[i]); } long getSum(int index) { long sum = 0; index = index + 1; while(index>0) { sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(int n, int index,int val) { index = index + 1; while(index <= n) { BITree[index] += val; index += index & (-index); } } void print() { for(int i=0;i<n;i++) out.print(getSum(i)+" "); out.println(); } } class sparseTable{ int n; long[][]dp; int log2[]; int P; void buildTable(long[] arr) { n=arr.length; P=(int)Math.floor(Math.log(n)/Math.log(2)); log2=new int[n+1]; log2[0]=log2[1]=0; for(int i=2;i<=n;i++) { log2[i]=log2[i/2]+1; } dp=new long[P+1][n]; for(int i=0;i<n;i++) { dp[0][i]=arr[i]; } for(int p=1;p<=P;p++) { for(int i=0;i+(1<<p)<=n;i++) { long left=dp[p-1][i]; long right=dp[p-1][i+(1<<(p-1))]; dp[p][i]=Math.max(left, right); } } } long maxQuery(int l,int r) { int len=r-l+1; int p=(int)Math.floor(log2[len]); long left=dp[p][l]; long right=dp[p][r-(1<<p)+1]; return Math.max(left, right); } } //Function to find number of set bits static int setBitNumber(long n) { if (n == 0) return 0; int msb = 0; n = n / 2; while (n != 0) { n = n / 2; msb++; } return msb; } static int getFirstSetBitPos(long n) { return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1; } static ArrayList<Integer> primes; static HashSet<Integer> primeSet; static boolean prime[]; static void sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. prime= new boolean[n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p 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) primeSet.add(i); } } static long mod(long a, long b) { long c = a % b; return (c < 0) ? c + b : c; } static void swap(long arr[],int i,int j) { long temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } static boolean util(int a,int b,int c) { if(b>a)util(b, a, c); while(c>=a) { c-=a; if(c%b==0) return true; } return (c%b==0); } static void flag(boolean flag) { out.println(flag ? "YES" : "NO"); out.flush(); } static void print(int a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print(long a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print_int(ArrayList<Integer> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void print_long(ArrayList<Long> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void printYesNo(boolean condition) { if (condition) { out.println("YES"); } else { out.println("NO"); } } static int LowerBound(int a[], int 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 int lowerIndex(int arr[], int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] >= x) h = mid - 1; else l = mid + 1; } return l; } // function to find last index <= y static int upperIndex(int arr[], int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int upperIndex(long arr[], int n, long y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int UpperBound(int a[], int 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 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 class DisjointUnionSets { int[] rank, parent; int n; // Constructor public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } // Creates n sets with single item in each void makeSet() { for (int i = 0; i < n; i++) parent[i] = i; } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } // Unites the set that includes x and the set // that includes x void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else // if ranks are the same { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } // if(xRoot!=yRoot) // parent[y]=x; } int connectedComponents() { int cnt=0; for(int i=0;i<n;i++) { if(parent[i]==i) cnt++; } return cnt; } } static class Graph { int v; ArrayList<Integer> list[]; Graph(int v) { this.v=v; list=new ArrayList[v+1]; for(int i=1;i<=v;i++) list[i]=new ArrayList<Integer>(); } void addEdge(int a, int b) { this.list[a].add(b); } } // static class GraphMap{ // Map<String,ArrayList<String>> graph; // GraphMap() { // // TODO Auto-generated constructor stub // graph=new HashMap<String,ArrayList<String>>(); // // } // void addEdge(String a,String b) // { // if(graph.containsKey(a)) // this.graph.get(a).add(b); // else { // this.graph.put(a, new ArrayList<>()); // this.graph.get(a).add(b); // } // } // } // static void dfsMap(GraphMap g,HashSet<String> vis,String src,int ok) // { // vis.add(src); // // if(g.graph.get(src)!=null) // { // for(String each:g.graph.get(src)) // { // if(!vis.contains(each)) // { // dfsMap(g, vis, each, ok+1); // } // } // } // // cnt=Math.max(cnt, ok); // } static double sum[]; static long cnt; // static void DFS(Graph g, boolean[] visited, int u) // { // visited[u]=true; // // for(int i=0;i<g.list[u].size();i++) // { // int v=g.list[u].get(i); // // if(!visited[v]) // { // cnt1=cnt1*2; // DFS(g, visited, v); // // } // // } // // // } 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) { // TODO Auto-generated method stub return this.x-o.x; } } static long sum_array(int a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static long sum_array(long a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } 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 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 reverse_array(int a[]) { int n=a.length; int i,t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static void reverse_array(long a[]) { int n=a.length; int i; long t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } // 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 res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static int power(int x, int y) { int res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static long gcd(long a, long b) { if (a == 0) return b; //cnt+=a/b; return gcd(b%a,a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } 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(); } 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 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(); } } static PrintWriter out=new PrintWriter(System.out); static int int_max=Integer.MAX_VALUE; static int int_min=Integer.MIN_VALUE; static long long_max=Long.MAX_VALUE; static long long_min=Long.MIN_VALUE; }
import java.io.*; import java.util.*; public class Practice { // static final long mod=7420738134811L; static int mod=1000000007; static final int size=501; static FastReader sc=new FastReader(System.in); // static Reader sc=new Reader(); // static Scanner sc=new Scanner(System.in); static PrintWriter out=new PrintWriter(System.out); static long[] factorialNumInverse; static long[] naturalNumInverse; static int[] sp; static long[] fact; static ArrayList<Integer> pr; public static void main(String[] args) throws IOException, CloneNotSupportedException { // System.setIn(new FileInputStream("input.txt")); // System.setOut(new PrintStream("output.txt")); // factorial(mod); // InverseofNumber(mod); // InverseofFactorial(mod); // make_seive(); int t=1; t=sc.nextInt(); for(int i=1;i<=t;i++) solve(i); out.close(); out.flush(); // System.out.flush(); // System.exit(0); } static void solve(int CASENO) throws IOException, CloneNotSupportedException { int n=sc.nextInt(); int k[]=new int[n]; int h[]=new int[n]; for(int i=0;i<n;i++) k[i]=sc.nextInt(); for(int i=0;i<n;i++) h[i]=sc.nextInt(); ArrayList<Pair> interval=new ArrayList<Pair>(); ArrayList<Pair> act=new ArrayList<Pair>(); for(int i=0;i<n;i++) interval.add(new Pair(k[i]-h[i]+1,k[i])); Collections.sort(interval); // out.println(interval); act.add(interval.get(0).clone()); for(int i=1;i<n;i++) { Pair p=act.get(act.size()-1); if(p.y<interval.get(i).x) act.add(interval.get(i).clone()); else p.y=Math.max(p.y, interval.get(i).y); } // out.println(act); long mana=0; for(Pair p: act) { long x=p.y-p.x+1; mana+=(x*(x+1))/2; } out.println(mana); } static class Pair implements Cloneable, Comparable<Pair> { int x,y; Pair(int a,int b) { this.x=a; this.y=b; } @Override public boolean equals(Object obj) { if(obj instanceof Pair) { Pair p=(Pair)obj; return p.x==this.x && p.y==this.y; } return false; } @Override public int hashCode() { return Math.abs(x)+500*Math.abs(y); } @Override public String toString() { return "("+x+" "+y+")"; } @Override protected Pair clone() throws CloneNotSupportedException { return new Pair(this.x,this.y); } @Override public int compareTo(Pair a) { long t=(this.x-a.x); if(t!=0) return t>0?1:-1; else return (int)(this.y-a.y); } public void swap() { this.y=this.y+this.x; this.x=this.y-this.x; this.y=this.y-this.x; } } static class Tuple implements Cloneable, Comparable<Tuple> { int x,y,z; Tuple(int a,int b,int c) { this.x=a; this.y=b; this.z=c; } public boolean equals(Object obj) { if(obj instanceof Tuple) { Tuple p=(Tuple)obj; return p.x==this.x && p.y==this.y && p.z==this.z; } return false; } @Override public int hashCode() { return (this.x+501*this.y); } @Override public String toString() { return "("+x+","+y+","+z+")"; } @Override protected Tuple clone() throws CloneNotSupportedException { return new Tuple(this.x,this.y,this.z); } @Override public int compareTo(Tuple a) { int x=this.z-a.z; if(x!=0) return x; int X= this.x-a.x; if(X!=0) return X; return a.y-this.y; } } static void arraySort(int arr[]) { ArrayList<Integer> a=new ArrayList<Integer>(); for (int i = 0; i < arr.length; i++) { a.add(arr[i]); } Collections.sort(a,Comparator.reverseOrder()); for (int i = 0; i < arr.length; i++) { arr[i]=a.get(i); } } static void arraySort(long arr[]) { ArrayList<Long> a=new ArrayList<Long>(); for (int i = 0; i < arr.length; i++) { a.add(arr[i]); } Collections.sort(a); for (int i = 0; i < arr.length; i++) { arr[i]=a.get(i); } } static HashSet<Integer> primeFactors(int n) { HashSet<Integer> ans=new HashSet<Integer>(); if(n%2==0) { ans.add(2); while((n&1)==0) n=n>>1; } for(int i=3;i*i<=n;i+=2) { if(n%i==0) { ans.add(i); while(n%i==0) n=n/i; } } if(n!=1) ans.add(n); return ans; } static void make_seive() { sp=new int[size]; pr=new ArrayList<Integer>(); for (int i=2; i<size; ++i) { if (sp[i] == 0) { sp[i] = i; pr.add(i); } for (int j=0; j<(int)pr.size() && pr.get(j)<=sp[i] && i*pr.get(j)<size; ++j) sp[i * pr.get(j)] = pr.get(j); } } public static void InverseofNumber(int p) { naturalNumInverse=new long[size]; naturalNumInverse[0] = naturalNumInverse[1] = 1; for(int i = 2; i < size; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse=new long[size]; factorialNumInverse[0] = factorialNumInverse[1] = 1; // pre-compute inverse of natural numbers for(int i = 2; i < size; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to 200001 public static void factorial(int p) { fact=new long[size]; fact[0] = 1; for(int i = 1; i < size; i++) fact[i] = (fact[i - 1] * (long)i) % p; } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R) { if(R<0) return 1; // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % mod * factorialNumInverse[N - R]) % mod; return ans; } static int findXOR(int x) //from 0 to x { if(x<0) return 0; if(x%4==0) return x; if(x%4==1) return 1; if(x%4==2) return x+1; return 0; } static boolean isPrime(long x) { if(x==1) return false; if(x<=3) return true; if(x%2==0 || x%3==0) return false; for(int i=5;i<=Math.sqrt(x);i+=2) if(x%i==0) return false; return true; } 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 class Node { int vertex,dis; HashSet<Node> adj; Node(int ver) { vertex=ver; dis=-1; adj=new HashSet<Node>(); } @Override public String toString() { return vertex+" "; } } static class Edge { Node to; int cost; Edge(Node t,int c) { this.to=t; this.cost=c; } @Override public String toString() { return "("+to.vertex+","+cost+") "; } } static long power(long x, long y) { if(x<=0) return 1; long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res%mod; } static long binomialCoeff(long n, long k) { if(n<k) return 0; long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (long i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } 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(); } 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 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(); } public void printarray(int arr[]) { for (int i = 0; i < arr.length; i++) System.out.print(arr[i]+" "); System.out.println(); } } }
1
Plagiarised
bd3051e3
e2e37533
import java.io.*; import java.util.*; public class Main { static BufferedReader br; static PrintWriter pr; static int cin() throws Exception { return Integer.valueOf(br.readLine()); } static int[] split() throws Exception { String[] cmd=br.readLine().split(" "); int[] ans=new int[cmd.length]; for(int i=0;i<cmd.length;i++) { ans[i]=Integer.valueOf(cmd[i]); } return ans; } static long[] splitL() throws IOException { String[] cmd=br.readLine().split(" "); long[] ans=new long[cmd.length]; for(int i=0;i<cmd.length;i++) { ans[i]=Long.valueOf(cmd[i]); } return ans; } static int sum(long n) { int ans=0; while(n!=0) { ans=ans+(int)(n%10); n=n/10; } return ans; } static long gcd(long x,long y) { if(x==0) return y; if(y==0) return x; if(x>y) return gcd(x%y,y); else return gcd(x,y%x); } public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub br=new BufferedReader(new InputStreamReader(System.in)); pr=new PrintWriter(new OutputStreamWriter(System.out)); int cases=cin(); while(cases!=0) { cases--; int[]ar=split(); int n=ar[0]; int m=ar[1]; int[][]mat=new int[n][m]; for(int i=0;i<n;i++) { String s=br.readLine(); for(int j=0;j<m;j++) { mat[i][j]=s.charAt(j)-'0'; } } ArrayList<String>ans=new ArrayList<>(); for(int i=0;i<n-1;i++) { for(int j=0;j<m-1;j++) { if(mat[i][j]==1 && mat[i][j+1]==0 && mat[i+1][j]==0 && mat[i+1][j+1]==0) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==0 && mat[i][j+1]==0 && mat[i+1][j]==0 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==0 && mat[i][j+1]==1 && mat[i+1][j]==0 && mat[i+1][j+1]==0) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==0 && mat[i][j+1]==0 && mat[i+1][j]==1 && mat[i+1][j+1]==0) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==1 && mat[i][j+1]==1 && mat[i+1][j]==1 && mat[i+1][j+1]==0) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); } else if(mat[i][j]==1 && mat[i][j+1]==0 && mat[i+1][j]==1 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==1 && mat[i][j+1]==1 && mat[i+1][j]==0 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==0 && mat[i][j+1]==1 && mat[i+1][j]==1 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==1 && mat[i][j+1]==1 && mat[i+1][j]==1 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==1 && mat[i][j+1]==1 && mat[i+1][j]==0 && mat[i+1][j+1]==0) { ans.add((i+2)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==0 && mat[i][j+1]==0 && mat[i+1][j]==1 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); } else if(mat[i][j]==1 && mat[i][j+1]==0 && mat[i+1][j]==1 && mat[i+1][j+1]==0) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==0 && mat[i][j+1]==1 && mat[i+1][j]==0 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==1 && mat[i][j+1]==0 && mat[i+1][j]==0 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); ans.add((i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==0 && mat[i][j+1]==1 && mat[i+1][j]==1 && mat[i+1][j+1]==0) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } mat[i][j]=mat[i+1][j]=mat[i][j+1]=mat[i+1][j+1]=0; } } System.out.println(ans.size()); for(int i=0;i<ans.size();i++) { System.out.println(ans.get(i)); } } } }
import java.io.*; import java.util.*; public class Main { static BufferedReader br; static PrintWriter pr; static int cin() throws Exception { return Integer.valueOf(br.readLine()); } static int[] split() throws Exception { String[] cmd=br.readLine().split(" "); int[] ans=new int[cmd.length]; for(int i=0;i<cmd.length;i++) { ans[i]=Integer.valueOf(cmd[i]); } return ans; } static long[] splitL() throws IOException { String[] cmd=br.readLine().split(" "); long[] ans=new long[cmd.length]; for(int i=0;i<cmd.length;i++) { ans[i]=Long.valueOf(cmd[i]); } return ans; } static int sum(long n) { int ans=0; while(n!=0) { ans=ans+(int)(n%10); n=n/10; } return ans; } static long gcd(long x,long y) { if(x==0) return y; if(y==0) return x; if(x>y) return gcd(x%y,y); else return gcd(x,y%x); } public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub br=new BufferedReader(new InputStreamReader(System.in)); pr=new PrintWriter(new OutputStreamWriter(System.out)); int cases=cin(); while(cases!=0) { cases--; int[]ar=split(); int n=ar[0]; int m=ar[1]; int[][]mat=new int[n][m]; for(int i=0;i<n;i++) { String s=br.readLine(); for(int j=0;j<m;j++) { mat[i][j]=s.charAt(j)-'0'; } } ArrayList<String>ans=new ArrayList<>(); for(int i=0;i<n-1;i++) { for(int j=0;j<m-1;j++) { if(mat[i][j]==1 && mat[i][j+1]==0 && mat[i+1][j]==0 && mat[i+1][j+1]==0) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==0 && mat[i][j+1]==0 && mat[i+1][j]==0 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==0 && mat[i][j+1]==1 && mat[i+1][j]==0 && mat[i+1][j+1]==0) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==0 && mat[i][j+1]==0 && mat[i+1][j]==1 && mat[i+1][j+1]==0) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==1 && mat[i][j+1]==1 && mat[i+1][j]==1 && mat[i+1][j+1]==0) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); } else if(mat[i][j]==1 && mat[i][j+1]==0 && mat[i+1][j]==1 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==1 && mat[i][j+1]==1 && mat[i+1][j]==0 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==0 && mat[i][j+1]==1 && mat[i+1][j]==1 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==1 && mat[i][j+1]==1 && mat[i+1][j]==1 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==1 && mat[i][j+1]==1 && mat[i+1][j]==0 && mat[i+1][j+1]==0) { ans.add((i+2)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==0 && mat[i][j+1]==0 && mat[i+1][j]==1 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); } else if(mat[i][j]==1 && mat[i][j+1]==0 && mat[i+1][j]==1 && mat[i+1][j+1]==0) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==0 && mat[i][j+1]==1 && mat[i+1][j]==0 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==1 && mat[i][j+1]==0 && mat[i+1][j]==0 && mat[i+1][j+1]==1) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)); ans.add((i+1)+" "+(j+2)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } else if(mat[i][j]==0 && mat[i][j+1]==1 && mat[i+1][j]==1 && mat[i+1][j+1]==0) { ans.add((i+1)+" "+(j+1)+" "+(i+1)+" "+(j+2)+" "+(i+2)+" "+(j+2)); ans.add((i+1)+" "+(j+1)+" "+(i+2)+" "+(j+1)+" "+(i+2)+" "+(j+2)); } mat[i][j]=mat[i+1][j]=mat[i][j+1]=mat[i+1][j+1]=0; } } System.out.println(ans.size()); for(int i=0;i<ans.size();i++) { System.out.println(ans.get(i)); } } } }
1
Plagiarised
24afd00e
424c930b
/* 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 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); Task solver = new Task(); solver.solve(in, out); out.close(); } // main solver static class Task{ double eps= 0.00000001; static final int MAXN = 10000001; // stores smallest prime factor for every number static int spf[] = new int[MAXN]; Map<Integer,Set<Integer>> dp= new HashMap<>(); // Calculating SPF (Smallest Prime Factor) for every // number till MAXN. // Time Complexity : O(nloglogn) public void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) // marking smallest prime factor for every // number to be itself. spf[i] = i; // separately marking spf for every even // number as 2 for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { // checking if i is prime if (spf[i] == i) { // marking SPF for all numbers divisible by i for (int j=i*i; j<MAXN; j+=i) // marking spf[j] if it is not // previously marked if (spf[j]==j) spf[j] = i; } } } // A O(log n) function returning primefactorization // by dividing by smallest prime factor at every step public Set<Integer> getFactorization(int x) { if(dp.containsKey(x)) return dp.get(x); Set<Integer> ret = new HashSet<>(); while (x != 1) { if(spf[x]!=2) ret.add(spf[x]); x = x / spf[x]; } dp.put(x,ret); return ret; } // function to find first index >= x public int lowerIndex(List<Integer> arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) >= x) h = mid - 1; else l = mid + 1; } return l; } // function to find last index <= y public int upperIndex(List<Integer> arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) <= y) l = mid + 1; else h = mid - 1; } return h; } // function to count elements within given range public int countInRange(List<Integer> arr, int n, int x, int y) { // initialize result int count = 0; count = upperIndex(arr, n, y) - lowerIndex(arr, n, x) + 1; return count; } long[] dpl, dpr; List<Integer>[] adj; int[] l,r; public void solve(InputReader in, PrintWriter out) { int t= in.nextInt(); while(t-->0){ int n= in.nextInt(); l= new int[n]; r= new int[n]; adj= new List[n]; for(int i=0;i<n;i++){ l[i]= in.nextInt(); r[i]= in.nextInt(); } for(int i=0;i<n;i++) adj[i]= new ArrayList<>(); for(int i=0;i<n-1;i++){ int u= in.nextInt()-1, v= in.nextInt()-1; adj[u].add(v); adj[v].add(u); } dpl= new long[n]; dpr= new long[n]; dfs(0,-1); out.println(Math.max(dpl[0],dpr[0])); } } public void dfs(int u, int p){ long suml=0, sumr=0; for(int v: adj[u] ){ if(v==p) continue; dfs(v,u); suml+= Math.max((long)Math.abs((long)l[u]-(long)l[v])+dpl[v], (long)Math.abs((long)l[u]-(long)r[v])+dpr[v]); sumr+= Math.max((long)Math.abs((long)r[u]-(long)l[v])+dpl[v],(long) Math.abs((long)r[u]-(long)r[v])+dpr[v]); } dpl[u]= suml; dpr[u]= sumr; } // public static class compareL implements Comparator<Tuple>{ // @Override // public int compare(Tuple t1, Tuple t2) { // return t2.l - t1.l; // } // } public int _gcd(int a, int b) { if(b == 0) { return a; } else { return _gcd(b, a % b); } } } // static class Data{ // List<Integer> arr; // BigInteger prod=new BigInteger(-1); // public Data(List<Integer> arr, BigInteger prod) { // this.arr= arr; // this.prod= prod; // } // } static class Tuple implements Comparable<Tuple>{ int x, y, z; public Tuple(int x, int y, int z){ this.x= x; this.y= y; this.z=z; } @Override public int compareTo(Tuple o){ return this.x-o.x; } } static class Pair implements Comparable<Pair>{ public int x; public int y; public Pair(int x, int y){ this.x= x; this.y= y; } @Override public int compareTo(Pair o) { return this.x-o.x; } } // fast input reader class; static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public String nextToken() { while (st == null || !st.hasMoreTokens()) { String line = null; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble(){ return Double.parseDouble(nextToken()); } public long nextLong(){ return Long.parseLong(nextToken()); } } }
0
Non-plagiarised
a60fba84
c695a974
/*input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 */ import java.util.*; import java.lang.*; import java.io.*; public class Main { static PrintWriter out; static int MOD = 1000000007; static FastReader scan; /*-------- I/O using 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; } 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(); long arr[] = new long[n]; lIA(arr); long ans = (long)(n*(arr[0] + arr[1])); long sum = arr[0] + arr[1]; long emin = arr[0], omin = arr[1]; for(int i=2; i<n; i++){ sum += arr[i]; if(i%2==0){ emin = Math.min(arr[i], emin); } else{ omin = Math.min(arr[i], omin); } long temp = sum - emin - omin; if(i%2==0) temp += (n-i/2)*emin + (n-i/2+1)*omin; else temp += (n-(i-1)/2)*(emin + omin); ans = Math.min(ans, temp); } pn(ans); } out.flush(); out.close(); } }
// これを翻訳している間、あなたはあなたの人生のいくつかの貴重な瞬間を無駄にしました import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Solution { static PrintWriter fop = new PrintWriter(System.out); public static void main(String[] args) { FastScanner fsca = new FastScanner(); int i, j = 0; int t = fsca.nextInt(); u: while (t-- > 0) { int n = fsca.nextInt(); int a[] = new int[n]; for(i=0;i<n;i++){ a[i] = fsca.nextInt(); } int y = Integer.MAX_VALUE; long sum[] = new long[n]; long min1[] = new long[n]; sum[0] = a[0]; sum[1] = a[1]; min1[0] = a[0]; min1[1] = a[1]; for(i=2;i<n;i++){ sum[i] = sum[i-2] + a[i]; min1[i] = Math.min(min1[i-2],a[i]); } long val = sum[0]*n + sum[1]*n; long min = sum[0]*n + sum[1]*n; for(i=2;i<n;i++){ val = sum[i] + min1[i]*(n-(i+2)/2) + sum[i-1] + min1[i-1]*(n-(i+1)/2); min = Math.min(min,val); } fop.println(min); } fop.flush(); fop.close(); } /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ // template starts from here : 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 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 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 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[][] readMat(int n, int m) { int a[][] = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = nextInt(); return a; } char[][] readCharMat(int n, int m) { char a[][] = new char[n][m]; for (int i = 0; i < n; i++) { String s = next(); for (int j = 0; j < m; j++) a[i][j] = s.charAt(j) ; } return a; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } static void print(int a[], int n) { for (int i = 0; i < n; i++) fop.append((a[i]) + " "); // fop.append("\n") ; } static void print(long a[], int n) { for (int i = 0; i < n; i++) fop.append((a[i]) + " "); // fop.append("\n") ; } }
0
Non-plagiarised
54488276
f4757480
import java.io.*; import java.util.*; public class C { static int n; public static void main (String[] args) throws IOException { Kattio io = new Kattio(); int t = io.nextInt(); for (int ii=0; ii<t; ii++) { n = io.nextInt(); String[] arr = new String[n]; for (int i=0; i<n; i++) { String str = io.next(); arr[i] = str; } char[] chars = new char[]{'a','b','c','d','e'}; int ans = -1; for (int i=0; i<5; i++) { ans = Math.max(ans, solve(arr, chars[i])); } System.out.println(ans); } } static int solve(String[] arr, char c) { //System.out.println("Comparing based on " + c); Arrays.sort(arr, new Comp(c)); int good = 0; int total = 0; int ret = 0; for (int i=0; i<n; i++) { //System.out.println(good + " " + total); for (int j=0; j<arr[i].length(); j++) { if (arr[i].charAt(j) == c) good++; } total += arr[i].length(); if (2 * good > total) { ret++; } else { return ret; } } return ret; } static class Comp implements Comparator<String> { char c; public Comp (char c) { this.c = c; } public int compare(String a, String b) { double cnt1 = 0; double cnt2 = 0; for (int i=0; i<a.length(); i++) { if (a.charAt(i) == c) { cnt1++; } } for (int i=0; i<b.length(); i++) { if (b.charAt(i) == c) { cnt2++; } } //higher ratio is better return -Double.compare(cnt1 - (a.length() - cnt1), cnt2 - (b.length() - cnt2)); } } static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(new FileWriter(problemName + ".out")); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } } }
import java.util.*; import java.io.*; public class CF_1551c{ public static final void main(String[] args){ Kattio io= new Kattio(); int t= io.getInt(); while(t-->0){ int n= io.getInt(); int[][] ps= new int[5][n]; for(int i=0; i<n; i++){ String w= io.getWord(); int len= w.length(); // count letters for(int j=0; j<len; j++) ps[w.charAt(j)-'a'][i]++; // calculate diffs letter-!letter // e.g. a-!a = a-(a+b+c+d+e-a) = a-(len-a) = a + (a-len) = 2a-len for(int k=0; k<5; k++) ps[k][i]+= ps[k][i]-len; } //sort diffs for(int k=0; k<5; k++) //mergeSort(ps[k]); Arrays.sort(ps[k]); //calculate prefix sums of diffs (until they're non-positive) //start from the end as sort is ascending //pick largest index out of 5 letter at which sum of diffs is positive int max= 0; for(int k=0; k<5; k++){ if(ps[k][n-1]<=0) continue; if(max==0) max= 1; for(int i=2; i<=n; i++){ ps[k][n-i]+= ps[k][n-i+1]; if(ps[k][n-i]<=0) break; if(i>max) max= i; } } io.println(max); } io.close(); } // using mergeSort to avoid Java quicksort TLE hacks static void mergeSort(int arr[]){ int n= arr.length; for (int sz= 1; sz<=n-1; sz=2*sz){ for (int l= 0; l<n-1; l+=2*sz){ int m= Math.min(l + sz-1, n-1); int r= Math.min(l + 2*sz-1, n-1); int n1= m-l+1, n2= r-m; int L[] = new int[n1]; for (int i= 0; i<n1; i++) L[i]= arr[l+i]; int R[] = new int[n2]; for (int j= 0; j<n2; j++) R[j]= arr[m+1+j]; int i= 0, j= 0, k= l; for(;i<n1 && j<n2; k++){ if(L[i]<=R[j]){arr[k]= L[i]; i++;} else{arr[k] = R[j]; j++;} } for(;i<n1; i++, k++) arr[k]= L[i]; for(;j<n2; j++, k++) arr[k]= R[j]; } } } static class Kattio extends PrintWriter { private BufferedReader r; private String line, token; private StringTokenizer st; public Kattio(){this(System.in);} public Kattio(InputStream i){ super(new BufferedOutputStream(System.out)); r= new BufferedReader(new InputStreamReader(i)); } public Kattio(InputStream i, OutputStream o){ super(new BufferedOutputStream(o)); r= new BufferedReader(new InputStreamReader(i)); } public boolean isLineEnd(){ return !st.hasMoreTokens(); } public boolean hasMoreTokens(){ return peekToken()!=null; } public int getInt(){ return Integer.parseInt(nextToken()); } public double getDouble(){ return Double.parseDouble(nextToken()); } public long getLong(){ return Long.parseLong(nextToken()); } public String getWord(){ return nextToken(); } public String getLine(){ try{if(!st.hasMoreTokens()) return r.readLine(); }catch(IOException e){} return null; } private String peekToken(){ if(token==null) try { while(st==null || !st.hasMoreTokens()) { line= r.readLine(); if(line==null) return null; st= new StringTokenizer(line); } token= st.nextToken(); }catch(IOException e){} return token; } private String nextToken() { String ans= peekToken(); token= null; return ans; } } }
0
Non-plagiarised
169e34bf
ba468e1f
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.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
42c7b9df
adbb2f71
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CSbalansirovannieKuchkiKamnei solver = new CSbalansirovannieKuchkiKamnei(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class CSbalansirovannieKuchkiKamnei { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } long t1 = 0; long t2 = (int) 1e9 + 10; long[] b = new long[n]; while (t1 < t2) { long mid = (t1 + t2 + 1) / 2; System.arraycopy(a, 0, b, 0, n); boolean fine = true; for (int i = n - 1; i >= 0; i--) { if (b[i] < mid) { fine = false; break; } if (i >= 2) { long how = Math.min(a[i], (b[i] - mid)) / 3; b[i - 1] += how; b[i - 2] += how * 2; } } if (fine) { t1 = mid; } else { t2 = mid - 1; } } out.println(t1); } } static class InputReader { private static final int BUFFER_LENGTH = 1 << 10; private InputStream stream; private byte[] buf = new byte[BUFFER_LENGTH]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int nextC() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String next() { int c = nextC(); while (isSpaceChar(c)) { c = nextC(); } StringBuilder res = new StringBuilder(); do { res.append((char) c); c = nextC(); } while (!isSpaceChar(c)); return res.toString(); } public int nextInt() { int c = skipWhileSpace(); int sgn = 1; if (c == '-') { sgn = -1; c = nextC(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = nextC(); } while (!isSpaceChar(c)); return res * sgn; } public int skipWhileSpace() { int c = nextC(); while (isSpaceChar(c)) { c = nextC(); } return c; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
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.StringTokenizer; import java.util.*; public class cp { public static void main(String args[]) throws Exception { FastScanner sc = new FastScanner(); int T = 1; T = sc.nextInt(); PrintWriter pw = new PrintWriter(System.out); StringBuilder sb = new StringBuilder(); while (T-- > 0) { solve(sc, pw, sb); } pw.print(sb); pw.close(); } public static void solve(FastScanner sc, PrintWriter pw, StringBuilder sb) throws Exception { int n=sc.nextInt(); int[] h=new int[n]; for(int i=0;i<n;i++){ h[i]=sc.nextInt(); } long low=0,high=(long)1e12,ans=low; while(low<=high){ long mid=low+(high-low)/2L; if(ok(mid,h)){ ans=mid; low=mid+1; } else{ high=mid-1; } } sb.append(ans+"\n"); } public static boolean ok(long value,int[] h){ int n=h.length; int[] copy=new int[n]; for(int i=0;i<n;i++){ copy[i]=h[i]; } for(int i=n-1;i-2>=0;i--){ if(copy[i]<value) return false; // we take this condition as the max stones that we can move is the number present in the heap not the copy by the condition di<=hi/3 long toSub=Math.min(copy[i]-value,h[i])/3L; // System.out.println("i: "+i+" toSub:"+toSub); copy[i-1]+=toSub; copy[i-2]+=toSub*2L; } // System.out.println(Arrays.toString(copy)); if(copy[0]<value || copy[1]<value) return false; // System.out.println("YES"); return true; } public static long[] sort(long[] arr){ ArrayList<Long> temp = new ArrayList<>(); for(long x:arr) temp.add(x); Collections.sort(temp); int i=0; for(long x:temp){ arr[i++]=x; } return arr; } public static int gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b); } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public int[] readIntArray(int n){ int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=nextInt(); } return arr; } public long[] readLongArray(int n){ long[] arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=nextLong(); } return arr; } } }
0
Non-plagiarised
5a81e159
866a2d52
/* * 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.*; import java.util.*; public class P1529C { public static final int MAX = 100000; private static final List<Integer>[] adjacency = new List[MAX]; private static final int[][] pair = new int[MAX][2]; public static void main(String[] args) { SpeedScanner in = new SpeedScanner(); for(int i = 0; i < MAX; ++i) adjacency[i] = new ArrayList<>(); int t = in.nextInt(); while(t-- != 0) { int nv = in.nextInt(); for(int i = 0; i < nv; ++i) { pair[i][0] = in.nextInt(); pair[i][1] = in.nextInt(); } for(int i = 1; i < nv; ++i) { int x = in.nextInt() - 1, y = in.nextInt() - 1; adjacency[x].add(y); adjacency[y].add(x); } long[] alts = dfsTraverse(0, -1); System.out.println(alts[0] >= alts[1] ? alts[0] : alts[1]); for(int i = 0; i < nv; ++i) adjacency[i].clear(); } } private static long[] dfsTraverse(int vertex, int parent) { long sumDiffL = 0, sumDiffR = 0; for(int child: adjacency[vertex]) { if(child == parent) continue; long[] temp = dfsTraverse(child, vertex); sumDiffL += Math.max(Math.abs(pair[vertex][0] - pair[child][0]) + temp[0], Math.abs(pair[vertex][0] - pair[child][1]) + temp[1]); sumDiffR += Math.max(Math.abs(pair[vertex][1] - pair[child][0]) + temp[0], Math.abs(pair[vertex][1] - pair[child][1]) + temp[1]); } return new long[]{sumDiffL, sumDiffR}; } } class SpeedScanner { BufferedReader in; StringTokenizer splitter; public SpeedScanner() { in = new BufferedReader(new InputStreamReader(System.in)); } String next() { while(splitter == null || !splitter.hasMoreElements()) try { splitter = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } return splitter.nextToken(); } String nextLine() { String s = ""; try { s = in.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } }
0
Non-plagiarised
0ecf356d
b2bae06a
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int[] h = new int[n]; int[] dp = new int[n]; for(int i = 0; i<n; i++){ h[i] = Integer.parseInt(st.nextToken()); } Stack<Integer> hi = new Stack<>(); Stack<Integer> lo = new Stack<>(); hi.push(0); lo.push(0); for(int i = 1; i<n; i++){ dp[i] = dp[i-1]+1; while(!hi.isEmpty() && h[hi.peek()]<h[i]){ dp[i] = Math.min(dp[i], dp[hi.peek()]+1); hi.pop(); } if(!hi.isEmpty()){ dp[i] = Math.min(dp[i], dp[hi.peek()]+1); if(h[i] == h[hi.peek()]) hi.pop(); } while(!lo.isEmpty() && h[lo.peek()]>h[i]){ dp[i] = Math.min(dp[i], dp[lo.peek()]+1); lo.pop(); } if(!lo.isEmpty()){ dp[i] = Math.min(dp[i], dp[lo.peek()]+1); if(h[i] == h[lo.peek()]) lo.pop(); } hi.push(i); lo.push(i); } System.out.println(dp[n-1]); } }
import java.util.*; import java.io.*; public class D{ static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int mod = (int)(1e9+7); public static long pow(long a,long b) { long ans = 1; while(b> 0) { if((b & 1)==1){ ans = (ans*a) % mod; } a = (a*a) % mod; b = b>>1; } return ans; } public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); int n = in.nextInt(); int[] arr = in.nextIntArray(n); Stack<Integer> min = new Stack<>(); Stack<Integer> max = new Stack<>(); int[] dp = new int[n]; // Arrays.fill(dp,(int)1e9); dp[0] = 0; min.push(0); max.push(0); for(int i=1;i<n;i++) { int h=dp[i-1]+1; while(!max.isEmpty() && arr[i]>arr[max.peek()]) { int x = arr[max.peek()]; h = Math.min(h,1+dp[max.pop()]); while(!max.isEmpty() && arr[max.peek()]==x) { max.pop(); } } if(!max.isEmpty()) { h = Math.min(h,1+dp[max.peek()]); } while(!min.isEmpty() && arr[i]<arr[min.peek()]) { int x = arr[min.peek()]; h = Math.min(h,1+dp[min.pop()]); while(!min.isEmpty() && arr[min.peek()]==x) { min.pop(); } } if(!min.isEmpty()) { h = Math.min(h,1+dp[min.peek()]); } dp[i] = h; min.push(i); max.push(i); } out.printLine(dp[n-1]); out.flush(); out.close(); } }
0
Non-plagiarised
66565156
6f393cfe
import java.io.*; import java.util.*; public class A1 { static final Reader s = new Reader(); static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int t = s.nextInt(); // int t=1; for(int i=1; i<=t; ++i) { // out.print("Case #"+i+": "); new Solver(); } out.close(); } static class Solver { Solver() { int n = s.nextInt(); String[] a = new String[n]; int[][] cnt = new int[n][5]; for(int i=0;i<n;i++) { char[] g = s.next().toCharArray(); for(int j=0;j<g.length;j++) { cnt[i][g[j]-'a']++; } } int g=0; for(int i=0;i<5;i++) { List<Integer> l = new ArrayList<>(); for(int j=0;j<n;j++) { int sum=0; for(int k=0;k<5;k++) { if(i==k)sum+=cnt[j][k]; else sum-=cnt[j][k]; } l.add(sum); } Collections.sort(l,Collections.reverseOrder()); int f=0; int v=0; for(int h:l) { v += h; if(v > 0)f++; else break; } g = Math.max(g, f); } out.println(g); } } static class Reader { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; String next() { while(st==null||!st.hasMoreTokens()) { try { st=new StringTokenizer(in.readLine()); } catch(Exception e) {} } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
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; } } }
1
Plagiarised
6f393cfe
d61f51c5
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.util.*; import java.lang.*; import java.io.*; public class InterestingStrory { public static void main(String[] args) throws java.lang.Exception { // your code goes here try { // Scanner sc=new Scanner(System.in); FastReader sc = new FastReader(); int t =sc.nextInt(); while (t-- > 0) { int n=sc.nextInt(); int[][] occ=new int[n][5]; for(int i=0;i<n;i++){ char[] warr=sc.next().toCharArray(); for(char ch:warr){ occ[i][ch-'a']++; } } int ans=Integer.MIN_VALUE;; for(int c=0;c<5;c++){ int[] sums=new int[n]; for(int i=0;i<n;i++){ int sum=0; for(int j=0;j<5;j++){ if(c==j){ sum+=occ[i][j]; } else{ sum-=occ[i][j]; } } sums[i]=sum; } Arrays.sort(sums); int currSum=0; int l=n-1; for(l=n-1;l>=0;l--){ if(currSum+sums[l]<=0){ break; } else{ currSum+=sums[l]; } } ans=Math.max(ans,n-l-1); } System.out.println(ans); } } catch (Exception e) { return; } } static int BS(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] < element) { low = mid + 1; } else { high = mid; } } if (arr[low] == element) { return low; } else if (arr[high] == element) { return high; } return -1; } static int lower_bound(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] < element) { low = mid + 1; } else { high = mid; } } if (arr[low] >= element) { return low; } else if (arr[high] >= element) { return high; } return -1; } static int upper_bound(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] <= element) { low = mid + 1; } else { high = mid; } } if (arr[low] > element) { return low; } else if (arr[high] > element) { return high; } return -1; } public static long gcd_long(long a,long b){ //a/b,a-> dividant b-> divisor if(b==0)return a; return gcd_long(b, a%b); } public static int gcd_int(int a,int b){ //a/b,a-> dividant b-> divisor if(b==0)return a; return gcd_int(b, a%b); } public static int lcm(int a,int b){ int gcd=gcd_int(a, b); return (a*b)/gcd; } 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()); } String nextLine() { String s = ""; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } Long nextLong() { return Long.parseLong(next()); } } } /* * public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){ * return true; } if(n<1 || m<1 || k<0){ return false; } boolean * tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ return true; } * return false; } */
1
Plagiarised
ac2dfc53
c4187a0e
import java.io.*; import java.util.*; /* */ public class C{ static FastReader fs=null; static Scanner sc=null; public static void main(String[] args) { sc=new Scanner(System.in); int t=sc.nextInt(); for(int tt=0;tt<t;tt++) { int n=sc.nextInt(); boolean visited[]=new boolean[n+1]; int p[]=new int[n+1]; for(int i=1;i<=n;i++) { if(visited[i])continue; while(true) { int val=Query(i); if(val==i)break; } int cur=i; while(!visited[cur]) { int val=Query(i); visited[cur]=true; p[cur]=val; cur=val; } } System.out.print("! "); for(int i=1;i<=n;i++)System.out.print(p[i]+" "); System.out.println(); } } static int Query(int id) { System.out.println("? "+id); int val=sc.nextInt(); return val; } 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; } } }
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.PrintStream; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); CHiddenPermutations solver = new CHiddenPermutations(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class CHiddenPermutations { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int[] arr = new int[n]; boolean[] vis = new boolean[n]; for (int i = 0; i < n; i++) { if (vis[i]) continue; ArrayList<Integer> cycle = new ArrayList<>(); int ans = query(i, in); int now = query(i, in); cycle.add(now); while (now != ans) { now = query(i, in); cycle.add(now); } for (int j = 0; j < cycle.size(); j++) { arr[cycle.get(j)] = cycle.get((j + 1) % cycle.size()) + 1; vis[cycle.get(j)] = true; } } out.print("! "); out.println(arr); out.flush(); } int query(int ind, InputReader in) { System.out.println("? " + (ind + 1)); return in.nextInt() - 1; } } static class InputReader { BufferedReader reader; 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()); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void print(int[] array) { for (int i = 0; i < array.length; i++) { if (i != 0) { writer.print(' '); } writer.print(array[i]); } } public void println(int[] array) { print(array); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } }
0
Non-plagiarised
1c4d348d
565f77b7
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Solution { public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class SortbyHeight implements Comparator<Struct> { public int compare(Struct a, Struct b) { return a.h - b.h; } } static class Struct{ int h,ind; Struct(int h,int ind){ this.h=h; this.ind=ind; } } public static void main(String[] args) throws java.lang.Exception { // your code goes here FastReader scn = new FastReader(); int t, k, i, j, l, f, max=0; t=scn.nextInt(); while(t-->0){ int n=scn.nextInt(); int m=scn.nextInt(); int x=scn.nextInt(); Struct a[]=new Struct[n]; for (i=0;i<n;i++){ a[i]=new Struct(scn.nextInt(),i); } Arrays.sort(a,new SortbyHeight()); int b[]=new int[n]; int ms=1; for (i=0;i<n;i++){ if (ms>m){ ms=1; } b[a[i].ind]=ms; ms++; } System.out.println("YES"); for (i=0;i<n;i++){ System.out.print(b[i]+" "); } System.out.println(); } } }
import java.util.*; import java.io.*; public class Solution { static Scanner scn = new Scanner(System.in); static PrintWriter out = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); public static void main(String[] HastaLaVistaLa) { int t = scn.nextInt(); while(t-- > 0) solve(); out.println(sb); out.close(); } public static void solve() { // Road To Specialist Day 3 int n = scn.nextInt(), m = scn.nextInt(), x = scn.nextInt(); int[] a = new int[n], ans = new int[n]; for(int i = 0; i < n; i++) a[i] = scn.nextInt(); PriorityQueue<Pair> pq = new PriorityQueue<>(); for(int i = 0; i < m; i++) pq.add(new Pair(0L, i)); for(int i = 0; i < n; i++) { int e = a[i]; Pair p = pq.poll(); p.value += e; pq.add(p); ans[i] = p.id + 1; } boolean check = false; long prev = pq.poll().value; while(!pq.isEmpty()) { long cur = pq.poll().value; if(Math.abs(cur - prev) > x) check = true; prev = cur; } if(check) sb.append("NO"); else { sb.append("YES\n"); for(int i : ans) sb.append(i + " "); } sb.append("\n"); } static class Pair implements Comparable<Pair> { int id; long value; public Pair(long value, int id) { this.id = id; this.value = value; } public int compareTo(Pair o) { return Long.compare(value, o.value); } } }
0
Non-plagiarised
5449d33c
dff5ff0a
import java.io.*; import java.util.*; import java.lang.*; public class C { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); solver.Main(in, out); out.close(); } static class Solver { public void Main(InputReader in, PrintWriter out) { int T = in.nextInt(); for (int t = 0; t < T; t++) { int n = in.nextInt(); String[] A = new String[n]; for (int i = 0; i < n; i++) { A[i] = in.next(); } int ans = 0; for (char c = 'a'; c <= 'e'; c++) { int[] ls = new int[n]; for (int i = 0; i < n; i++) { int delta = 0; for (int j = 0; j < A[i].length(); j++) { if (A[i].charAt(j) == c) { delta += 1; } else { delta -= 1; } } ls[i] = delta; } Arrays.sort(ls, 0, n); int cur = 0; int score = 0; for (int i = n - 1; i >= 0; i--) { if (cur + ls[i] >= 1) { cur += ls[i]; score += 1; } } ans = Math.max(ans, score); } out.println(ans); } } } 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()); } } }
import java.io.DataInputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; public class Main { private static void run() throws IOException { int n = in.nextInt(); char[][] a = new char[n][]; for (int i = 0; i < n; i++) { a[i] = in.next().toCharArray(); } int ans = Integer.MIN_VALUE; for (char now = 'a'; now <= 'e'; now++) { ans = Math.max(ans, check(a, now)); } out.println(ans); } private static int check(char[][] a, char target) { int[] count = new int[a.length]; for (int i = 0; i < a.length; i++) { for (char c : a[i]) { if (c == target) { count[i]++; } else { count[i]--; } } } Arrays.sort(count); int ans = 0; int sum = 0; for (int i = a.length - 1; i >= 0; i--) { if (count[i] <= 0) break; ans++; sum += count[i]; } if (ans == 0) return 0; for (int i = a.length - 1; i >= 0; i--) { if (count[i] > 0) continue; if (sum > -count[i]) { sum += count[i]; ans++; } } return ans; } public static void main(String[] args) throws IOException { in = new Reader(); out = new PrintWriter(new OutputStreamWriter(System.out)); int t = in.nextInt(); for (int i = 0; i < t; i++) { run(); } out.flush(); in.close(); out.close(); } private static int gcd(int a, int b) { if (a == 0 || b == 0) return 0; while (b != 0) { int tmp; tmp = a % b; a = b; b = tmp; } return a; } static final long mod = 1000000007; static long pow_mod(long a, long b) { long result = 1; while (b != 0) { if ((b & 1) != 0) result = (result * a) % mod; a = (a * a) % mod; b >>= 1; } return result; } private static long multiplied_mod(long... longs) { long ans = 1; for (long now : longs) { ans = (ans * now) % mod; } return ans; } @SuppressWarnings("FieldCanBeLocal") private static Reader in; private static PrintWriter out; private static int[] read_int_array(int len) throws IOException { int[] a = new int[len]; for (int i = 0; i < len; i++) { a[i] = in.nextInt(); } return a; } private static long[] read_long_array(int len) throws IOException { long[] a = new long[len]; for (int i = 0; i < len; i++) { a[i] = in.nextLong(); } return a; } private static void print_array(int[] array) { for (int now : array) { out.print(now); out.print(' '); } out.println(); } private static void print_array(long[] array) { for (long now : array) { out.print(now); out.print(' '); } out.println(); } static class Reader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException { byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() throws IOException { int b; // noinspection ALL while ((b = read()) != -1 && isSpaceChar(b)) { ; } return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final 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(); } final 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(); } final 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 { din.close(); } } }
0
Non-plagiarised
47d54299
da0b7cb2
// package com.company.Codechef; // import com.sun.security.jgss.GSSUtil; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class a { static int depth[]; static ArrayList<Integer> tree[]; static int diameter=0; public static void main(String[] args) { FastReader input=new FastReader(); int t=input.nextInt(); while (t-->0){ diameter=0; int n=input.nextInt(); int a=input.nextInt(); int b=input.nextInt(); int da=input.nextInt(); int db=input.nextInt(); depth=new int[n+1]; tree=new ArrayList[n+1]; for (int i = 0; i <=n ; i++) { tree[i]=new ArrayList<>(); } for (int i = 0; i <n-1 ; i++) { int x=input.nextInt(); int y=input.nextInt(); tree[x].add(y); tree[y].add(x); } dfs(a,0); // System.out.println(depth[b]); if (2 * da >= Math.min(diameter, db) ||depth[b]<=da){ System.out.println("Alice"); }else { System.out.println("Bob"); } } } private static int dfs(int root, int p) { int len=0; for(int child:tree[root]){ if (child!=p){ depth[child]=depth[root]+1; int curr=1+dfs(child,root); diameter=Math.max(diameter,curr+len); len=Math.max(len,curr); } } return len; } 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.*; import java.util.*; import static java.lang.Math.*; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Codeforces { public static void main(String[] args) throws IOException { // InputStream inputStream = new FileInputStream("input.txt"); // OutputStream outputStream = new FileOutputStream("output.txt"); InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(in.nextInt(), in, out); out.close(); } static class TaskA { long mod = (long)(1000000007); long fact[]; int depth[]; int parentTable[][]; int degree[]; ArrayList<Integer> leaves; int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; int diam = 0; public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException { while(testNumber-->0){ int n = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); int na = in.nextInt(); int nb = in.nextInt(); ArrayList<ArrayList<Integer>> g = new ArrayList<>(); for(int i=0;i<=n;i++) g.add(new ArrayList<>()); for(int i=1;i<n;i++){ int u = in.nextInt(); int v = in.nextInt(); g.get(u).add(v); g.get(v).add(u); } if(2*na>=nb){ out.println("Alice"); continue; } // parentTable = new int[n+1][31]; depth = new int[n+1]; diam = 0; dfs(g , a , 0); int distance = depth[b]; // out.println(distance); if(distance <= na){ out.println("Alice"); continue; } if(2*na >= min(nb , diam)) out.println("Alice"); else out.println("Bob"); // max = Integer.MIN_VALUE; // int diameter = diameter(g , 1 , 0); // int height = 0; // for(int i=1;i<=n;i++) // height = max(height , depth[i]); // // out.println("diameter" + diameter); // // out.println("height" + height); // // out.println("max" + max); // diameter = max(diameter-1 , height-1); // // diameter = max(diameter , max-1); // nb = min(nb , diameter); // na = min(na , diameter); // if(na*2 >= nb) // out.println("Alice"); // else // out.println("Bob"); } } int dfs(ArrayList<ArrayList<Integer>> a , int x, int p) { int len = 0; for(int y : a.get(x)) { if(y != p) { depth[y] = depth[x] + 1; int cur = 1 + dfs(a , y, x); diam = max(diam, cur + len); len = max(len, cur); } } return len; } public int diameter(ArrayList<ArrayList<Integer>> a , int index , int parent){ if(index == parent) return 0; // if(a.get(index).size() == 1 && a.get(index).get(0) == parent) // return 1; ArrayList<Integer> x = new ArrayList<>(); x.add(0); x.add(0); for(int i=0;i<a.get(index).size();i++){ if(a.get(index).get(i) == parent) continue; x.add(diameter(a , a.get(index).get(i) , index)); } Collections.sort(x); if(x.size() >= 4){ int n = x.size(); max = max(max , 1+x.get(n-1) + x.get(n-2)); } return 1 + x.get(x.size()-1); } // public void dfs(ArrayList<ArrayList<Integer>> a , int index , int parent){ // parentTable[index][0] = parent; // for(int i=1;i<31;i++) // parentTable[index][i] = parentTable[parentTable[parent][i-1]][i-1]; // depth[index] = 1+depth[parent]; // int l = a.get(index).size(); // for(int i=0;i<l;i++){ // if(a.get(index).get(i) == parent) // continue; // dfs(a , a.get(index).get(i) , index); // } // } public int distance(ArrayList<ArrayList<Integer>> a , int u , int v){ return depth[u]+depth[v] - 2*depth[lca(a , u , v)]; } public int lca(ArrayList<ArrayList<Integer>> a , int u , int v){ if(depth[v]<depth[u]){ int x = u; u = v; v = x; } int diff = depth[v] - depth[u]; for(int i=0;i<parentTable[v].length;i++){ // checking whether the ith bit is set in the diff variable if(((diff>>i)&1) == 1) v = parentTable[v][i]; } if(v == u) return v; for(int i=parentTable[v].length-1;i>=0;i--){ if(parentTable[u][i] != parentTable[v][i]){ v = parentTable[v][i]; u = parentTable[u][i]; } } return parentTable[u][0]; } class Pair{ int start; int end; int index; public Pair(int a , int b , int c){ start = a; end = b; end = c; } } class Node{ char value; Node left; Node right; public Node(char value){ this.value = value; left = null; right = null; } } public int[][] multiply(int a[][] , int b[][]){ int c[][] = new int[a.length][b[0].length]; for(int i=0;i<a.length;i++){ for(int j=0;j<b[0].length;j++){ for(int k=0;k<b.length;k++) c[i][j] += a[i][k]*b[k][j]; } } return c; } public int[][] multiply(int a[][] , int b[][] , int mod){ int c[][] = new int[a.length][b[0].length]; for(int i=0;i<a.length;i++){ for(int j=0;j<b[0].length;j++){ for(int k=0;k<b.length;k++){ c[i][j] += a[i][k]*b[k][j]; c[i][j]%=mod; } } } return c; } public int[][] pow(int a[][] , long b){ int res[][] = new int[a.length][a[0].length]; for(int i=0;i<a.length;i++) res[i][i] = 1; while(b>0){ if((b&1) == 1) res = multiply(res , a , 10); a = multiply(a , a , 10); b>>=1; } return res; } // for the min max problems public void build(int lookup[][] , int arr[], int n) { for (int i = 0; i < n; i++) lookup[i][0] = arr[i]; for (int j = 1; (1 << j) <= n; j++) { for (int i = 0; (i + (1 << j) - 1) < n; i++) { if (lookup[i][j - 1] > lookup[i + (1 << (j - 1))][j - 1]) lookup[i][j] = lookup[i][j - 1]; else lookup[i][j] = lookup[i + (1 << (j - 1))][j - 1]; } } } public int query(int lookup[][] , int L, int R) { int j = (int)(Math.log(R - L + 1)/Math.log(2)); if (lookup[L][j] >= lookup[R - (1 << j) + 1][j]) return lookup[L][j]; else return lookup[R - (1 << j) + 1][j]; } // for printing purposes public void print1d(int a[] , PrintWriter out){ for(int i=0;i<a.length;i++) out.print(a[i] + " "); out.println(); } public void print1d(long a[] , PrintWriter out){ for(int i=0;i<a.length;i++) out.print(a[i] + " "); out.println(); } public void print2d(int a[][] , PrintWriter out){ for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++) out.print(a[i][j] + " "); out.println(); } // out.println(); } public void print2d(long a[][] , PrintWriter out){ for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++) out.print(a[i][j] + " "); out.println(); } // out.println(); } public void sieve(int a[]){ a[0] = a[1] = 1; int i; for(i=2;i*i<=a.length;i++){ if(a[i] != 0) continue; a[i] = i; for(int k = (i)*(i);k<a.length;k+=i){ if(a[k] != 0) continue; a[k] = i; } } } public long nCrPFermet(int n , int r , long p){ if(r==0) return 1l; // long fact[] = new long[n+1]; // fact[0] = 1; // for(int i=1;i<=n;i++) // fact[i] = (i*fact[i-1])%p; long modInverseR = pow(fact[r] , p-2 , p); long modInverseNR = pow(fact[n-r] , p-2 , p); long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p; return w; } public long pow(long a, long b, long m) { a %= m; long res = 1; while (b > 0) { long x = b&1; if (x == 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } public long pow(long a, long b) { long res = 1; while (b > 0) { long x = b&1; if (x == 1) res = res * a; a = a * a; b >>= 1; } return res; } public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) { if (start > end) { return; } int mid = (start + end) / 2; a.add(mid); sortedArrayToBST(a, start, mid - 1); sortedArrayToBST(a, mid + 1, end); } class Combine{ int value; int delete; Combine(int val , int delete){ this.value = val; this.delete = delete; } } class Sort2 implements Comparator<Combine>{ public int compare(Combine a , Combine b){ if(a.value > b.value) return 1; else if(a.value == b.value && a.delete>b.delete) return 1; else if(a.value == b.value && a.delete == b.delete) return 0; return -1; } } public int lowerLastBound(ArrayList<Integer> a , int x){ int l = 0; int r = a.size()-1; if(a.get(l)>=x) return -1; if(a.get(r)<x) return r; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a.get(mid) == x && a.get(mid-1)<x) return mid-1; else if(a.get(mid)>=x) r = mid-1; else if(a.get(mid)<x && a.get(mid+1)>=x) return mid; else if(a.get(mid)<x && a.get(mid+1)<x) l = mid+1; } return mid; } public int upperFirstBound(ArrayList<Integer> a , Integer x){ int l = 0; int r = a.size()-1; if(a.get(l)>x) return l; if(a.get(r)<=x) return r+1; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a.get(mid) == x && a.get(mid+1)>x) return mid+1; else if(a.get(mid)<=x) l = mid+1; else if(a.get(mid)>x && a.get(mid-1)<=x) return mid; else if(a.get(mid)>x && a.get(mid-1)>x) r = mid-1; } return mid; } public int lowerLastBound(int a[] , int x){ int l = 0; int r = a.length-1; if(a[l]>=x) return -1; if(a[r]<x) return r; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a[mid] == x && a[mid-1]<x) return mid-1; else if(a[mid]>=x) r = mid-1; else if(a[mid]<x && a[mid+1]>=x) return mid; else if(a[mid]<x && a[mid+1]<x) l = mid+1; } return mid; } public int upperFirstBound(long a[] , long x){ int l = 0; int r = a.length-1; if(a[l]>x) return l; if(a[r]<=x) return r+1; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a[mid] == x && a[mid+1]>x) return mid+1; else if(a[mid]<=x) l = mid+1; else if(a[mid]>x && a[mid-1]<=x) return mid; else if(a[mid]>x && a[mid-1]>x) r = mid-1; } return mid; } public long log(float number , int base){ return (long) Math.ceil((Math.log(number) / Math.log(base)) + 1e-9); } public long gcd(long a , long b){ if(a<b){ long c = b; b = a; a = c; } while(b!=0){ long c = a; a = b; b = c%a; } return a; } public long[] gcdEx(long p, long q) { if (q == 0) return new long[] { p, 1, 0 }; long[] vals = gcdEx(q, p % q); long d = vals[0]; long a = vals[2]; long b = vals[1] - (p / q) * vals[2]; // 0->gcd 1->xValue 2->yValue return new long[] { d, a, b }; } public void sievePhi(int a[]){ a[0] = 0; a[1] = 1; for(int i=2;i<a.length;i++) a[i] = i-1; for(int i=2;i<a.length;i++) for(int j = 2*i;j<a.length;j+=i) a[j] -= a[i]; } public void lcmSum(long a[]){ int sievePhi[] = new int[(int)1e6 + 1]; sievePhi(sievePhi); a[0] = 0; for(int i=1;i<a.length;i++) for(int j = i;j<a.length;j+=i) a[j] += (long)i*sievePhi[i]; } } 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()); } } }
1
Plagiarised
0b04b41e
fdd41565
import java.io.BufferedReader; import java.io.*; import java.util.*; public class josph { static BufferedReader br; static long mod = 1000000000 + 7; static HashSet<Integer> p = new HashSet<>(); static boolean debug =true; // Arrays.sort(time , (a1,a2) -> (a1[0]-a2[0])); 2d array sort lamda public static void main(String[] args) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pr = new PrintWriter(System.out); int tc = 1; tc= cinI(); while(tc-->0){ int n =cinI(); String[] a= new String[n]; int[][] f =new int[10][n]; for(int i=0;i<n;i++){ a[i]=cin(); char[] x = a[i].toCharArray(); for(char c:x){ int index = c-'a'; f[index][i]+=1; } for(int j=0;j<10;j++){ int rem =x.length-f[j][i]; f[j][i]-=rem; } } int max=0; for(int j=0;j<10;j++){ Arrays.sort(f[j]); int cnt=0; int sum=0; for(int i=n-1;i>=0;i--){ sum+=f[j][i]; if(sum>0){ cnt+=1; } else{ break; } } max=max(max,cnt); } System.out.println(max); } } public static <E> void print(String var ,E e){ if(debug==true){ System.out.println(var +" "+e); } } private static long[] sort(long[] e){ ArrayList<Long> x=new ArrayList<>(); for(long c:e){ x.add(c); } Collections.sort(x); long[] y = new long[e.length]; for(int i=0;i<x.size();i++){ y[i]=x.get(i); } return y; } public static void printDp(long[][] dp) { int n = dp.length; for (int i = 0; i < n; i++) { for (int j = 0; j < dp[0].length; j++) { System.out.print(dp[i][j] + " "); } System.out.println(); } } private static long gcd(long a, long b) { if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a % b, b); return gcd(a, b % a); } public static long min(long a, long b) { return Math.min(a, b); } public static int min(int a, int b) { return Math.min(a, b); } public static void sieve() { int[] pf = new int[100000000 + 1]; //0 prime //1 not prime pf[0] = 1; pf[1] = 1; for (int j = 2; j <= 10000; j++) { if (pf[j] == 0) { p.add(j); for (int k = j * j; k < pf.length; k += j) { pf[k] = 1; } } } } public static int[] readArray(int n, int x, int z) throws Exception { int[] arr = new int[n]; String[] ar = cinA(); for (int i = x; i < n + x; i++) { arr[i] = getI(ar[i - x]); } return arr; } public static long[] readArray(int n, int x) throws Exception { long[] arr = new long[n]; String[] ar = cinA(); for (int i = x; i < n + x; i++) { arr[i] = getL(ar[i - x]); } return arr; } public static void arrinit(String[] a, long[] b) throws Exception { for (int i = 0; i < a.length; i++) { b[i] = Long.parseLong(a[i]); } } public static HashSet<Integer>[] Graph(int n, int edge, int directed) throws Exception { HashSet<Integer>[] tree = new HashSet[n]; for (int j = 0; j < edge; j++) { String[] uv = cinA(); int u = getI(uv[0]); int v = getI(uv[1]); if (directed == 0) { tree[v].add(u); } tree[u].add(v); } return tree; } public static void arrinit(String[] a, int[] b) throws Exception { for (int i = 0; i < a.length; i++) { b[i] = Integer.parseInt(a[i]); } } static double findRoots(int a, int b, int c) { // If a is 0, then equation is not //quadratic, but linear int d = b * b - 4 * a * c; double sqrt_val = Math.sqrt(Math.abs(d)); // System.out.println("Roots are real and different \n"); return Math.max((double) (-b + sqrt_val) / (2 * a), (double) (-b - sqrt_val) / (2 * a)); } public static String cin() throws Exception { return br.readLine(); } public static String[] cinA() throws Exception { return br.readLine().split(" "); } public static String[] cinA(int x) throws Exception { return br.readLine().split(""); } public static String ToString(Long x) { return Long.toBinaryString(x); } public static void cout(String s) { System.out.println(s); } public static Integer cinI() throws Exception { return Integer.parseInt(br.readLine()); } public static int getI(String s) throws Exception { return Integer.parseInt(s); } public static long getL(String s) throws Exception { return Long.parseLong(s); } public static long max(long a, long b) { return Math.max(a, b); } public static int max(int a, int b) { return Math.max(a, b); } public static void coutI(int x) { System.out.println(String.valueOf(x)); } public static void coutI(long x) { System.out.println(String.valueOf(x)); } public static Long cinL() throws Exception { return Long.parseLong(br.readLine()); } public static void arrInit(String[] arr, int[] arr1) throws Exception { for (int i = 0; i < arr.length; i++) { arr1[i] = getI(arr[i]); } } }
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
9069684f
bf85ab7b
import java.io.*; import java.util.*; public class Test { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static boolean[] vis; static ArrayList<Integer>[] adj; static int[] l, r; static long[][] dp; @SuppressWarnings("unchecked") public static void main(String[] args) throws IOException { int T = readInt(); for (int t = 0; t < T; t++) { int n = readInt(); l = new int[n + 1]; r = new int[n + 1]; for (int i = 1; i <= n; i++) { l[i] = readInt(); r[i] = readInt(); } adj = new ArrayList[n + 1]; for (int i = 1; i <= n; i++) adj[i] = new ArrayList<>(); for (int i = 0; i < n - 1; i++) { int u = readInt(), v = readInt(); adj[u].add(v); adj[v].add(u); } dp = new long[n + 1][2]; vis = new boolean[n + 1]; vis[1] = true; dfs(1); System.out.println(Math.max(dp[1][0], dp[1][1])); } } static void dfs(int u) { for (int x : adj[u]) { if (!vis[x]) { vis[x] = true; dfs(x); dp[u][0] += Math.max(dp[x][0] + Math.abs(l[u] - l[x]), dp[x][1] + Math.abs(l[u] - r[x])); dp[u][1] += Math.max(dp[x][0] + Math.abs(r[u] - l[x]), dp[x][1] + Math.abs(r[u] - r[x])); } } } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } static int readInt() throws IOException { return Integer.parseInt(next()); } }
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
ba468e1f
fb312dc6
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; } } }
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.StringTokenizer; public class D { static class Pair{ int val; char c; Pair(int a,char b){ this.val=a; this.c=b; } } public static void main(String[] args) { FastScanner sc=new FastScanner(); int t=sc.nextInt(); PrintWriter pw=new PrintWriter(System.out); while(t-->0) { int n=sc.nextInt(); int[] a=sc.readArray(n); char[] s=sc.next().toCharArray(); boolean ok=true; ArrayList<Integer> blues=new ArrayList<>(); ArrayList<Integer> reds=new ArrayList<>(); for(int i=0;i<n;i++){ if(s[i]=='B'){ blues.add(a[i]); } else { reds.add(a[i]); } } Collections.sort(blues); Collections.sort(reds); for(int i=0;i<blues.size();i++){ if(blues.get(i)<(i+1)){ ok=false; break; } } int start=blues.size()+1; for(int i=0;i<reds.size();i++){ if(reds.get(i)>(start++)){ ok=false; break; } } if(ok){ pw.println("YES"); } else { pw.println("NO"); } } pw.flush(); } 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
505f8562
ed37ba7d
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.lang.System.out; import static java.lang.Long.MAX_VALUE; public final class Main{ FastReader in; StringBuffer sb; public static void main(String[] args) { new Main().run(); } void run(){ in= new FastReader(); start(); } void start(){ sb= new StringBuffer(); for(int t=in.nextInt();t>0;t--) solve(); out.print(sb); } long MOD= 1000000007; void solve(){ int n= in.nextInt(); int k= in.nextInt(); long[] a=longArr(k); long[] t=longArr(k); long[] ans= new long[n]; for(int i=0;i<n;i++){ ans[i]= Integer.MAX_VALUE; } for(int i=0;i<k;i++){ ans[(int) (a[i]-1)]= t[i]; } long[] left= new long[n]; left[0]= ans[0]; for(int i=1;i<n;i++) { left[i]= min(left[i-1]+1,ans[i]); } long[] right= new long[n]; right[n-1]= ans[n-1]; for(int i=n-2;i>=0;i--) { right[i]= min(right[i+1]+1,ans[i]); } for(int i=0;i<n;i++) sb.append(min(left[i], right[i])).append(" "); sb.append("\n"); } int upper_bound(long[] arr, int key) { int i=0, j=arr.length-1; if (arr[j]<=key) return j+1; if(arr[i]>key) return i; while (i<j){ int mid= (i+j)/2; if(arr[mid]<=key){ i= mid+1; }else{ j=mid; } } return i; } 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); } long[] longArr(int n){ long[] res= new long[n]; for(int i=0;i<n;i++){ res[i]= in.nextLong(); } return res; } // sieve of eratosthenes code for precomputing whether numbers are prime or not up to MAX_VALUE long MAX= MAX_VALUE; int[] precomp= new int[(int) (MAX+1)]; void sieve(){ long n= MAX; boolean[] prime = new boolean[(int) (n+1)]; for(int i=0;i<=n;i++) prime[i] = true; for(long p = 2; p*p <=n; p++) { // If prime[p] is not changed, then it is a prime if(prime[(int) p]) { // Update all multiples of p for(long i = p*p; i <= n; i += p) prime[(int) i] = false; } } // Print all prime numbers for(long i = 2; i <= n; i++) { if(prime[(int) i]) precomp[(int) i]= 1; } } boolean isDigitSumPalindrome(long N) { // code here long sum= sumOfDigits(String.valueOf(N)); long rev=0; long org= sum; while (sum!=0){ long d= sum%10; rev = rev*10 +d; sum /= 10; } return org == rev; } long sumOfDigits(String n){ long sum= 0; for (char c: n.toCharArray()){ sum += Integer.parseInt(String.valueOf(c)); } return sum; } long[] revArray(long[] arr) { int n= arr.length; int i=0, j=n-1; while (i<j){ long temp= arr[i]; arr[i]= arr[j]; arr[j]= temp; i++; j--; } return arr; } long gcd(long a, long b){ if (b==0) return a; return gcd(b, a%b); } long lcm(long a,long b){ return (a*b)/gcd(a,b); } static class Pair{ long first; long second; Pair(long x,long y){ this.first=x; this.second=y; } } static class Compare { static void compare(ArrayList<Pair> arr, long n) { arr.sort(new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return (int) (p1.first - p2.first); } }); } } 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 (Exception e){ e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } float nextFloat(){ return Float.parseFloat(next()); } String nextLine(){ String str=""; try{ str=br.readLine(); }catch (Exception e){ e.printStackTrace(); } return str; } } }
import java.io.*; import java.util.*; import java.lang.*; public class codeforces { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; if (System.getProperty("ONLINE_JUDGE") == null) { long startTime = System.currentTimeMillis(); try { sc = new InputReader(new FileInputStream("input.txt")); out = new PrintWriter(new FileOutputStream("output.txt")); pr = new PrintWriter(new FileOutputStream("error.txt")); } catch (Exception ignored) { } int t = 1; int tt = t; t = sc.nextInt(); while (t-- > 0) { solve(); } long endTime = System.currentTimeMillis(); System.out.println("Time: " + (endTime - startTime) / tt + " ms"); out.flush(); pr.flush(); } else { sc = new InputReader(inputStream); out = new PrintWriter(outputStream); pr = new PrintWriter(outputStream); int t = 1; t = sc.nextInt(); while (t-- > 0) { solve(); } out.flush(); } } public static void solve() { n = sc.nextInt(); for (int i = 0; i < n; i++) { dp[i] = inf; ans[i] = inf; } m = sc.nextInt(); for (int i = 0; i < m; i++) arr[i] = sc.nextInt() - 1; for (int i = 0; i < m; i++) { arr2[i] = sc.nextInt(); dp[(int)arr[i]] = arr2[i]; } temp = inf; for (int i = 0; i < n; i++) { temp = Math.min(temp, dp[i]); ans[i] = Math.min(ans[i], temp); temp++; } temp = inf; for (int i = (int)n - 1; i > -1; i--) { temp = Math.min(temp, dp[i]); ans[i] = Math.min(ans[i], temp); temp++; } for (int i = 0; i < n; i++) out.print(ans[i] + " "); out.println(""); } /* * Set Iterator Iterator value = set.iterator(); Displaying the values after * iterating through the iterator * System.out.println("The iterator values are: "); while (value.hasNext()) { * System.out.println(value.next()); } */ /* * Map Iterator: for (Map.Entry<Integer, Integer> entry : map.entrySet()){ * System.out.println("Key => " + entry.getKey() + ", Value => " + * entry.getValue());} */ // Globals public static long n, m, temp; public static int template_array_size = (int) 1e6 + 16813; public static long[] arr = new long[template_array_size]; public static long[] arr2 = new long[template_array_size]; public static long[] dp = new long[template_array_size]; public static long[] ans = new long[template_array_size]; public static int inf = Integer.MAX_VALUE; public static int minf = Integer.MIN_VALUE; public static int mod = 1000000007; public static int ml = (int) 1e9; public static String s = ""; public static InputReader sc; public static PrintWriter out; public static PrintWriter pr; // Pair static class Pair { int first, second; Pair(int x, int y) { this.first = x; this.second = y; } } // FastReader Class 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()); } } // Req Functions public static void fill(int[][] dp, int x) { for (int i = 0; i < dp.length; ++i) { for (int j = 0; j < dp[0].length; ++j) { dp[i][j] = x; } } } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } public static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } public static long nCr(int n, int k) { long ans = 1L; k = k > n - k ? n - k : k; int j = 1; for (; j <= k; j++, n--) { if (n % j == 0) { ans *= n / j; } else if (ans % j == 0) { ans = ans / j * n; } else { ans = (ans * n) / j; } } return ans; } public static String reverseString(String input) { StringBuilder str = new StringBuilder(""); for (int i = input.length() - 1; i >= 0; i--) { str.append(input.charAt(i)); } return str.toString(); } public static int maxOf3(int x, int y, int z) { return Math.max(x, Math.max(y, z)); } public static int minof3(int x, int y, int z) { return Math.min(x, Math.min(y, z)); } public static long maxOf3(long x, long y, long z) { return Math.max(x, Math.max(y, z)); } public static long minof3(long x, long y, long z) { return Math.min(x, Math.min(y, z)); } public static void arrInput(int[] arr, int n) { for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); } public static void arrInput(long[] arr, int n) { for (int i = 0; i < n; i++) arr[i] = sc.nextLong(); } public static void arrInput(Pair[] pair, int n) { for (int i = 0; i < n; i++) pair[i] = new Pair(sc.nextInt(), sc.nextInt()); } public static int maxarrInput(int[] arr, int n) { int max = minf; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); max = Math.max(max, arr[i]); } return max; } public static long maxarrInput(long[] arr, int n) { long max = minf; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); max = Math.max(max, arr[i]); } return max; } public static int minarrInput(int[] arr, int n) { int min = inf; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); min = Math.max(min, arr[i]); } return min; } public static long minarrInput(long[] arr, int n) { long min = inf; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); min = Math.max(min, arr[i]); } return min; } public static int lowerBound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] >= x) r = m; else l = m; } return r; } public static int upperBound(int[] arr, int x) { int l = -1, r = arr.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (arr[m] <= x) l = m; else r = m; } return l + 1; } public static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int[n1]; int R[] = new int[n2]; for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] >= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } public static void reversesort(int arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; reversesort(arr, l, m); reversesort(arr, m + 1, r); merge(arr, l, m, r); } } public static void merge(long arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; long L[] = new long[n1]; long R[] = new long[n2]; for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] >= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } public static void reversesort(long arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; reversesort(arr, l, m); reversesort(arr, m + 1, r); merge(arr, l, m, r); } } // debug public static boolean sysFlag = System.getProperty("ONLINE_JUDGE") == null; public static void debug(int[][] dp) { if (sysFlag) { for (int i = 0; i < dp.length; ++i) { pr.print(i + "--> "); for (int j = 0; j < dp[0].length; ++j) { pr.print(dp[i][j] + " "); } pr.println(""); } } } public static void debug(long[][] dp) { if (sysFlag) { for (int i = 0; i < dp.length; ++i) { pr.print(i + "--> "); for (int j = 0; j < dp[0].length; ++j) { pr.print(dp[i][j] + " "); } pr.println(""); } } } public static void debug(int x) { if (sysFlag) pr.println("Int-Ele: " + x); } public static void debug(String x) { if (sysFlag) pr.println("String: " + x); } public static void debug(char x) { if (sysFlag) pr.println("Char: " + x); } public static void debug(long x) { if (sysFlag) pr.println("Long-Ele: " + x); } public static void debug(int[] arr, int n) { if (sysFlag) { for (int i = 0; i < n; ++i) { pr.println(i + " -> " + arr[i]); } } } public static void debug(char[] arr) { if (sysFlag) { for (int i = 0; i < arr.length; ++i) { pr.println(i + " -> " + arr[i]); } } } public static void debug(long[] arr, int n) { if (sysFlag) { for (int i = 0; i < n; ++i) { pr.println(i + " -> " + arr[i]); } } } public static void debug(ArrayList<Integer> list) { if (sysFlag) { for (int i = 0; i < list.size(); ++i) { pr.println(i + " -> " + list.get(i)); } } } public static void Ldebug(ArrayList<Long> list) { if (sysFlag) { for (int i = 0; i < list.size(); ++i) { pr.println(i + " -> " + list.get(i)); } } } public static void debugmapII(HashMap<Integer, Integer> map) { if (sysFlag) { for (Map.Entry<Integer, Integer> entry : map.entrySet()) pr.println("Key => " + entry.getKey() + ", Value => " + entry.getValue()); } } public static void debugmapLI(HashMap<Long, Integer> map) { if (sysFlag) { for (Map.Entry<Long, Integer> entry : map.entrySet()) pr.println("Key => " + entry.getKey() + ", Value => " + entry.getValue()); } } public static void debugmapLL(HashMap<Long, Long> map) { if (sysFlag) { for (Map.Entry<Long, Long> entry : map.entrySet()) pr.println("Key => " + entry.getKey() + ", Value => " + entry.getValue()); } } public static void debugmapIL(HashMap<Integer, Long> map) { if (sysFlag) { for (Map.Entry<Integer, Long> entry : map.entrySet()) pr.println("Key => " + entry.getKey() + ", Value => " + entry.getValue()); } } public static void debugmapSL(HashMap<String, Long> map) { if (sysFlag) { for (Map.Entry<String, Long> entry : map.entrySet()) pr.println("Key => " + entry.getKey() + ", Value => " + entry.getValue()); } } public static void debugmapCL(HashMap<Character, Long> map) { if (sysFlag) { for (Map.Entry<Character, Long> entry : map.entrySet()) pr.println("Key => " + entry.getKey() + ", Value => " + entry.getValue()); } } public static void debugmapSI(HashMap<String, Integer> map) { if (sysFlag) { for (Map.Entry<String, Integer> entry : map.entrySet()) pr.println("Key => " + entry.getKey() + ", Value => " + entry.getValue()); } } public static void debugmapCI(HashMap<Character, Integer> map) { if (sysFlag) { for (Map.Entry<Character, Integer> entry : map.entrySet()) pr.println("Key => " + entry.getKey() + ", Value => " + entry.getValue()); } } public static void debug(Set<Integer> set) { if (sysFlag) { Iterator value = set.iterator(); int i = 1; while (value.hasNext()) { pr.println((i++) + "-> " + value.next()); } } } }
0
Non-plagiarised
d3a96420
ff34fab2
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(); } }
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
018c15bd
378bb1ca
import java.util.*; import java.util.Map.Entry; import java.io.*; import java.math.*; import static java.util.stream.Collectors.*; import static java.util.Map.Entry.*; /* Name of the class has to be "Main" only if the class is public. */ public class Hey { public static void main (String[] args) throws IOException { final long mod=(long) (1e9+7); Reader s=new Reader(); PrintWriter pt=new PrintWriter(System.out); // BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int T=s.nextInt(); // int T=Integer.parseInt(br.readLine()); // int T=1; while(T-->0) { int n=s.nextInt(); int c[]=new int[n]; for(int i=0;i<n;i++) { c[i]=s.nextInt(); } long mo[]=new long[n]; long me[]=new long[n]; mo[0]=-1; me[0]=-1; mo[1]=c[0]; me[1]=c[1]; long so[]=new long[n]; long se[]=new long[n]; so[1]=c[0]; se[1]=c[1]; for(int i=2;i<n;i++) { if(i%2==0) { mo[i]=Math.min(c[i], mo[i-1]); me[i]=me[i-1]; so[i]=so[i-1]+c[i]; se[i]=se[i-1]; } else { mo[i]=mo[i-1]; me[i]=Math.min(c[i], me[i-1]); so[i]=so[i-1]; se[i]=se[i-1]+c[i]; } } // pa(mo); // pa(me); // pa(so); // pa(se); long min=Long.MAX_VALUE; for(int i=1;i<n;i++) { long k1=0, k2=0; if(i%2==1) { k1=i/2+1; k2=i/2+1; } else { k2=i/2; k1=k2+1; } min=Math.min((n-k1)*mo[i]+so[i]+(n-k2)*me[i]+se[i], min); } pt.println(min); } pt.close(); } static boolean isSorted(int arr[], int n) { for(int i=1;i<n;i++) { if(arr[i]>=arr[i-1]==false) { return false; } } return true; } static int setBit(int S, int j) { return S | 1 << j; } static int clearBit(int S, int j) { return S & ~(1 << j); } static int toggleBit(int S, int j) { return S ^ 1 << j; } static boolean isOn(int S, int j) { return (S & 1 << j) != 0; } static int turnOnLastZero(int S) { return S | S + 1; } static int turnOnLastConsecutiveZeroes(int S) { return S | S - 1; } static int turnOffLastBit(int S) { return S & S - 1; } static int turnOffLastConsecutiveBits(int S) { return S & S + 1; } static int lowBit(int S) { return S & -S; } static int setAll(int N) { return (1 << N) - 1; } static int modulo(int S, int N) { return (S & N - 1); } //S%N, N is a power of 2 static boolean isPowerOfTwo(int S) { return (S & S - 1) == 0; } static boolean isWithin(long x, long y, long d, long k) { return x*k*x*k + y*k*y*k <= d*d; } static long modFact(long n, long p) { if (n >= p) return 0; long result = 1; for (int i = 1; i <= n; i++) result = (result * i) % p; return result; } static int sum(int[] arr, int n) { int inc[]=new int[n+1]; int dec[]=new int[n+1]; inc[0] = arr[0]; dec[0] = arr[0]; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (arr[j] > arr[i]) { dec[i] = max(dec[i], inc[j] + arr[i]); } else if (arr[i] > arr[j]) { inc[i] = max(inc[i], dec[j] + arr[i]); } } } return max(inc[n - 1], dec[n - 1]); } static long nc2(long a) { return a*(a-1)/2; } public static int numberOfprimeFactors(int n) { // Print the number of 2s that divide n HashSet<Integer> hs = new HashSet<Integer>(); while (n%2==0) { hs.add(2); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n while (n%i == 0) { hs.add(i); n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (n > 2) hs.add(n); return hs.size(); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static void reverse(int arr[],int start, int end) { int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } static void reverse(long arr[],int start, int end) { long temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop 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 int p2(int n) { int k=0; while(n>1) { if(n%2!=0) return k; n/=2; k++; } return k; } static boolean isp2(int n) { while(n>1) { if(n%2==1) return false; n/=2; } return true; } static int binarySearch(int arr[], int first, int last, int key){ int mid = (first + last)/2; while( first <= last ){ if ( arr[mid] < key ){ first = mid + 1; }else if ( arr[mid] == key ){ return mid; }else{ last = mid - 1; } mid = (first + last)/2; } return -1; } static void print(int a[][]) { for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) System.out.print(a[i][j]+" "); System.out.println(); } } static int max (int x, int y) { return (x > y)? x : y; } static int search(Pair[] p, Pair pair) { int l=0, r=p.length; while (l <= r) { int m = l + (r - l) / 2; if (p[m].compareTo(pair)==0) return m; if (p[m].compareTo(pair)<0) l = m + 1; else r = m - 1; } return -1; } static void pa(int a[]) { for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } static void pa(long a[]) { for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } static void reverseArray(int arr[], int start, int end) { int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } static boolean isPalindrome(String s) { int l=s.length(); for(int i=0;i<l/2;i++) { if(s.charAt(i)!=s.charAt(l-i-1)) return false; } return true; } static long nc2(long n, long m) { return (n*(n-1)/2)%m; } static long c(long a) { return a*(a+1)/2; } static int next(int[] arr, int target) { int start = 0, end = arr.length - 1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; // Move to right side if target is // greater. if (arr[mid] < target) { start = mid + 1; } // Move left side. else { ans = mid; end = mid - 1; } } return ans; } static long power(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 power(n, p-2, p); } static long nCrModP(long n, long r, long p) { if(r>n) 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; } static String reverse(String str) { return new StringBuffer(str).reverse().toString(); } static long fastpow(long x, long y, long m) { if (y == 0) return 1; long p = fastpow(x, y / 2, m) % m; p = (p * p) % m; if (y % 2 == 0) return p; else return (x * p) % m; } static boolean isPerfectSquare(long l) { return Math.pow((long)Math.sqrt(l),2)==l; } static void merge(long[] arr, int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long [n1]; long R[] = new long [n2]; /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() static void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } static void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int [n1]; int R[] = new int [n2]; /*Copy data to temp arrays*/ for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } static class Pair implements Comparable<Pair>{ int a; int b; Pair(int a,int b){ this.a=a; this.b=b; } public int compareTo(Pair p){ if(a>p.a) return 1; if(a==p.a) return (b-p.b); return -1; } } 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[128]; // 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(); } } }
//package Current; import java.util.*; public class test { static final long mod = (long) 1e9 + 7; static class pair { int x, y; public pair(int x, int y) { this.x = x; this.y = y; } } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = 1; t = sc.nextInt(); while (t-- > 0) { // Start code int n = sc.nextInt(); long[] cost = new long[n]; long[] a = new long[n]; for (int i = 0; i < n; i++) { cost[i] = sc.nextLong(); a[i] = cost[i]; if (i > 1) a[i] = Math.min(a[i], a[i - 2]); } long sum = cost[0]; long res = Long.MAX_VALUE; for (int i = 1; i < n; i++) { sum += cost[i]; long cur = sum - a[i] - a[i - 1]; int p = (i + 1) / 2; int q = (i + 1) - p; int x = n - p + 1; int y = n - q + 1; cur += (a[i - 1] * x + a[i] * y); res = Math.min(res, cur); } println(res); } sc.close(); } static void print(Object o) { System.out.print(o + " "); } static void println(Object o) { System.out.println(o); } static void swap(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static ArrayList<Long> factorial(int num) { ArrayList<Long> fac = new ArrayList<>(); fac.add((long) 0); fac.add((long) 1); for (int i = 2; i < num; i++) { fac.add((fac.get(i - 1) * i) % mod); } return fac; } static long ncr(long x, long y, ArrayList<Long> fac) { if (y >= x) return (long) 1; long res = fac.get((int) x); long z = (fac.get((int) y) * fac.get((int) (x - y))) % mod; z = modInv(z); res = (res * z) % mod; return res; } static long modInv(long x) { return modExpo(x, mod - 2); } static long modExpo(long x, long y) { long res = 1; x = x % mod; while (y > 0) { if (y % 2 == 1) res = (res * x) % mod; y = y / 2; x = (x * x) % mod; } return res; } static int lowerBound(int n, long[] arr, long value) { int res = (int) 1e7; int l = 0, r = n - 1; while (l <= r) { int mid = l + (r - l) / 2; if (arr[mid] >= value) { res = mid; r = mid - 1; } else l = mid + 1; } return res; } }
0
Non-plagiarised
07043d35
97a7fab5
// Piyush Nagpal import java.util.*; import java.io.*; public class C{ static int MOD=1000000007; static PrintWriter pw; static FastReader sc; 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());} public char nextChar() throws IOException {return next().charAt(0);} String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int[] intArr(int n) throws IOException {int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();return in;} public long[] longArr(int n) throws IOException {long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();return in;} } public static long gcd(long a,long b){if( b>a ){return gcd(b,a);}if( b==0 ){return a;} return gcd(b,a%b);} public static long expo( long a,long b,long M ){long result=1;while(b>0){if( b%2==1 ){result=(result*a)%M;}a=(a*a)%M;b=b/2;}return result%M;} public static ArrayList<Long> Sieve(int n){boolean [] arr= new boolean[n+1];Arrays.fill(arr, true);ArrayList<Long> list= new ArrayList<>();for(int i=2;i<=n;i++){if( arr[i]==true ){list.add((long)i);}for(int j=2*i;j<=n;j+=i){arr[j]=false;}}return list;} public static void printarr(int [] arr){for(int i=0;i<arr.length;i++){pw.print(arr[i]+" ");}} public static void printarr(long [] arr){for(int i=0;i<arr.length;i++){pw.print(arr[i]+" ");}} // int [] arr=sc.intArr(n); static int Max=2*100005; // static int [][] arr= new int [Max][5]; // static int [] tot= new int [Max]; static ArrayList<ArrayList<Integer>> arr= new ArrayList<>(); static ArrayList<Integer> tot= new ArrayList<>(); static int fun(int ch , int n){ ArrayList<Integer> list= new ArrayList<>(); for(int i=0;i<n;i++){ list.add( (arr.get(i).get(ch))-(tot.get(i)-(arr.get(i).get(ch))) ); } Collections.sort(list,Collections.reverseOrder()); int count=0,sum=0; for(Integer x: list){ sum+=x; if(sum>0){ count++; } } return count; } static void solve() throws Exception{ int n=sc.nextInt(); arr= new ArrayList<>(); tot= new ArrayList<>(); for(int i=0;i<n;i++){ arr.add( new ArrayList<>()); tot.add(0); for(int j=0;j<5;j++){ ArrayList<Integer> list=arr.get(i); list.add(0); } } // pw.println(tot); // pw.println(arr); // return; for(int i=0;i<n;i++){ String s=sc.next(); tot.set(i,s.length()); for(int j=0;j<s.length();j++){ ArrayList<Integer> list=arr.get(i); list.set((s.charAt(j)-'a'),list.get((s.charAt(j)-'a'))+1); // list.set((s.charAt(j)-'a'),1); } } int ans =0; for(int i=0;i<5;i++){ ans = Math.max(ans,fun(i,n)); } pw.println(ans); } public static void main(String[] args) throws Exception{ try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } sc= new FastReader(); pw = new PrintWriter(System.out); int tc=1; tc=sc.nextInt(); for(int i=1;i<=tc;i++) { // pw.printf("Case #%d: "b, i); solve(); } pw.flush(); } }
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.Collections; import java.util.HashSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.Vector; public class Main{ static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(outputStream); public static void main(String[] args) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); String[] v = new String[n]; for (int i = 0; i < n; i++) { v[i] = in.next(); } ArrayList<ArrayList<Integer>> v1 = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> list = new ArrayList<>(); for (int j = 0; j < 5; j++) { list.add(0); } v1.add(list); } for (int i = 0; i < n; i++) { String s = v[i]; for (int j = 0; j < s.length(); j++) { int val = v1.get(i).get(s.charAt(j) - 'a'); val++; v1.get(i).set(s.charAt(j) - 'a', val); } } int ret = 0; for (int i = 0; i < 5; i++) { ArrayList<Integer> v3 = new ArrayList<>(); for (int j = 0; j < n; j++) { String s1 = v[j]; int n2 = s1.length(); int cnt = v1.get(j).get(i); int diff = cnt - (n2 - cnt); v3.add(diff); } Collections.sort(v3); Collections.reverse(v3); int till = 0; for (int k = 0; k < n; k++) { till += v3.get(k); if (till > 0) { ret = Math.max(ret, k + 1); } } } out.println(ret); } out.close(); } 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 long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public String nextLine() throws IOException { return reader.readLine().trim(); } } }
0
Non-plagiarised
24b20554
43b10dec
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); } }
// package eround101; import java.util.*; import java.io.*; import java.lang.*; import java.util.StringTokenizer; import java.util.concurrent.TimeUnit; public class C101 { static HritikScanner sc = new HritikScanner(); static PrintWriter pw = new PrintWriter(System.out, true); final static int MOD = (int) 1e9 + 7; static StringBuilder sb = new StringBuilder(); public static void main(String[] args) { int t = ni(); while (t-- > 0) { solve(); } } static void solve() { int n = ni(); int m = ni(), x = ni(); Map<Integer, ArrayList<Integer>> map = new TreeMap<>(); pl("YES"); for(int i = 0; i < n; i++) { int num = ni(); if(!map.containsKey(num)) { map.put(num, new ArrayList<Integer>()); } map.get(num).add(i); } int[] ans = new int[n]; int q = 0; for(int block : map.keySet()) { for(int i = 0; i < map.get(block).size(); i++) { ans[map.get(block).get(i)] = (q++)%m+1; } } for(int ele : ans) { p(ele + " "); } pl(); } ///////////////////////////////////////////////////////////////////////////////// static int[] nextIntArray(int n) { int[] arr = new int[n]; int i = 0; while (i < n) { arr[i++] = ni(); } return arr; } static long[] nextLongArray(int n) { long[] arr = new long[n]; int i = 0; while (i < n) { arr[i++] = nl(); } return arr; } static int[] nextIntArray1(int n) { int[] arr = new int[n + 1]; int i = 1; while (i <= n) { arr[i++] = ni(); } return arr; } ///////////////////////////////////////////////////////////////////////////////// static int ni() { return sc.nextInt(); } static long nl() { return sc.nextLong(); } static double nd() { return sc.nextDouble(); } ///////////////////////////////////////////////////////////////////////////////// static void pl() { pw.println(); } static void p(Object o) { pw.print(o); } static void pl(Object o) { pw.println(o); } static void psb(StringBuilder sb) { pw.print(sb); } static void pa(Object arr[]) { for (Object o : arr) { p(o); } pl(); } static void pa(int arr[]) { for (int o : arr) { p(o); } pl(); } static void pa(long arr[]) { for (long o : arr) { p(o); } pl(); } static void pa(double arr[]) { for (double o : arr) { p(o); } pl(); } static void pa(char arr[]) { for (char o : arr) { p(o); } pl(); } static void pa(List list) { for (Object o : list) { p(o); } pl(); } static void pa(Object[][] arr) { for (int i = 0; i < arr.length; ++i) { for (Object o : arr[i]) { p(o); } pl(); } } static void pa(int[][] arr) { for (int i = 0; i < arr.length; ++i) { for (int o : arr[i]) { p(o); } pl(); } } static void pa(long[][] arr) { for (int i = 0; i < arr.length; ++i) { for (long o : arr[i]) { p(o); } pl(); } } static void pa(char[][] arr) { for (int i = 0; i < arr.length; ++i) { for (char o : arr[i]) { p(o); } pl(); } } static void pa(double[][] arr) { for (int i = 0; i < arr.length; ++i) { for (double o : arr[i]) { p(o); } pl(); } } ///////////////////////////////////////////////////////////////////////////////// static void print(Object s) { System.out.println(s); } ///////////////////////////////////////////////////////////////////////////////// //-----------HritikScanner class for faster input----------// static class HritikScanner { BufferedReader br; StringTokenizer st; public HritikScanner() { 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 num, row,col; Pair(int num, int row, int col) { this.num = num; this.row = row; this.col = col; } public int get1() { return num; } public int get2() { return row; } public int compareTo(Pair A) { return this.num - A.num; } public String toString() { return num +" "+ row+" "+col; } } ////////////////////////////////////////////////////////////////// // Function to return gcd of a and b time complexity O(log(a+b)) static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } // method to return LCM of two numbers static long lcm(int a, int b) { return (a / gcd(a, b)) * b; } ////////////////////////////////////////////////////////////////// static boolean isPrime(long n) { // Corner cases if (n <= 1) { return false; } if (n <= 3) { return true; } // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) { return false; } for (long i = 5; i * i <= n; i = i + 6) { if (n % i == 0 || n % (i + 2) == 0) { return false; } } return true; } ////////////////////////////////////////////////////////////////// static boolean isPowerOfTwo(long n) { if (n == 0) { return false; } return (long) (Math.ceil((Math.log(n) / Math.log(2)))) == (long) (Math.floor(((Math.log(n) / Math.log(2))))); } public static long getFact(int n) { long ans = 1; while (n > 0) { ans *= n; ans %= MOD; n--; } return ans; } public static long pow(long n, int pow) { if (pow == 0) { return 1; } long temp = pow(n, pow / 2) % MOD; temp *= temp; temp %= MOD; if (pow % 2 == 1) { temp *= n; } temp %= MOD; return temp; } public static long nCr(int n, int r) { long ans = 1; int temp = n - r; while (n > temp) { ans *= n; ans %= MOD; n--; } ans *= pow(getFact(r) % MOD, MOD - 2) % MOD; ans %= MOD; return ans; } ////////////////////////////////////////////////////////////////// // method returns Nth power of A static double nthRoot(int A, int N) { // intially guessing a random number between // 0 and 9 double xPre = Math.random() % 10; // smaller eps, denotes more accuracy double eps = 0.001; // initializing difference between two // roots by INT_MAX double delX = 2147483647; // xK denotes current value of x double xK = 0.0; // loop untill we reach desired accuracy while (delX > eps) { // calculating current value from previous // value by newton's method xK = ((N - 1.0) * xPre + (double) A / Math.pow(xPre, N - 1)) / (double) N; delX = Math.abs(xK - xPre); xPre = xK; } return xK; } }
1
Plagiarised
1a6f8b20
77448a05
import java.io.*; import java.util.*; public class Main { public static void main(String[] args)throws Exception { Main ob=new Main(); ob.fun(); } public void fun()throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out); int t=Integer.parseInt(br.readLine()); while(t-->0) { int n=Integer.parseInt(br.readLine()); int ar[][]=new int [n][5]; int len[]=new int[n]; for(int i=0;i<n;i++) { String s=(br.readLine()); for(int j=0;j<s.length();j++) { ar[i][s.charAt(j)-'a']++; len[i]=s.length(); } } int max=0; for(int i=0;i<5;i++) { int num=fun2(ar,len,i); max=Math.max(num,max); } pw.println(max); } pw.flush(); } public int fun2(int ar[][],int len[],int col) { int ct=0; int n=ar.length; PriorityQueue<Integer> pq=new PriorityQueue<Integer>(Collections.reverseOrder()); for(int i=0;i<n;i++) { int dif=2*ar[i][col]-len[i]; pq.add(dif); } int sum=0; while(pq.size()>0) { int num=(int)(pq.poll()); if((sum+num)>0) { ct++; sum+=num; } } return ct; } }
//package currentContest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Random; import java.util.StringTokenizer; public class P4 { static int N = 1000001; // Array to store inverse of 1 to N static long[] factorialNumInverse = new long[N + 1]; // Array to precompute inverse of 1! to N! static long[] naturalNumInverse = new long[N + 1]; // Array to store factorial of first N numbers static long[] fact = new long[N + 1]; // Function to precompute inverse of numbers public static void InverseofNumber(int p) { naturalNumInverse[0] = naturalNumInverse[1] = 1; for (int i = 2; i <= N; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long) (p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse[0] = factorialNumInverse[1] = 1; // Precompute inverse of natural numbers for (int i = 2; i <= N; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to N public static void factorial(int p) { fact[0] = 1; // Precompute factorials for (int i = 1; i <= N; i++) { fact[i] = (fact[i - 1] * (long) i) % p; } } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R, int p) { // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p; return ans; } public static void main(String[] args) { // TODO Auto-generated method stub FastReader sc = new FastReader(); int t; t = sc.nextInt(); StringBuilder st = new StringBuilder(); // int p = 998244353; // InverseofNumber(p); // InverseofFactorial(p); // factorial(p); // System.out.println(3|1); while (t-- != 0) { int n=sc.nextInt(); String s[]=new String[n]; for(int i=0;i<n;i++) { s[i]=sc.nextLine(); } int max=0; ArrayList<Integer> freq=new ArrayList<>(); for(int i=0;i<5;i++) { int f1=0; freq=new ArrayList<>(); for(int j=0;j<n;j++) { f1=0; for(int k=0;k<s[j].length();k++) { if(s[j].charAt(k)==i+'a') { f1++; }else { f1--; } } freq.add(f1); } Collections.sort(freq); int x=0; int total=0; for(int j=n-1;j>=0;j--) { total=total+freq.get(j); if(total>0) { x++; }else { break; } } max=Math.max(max, x); } st.append(max+"\n"); } System.out.println(st); } static FastReader sc = new FastReader(); public static void solvegraph() { int n = sc.nextInt(); int edge[][] = new int[n - 1][2]; for (int i = 0; i < n - 1; i++) { edge[i][0] = sc.nextInt() - 1; edge[i][1] = sc.nextInt() - 1; } ArrayList<ArrayList<Integer>> ad = new ArrayList<>(); for (int i = 0; i < n; i++) { ad.add(new ArrayList<Integer>()); } for (int i = 0; i < n - 1; i++) { ad.get(edge[i][0]).add(edge[i][1]); ad.get(edge[i][1]).add(edge[i][0]); } int parent[] = new int[n]; Arrays.fill(parent, -1); parent[0] = n; ArrayDeque<Integer> queue = new ArrayDeque<>(); queue.add(0); int child[] = new int[n]; Arrays.fill(child, 0); ArrayList<Integer> lv = new ArrayList<Integer>(); while (!queue.isEmpty()) { int toget = queue.getFirst(); queue.removeFirst(); child[toget] = ad.get(toget).size() - 1; for (int i = 0; i < ad.get(toget).size(); i++) { if (parent[ad.get(toget).get(i)] == -1) { parent[ad.get(toget).get(i)] = toget; queue.addLast(ad.get(toget).get(i)); } } lv.add(toget); } child[0]++; } 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(); } public 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
3dd65549
c1638a45
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); }
import java.io.*; import java.util.*; public class qC { 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()); char[] curr = br.readLine().toCharArray(); char[] sol = br.readLine().toCharArray(); // int oddP = 0; // int evenP = 0; // int even1 = 0; // int odd1 = 0; // int even0 = 0; // int odd0 = 0; // boolean alreadySolved = true; // for(int i = 0;i < N;i++) { // if(curr[i] == sol[i]) { // evenP++; // if(curr[i] == '1') even1++; // else even0++; // } // else { // oddP++; // if(curr[i] == '1') odd1++; // else odd0++; // alreadySolved = false; // } // } // // if(alreadySolved) { // System.out.println(0); // continue; // } // int minSwaps = Integer.MAX_VALUE; // if(N % 2 == 1) { // if(evenP % 2 == 1 && Math.abs(odd1 - odd0) <= 1 && odd1 > 0 && odd0 > 0) { // minSwaps = Math.min(minSwaps, oddP); // } // if(oddP % 2 == 0 && Math.abs(even1 - even0) <= 1 && even1 > 0 && even1 > 0) { // minSwaps = Math.min(minSwaps, evenP); // } // } // else { // if(evenP % 2 == 0 && Math.abs(odd1 - odd0) <= 1 && odd1 > 0 && odd0 > 0) { // minSwaps = Math.min(minSwaps, oddP); // } // if(oddP % 2 == 1 && Math.abs(even1 - even0) <= 1 && even1 > 0 && even1 > 0) { // minSwaps = Math.min(minSwaps, evenP); // } // } // System.out.println((minSwaps == Integer.MAX_VALUE) ? -1: minSwaps); int curr1 = 0; int sol1 = 0; int mismatch = 0; for(int i = 0;i < N;i++) { if(curr[i] == '1') curr1++; if(sol[i] == '1') sol1++; if(curr[i] != sol[i]) mismatch++; } int minAns = Integer.MAX_VALUE; //even operations if(curr1 == sol1 && mismatch % 2 == 0) { minAns = Math.min(mismatch, minAns); } //odd operations for(int i = 0;i < N;i++) { if(curr[i] == '1') { int tempcurr1 = N - curr1 + 1; int tempmismatch; if(sol[i] == '0') { tempmismatch = N - mismatch; } else { tempmismatch = N - mismatch - 1; } if(tempcurr1 == sol1 && tempmismatch % 2 == 0) { minAns = Math.min(minAns, tempmismatch + 1); } } } System.out.println((minAns == Integer.MAX_VALUE) ? -1 : minAns); } } }
0
Non-plagiarised
7f42483d
c9a316ca
import java.io.*; import java.util.*; public class Main { static final boolean INPUT_FROM_FILE = false; static final String INPUT_FILE = "input/input.txt"; static final String OUTPUT_FILE = "input/output.txt"; static final long M = (long) 1e9 + 7; static FastReader in; static FastWriter out; static { try { in = new FastReader(); out = new FastWriter(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { int t = in.nextInt(); while (t-- > 0) solve(); out.close(); } static long[][] dp; static long dfs(List<List<Integer>> tree, int parent, int current, int parentState, int[][] vRange) { if(dp[current][parentState] != -1) { return dp[current][parentState]; } long left = Math.abs(vRange[current][0] - vRange[parent][parentState]); long right = Math.abs(vRange[current][1] - vRange[parent][parentState]); for(int child : tree.get(current)) { if(child != parent) { left += dfs(tree, current, child, 0, vRange); right += dfs(tree, current, child, 1, vRange); } } dp[current][parentState] = Math.max(left, right); return dp[current][parentState]; } private static void solve() { int n = in.nextInt(); int[][] vRange = new int[n+1][2]; for(int i=1; i<=n; i++) { int l = in.nextInt(), r = in.nextInt(); vRange[i][0] = l; vRange[i][1] = r; } List<List<Integer>> tree = new ArrayList<>(); for(int i=0; i<=n; i++) tree.add(new LinkedList<>()); for(int i=0; i<n-1; i++) { int u = in.nextInt(); int v = in.nextInt(); tree.get(u).add(v); tree.get(v).add(u); } dp = new long[n+1][2]; for(int i=0; i<=n; i++) { Arrays.fill(dp[i], -1); } long left = 0, right = 0; for(int v : tree.get(1)) { left += dfs(tree, 1, v, 0, vRange); right += dfs(tree, 1, v, 1, vRange); } out.println(Math.max(left, right)); } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br; public FastReader() throws FileNotFoundException { if (INPUT_FROM_FILE) { this.stream = new FileInputStream(INPUT_FILE); br = new BufferedReader(new FileReader(INPUT_FILE)); } else { this.stream = System.in; br = new BufferedReader(new InputStreamReader(System.in)); } } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } static class FastWriter { BufferedWriter writer; StringBuilder sb; public FastWriter() throws IOException { if (INPUT_FROM_FILE) writer = new BufferedWriter(new FileWriter(OUTPUT_FILE)); else writer = new BufferedWriter(new PrintWriter(System.out)); sb = new StringBuilder(); } public void print(Object obj) { sb.append(obj); } public void println(Object obj) { sb.append(obj).append('\n'); } public void close() throws IOException { writer.write(sb.toString()); writer.close(); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import static javax.swing.UIManager.get; import static javax.swing.UIManager.getString; public class Main { static class Pair implements Comparable<Pair> { int x = 0; int y = 0; public Pair(int x1, int y1) { x = x1; y = y1; } @Override public int compareTo(Pair o) { return this.x - o.x; } } static boolean checkPallindrome(int n) { ArrayList<Integer> list = new ArrayList<>(); while (n > 0) { list.add(n % 10); n /= 10; } int low = 0, high = list.size() - 1; while (low <= high) { if (list.get(low) != list.get(high)) return false; low++; high--; } return true; } static boolean check(String s) { int low = 0, high = s.length() - 1; while (low <= high) { if (s.charAt(low) != s.charAt(high)) return false; low++; high--; } return true; } class Node implements Comparable<Node> { int x = 0, y = 0, z = 0; @Override public int compareTo(Node o) { return this.y - o.y; } } static int min = Integer.MAX_VALUE; public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); //(1)very very important**(never take the first problem for granted, always check the test cases) take 5 minutes more and check the edge cases // 5 minutes will not decreases rating as much as a wrong submission does it is easy u just think with an open mind and u will surely get the answer //(2)let ur brain consume the problem don't just jump to the solution. after reading the problem take a pause 1 minute //(3)go through the example test cases and also at least two of ur own test cases.Think of testcases which are difficult(edge cases).dry run ur concept //(4) sometimes if else condition is not required but due to if else you miss some points and get wrong answer int t = sc.nextInt(); while (t-- > 0) { int n =sc.nextInt(); ArrayList<ArrayList<Integer>> list = new ArrayList<>(); for(int i=0;i<n;i++) list.add(new ArrayList<Integer>()); ArrayList<Pair> list1 = new ArrayList<>(); for(int i=0;i<n;i++) list1.add(new Pair(sc.nextInt(),sc.nextInt())); for(int i=0;i<n-1;i++) { int a =sc.nextInt()-1,b=sc.nextInt()-1; list.get(a).add(b); list.get(b).add(a); } long[][] dp = new long[2][n]; dfs(0,-1,dp,list,list1); System.out.println(Math.max(dp[0][0],dp[1][0])); } } static void dfs(int u,int p,long[][] dp,ArrayList<ArrayList<Integer>> list,ArrayList<Pair> list1) { for(int v:list.get(u)) { if(v==p) continue; dfs(v,u,dp,list,list1); dp[1][u]+= Math.max(Math.abs(list1.get(u).y-list1.get(v).x)+dp[0][v],Math.abs(list1.get(u).y-list1.get(v).y)+dp[1][v]); dp[0][u]+=Math.max(Math.abs(list1.get(u).x-list1.get(v).x)+dp[0][v],Math.abs(list1.get(u).x-list1.get(v).y)+dp[1][v]); } } //static int lcs( int[] X, int[] Y, int m, int n ) //{ // int L[][] = new int[m+1][n+1]; // // /* Following steps build L[m+1][n+1] in bottom up fashion. Note // that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */ // for (int i=0; i<=m; i++) // { // for (int j=0; j<=n; j++) // { // if (i == 0 || j == 0) // L[i][j] = 0; // else if (X[i-1] == Y[j-1]) // L[i][j] = L[i-1][j-1] + 1; // else // L[i][j] = Math.max(L[i-1][j], L[i][j-1]); // } // } // return L[m][n]; //} // syntax of conditional operator y=(x==1)?1:0; //Things to check when u r getting wrong answer // 1- check the flow of the code //2- If ur stuck read the problem once again //3- before submitting always check the output format of ur code //4- don't check standings until problem B is done //5- if u r thinking ur concept is correct but still u r getting wrong answer try to implement it in another way //6- By default, java interpret all numeral literals as 32-bit integer values. // If you want to explicitely specify that this is something bigger then 32-bit integer you should use suffix L for long values. example long a = 600851475143L //All the functions long pow(long a,long b,long m) { a%=m; long res=1L; while(b>0) { long temp=b&1; if(temp==1) res=(res*a)%m; a=(a*a)%m; b>>=1; } return res; } static class DisjointUnionSets { int[] rank, parent; int n; // Constructor public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } // Creates n sets with single item in each void makeSet() { for (int i = 0; i < n; i++) { // Initially, all elements are in // their own set. parent[i] = i; } } // Returns representative of x's set int find(int x) { // Finds the representative of the set // that x is an element of if (parent[x] != x) { // if x is not the parent of itself // Then x is not the representative of // his set, parent[x] = find(parent[x]); // so we recursively call Find on its parent // and move i's node directly under the // representative of this set } return parent[x]; } // Unites the set that includes x and the set // that includes x void union(int x, int y) { // Find representatives of two sets int xRoot = find(x), yRoot = find(y); // Elements are in the same set, no need // to unite anything. if (xRoot == yRoot) return; // If x's rank is less than y's rank if (rank[xRoot] < rank[yRoot]) // Then move x under y so that depth // of tree remains less parent[xRoot] = yRoot; // Else if y's rank is less than x's rank else if (rank[yRoot] < rank[xRoot]) // Then move y under x so that depth of // tree remains less parent[yRoot] = xRoot; else // if ranks are the same { // Then move y under x (doesn't matter // which one goes where) parent[yRoot] = xRoot; // And increment the result tree's // rank by 1 rank[xRoot] = rank[xRoot] + 1; } } } static void debug(String s) { System.out.println(s); } //collections.sort use merge sort instead of quick sort but arrays.sort use quicksort whose worst time complexity is O(n^2) static int[] sort(int[] a) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<a.length;i++) list.add(a[i]); Collections.sort(list); int ind=0; for(int x:list) a[ind++]=x; return a; } //function to print an array for debugging static void print(int[] a) { for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } static void printc(char[] a) { for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } //normal gcd function, always put the greater number as a and the smaller number as b static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static int lcm(int a,int b) { return (a*b)/gcd(a,b); } //to find gcd and lcm for numbers of long data type static long gcdl(long a, long b) { if (b == 0) return a; return gcdl(b, a % b); } static long lcml(long a,long b) { return (a*b)/gcdl(a,b); } //Input Reader to read faster input 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[] a = new int[n]; for(int i=0;i<n;i++) a[i]=nextInt(); return a; } } }
0
Non-plagiarised
0d4d22e0
f898cee0
import java.util.*; public class CodeForces { static ArrayList<String>res; public static void main(String[] args) { Scanner input = new Scanner(System.in); int test = input.nextInt(); while(test-->0){ int n = input.nextInt(); int m = input.nextInt(); char arr[][] = new char[n][m]; res = new ArrayList<>(); for(int i =0;i<n;i++){ for(int j =0;j<1;j++){ arr[i] = input.next().toCharArray(); } } for(int i =0;i<n-1;i++){ for(int j =0;j<m-1;j++){ int count =0; if(arr[i+1][j] == '1')count++; if(arr[i][j+1] == '1')count++; if(arr[i][j] == '1')count++; if(arr[i+1][j+1] == '1')count++; if(count == 0)continue; if(count == 1)one(arr,i,j); if(count == 2)two(arr,i,j); if(count == 3)three(arr,i,j); if(count == 4)four(arr,i,j); } } System.out.println(res.size()); for(String it:res)System.out.println(it); } } public static void one(char arr[][],int i,int j){ int row = i+2; int col = j+2; int count =0; StringBuilder sb = new StringBuilder(); for(int x=i;x<row;x++){ for(int y = j;y<col;y++){ if(arr[x][y] == '1' ){ sb.append(x+1); sb.append(" "); sb.append(y+1); arr[x][y] = '0'; sb.append(" "); }else if(arr[x][y] == '0' && count<2){ sb.append(x+1); sb.append(" "); sb.append(y+1); arr[x][y] = '1'; sb.append(" "); count++; } } } res.add(sb.toString()); two(arr,i,j); } public static void two(char arr[][],int i,int j){ int row = i+2; int col = j+2; boolean ok = true; StringBuilder sb = new StringBuilder(); for(int x=i;x<row;x++){ for(int y = j;y<col;y++){ if(arr[x][y] == '1' && ok){ sb.append(x+1); sb.append(" "); sb.append(y+1); arr[x][y] = '0'; sb.append(" "); ok = false; }else if(arr[x][y] == '0'){ sb.append(x+1); sb.append(" "); sb.append(y+1); arr[x][y] = '1'; sb.append(" "); } } } res.add(sb.toString()); three(arr,i,j); } public static void three(char arr[][],int i,int j){ StringBuilder sb = new StringBuilder(); if(arr[i][j] == '1'){ sb.append(i+1); sb.append(" "); sb.append(j+1); arr[i][j] = '0'; sb.append(" "); } if(arr[i+1][j] == '1'){ sb.append(i+2); sb.append(" "); sb.append(j+1); arr[i+1][j] = '0'; sb.append(" "); } if(arr[i][j+1] == '1'){ arr[i][j+1] ='0'; sb.append(i+1); sb.append(" "); sb.append(j+2); sb.append(" "); } if(arr[i+1][j+1] == '1'){ arr[i+1][j+1] = '0'; sb.append(i+2); sb.append(" "); sb.append(j+2); } res.add(sb.toString()); } public static void four(char arr[][],int i,int j){ int row = i+2; int col = j+2; int count =0; StringBuilder sb = new StringBuilder(); for(int x=i;x<row;x++){ for(int y = j;y<col;y++){ if(count<3){ sb.append(x+1); sb.append(" "); sb.append(y+1); arr[x][y] = '0'; sb.append(" "); count++; } } } res.add(sb.toString()); one(arr,i,j); } } /* 1.make sure to include continue or return if u are handling a specific case separately!!!! and check the constraints of every alphabet(missed to check for case 0)!!!!!!!!! 2.when u have transitions between states and its not dp try dfs (also think of topo sort) */
import java.util.*; import java.io.*; public class C{ static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int mod = (int)(1e9+7); public static long pow(long a,long b) { long ans = 1; while(b> 0) { if((b & 1)==1){ ans = (ans*a) % mod; } a = (a*a) % mod; b = b>>1; } return ans; } 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 void flip(char[][] arr,int i,int j) { if(arr[i][j]=='1') arr[i][j] = '0'; else arr[i][j] = '1'; } static void add2s(StringBuilder sb,int n,int m) { sb.append((n) +" " + (m-1) + " " +(n-1) + " " + (m) + " " +(n) + " " + (m)+"\n"); sb.append((n-1) +" " + (m-1) + " " +(n-1) + " " + (m) + " " +(n) + " " + (m)+"\n"); } static void add3s(StringBuilder sb,int n,int m) { sb.append((n) +" " + (m-1) + " " +(n-1) + " " + (m-1) + " " +(n) + " " + (m)+"\n"); sb.append((n-1) +" " + (m-1) + " " +(n) + " " + (m-1) + " " +(n-1) + " " + (m)+"\n"); } public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); int t = in.nextInt(); while(t-- >0) { // out.printLine("UAShd"); int n = in.nextInt(),m = in.nextInt(); char[][] arr = new char[n][m]; for(int i=0;i<n;i++) arr[i] = in.nextLine().toCharArray(); // boolean[][] flip = new boolean[n][m]; int nops = 0; StringBuilder res = new StringBuilder(); //do till row n-1 for(int i=0;i<n-1;i++) { for(int j=0;j<m;j++) { if(arr[i][j]=='1') { nops++; if(j==m-1) { res.append((i+1) + " " + (j+1) + " " + (i+2) + " " + (j+1) + " " + (i+2) + " " + (j) + "\n"); flip(arr,i,j); flip(arr,i+1,j); flip(arr,i+1,j-1); } else { res.append((i+1) + " " + (j+1) + " " + (i+2) + " " + (j+1) + " " + (i+2) + " " + (j+2) + "\n"); flip(arr,i,j); flip(arr,i+1,j); flip(arr,i+1,j+1); } } } } // out.printLine(nops); int i = n-2; for(int j=0;j<m-1;j++) { char a = arr[i][j],b = arr[i+1][j]; if(a=='0' && b=='0') { continue; } else if(a=='0' && b=='1') { nops++; res.append((i+2) + " " + (j+1) + " " + (i+1) + " " + (j+2) + " " + (i+2) + " " + (j+2) + "\n"); flip(arr,i+1,j); flip(arr,i,j+1); flip(arr,i+1,j+1); } else if(a=='1' && b=='0') { nops++; res.append((i+1) + " " + (j+1) + " " + (i+1) + " " + (j+2) + " " + (i+2) + " " + (j+2) + "\n"); flip(arr,i,j); flip(arr,i,j+1); flip(arr,i+1,j+1); } else { nops++; res.append((i+2) + " " + (j+1) + " " + (i+1) + " " + (j+1) + " " + (i+2) + " " + (j+2) + "\n"); flip(arr,i+1,j); flip(arr,i,j); flip(arr,i+1,j+1); } } // out.printLine(nops); char a = arr[n-2][m-1]; char b = arr[n-1][m-1]; if(a=='0' && b=='0') { } else if(a=='1' && b=='0') { nops+=3; res.append((n-1) + " " + (m) + " " + (n-1) + " " + (m-1) + " " + (n) + " " + (m-1)+"\n"); add2s(res,n,m); } else if(a=='0' && b=='1') { nops+=3; res.append((n) + " " + (m) + " " + (n-1) + " " + (m-1) + " " + (n) + " " + (m-1)+"\n"); add2s(res,n,m); } else { nops+=2; add3s(res,n,m); } // out.printLine("UAShd"); // for(i=0;i<n;i++) // out.printLine(new String(arr[i])); // if(nops>0) // res.deleteCharAt(res.length()-1); out.printLine(nops); if(nops>0) out.print(res); } out.flush(); out.close(); } }
0
Non-plagiarised
6db218a2
90b71536
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.InputMismatchException; import java.util.*; import java.io.*; import java.lang.*; public class Main{ public static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static long gcd(long a, long b){ if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a*b)/gcd(a, b); } public static void sortbyColumn(int arr[][], int col) { Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(final int[] entry1, final int[] entry2) { if (entry1[col] > entry2[col]) return 1; else return -1; } }); } static long func(long a[],int size,int s){ long max1=a[s]; long maxc=a[s]; for(int i=s+1;i<size;i++){ maxc=Math.max(a[i],maxc+a[i]); max1=Math.max(maxc,max1); } return max1; } public static class Pair<U extends Comparable<U>, V extends Comparable<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 hashCode() { return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode()); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<U, V> p = (Pair<U, V>) o; return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y)); } public int compareTo(Pair<U, V> b) { int cmpU = x.compareTo(b.x); return cmpU != 0 ? cmpU : y.compareTo(b.y); } public String toString() { return String.format("(%s, %s)", x.toString(), y.toString()); } } static class MultiSet<U extends Comparable<U>> { public int sz = 0; public TreeMap<U, Integer> t; public MultiSet() { t = new TreeMap<>(); } public void add(U x) { t.put(x, t.getOrDefault(x, 0) + 1); sz++; } public void remove(U x) { if (t.get(x) == 1) t.remove(x); else t.put(x, t.get(x) - 1); sz--; } } static void shuffle(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static long myceil(long a, long b){ return (a+b-1)/b; } static long C(long n,long r){ long count=0,temp=n; long ans=1; long num=n-r+1,div=1; while(num<=n){ ans*=num; //ans+=MOD; ans%=MOD; ans*=mypow(div,MOD-2); //ans+=MOD; ans%=MOD; num++; div++; } ans+=MOD; return ans%MOD; } static long fact(long a){ long i,ans=1; for(i=1;i<=a;i++){ ans*=i; ans%=MOD; } return ans%=MOD; } static void sieve(int n){ is_sieve[0]=1; is_sieve[1]=1; int i,j,k; for(i=2;i<n;i++){ if(is_sieve[i]==0){ sieve[i]=i; tr.add(i); for(j=i*i;j<n;j+=i){ if(j>n||j<0){ break; } is_sieve[j]=1; sieve[j]=i; } } } } // static void calc(int n){ // int i,j; // dp[n-1]=0; // if(n>1) // dp[n-2]=1; // for(i=n-3;i>=0;i--){ // long ind=n-i-1; // dp[i]=((ind*(long)mypow(10,ind-1))%MOD+dp[i+1])%MOD; // } // } static long mypow(long x,long y){ long temp; if( y == 0) return 1; temp = mypow(x, y/2); if (y%2 == 0) return (temp*temp)%MOD; else return ((x*temp)%MOD*(temp)%MOD)%MOD; } static long dist[],dp[][],left[],right[]; static int visited[],isit[]; static ArrayList<Pair<Integer,Pair<Long,Long>>> adj[],li; //static int dp[][][]; static int MOD=1000000007; static char ch[]; static int[] sieve,is_sieve; static TreeSet<Integer> tr; static long mat[][]; static void dfs(int node,int par, Pair<Long,Long> p[]){ for(Pair<Integer,Pair<Long,Long>> pp:adj[node]){ if(pp.x!=par){ //sum+=Math.abs(selected[node]-selected[pp.x]); dfs(pp.x,node,p); //System.out.println(node+" "+pp.x); long x=Math.abs(p[node].x-p[pp.x].x); long y=Math.abs(p[node].x-p[pp.x].y); long z=Math.abs(p[node].y-p[pp.x].x); long w=Math.abs(p[node].y-p[pp.x].y); left[node]+=Math.max(x+left[pp.x],y+right[pp.x]); right[node]+=Math.max(z+left[pp.x],w+right[pp.x]); } } } public static void main(String args[]){ InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter w = new PrintWriter(outputStream); int t,i,j,tno=0,tte; t=in.nextInt(); //t=1; //tte=t; while(t-->0){ //sum=0; int n=in.nextInt(); adj=new ArrayList[n+1]; left=new long[n+1]; right=new long[n+1]; visited=new int[n+1]; for(i=0;i<n+1;i++){ adj[i]=new ArrayList<>(); } Pair<Long,Long> p[]=new Pair[n+1]; for(i=1;i<=n;i++){ p[i]=new Pair<>(in.nextLong(),in.nextLong()); } for(i=0;i<n-1;i++){ int u,v; u=in.nextInt(); v=in.nextInt(); adj[u].add(new Pair<>(v,p[v])); adj[v].add(new Pair<>(u,p[u])); } dfs(1,-1,p); // for(i=0;i<n+1;i++){ // w.print(selected[i]+" "); // } // w.println(); w.println((long)Math.max(left[1],right[1])); } w.close(); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.InputMismatchException; import java.util.*; import java.io.*; import java.lang.*; public class Main{ public static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static long gcd(long a, long b){ if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a*b)/gcd(a, b); } public static void sortbyColumn(int arr[][], int col) { Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(final int[] entry1, final int[] entry2) { if (entry1[col] > entry2[col]) return 1; else return -1; } }); } static long func(long a[],int size,int s){ long max1=a[s]; long maxc=a[s]; for(int i=s+1;i<size;i++){ maxc=Math.max(a[i],maxc+a[i]); max1=Math.max(maxc,max1); } return max1; } public static class Pair<U extends Comparable<U>, V extends Comparable<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 hashCode() { return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode()); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<U, V> p = (Pair<U, V>) o; return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y)); } public int compareTo(Pair<U, V> b) { int cmpU = x.compareTo(b.x); return cmpU != 0 ? cmpU : y.compareTo(b.y); } public String toString() { return String.format("(%s, %s)", x.toString(), y.toString()); } } static class MultiSet<U extends Comparable<U>> { public int sz = 0; public TreeMap<U, Integer> t; public MultiSet() { t = new TreeMap<>(); } public void add(U x) { t.put(x, t.getOrDefault(x, 0) + 1); sz++; } public void remove(U x) { if (t.get(x) == 1) t.remove(x); else t.put(x, t.get(x) - 1); sz--; } } static void shuffle(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static long myceil(long a, long b){ return (a+b-1)/b; } static long C(long n,long r){ long count=0,temp=n; long ans=1; long num=n-r+1,div=1; while(num<=n){ ans*=num; //ans+=MOD; ans%=MOD; ans*=mypow(div,MOD-2); //ans+=MOD; ans%=MOD; num++; div++; } ans+=MOD; return ans%MOD; } static long fact(long a){ long i,ans=1; for(i=1;i<=a;i++){ ans*=i; ans%=MOD; } return ans%=MOD; } static void sieve(int n){ is_sieve[0]=1; is_sieve[1]=1; int i,j,k; for(i=2;i<n;i++){ if(is_sieve[i]==0){ sieve[i]=i; tr.add(i); for(j=i*i;j<n;j+=i){ if(j>n||j<0){ break; } is_sieve[j]=1; sieve[j]=i; } } } } // static void calc(int n){ // int i,j; // dp[n-1]=0; // if(n>1) // dp[n-2]=1; // for(i=n-3;i>=0;i--){ // long ind=n-i-1; // dp[i]=((ind*(long)mypow(10,ind-1))%MOD+dp[i+1])%MOD; // } // } static long mypow(long x,long y){ long temp; if( y == 0) return 1; temp = mypow(x, y/2); if (y%2 == 0) return (temp*temp)%MOD; else return ((x*temp)%MOD*(temp)%MOD)%MOD; } static long dist[],dp[][],left[],right[]; static int visited[],isit[]; static ArrayList<Pair<Integer,Pair<Long,Long>>> adj[],li; //static int dp[][][]; static int MOD=1000000007; static char ch[]; static int[] sieve,is_sieve; static TreeSet<Integer> tr; static long mat[][]; // static void bfs(int node,int par,Pair<Long,Long> p[],long taken){ // LinkedList<Integer> li=new LinkedList<>(); // li.add(node); // while(!li.isEmpty()){ // int x=li.pollFirst(); // long lowNode=p[x-1].x; // long highNode=p[x-1].y; // int left=0,right=0; // visited[x]=1; // for(Pair<Integer,Pair<Long,Long>> pp:adj[x]){ // long max=0; // if(selected[pp.x]==0){ // max=Math.max(Math.abs(lowNode-pp.y.y),Math.abs(highNode-pp.y.x)); // if(max==Math.abs(lowNode-pp.y.y)){ // left++; // }else{ // right++; // } // }else{ // max=Math.max(Math.abs(lowNode-selected[pp.x]),Math.abs(highNode-selected[pp.x])); // if(max==Math.abs(lowNode-selected[pp.x])){ // left++; // }else{ // right++; // } // } // if(visited[pp.x]==0) // li.add(pp.x); // } // if(left>=right){ // selected[x]=lowNode; // }else{ // selected[x]=highNode; // } // } // } static void dfs(int node,int par, Pair<Long,Long> p[]){ for(Pair<Integer,Pair<Long,Long>> pp:adj[node]){ if(pp.x!=par){ //sum+=Math.abs(selected[node]-selected[pp.x]); dfs(pp.x,node,p); //System.out.println(node+" "+pp.x); long x=Math.abs(p[node].x-p[pp.x].x); long y=Math.abs(p[node].x-p[pp.x].y); long z=Math.abs(p[node].y-p[pp.x].x); long w=Math.abs(p[node].y-p[pp.x].y); left[node]+=Math.max(x+left[pp.x],y+right[pp.x]); right[node]+=Math.max(z+left[pp.x],w+right[pp.x]); } } } public static void main(String args[]){ InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter w = new PrintWriter(outputStream); int t,i,j,tno=0,tte; t=in.nextInt(); //t=1; //tte=t; while(t-->0){ //sum=0; int n=in.nextInt(); adj=new ArrayList[n+1]; left=new long[n+1]; right=new long[n+1]; visited=new int[n+1]; for(i=0;i<n+1;i++){ adj[i]=new ArrayList<>(); } Pair<Long,Long> p[]=new Pair[n+1]; for(i=1;i<=n;i++){ p[i]=new Pair<>(in.nextLong(),in.nextLong()); } for(i=0;i<n-1;i++){ int u,v; u=in.nextInt(); v=in.nextInt(); adj[u].add(new Pair<>(v,p[v])); adj[v].add(new Pair<>(u,p[u])); } //bfs(1,-1,p,Long.MAX_VALUE); dfs(1,-1,p); // for(i=0;i<n+1;i++){ // w.print(selected[i]+" "); // } // w.println(); w.println((long)Math.max(left[1],right[1])); } w.close(); } }
1
Plagiarised
60c85a4f
8926151a
import java.util.*; import java.io.*; import java.lang.*; public class Solution { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(""); String next() { if (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } public static void main(String[] args) { new Solution().solve(); } long res = Long.MAX_VALUE; List<Integer> l = new ArrayList<>(), r = new ArrayList<>(); long dp[][]; long ok(int i, int j) { if (dp[i][j] != -1) return dp[i][j]; if (i >= l.size()) { return 0; } if (j >= r.size()) return Integer.MAX_VALUE; long op1 = ok(i + 1, j + 1) + Math.abs(l.get(i) - r.get(j)); long op2 = ok(i, j + 1); return dp[i][j] = Math.min(op1, op2); } void solve() { int t = 1; // t = nextInt(); for (int tt = 0; tt < t; tt++) { int n = nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); if (arr[i] == 0) r.add(i + 1); else l.add(i + 1); } dp = new long[n][n]; for (int i = 0; i < n; i++) Arrays.fill(dp[i], -1); out.println(ok(0, 0)); } out.close(); } }
import java.util.*; import java.io.*; public class _1525_D { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int n = Integer.parseInt(in.readLine()); int[] a = new int[n]; StringTokenizer line = new StringTokenizer(in.readLine()); int c = 0; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(line.nextToken()); if (a[i] == 1) c++; } int[] pos = new int[c]; int count = 0; for (int i = 0; i < n; i++) { if (a[i] == 1) { pos[count] = i; count++; } } int[][] dp = new int[c + 1][n]; for (int i = 1; i <= c; i++) { Arrays.fill(dp[i], Integer.MAX_VALUE); } for (int i = 1; i <= c; i++) { for (int j = 0; j < n; j++) { if (j > 0) { if (a[j] == 0) { if (dp[i - 1][j - 1] != Integer.MAX_VALUE) { dp[i][j] = dp[i - 1][j - 1] + Math.abs(j - pos[i - 1]); } } dp[i][j] = Math.min(dp[i][j], dp[i][j - 1]); } else { if (a[j] == 0 && i == 1) { dp[i][j] = Math.abs(j - pos[i - 1]); } } } } out.println(dp[c][n - 1]); in.close(); out.close(); } }
0
Non-plagiarised
a4d6775d
cb87df79
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 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
7d9b95c0
9d33ca79
import java.util.*; import java.math.*; public class Main { public static class Edge{ int u; int v; // int wt; Edge(int u, int v){ this.u = u; this.v = v; // this.wt = wt; } } public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); StringBuilder sb = new StringBuilder(""); for(int A=0 ; A<t ; A++) { int n = scn.nextInt(); List<Edge>[] graph = new ArrayList[n]; for(int i=0 ; i<n ; i++) { graph[i] = new ArrayList<>(); } String[] arr = new String[n-1]; for(int i=0 ; i<n-1 ; i++) { int u = scn.nextInt(); int v = scn.nextInt(); arr[i] = (u-1) + " " + (v-1); graph[u-1].add(new Edge(u-1, v-1)); graph[v-1].add(new Edge(v-1, u-1)); } boolean flag = false; int src = 0; for(int i=0 ; i<n ; i++) { if(graph[i].size() > 2) { flag = true; }else if(graph[i].size() == 1) src = i; } if(flag) { sb.append(-1 + "\n"); }else { Map<String, Integer> hm = new HashMap<>(); int count = 0; int val = 2; // System.out.println(src); while(count < n) { List<Edge> e = graph[src]; int i=0; while(i < e.size() && hm.containsKey(src + " " + e.get(i).v)) i++; if(i < e.size()) { int nbr = e.get(i).v; // System.out.println(src + " " + nbr); hm.put(src + " " + nbr, val); hm.put(nbr + " " + src, val); val = 5 - val; src = nbr; } count++; } for(int i=0 ; i<arr.length ; i++) { int wt = hm.get(arr[i]); sb.append(wt + " "); } sb.append("\n"); } } System.out.println(sb); } }
import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.StringTokenizer; import java.util.TreeMap; public class NotAssigning { static ArrayList<Integer>[]adj; static boolean vis []; static int edges[]; // we need to check that every path of length 1 or 2 must be a prime number // Idea--> we will only use 2 , 3 for the weight assignment // no assignment will be valid if there exists a node connected to 3 others 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(); vis= new boolean [n]; adj= new ArrayList[n]; TreeMap<Integer,Pair> idx = new TreeMap<>(); TreeMap<Pair,Integer> w= new TreeMap<>(); boolean notValid = false; for(int i =0;i<n;i++){ adj[i]= new ArrayList<>(); } for(int i =1;i<n;i++){ int u = sc.nextInt()-1; int v = sc.nextInt()-1; int max = Math.max(u,v); int min = Math.min(u,v); idx.put(i,new Pair(min , max)); adj[u].add(v); adj[v].add(u); if(adj[u].size()>2||adj[v].size()>2)notValid=true; } if(notValid){ pw.println(-1); continue; } dfs(0,2,w); // vis[0]=true; // dfs(adj[0].get(0),2,w); // if(adj[0].size()==2)dfs(adj[0].get(1),3,w); for(int i =1;i<n;i++){ pw.print(w.get(idx.get(i))+" "); } pw.println(); } pw.close(); } static void dfs(int node , int w , TreeMap<Pair , Integer>weight){ vis[node]=true; int i =0; for(int x : adj[node]){ if(!vis[x]) { int min = Math.min(x , node); int max = Math.max(x , node); if(i%2==0){ weight.put(new Pair(min , max),w); dfs(x, 5-w,weight); } else{ weight.put(new Pair(min , max),5-w); dfs(x, w, weight); } i++; } } } 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 this.y-o.y; return this.x-o.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 { 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(); } } }
0
Non-plagiarised
354d060f
e1c4f3db
import java.util.*; import java.io.*; public class CodeForces { // For fast input output static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader( new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { 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; } } // end of fast i/o code public static void main(String[] args) { FastReader in = new FastReader(); OutputStream op = System.out; PrintWriter out = new PrintWriter(op); int t = in.nextInt(); for (int i = 1; i <= t; i++) { int n=in.nextInt(); int arr[]=new int[n]; for(int j = 0; j < n; j++) arr[j]=in.nextInt(); helper(n,arr,out); out.println(); } out.close(); } public static void helper(int n,int arr[],PrintWriter o) { int max=Integer.MIN_VALUE; for(int i=2;i<n;i++) max=Math.max(max,arr[i]); int ans=1,low=1,high=max; while(low<=high) { int mid=low+(high-low)/2; if(canFit(mid,arr)) { ans=mid; low=mid+1; } else high=mid-1; } o.print(ans); } static boolean canFit(int mid,int arr[]) { int copy[]=Arrays.copyOf(arr, arr.length); for(int i=arr.length-1;i>=0;i--) { if(copy[i]<mid) return false; int min=Math.min(copy[i]-mid,arr[i])/3; if(i>=2){ copy[i-1]+=min; copy[i-2]+=2*min; }} return true; } }
import java.util.*; public class BalancedStoneHeaps { public static boolean check(int n, int x, int[] h) { int[] c_h = new int[n]; for (int i = 0; i < n; i++) c_h[i] = h[i]; for (int i = n - 1; i >= 2; i--) { if (c_h[i] < x) return false; int d = Math.min(h[i], c_h[i] - x) / 3; c_h[i - 1] += d; c_h[i - 2] += 2 * d; } return c_h[0] >= x && c_h[1] >= x; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] h = new int[n]; int max = Integer.MIN_VALUE; for (int i = 0; i < n; i++) { h[i] = sc.nextInt(); if (h[i] > max) { max = h[i]; } } int l = 0; int r = max; while (l < r) { int mid = l + (r - l + 1) / 2; if (check(n, mid, h)) { l = mid; } else { r = mid - 1; } } System.out.println(l); } } }
0
Non-plagiarised
0f3a2acf
16e6d8bb
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Practice { static int[][] vals; static ArrayList<ArrayList<Integer>> adjList; static long[][] ans; public static void main(String[] args) throws IOException { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(bu.readLine()); while (t-- > 0) { Integer n = Integer.parseInt(bu.readLine()); vals = new int[2][n]; adjList = new ArrayList<>(); for(int i=0;i<n;i++){ String st[]=bu.readLine().split(" "); vals[0][i] = Integer.parseInt(st[0]); vals[1][i] = Integer.parseInt(st[1]); adjList.add(new ArrayList<>()); } for(int i=0;i<n-1;i++){ String st[]=bu.readLine().split(" "); int source = Integer.parseInt(st[0]);; int dest = Integer.parseInt(st[1]);; adjList.get(source-1).add(dest-1); adjList.get(dest-1).add(source-1); } ans = new long[2][n]; DFS(0, adjList, -1, ans, vals); System.out.println(Math.max(ans[0][0], ans[1][0])); } } private static void DFS(Integer current, ArrayList<ArrayList<Integer>> adjList, Integer prev, long[][] ans, int[][] vals) { ans[0][current] = 0L; ans[1][current] = 0L; for(Integer node: adjList.get(current)){ if(node.equals(prev)) continue; DFS(node, adjList, current, ans, vals); ans[0][current] += Math.max(ans[0][node] + Math.abs(vals[0][current] - vals[0][node]), ans[1][node] + Math.abs(vals[0][current] - vals[1][node])); ans[1][current] += Math.max(ans[0][node] + Math.abs(vals[1][current] - vals[0][node]) , ans[1][node] + Math.abs(vals[1][current] - vals[1][node])); } } }
import java.util.*; import java.io.*; import java.lang.*; public class CodeChef { static long dp[][]; public static void main (String[] args) throws java.lang.Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t-->0) { int n=Integer.parseInt(br.readLine()); int arr[][]=new int[n+1][2]; for(int i=1;i<=n;i++) { String str[]=br.readLine().split(" "); int l=Integer.parseInt(str[0]);int r=Integer.parseInt(str[1]); arr[i][0]=l;arr[i][1]=r; } dp=new long[n+1][2]; Set<Integer>adj[]=new HashSet[n+1]; for(int i=0;i<=n;i++)adj[i]=new HashSet(); for(int i=0;i<n-1;i++) { String str[]=br.readLine().split(" "); int src=Integer.parseInt(str[0]); int dest=Integer.parseInt(str[1]); adj[src].add(dest); adj[dest].add(src); } fun(1,adj,arr);System.out.println(Math.max(dp[1][0], dp[1][1])); } } static void fun(int root,Set<Integer>[] adj,int arr[][]) { dp[root][0]=dp[root][1]=0; Iterator<Integer>iter=adj[root].iterator(); while(iter.hasNext()) { int i=iter.next(); adj[i].remove(root); fun(i,adj,arr); dp[root][0]+=Math.max(Math.abs(arr[root][0]-arr[i][0])+dp[i][0], Math.abs(arr[root][0]-arr[i][1])+dp[i][1]); dp[root][1]+=Math.max(Math.abs(arr[root][1]-arr[i][0])+dp[i][0], Math.abs(arr[root][1]-arr[i][1])+dp[i][1]); } } }
1
Plagiarised
1097b326
f1f600d9
// 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(); } }
// import java.util.Vector; import java.util.*; import java.lang.Math; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import javax.management.Query; import java.io.*; import java.math.BigInteger; public class Main { static int mod = 1000000007; /* ======================DSU===================== */ static class dsu { static int parent[], n;// min[],value[]; static long size[]; dsu(int n) { parent = new int[n + 1]; size = new long[n + 1]; // min=new int[n+1]; // value=new int[n+1]; this.n = n; makeSet(); } static void makeSet() { for (int i = 1; i <= n; i++) { parent[i] = i; size[i] = 1; // min[i]=i; } } static int find(int a) { if (parent[a] == a) return a; else { return parent[a] = find(parent[a]);// Path Compression } } static void union(int a, int b) { int setA = find(a); int setB = find(b); if (setA == setB) return; if (size[setA] >= size[setB]) { parent[setB] = setA; size[setA] += size[setB]; } else { parent[setA] = setB; size[setB] += size[setA]; } } } /* ======================================================== */ static class Pair implements Comparator<Pair> { long x; long y; // Constructor public Pair(long x, long y) { this.x = x; this.y = y; } public Pair() { } @Override public int compare(Main.Pair o1, Main.Pair o2) { return ((int) (o1.x - o2.x)); } } 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[] intArr(int n) { int res[] = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] longArr(int n) { long res[] = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } } static FastReader f = new FastReader(); static BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out)); 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 (long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int LowerBound(int a[], int 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 int UpperBound(int a[], int 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 long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } static long power(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; // y = y/2 x = (x * x) % p; } return res; } static long power(long x, long y) { long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static int power(int x, int y) { int res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } 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)); } /* * ===========Modular Operations================== */ static long modInverse(long n, long p) { return power(n, p - 2, p); } static long modAdd(long a, long b) { return (a % mod + b % mod) % mod; } static long modMul(long a, long b) { return ((a % mod) * (b % mod)) % mod; } static long nCrModPFermat(int n, int r) { long p = 1000000007; if (r == 0) return 1; long[] fac = new long[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } /* * =============================================== */ static List<Character> removeDup(ArrayList<Character> list) { List<Character> newList = list.stream().distinct().collect(Collectors.toList()); return newList; } static void ruffleSort(long[] a) { int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n); long temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } static void ruffleSort(int[] a) { int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n); int temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } /* * ===========Dynamic prog Recur Section=========== */ static int DP[][]; static ArrayList<ArrayList<Integer>> g; static int count = 0; static ArrayList<Long> bitMask(ArrayList<Long> ar, int n) { ArrayList<Long> ans = new ArrayList<>(); for (int mask = 0; mask <= Math.pow(2, n) - 1; mask++) { long sum = 0; for (int i = 0; i < n; i++) { if (((1 << i) & mask) > 0) { sum += ar.get(i); } } ans.add(sum); } return ans; } /* * ====================================Main================================= */ public static void main(String args[]) throws Exception { // File file = new File("D:\\VS Code\\Java\\Output.txt"); // FileWriter fw = new FileWriter("D:\\VS Code\\Java\\Output.txt"); Random rand = new Random(); int t = 1; t = f.nextInt(); int tc = 1; while (t-- != 0) { int n = f.nextInt(); int c[] = new int[n]; long minOdd = 0, minEven = 0; long sumEven = 0, sumOdd = 0; for (int i = 0; i < n ; i++) { c[i] = f.nextInt(); // if (i % 2 == 0) { // minEven = (c[minEven] > c[i]) ? i : minEven; // sumEven += c[i]; // } else { // minOdd = (minOdd > c[i]) ? i : minOdd; // sumOdd += c[i]; // } } minEven = c[0]; minOdd = c[1]; sumEven=c[0]; sumOdd=c[1]; long min=minEven*n + minOdd*n;//for k=2 int even=1,odd=1; for (int k = 3; k <= n; k++) { if(k%2==1){ sumEven+=c[k-1]; minEven=Math.min(minEven, c[k-1]); even++; }else{ sumOdd+=c[k-1]; minOdd=Math.min(minOdd, c[k-1]); odd++; } min=Math.min(min, sumEven-minEven+minEven*(n-even+1) + sumOdd-minOdd+minOdd*(n-odd+1)); } w.write(min+"\n"); } w.flush(); } }
0
Non-plagiarised
085ddefc
1500a4fa
import java.io.*; import java.util.*; public class a { public static void main(String[] args){ FastScanner sc = new FastScanner(); int t = sc.nextInt(); while(t-- > 0){ int n = sc.nextInt(); String a = sc.next(); String b = sc.next(); int change = 0; int nochange = 0; for(int i=0; i<n; i++){ if(a.charAt(i) == b.charAt(i)){ nochange++; } else{ change++; } } if(change%2 == 0 && nochange%2 == 0){ int count1 = 0; for(int i=0; i<n; i++){ if(a.charAt(i) != b.charAt(i) && a.charAt(i) == '1'){ count1++; } } if(count1 == change/2){ System.out.println(change); } else{ System.out.println(-1); } } else if(change%2 == 0 && nochange%2 == 1){ // both ways possible int count1 = 0; int ans = Integer.MAX_VALUE; for(int i=0; i<n; i++){ if(a.charAt(i) == b.charAt(i) && a.charAt(i) == '1'){ count1++; } } if(count1 == nochange/2 + 1){ ans = Math.min(ans, nochange); } count1 = 0; for(int i=0; i<n; i++){ if(a.charAt(i) != b.charAt(i) && a.charAt(i) == '1'){ count1++; } } if(count1 == change/2){ ans = Math.min(ans, change); } if(ans == Integer.MAX_VALUE){ System.out.println(-1); } else{ System.out.println(ans); } } else if(change%2 == 1 && nochange%2 == 0){ System.out.println(-1); } else{ int count1 = 0; for(int i=0; i<n; i++){ if(a.charAt(i) == b.charAt(i) && a.charAt(i) == '1'){ count1++; } } if(count1 == nochange/2 + 1){ System.out.println(nochange); } else{ System.out.println(-1); } } } } } class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
import java.io.*; import java.util.*; public class GFG { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); String a=sc.next(); String b=sc.next(); int i; int zero=0,one=0; int x=0,y=0,x1=0,y1=0; for(i=0;i<n;i++){ if(a.charAt(i)=='0') zero++; else one++; } if(one==0){ if(a.equals(b)) System.out.println("0"); else System.out.println("-1"); } else{ int same=0,diff=0; for(i=0;i<n;i++){ if(a.charAt(i)==b.charAt(i)){ same++; x++; if(a.charAt(i)=='1'){ x1++; } } else{ diff++; y++; if(a.charAt(i)=='1'){ y1++; } } } int ans=Integer.MAX_VALUE; if(x%2!=0&&(x+1)/2==x1){ ans=x; } if(y%2==0&&(y/2)==y1){ ans=Math.min(ans,y); } if(ans==Integer.MAX_VALUE){ System.out.println("-1"); }else{ System.out.println(ans); } } } } }
0
Non-plagiarised
11373c16
7011024d
/* 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 {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 { FastReader scan = new FastReader(); PrintWriter pw = new PrintWriter(System.out); int n = scan.nextInt(); ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); for(int i=0;i<n;i++){ int x = scan.nextInt(); if(x==1) a.add(i); else b.add(i); } int x = a.size(); if(x==0){ pw.println(0); pw.flush(); return; } int y = b.size(); int dp[][] = new int[x][y]; int min = Integer.MAX_VALUE; for(int i=0;i<y;i++){ min = Math.min(Math.abs(a.get(0) - b.get(i)),min); dp[0][i] = min; } for(int i=1;i<x;i++){ min = Integer.MAX_VALUE; for(int j=i;j<y;j++){ min = Math.min(Math.abs(a.get(i)-b.get(j))+dp[i-1][j-1],min); dp[i][j] = min; } } pw.println(dp[x-1][y-1]); pw.flush(); } }
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
41e69083
54eb1a6b
import java.util.*; public class Solution { public static void main(String[] args) { // TODO Auto-generated method stub Scanner io =new Scanner (System.in); int t = io.nextInt(); for (int iii=0; iii<t; iii++) { int n = io.nextInt(); long k = io.nextLong(); Long[] arr = new Long[n]; for (int i=0; i<n; i++) { arr[i] = io.nextLong(); } Arrays.sort(arr); long[] psum = new long[n]; psum[0] = arr[0]; for (int i=1; i<n; i++) { psum[i] = psum[i-1] + arr[i]; } if (psum[n-1] <= k) { System.out.println(0); continue; } //System.out.println(psum[n-1]); long ans = Long.MAX_VALUE; for (int i=0; i<n; i++) { long sum = psum[i] + (n-i-1)*arr[0]; if (sum <= k) { ans = Math.min(ans, n-1-i); continue; } long ops = (long) Math.ceil((double) (sum - k)/(double) (n-i)); ans = Math.min(ans, n-1-i + ops); //System.out.println(sum); } System.out.println(ans); } } }
import java.util.*; public class Solution { public static void main(String[] args) { // TODO Auto-generated method stub Scanner io =new Scanner (System.in); int t = io.nextInt(); for (int iii=0; iii<t; iii++) { int n = io.nextInt(); long k = io.nextLong(); Long[] arr = new Long[n]; for (int i=0; i<n; i++) { arr[i] = io.nextLong(); } Arrays.sort(arr); long[] psum = new long[n]; psum[0] = arr[0]; for (int i=1; i<n; i++) { psum[i] = psum[i-1] + arr[i]; } if (psum[n-1] <= k) { System.out.println(0); continue; } //System.out.println(psum[n-1]); long ans = Long.MAX_VALUE; for (int i=0; i<n; i++) { long sum = psum[i] + (n-i-1)*arr[0]; if (sum <= k) { ans = Math.min(ans, n-1-i); continue; } long ops = (long) Math.ceil((double) (sum - k)/(double) (n-i)); ans = Math.min(ans, n-1-i + ops); //System.out.println(sum); } System.out.println(ans); } } }
1
Plagiarised
00af3420
f4d6d28d
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.util.*; import java.io.*; import java.lang.*; import static java.lang.Math.*; public class cp{ static BufferedReader br; static StringTokenizer st; public static void main(String[] args)throws IOException{ br = new BufferedReader(new InputStreamReader(System.in)); List<Integer> answer = new ArrayList<Integer>(); for(int t=Integer.parseInt(br.readLine()); t>0; t--){ st = readLine(); int n = tokenInt(); String[] words = new String[n]; int i=0; while(n-- >0){ words[i] = br.readLine(); i++; } answer.add(new Solver().solve(words)); } for (int ans : answer ) System.out.println(ans); } static StringTokenizer readLine() throws IOException { return new StringTokenizer(br.readLine()); } static int tokenInt() { return Integer.parseInt(st.nextToken()); } static long tokenLong(){ return Long.parseLong(st.nextToken()); } static double tokenDouble(){ return Double.parseDouble(st.nextToken()); } static char[] tokenChar(){ return st.nextToken().toCharArray(); } static int[] readIntArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=tokenInt(); return a; } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b);} static int lcm(int a, int b) { return (a * b) / gcd(a,b);} static void printArr(int[] arr){ for (int i=0; i<arr.length; i++) System.out.print(arr[i] + " "); System.out.println(); return; } static void printArr(char[] arr){ for (int i=0; i<arr.length; i++) System.out.print(arr[i] + " "); System.out.println(); return; } } class Solver{ public int solve(String[] words) throws IOException{ char[] alphabets = {'a', 'b', 'c', 'd', 'e'}; int[][] arr = new int[5][words.length]; int i=0; for(String s : words) { char[] wordArr = s.toCharArray(); int j=0; for (char alpha : alphabets) { int counter = 0; for(char letter : wordArr){ if(alpha == letter) counter++; } arr[j][i] = 2*counter - s.length(); j++; } i++; } int[] ans = new int[5]; int m=0; for (int[] ar : arr) { Arrays.sort(ar); int val=0; int counter=0; for(i=ar.length-1; i>=0; i--){ val += ar[i]; if(val>0) counter++; else break; } ans[m] = counter; m++; } return Arrays.stream(ans).max().getAsInt(); } } /* bac aaada e */
0
Non-plagiarised
0951d079
0ecf356d
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; import java.util.StringTokenizer; public class D { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader file = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(file.readLine()); int[] heights = new int[n]; StringTokenizer st = new StringTokenizer(file.readLine()); for(int i = 0; i < n; i++) { heights[i] = Integer.parseInt(st.nextToken()); } int[] dp = new int[n]; dp[0] = 0; Stack<Integer> high = new Stack<>(); Stack<Integer> low = new Stack<>(); high.push(0); low.push(0); for(int i = 1; i < n; i++) { dp[i] = dp[i-1]+1; while(!high.isEmpty() && heights[high.peek()] < heights[i]) { dp[i] = Math.min(dp[i], dp[high.peek()]+1); high.pop(); } if(!high.isEmpty()) { dp[i] = Math.min(dp[i], dp[high.peek()]+1); if(heights[high.peek()] == heights[i]) high.pop(); } while(!low.isEmpty() && heights[low.peek()] > heights[i]) { dp[i] = Math.min(dp[i], dp[low.peek()]+1); low.pop(); } if(!low.isEmpty()) { dp[i] = Math.min(dp[i], dp[low.peek()]+1); if(heights[low.peek()] == heights[i]) low.pop(); } high.push(i); low.push(i); } System.out.println(dp[n-1]); } }
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); int[] h = new int[n]; int[] dp = new int[n]; for(int i = 0; i<n; i++){ h[i] = Integer.parseInt(st.nextToken()); } Stack<Integer> hi = new Stack<>(); Stack<Integer> lo = new Stack<>(); hi.push(0); lo.push(0); for(int i = 1; i<n; i++){ dp[i] = dp[i-1]+1; while(!hi.isEmpty() && h[hi.peek()]<h[i]){ dp[i] = Math.min(dp[i], dp[hi.peek()]+1); hi.pop(); } if(!hi.isEmpty()){ dp[i] = Math.min(dp[i], dp[hi.peek()]+1); if(h[i] == h[hi.peek()]) hi.pop(); } while(!lo.isEmpty() && h[lo.peek()]>h[i]){ dp[i] = Math.min(dp[i], dp[lo.peek()]+1); lo.pop(); } if(!lo.isEmpty()){ dp[i] = Math.min(dp[i], dp[lo.peek()]+1); if(h[i] == h[lo.peek()]) lo.pop(); } hi.push(i); lo.push(i); } System.out.println(dp[n-1]); } }
1
Plagiarised
1be078c4
f339ef6f
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.Comparator; import java.util.List; import java.util.StringTokenizer; public class Main { static FastReader fr; static int arrForIndexSort[]; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Pair{ int first; int second; Pair(int first, int second){ this.first = first; this.second = second; } @Override public boolean equals(Object b) { Pair a = (Pair)b; if(this.first == a.first && this.second==a.second) { return true; } return false; } } class PairSorter implements Comparator<Main.Pair>{ public int compare(Pair a, Pair b) { if(a.first!=b.first) { return a.first-b.first; } return a.second-b.second; } } class IndexSorter implements Comparator<Integer>{ public int compare(Integer a, Integer b) { //desc if(arrForIndexSort[b]==arrForIndexSort[a]) { return b-a; } return arrForIndexSort[b]-arrForIndexSort[a]; } } class ListSorter implements Comparator<List>{ public int compare(List a, List b) { return b.size()-a.size(); } } static class DisjointSet{ int[] dsu; public DisjointSet(int n) { makeSet(n); } public void makeSet(int n) { dsu = new int[n+1]; //*** 1 Based indexing *** for(int i=1;i<=n;i++) { dsu[i] = -1; } } public int find(int i) { while(dsu[i] > 0) { i = dsu[i]; } return i; } public void union(int i, int j) { int iRep = find(i); int jRep = find(j); if(iRep == jRep) { return; } if(dsu[iRep]>dsu[jRep]) { dsu[jRep] += dsu[iRep]; dsu[iRep] = jRep; } else { dsu[iRep] += dsu[jRep]; dsu[jRep] = iRep; } } } public static void main(String[] args) { fr = new FastReader(); int T = 1; T = fr.nextInt(); int t1 = T; while (T-- > 0) { solve(); } } public static void solve() { int n=fr.nextInt(); int k=fr.nextInt(); int[] a = new int[k]; int[] map = new int[n]; int[] temp = new int[n]; Arrays.fill(temp, Integer.MAX_VALUE); Arrays.fill(map, Integer.MAX_VALUE); int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for(int i=0;i<k;i++) { a[i] = fr.nextInt()-1; min = Math.min(a[i], min); max = Math.max(a[i], max); } for(int i=0;i<k;i++) { map[a[i]] = fr.nextInt(); } int diff = 0; int curr = map[min]; for(int i=min;i<n;i++) { int airConditioner = map[i]; if(airConditioner<curr+diff) { temp[i] = airConditioner; diff = 1; curr = airConditioner; } else { temp[i] = curr+diff; diff++; } } diff = 0; curr = map[max]; for(int i=max;i>=0;i--) { int airConditioner = map[i]; if(airConditioner<curr+diff) { temp[i] = Math.min(temp[i], airConditioner); diff = 1; curr = airConditioner; } else { temp[i] = Math.min(temp[i], curr+diff); diff++; } } System.out.println(Arrays.toString(temp).replaceAll(",|\\[|\\]","")); } }
0
Non-plagiarised
1386da0e
f87eb1b3
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.function.Function; import java.util.stream.Collectors; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; public class Solution { public static int INF= Integer.MAX_VALUE; public static long MOD= 1000000000+7L; public static int WHITE= 0; public static int GRAY= 1; public static int BLACK= 2; class Pair{ public int day; int count; Pair(int d, int c){ day=d; count=c; } } static class Solver { public void solve(InputReader in, PrintWriter out) { int tt=in.nextInt(); //int tt=1; for(int cases=1;cases<=tt;cases++){ int n= in.nextInt(); long odd_min= Long.MAX_VALUE; long even_min= Long.MAX_VALUE; long ans= Long.MAX_VALUE; int odd_length=0; int even_length=0; long total=0; for(int segment=1;segment<=n;segment++){ int cost= in.nextInt(); total+=cost; if(segment%2==1){ odd_min = Math.min(odd_min, cost); odd_length++; } else{ even_min = Math.min(even_min, cost); even_length++; } if(segment>1){ { long value=total-(odd_min+even_min); value+= odd_min * (n-odd_length+1); value+= even_min * (n-even_length+1); ans= Math.min(value, ans); } } } System.out.println(ans); } } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); solver.solve(in, out); out.close(); } 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()); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.PrintStream; import java.io.PrintWriter; public class A { /* 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 aa, int bb) { first = aa; second = bb; } public int compareTo(Pair o) { if(this.second < o.second) return -1; if(this.second > o.second) return +1; return this.first - o.first; } } static class Tuple implements Comparable<Tuple>{ long first, second,third; public Tuple(long first, long second, long 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 boolean isPrime(long n) { if(n == 1) { return false; } 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[n+1]; Arrays.fill(prime, true); ArrayList<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 gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static int lcm(int a, int b) { return (a / gcd(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(int n, int k) { long ans = 1; for(int i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } public static void Sort(int[] a) { List<Integer> l = new ArrayList<>(); for (int 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); } //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)); } /* * * >= <= 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(int a[], int x) { int l=-1, r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { long INF = 1000000000000000007L; int n = sc.nextInt(); long[] c = new long[n+1]; for(int i = 1;i<=n;i++) c[i] = sc.nextLong(); long ans = INF; long mo = INF, so = 0, co = 0; long me = INF, se = 0, ce = 0; for(int i=1;i<=n;i++) { if(i%2 == 1) { mo = min(mo,c[i]); so += c[i]; co++; } else { me = min(me,c[i]); se += c[i]; ce++; } if(i>=2) { long x = so + (n - co) * mo + se + (n - ce) * me; ans=min(ans,x); } } fout.println(ans); } fout.close(); } }
0
Non-plagiarised
3b498a39
e6b7a899
/* * HI THERE! (: * * CREATED BY : NAITIK V * * JAI HIND * */ 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 A[]=input(n); dp=new long[n+1][n+1]; for(int i=0;i<=n;i++) { Arrays.fill(dp[i],-1); } ArrayList<Integer> l=new ArrayList<Integer>(); ArrayList<Integer> m=new ArrayList<Integer>(); for(int i=0;i<n;i++) { if(A[i]==0) { l.add(i+1); } else { m.add(i+1); } } A=new int[m.size()]; int B[]=new int[l.size()]; for(int i=0;i<l.size();i++) { B[i]=l.get(i); } for(int i=0;i<m.size();i++) { A[i]=m.get(i); } n=m.size(); int o=l.size(); System.out.println(go(A,B,0,0,n,o)); } //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 go(int[] A, int[] B, int i, int j, int n, int m) { if(i==n) return 0; if(j==m) return Integer.MAX_VALUE; if(dp[i][j]!=-1) return dp[i][j]; long op1=go(A, B, i+1, j+1, n, m)+Math.abs(A[i]-B[j]); long op2=go(A, B, i, j+1, n, m); return dp[i][j]=Math.min(op1, op2); } 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 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 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; } } }
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
1a6f8b20
f6ed6826
import java.io.*; import java.util.*; public class Main { public static void main(String[] args)throws Exception { Main ob=new Main(); ob.fun(); } public void fun()throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(System.out); int t=Integer.parseInt(br.readLine()); while(t-->0) { int n=Integer.parseInt(br.readLine()); int ar[][]=new int [n][5]; int len[]=new int[n]; for(int i=0;i<n;i++) { String s=(br.readLine()); for(int j=0;j<s.length();j++) { ar[i][s.charAt(j)-'a']++; len[i]=s.length(); } } int max=0; for(int i=0;i<5;i++) { int num=fun2(ar,len,i); max=Math.max(num,max); } pw.println(max); } pw.flush(); } public int fun2(int ar[][],int len[],int col) { int ct=0; int n=ar.length; PriorityQueue<Integer> pq=new PriorityQueue<Integer>(Collections.reverseOrder()); for(int i=0;i<n;i++) { int dif=2*ar[i][col]-len[i]; pq.add(dif); } int sum=0; while(pq.size()>0) { int num=(int)(pq.poll()); if((sum+num)>0) { ct++; sum+=num; } } return ct; } }
import java.util.*; import java.io.*; public class Solution { static FastScanner scr=new FastScanner(); // static Scanner scr=new Scanner(System.in); static PrintStream out=new PrintStream(System.out); static StringBuilder sb=new StringBuilder(); static class pair{ int x;int y; pair(int x,int y){ this.x=x;this.y=y; } } static class triplet{ int x; long y; int z; triplet(int x,long y,int z){ this.x=x; this.y=y; this.z=z; } } 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(); } long gcd(long a,long b){ if(b==0) { return a; } return gcd(b,a%b); } int[] sort(int a[]) { int multiplier = 1, len = a.length, max = Integer.MIN_VALUE; int b[] = new int[len]; int bucket[]; for (int i = 0; i < len; i++) if (max < a[i]) max = a[i]; while (max / multiplier > 0) { bucket = new int[10]; for (int i = 0; i < len; i++) bucket[(a[i] / multiplier) % 10]++; for (int i = 1; i < 10; i++) bucket[i] += (bucket[i - 1]); for (int i = len - 1; i >= 0; i--) b[--bucket[(a[i] / multiplier) % 10]] = a[i]; for (int i = 0; i < len; i++) a[i] = b[i]; multiplier *= 10; } return a; } long modPow(long base,long exp) { if(exp==0) { return 1; } if(exp%2==0) { long res=(modPow(base,exp/2)); return (res*res); } return (base*modPow(base,exp-1)); } int []reverse(int a[]){ int b[]=new int[a.length]; int index=0; for(int i=a.length-1;i>=0;i--) { b[index]=a[i]; } return b; } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readLongArray(int n) { long [] a=new long [n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } static HashMap<Character,ArrayList<Integer>>hm=new HashMap<>(); static void solve() { int n=scr.nextInt(); int count[]=new int[5]; int store[][]=new int[n][5]; int max=MIN; int length[]=new int[n]; for(int i=0;i<n;i++) { String s=scr.next(); length[i]=s.length(); for(int j=0;j<s.length();j++) { store[i][s.charAt(j)-'a']++; count[s.charAt(j)-'a']++; } } for(int j=0;j<5;j++) { ArrayList<Integer>a=new ArrayList<>(); for(int i=0;i<n;i++) { a.add((store[i][j]*2)-length[i]); } Collections.sort(a,Collections.reverseOrder()); long sum=0; int ans=0; for(int i=0;i<a.size();i++) { sum+=a.get(i); if(sum>0) { ans++; }else { break; } } max=Math.max(max, ans); } out.println(max); } static int MAX = Integer.MAX_VALUE; static int MIN = Integer.MIN_VALUE; public static void main(String []args) { int t=scr.nextInt(); while(t-->0) { solve(); } // solve(); // out.println(sb); } }
0
Non-plagiarised
55ab9b5d
ae4d5b49
import java.io.*; import java.util.*; public class Main { static int solve(char ch, char[][] str, int N) { int[] a = new int[N]; for (int i = 0; i < N; i++) { int c1 = 0, c2 = 0; for (char x : str[i]) { if (x == ch) c1++; else c2++; } a[i] = c1 - c2; } int sum = 0, count = N; for (int x : a) sum += x; Arrays.sort(a); for (int i = 0; i < N; i++) { if (sum > 0) break; count--; sum -= a[i]; } return count; } public static void main(String[] args) throws IOException { // System.out.println("===== input ====="); // BufferedReader br = new BufferedReader(new FileReader("input/input1.txt")); // BufferedReader br2 = new BufferedReader(new FileReader("input/input1.txt")); // String s; // while ((s = br2.readLine()) != null) { // System.out.println(s); // } // System.out.println("===== output ====="); 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) { int n = Integer.parseInt(br.readLine().trim()); char[][] str = new char[n][]; for (int i = 0; i < n; i++) { str[i] = br.readLine().trim().toCharArray(); } int ans = 0; for (char ch = 'a'; ch <= 'e'; ch++) { ans = Math.max(ans, solve(ch, str, n)); } out.append(ans).append("\n"); } System.out.println(out); } }
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException{ //BufferedReader f = new BufferedReader(new FileReader("uva.in")); BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int t = Integer.parseInt(f.readLine()); while(t-- > 0) { int n = Integer.parseInt(f.readLine()); int[][] occ = new int[n][6]; for(int i = 0; i < n; i++) { char[] temp = f.readLine().toCharArray(); for(char j: temp) { occ[i][j-'a']++; } occ[i][5] = occ[i][0]+occ[i][1]+occ[i][2]+occ[i][3]+occ[i][4]; } int max = 0; for(int i = 0; i < 5; i++) { int[] temp = new int[n]; for(int j = 0; j < n; j++) { temp[j] = occ[j][i]-(occ[j][5]-occ[j][i]); } Arrays.sort(temp); int j; int cur = 0; for(j = n-1; j >= 0; j--) { if(cur+temp[j] <= 0) { break; } cur += temp[j]; } max = Math.max(max, n-j-1); } out.println(max); } f.close(); out.close(); } }
0
Non-plagiarised
2.921E+060
46e9aed4
import java.util.*; import java.io.*; import java.math.*; /** * * @Har_Har_Mahadev */ /** * Main , Solution , Remove Public */ public class A { private static long[][] dp; private static ArrayList<Integer> lis1,lis0; public static void process() throws IOException { int n = sc.nextInt(); int arr[] = sc.readArray(n); lis1 = new ArrayList<Integer>(); lis0 = new ArrayList<Integer>(); for(int i = 0; i<n; i++) { if(arr[i] == 1)lis1.add(i+1); else lis0.add(i+1); } Collections.sort(lis0); Collections.sort(lis1); int n0 = lis0.size(); int n1 = lis1.size(); dp = new long[n0+1][n1+1]; for(int i = 0; i<=n0; i++) { Arrays.fill(dp[i], -1); } long ans = solve(0,0,n0,n1); println(ans); } private static long solve(int i, int j, int n0, int n1) { if(j == n1)return 0; if(i == n0)return INF; if(dp[i][j] != -1)return dp[i][j]; long ans = solve(i+1, j, n0, n1); ans = min(ans,abs(lis0.get(i) - lis1.get(j)) + solve(i+1, j+1, n0, n1)); return dp[i][j] = ans; } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353; private static int N = 0; private static void google(int tt) { System.out.print("Case #" + (tt) + ": "); } static FastScanner sc; static PrintWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new PrintWriter(System.out); } else { sc = new FastScanner(100); out = new PrintWriter("output.txt"); } int t = 1; // t = sc.nextInt(); int TTT = 1; while (t-- > 0) { // google(TTT++); process(); } out.flush(); out.close(); } static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Integer.compare(this.x, o.x); } // @Override // public boolean equals(Object o) { // if (this == o) return true; // if (!(o instanceof Pair)) return false; // Pair key = (Pair) o; // return x == key.x && y == key.y; // } // // @Override // public int hashCode() { // int result = x; // result = 31 * result + y; // return result; // } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static void println(Object o) { out.println(o); } static void println() { out.println(); } static void print(Object o) { out.print(o); } static void pflush(Object o) { out.println(o); out.flush(); } 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 int max(int x, int 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 long sqrt(long z) { long sqz = (long) Math.sqrt(z); while (sqz * 1L * sqz < z) { sqz++; } while (sqz * 1L * sqz > z) { sqz--; } return sqz; } 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 int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static int lower_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(System.in)); } FastScanner(int a) throws FileNotFoundException { br = new BufferedReader(new FileReader("input.txt")); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) throws IOException { int[] A = new int[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextInt(); } return A; } long[] readArrayLong(int n) throws IOException { long[] A = new long[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextLong(); } return A; } } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } }
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(); } }
0
Non-plagiarised
23594fdf
b185d034
import java.io.*; import java.util.*; public class Main { public static class StringPair implements Comparable{ String s; Integer charCount; public StringPair(String s, Integer charCount){ this.s = s; this.charCount = charCount; } @Override public int compareTo(Object o) { StringPair f = (StringPair) o; if(f.charCount < this.charCount){ return 1; } else if(f.charCount == this.charCount){ if(f.s.length() - f.charCount > this.s.length() - this.charCount){ return -1; } else if(f.s.length() - f.charCount < this.s.length() - this.charCount){ return 1; } else{ return 0; } } else{ return -1; } } } 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(); String[] s = new String[n]; for(int i = 0 ; i < n ; i++){ s[i] = sc.next(); } Integer[][] count = new Integer[n][5]; Integer[] totalCount = new Integer[5]; for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < 5 ; j++){ count[i][j]=0; } } for(int i = 0 ; i < 5 ; i++){ totalCount[i] = 0; } Integer total = 0; for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < s[i].length() ; j++){ int val = s[i].charAt(j) - 'a'; count[i][val]++; totalCount[val]++; total++; } } //2*count[]char > len of total int ans = -1; for(int ch = 0 ; ch < 5 ; ch++){ List<StringPair> ls = new ArrayList<>(); for(int i = 0 ; i < n ; i++){ int c = s[i].length() - (2 * count[i][ch]); ls.add(new StringPair(s[i],c)); } Collections.sort(ls); int finalCount = n; int thisTotal = total; int thisTotalCharcount = totalCount[ch]; int idx = n-1; while(finalCount > ans && 2*thisTotalCharcount <= thisTotal && idx >= 0){//condition true){ thisTotal -= ls.get(idx).s.length(); thisTotalCharcount -= (ls.get(idx).s.length() - ls.get(idx).charCount)/2; idx--; finalCount--; } ans = Integer.max(ans,finalCount); } System.out.println(ans); } } }
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
90f01508
e00b1794
// import java.io.DataInputStream; // import java.io.FileInputStream; // import java.io.IOException; import java.io.*; import java.util.*; public class one { static Scanner sc=new Scanner(System.in); boolean prime[]; static int prev=-1; static int dp[][]; public static int[] input(int size){ int arr[]=new int[size]; for(int i=0;i<size;i++) arr[i]=sc.nextInt(); return arr; } public static void main(String[] args) { //int testcase=1; int testcase=sc.nextInt(); //System.out.println("HI"); while(testcase-->0){ // int x=sc.nextInt(); // int y=sc.nextInt(); //String str[]=new String[size]; solve(); System.out.println(); } } public static void solve(){ HashMap<Integer,Integer> map=new HashMap<Integer,Integer>(); int size=sc.nextInt(); int arr[][]=new int[size-1][2]; for(int i=0;i<size-1;i++){ arr[i][0]=sc.nextInt(); arr[i][1]=sc.nextInt(); } for(int x[]:arr){ map.put(x[0],map.getOrDefault(x[0], 0)+1); map.put(x[1],map.getOrDefault(x[1], 0)+1); if(map.get(x[0])>2||map.get(x[1])>2){ System.out.println(-1); return; } } List<List<Integer>> adj=new ArrayList<>(); for(int i=0;i<=size;i++) adj.add(new ArrayList<Integer>()); for(int x[]:arr){ adj.get(x[0]).add(x[1]); adj.get(x[1]).add(x[0]); } //System.out.println(adj); int vist[]=new int[size+1]; HashMap<String,Integer> ans=new HashMap<String,Integer>(); for(int i=1;i<=size;i++){ if(vist[i]==0){ dfs(i,vist,adj,ans,2); } } //System.out.println(ans); for(int x[]:arr){ //System.out.print(map.get(x[0])); int a=Math.min(x[0],x[1]); int b=Math.max(x[0],x[1]); String s=a+" "+b; System.out.print(ans.get(s)+" "); } // map=new HashMap<Integer,Integer>(); // for(int x[]:arr){ // if(map.containsKey(x[0])){ // int val=13-map.get(x[0]); // map.put(x[1],val); // System.out.print(val+" "); // }else if(map.containsKey(x[1])){ // int val=13-map.get(x[1]); // map.put(x[0],val); // System.out.print(val+" "); // }else{ // System.out.print(2+" "); // map.put(x[0],2); // map.put(x[1],2); // } // } } public static void dfs(int node,int vist[],List<List<Integer>> adj,HashMap<String,Integer> ans,int val){ vist[node]=1; for(int i:adj.get(node)){ if(vist[i]==1) continue; int x=Math.min(i, node); int y=Math.max(i, node); ans.put(x+" "+y,val); dfs(i,vist,adj,ans,5-val); val=5-val; } } }
import java.util.*; import java.io.*; public class Main{ static final Random random=new Random(); static long mod=1000000007L; static HashMap<String,Integer>map=new HashMap<>(); static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null || !st.hasMoreTokens()){ try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } int[] readIntArray(int n){ int[] res=new int[n]; for(int i=0;i<n;i++)res[i]=nextInt(); return res; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } public static void main(String[] args) { try { FastReader in=new FastReader(); FastWriter out = new FastWriter(); int testCases=in.nextInt(); //out.println("hee"); //int testCases=1; while(testCases-- > 0){ solve(in); } out.close(); } catch (Exception e) { return; } } public static void solve( FastReader in){ int n=in.nextInt(); List<List<Pair>> graph=new ArrayList<>(); for(int i=0;i<n;i++){ graph.add(new ArrayList<>()); } //out.println("hee"); for(int i=0;i<n-1;i++){ int u=in.nextInt(); int v=in.nextInt(); u--;v--; graph.get(u).add(new Pair(v,i)); graph.get(v).add(new Pair(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[] ans=new int[n-1]; int[] vis=new int[n+1]; vis[start]=1; int w=2; Queue<Integer> q=new LinkedList<>(); q.add(start); while(!q.isEmpty()){ int x=q.poll(); for(Pair p:graph.get(x)){ if(vis[p.node]==0){ vis[p.node]=1; q.add(p.node); int idx=p.eg_indx; ans[idx]=w; if(w==2)w=5; else w=2; } } } for(int i:ans){ System.out.print(i+" "); } System.out.println(""); } static class Pair{ int node; int eg_indx; Pair(int node,int eg_indx){ this.node=node; this.eg_indx=eg_indx; } } static int gcd(int a,int b){ if(b==0){ return a; } return gcd(b,a%b); } }
0
Non-plagiarised
d8a171a3
f229aa7f
import java.io.*; import java.util.*; public class B { public static void main(String[] args)throws IOException { FastScanner scan = new FastScanner(); PrintWriter output = new PrintWriter(System.out); int t = scan.nextInt(); for(int tt = 0;tt<t;tt++) { int n = scan.nextInt(); char initial[] = scan.next().toCharArray(); char desired[] = scan.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(ans, count); } 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) ans = -1; output.println(ans); } output.flush(); } public static int[] sort(int arr[]) { List<Integer> list = new ArrayList<>(); for(int i:arr) list.add(i); Collections.sort(list); for(int i = 0;i<list.size();i++) arr[i] = list.get(i); return arr; } public static int gcd(int a, int b) { if(a == 0) return b; return gcd(b%a, a); } public static void printArray(int arr[]) { for(int i:arr) System.out.print(i+" "); System.out.println(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
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
54d7c21e
80881cae
import java.io.*; import java.util.*; public class cp { static int mod=(int)1e9+7; // static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static int[] sp; static int size=(int)1e6; static int[] arInt; static long[] arLong; public static void main(String[] args) throws IOException { long tc=sc.nextLong(); // Scanner sc=new Scanner(System.in); // int tc=1; // primeSet=new HashSet<>(); // sieveOfEratosthenes((int)1e6+5); while(tc-->0) { int n=sc.nextInt(); int k[]=new int[n]; int h[]=new int[n]; for(int i=0;i<n;i++) k[i]=sc.nextInt(); for(int i=0;i<n;i++) h[i]=sc.nextInt(); ArrayList<Pair> interval=new ArrayList<Pair>(); ArrayList<Pair> act=new ArrayList<Pair>(); for(int i=0;i<n;i++) interval.add(new Pair(k[i]-h[i]+1,k[i])); Collections.sort(interval); // out.println(interval); act.add(interval.get(0)); for(int i=1;i<n;i++) { Pair p=act.get(act.size()-1); if(p.y<interval.get(i).x) act.add(interval.get(i)); else p.y=Math.max(p.y, interval.get(i).y); } // out.println(act); long mana=0; for(Pair p: act) { long x=p.y-p.x+1; mana+=(x*(x+1))/2; } out.println(mana); // int n=sc.nextInt(); // long days[]=new long[n]; // long power[]=new long[n]; // for (int i = 0; i < power.length; i++) { // days[i]=sc.nextLong(); // } // for (int i = 0; i < power.length; i++) { // power[i]=sc.nextLong(); // // } // // long ans=0; // for(int i=0;i<n;i++) // { // if(i==0) // { // ans+=power[i]*(power[i]+1L)/2L; // continue; // } // // long temp=power[i]*(power[i]+1)/2L; // long temp2=(power[i-1]+days[i]-days[i-1])*(power[i-1]+days[i]-days[i-1]+1L)/2L; // temp2-=power[i-1]*(power[i-1]+1L)/2L; // ans+=Math.min(temp, temp2); //// if(days[i]-days[i-1]<=power[i]) //// { //// ans+=power[i]*(power[i]+1)/2; //// } //// else { //// ans+=power[i]*(power[i]+1)/2; //// ans-=power[i-1]*(power[i-1]+1)/2; //// } // // // } // // out.println(ans); } out.flush(); out.close(); System.gc(); } /* ...SOLUTION ENDS HERE...........SOLUTION ENDS HERE... */ static int util(char a,char b) { int A=a-'0'; int B=b-'0'; return A+B; } static boolean check(int x,int[] rich,int[] poor) { int cnt=0; for(int i=0;i<rich.length;i++) { if(x-1-rich[i]<=cnt && cnt<=poor[i]) cnt++; } return cnt>=x; } static void arrInt(int n) throws IOException { arInt=new int[n]; for (int i = 0; i < arInt.length; i++) { arInt[i]=sc.nextInt(); } } static void arrLong(int n) throws IOException { arLong=new long[n]; for (int i = 0; i < arLong.length; i++) { arLong[i]=sc.nextLong(); } } static ArrayList<Integer> add(int id,int c) { ArrayList<Integer> newArr=new ArrayList<>(); for(int i=0;i<id;i++) newArr.add(arInt[i]); newArr.add(c); for(int i=id;i<arInt.length;i++) { newArr.add(arInt[i]); } return newArr; } // function to find last index <= y static int upper(ArrayList<Integer> arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) <= y) l = mid + 1; else h = mid - 1; } return h; } static int lower(ArrayList<Integer> arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) >= x) h = mid - 1; else l = mid + 1; } return l; } static int N = 501; // Array to store inverse of 1 to N static long[] factorialNumInverse = new long[N + 1]; // Array to precompute inverse of 1! to N! static long[] naturalNumInverse = new long[N + 1]; // Array to store factorial of first N numbers static long[] fact = new long[N + 1]; // Function to precompute inverse of numbers public static void InverseofNumber(int p) { naturalNumInverse[0] = naturalNumInverse[1] = 1; for(int i = 2; i <= N; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse[0] = factorialNumInverse[1] = 1; // Precompute inverse of natural numbers for(int i = 2; i <= N; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to N public static void factorial(int p) { fact[0] = 1; // Precompute factorials for(int i = 1; i <= N; i++) { fact[i] = (fact[i - 1] * (long)i) % p; } } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R, int p) { // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p; return ans; } static String tr(String s) { int now = 0; while (now + 1 < s.length() && s.charAt(now)== '0') ++now; return s.substring(now); } static ArrayList<Integer> ans; static void dfs(int node,Graph gg,int cnt,int k,ArrayList<Integer> temp) { if(cnt==k) return; for(Integer each:gg.list[node]) { if(each==0) { temp.add(each); ans=new ArrayList<>(temp); temp.remove(temp.size()-1); continue; } temp.add(each); dfs(each,gg,cnt+1,k,temp); temp.remove(temp.size()-1); } return; } static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop 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 ArrayList<Integer> commDiv(int a, int b) { // find gcd of a, b int n = gcd(a, b); // Count divisors of n. ArrayList<Integer> Div=new ArrayList<>(); for (int i = 1; i <= Math.sqrt(n); i++) { // if 'i' is factor of n if (n % i == 0) { // check if divisors are equal if (n / i == i) Div.add(i); else { Div.add(i); Div.add(n/i); } } } return Div; } static HashSet<Integer> factors(int x) { HashSet<Integer> a=new HashSet<Integer>(); for(int i=2;i*i<=x;i++) { if(x%i==0) { a.add(i); a.add(x/i); } } return a; } static class Node { int vertex; HashSet<Node> adj; boolean rem; Node(int ver) { vertex=ver; rem=false; adj=new HashSet<Node>(); } @Override public String toString() { return vertex+" "; } } static class Tuple{ int a; int b; int c; public Tuple(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } } //function to find prime factors of n static HashMap<Long,Long> findFactors(long n2) { HashMap<Long,Long> ans=new HashMap<>(); if(n2%2==0) { ans.put(2L, 0L); // cnt++; while((n2&1)==0) { n2=n2>>1; ans.put(2L, ans.get(2L)+1); // } } for(long i=3;i*i<=n2;i+=2) { if(n2%i==0) { ans.put((long)i, 0L); // cnt++; while(n2%i==0) { n2=n2/i; ans.put((long)i, ans.get((long)i)+1); } } } if(n2!=1) { ans.put(n2, ans.getOrDefault(n2, (long) 0)+1); } return ans; } //fenwick tree implementaion static class fwt { int n; long BITree[]; fwt(int n) { this.n=n; BITree=new long[n+1]; } fwt(int arr[], int n) { this.n=n; BITree=new long[n+1]; for(int i = 0; i < n; i++) updateBIT(n, i, arr[i]); } long getSum(int index) { long sum = 0; index = index + 1; while(index>0) { sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(int n, int index,int val) { index = index + 1; while(index <= n) { BITree[index] += val; index += index & (-index); } } void print() { for(int i=0;i<n;i++) out.print(getSum(i)+" "); out.println(); } } class sparseTable{ int n; long[][]dp; int log2[]; int P; void buildTable(long[] arr) { n=arr.length; P=(int)Math.floor(Math.log(n)/Math.log(2)); log2=new int[n+1]; log2[0]=log2[1]=0; for(int i=2;i<=n;i++) { log2[i]=log2[i/2]+1; } dp=new long[P+1][n]; for(int i=0;i<n;i++) { dp[0][i]=arr[i]; } for(int p=1;p<=P;p++) { for(int i=0;i+(1<<p)<=n;i++) { long left=dp[p-1][i]; long right=dp[p-1][i+(1<<(p-1))]; dp[p][i]=Math.max(left, right); } } } long maxQuery(int l,int r) { int len=r-l+1; int p=(int)Math.floor(log2[len]); long left=dp[p][l]; long right=dp[p][r-(1<<p)+1]; return Math.max(left, right); } } //Function to find number of set bits static int setBitNumber(long n) { if (n == 0) return 0; int msb = 0; n = n / 2; while (n != 0) { n = n / 2; msb++; } return msb; } static int getFirstSetBitPos(long n) { return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1; } static ArrayList<Integer> primes; static HashSet<Integer> primeSet; static boolean prime[]; static void sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. prime= new boolean[n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p 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) primeSet.add(i); } } static long mod(long a, long b) { long c = a % b; return (c < 0) ? c + b : c; } static void swap(long arr[],int i,int j) { long temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } static boolean util(int a,int b,int c) { if(b>a)util(b, a, c); while(c>=a) { c-=a; if(c%b==0) return true; } return (c%b==0); } static void flag(boolean flag) { out.println(flag ? "YES" : "NO"); out.flush(); } static void print(int a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print(long a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print_int(ArrayList<Integer> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void print_long(ArrayList<Long> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void printYesNo(boolean condition) { if (condition) { out.println("YES"); } else { out.println("NO"); } } static int LowerBound(int a[], int 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 int lowerIndex(int arr[], int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] >= x) h = mid - 1; else l = mid + 1; } return l; } // function to find last index <= y static int upperIndex(int arr[], int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int upperIndex(long arr[], int n, long y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int UpperBound(int a[], int 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 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 class DisjointUnionSets { int[] rank, parent; int n; // Constructor public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } // Creates n sets with single item in each void makeSet() { for (int i = 0; i < n; i++) parent[i] = i; } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } // Unites the set that includes x and the set // that includes x void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else // if ranks are the same { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } // if(xRoot!=yRoot) // parent[y]=x; } int connectedComponents() { int cnt=0; for(int i=0;i<n;i++) { if(parent[i]==i) cnt++; } return cnt; } } static class Graph { int v; ArrayList<Integer> list[]; Graph(int v) { this.v=v; list=new ArrayList[v+1]; for(int i=1;i<=v;i++) list[i]=new ArrayList<Integer>(); } void addEdge(int a, int b) { this.list[a].add(b); } } // static class GraphMap{ // Map<String,ArrayList<String>> graph; // GraphMap() { // // TODO Auto-generated constructor stub // graph=new HashMap<String,ArrayList<String>>(); // // } // void addEdge(String a,String b) // { // if(graph.containsKey(a)) // this.graph.get(a).add(b); // else { // this.graph.put(a, new ArrayList<>()); // this.graph.get(a).add(b); // } // } // } // static void dfsMap(GraphMap g,HashSet<String> vis,String src,int ok) // { // vis.add(src); // // if(g.graph.get(src)!=null) // { // for(String each:g.graph.get(src)) // { // if(!vis.contains(each)) // { // dfsMap(g, vis, each, ok+1); // } // } // } // // cnt=Math.max(cnt, ok); // } static double sum[]; static long cnt; // static void DFS(Graph g, boolean[] visited, int u) // { // visited[u]=true; // // for(int i=0;i<g.list[u].size();i++) // { // int v=g.list[u].get(i); // // if(!visited[v]) // { // cnt1=cnt1*2; // DFS(g, visited, v); // // } // // } // // // } 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) { // TODO Auto-generated method stub return this.x-o.x; } } static long sum_array(int a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static long sum_array(long a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } 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 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 reverse_array(int a[]) { int n=a.length; int i,t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static void reverse_array(long a[]) { int n=a.length; int i; long t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } // 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 res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static int power(int x, int y) { int res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static long gcd(long a, long b) { if (a == 0) return b; //cnt+=a/b; return gcd(b%a,a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } 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(); } 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 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(); } } static PrintWriter out=new PrintWriter(System.out); static int int_max=Integer.MAX_VALUE; static int int_min=Integer.MIN_VALUE; static long long_max=Long.MAX_VALUE; static long long_min=Long.MIN_VALUE; }
//package Codeforces; import java.io.*; import java.util.*; public class CP { static Scanner sc=new Scanner(System.in); public static void main(String[] args) throws IOException, CloneNotSupportedException { int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k[]=new int[n]; int h[]=new int[n]; for(int i=0;i<n;i++) k[i]=sc.nextInt(); for(int i=0;i<n;i++) h[i]=sc.nextInt(); ArrayList<Pair> interval=new ArrayList<Pair>(); ArrayList<Pair> act=new ArrayList<Pair>(); for(int i=0;i<n;i++) interval.add(new Pair(k[i]-h[i]+1,k[i])); Collections.sort(interval); // out.println(interval); act.add(interval.get(0).clone()); for(int i=1;i<n;i++) { Pair p=act.get(act.size()-1); if(p.y<interval.get(i).x) act.add(interval.get(i).clone()); else p.y=Math.max(p.y, interval.get(i).y); } // out.println(act); long mana=0; for(Pair p: act) { long x=p.y-p.x+1; mana+=(x*(x+1))/2; } System.out.println(mana); } } static class Pair implements Cloneable, Comparable<Pair> { int x,y; Pair(int a,int b) { this.x=a; this.y=b; } // @Override // public boolean equals(Object obj) // { // if(obj instanceof Pair) // { // Pair p=(Pair)obj; // return p.x==this.x && p.y==this.y; // } // return false; // } // @Override // public int hashCode() // { // return Math.abs(x)+500*Math.abs(y); // } // @Override // public String toString() // { // return "("+x+" "+y+")"; // } @Override protected Pair clone() throws CloneNotSupportedException { return new Pair(this.x,this.y); } @Override public int compareTo(Pair a) { long t=(this.x-a.x); if(t!=0) return t>0?1:-1; else return (int)(this.y-a.y); } // public void swap() // { // this.y=this.y+this.x; // this.x=this.y-this.x; // this.y=this.y-this.x; // } } }
1
Plagiarised
45e38fad
ec8566b8
import java.util.*; import java.io.*; public class C_Not_Assigning { // For fast input output static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader( new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Edge{ public int node; public int next; Edge(int node, int next){ this.node = node; this.next = next; } } public static void dfs(int u, ArrayList<ArrayList<Edge>> g,int[] vis, int flag, int[] weight){ vis[u] = 1; for(Edge e : g.get(u)){ if(vis[e.node] == 0){ if(flag == 0) { weight[e.next] = 2; dfs(e.node, g, vis, 1, weight); } else { weight[e.next] = 5; dfs(e.node, g, vis, 0, weight); } } } } // end of fast i/o code public static void main(String[] args) { FastReader reader = new FastReader(); int t = reader.nextInt(); while(t-->0){ int n = reader.nextInt(); int[] order = new int[n+1]; int flag = 0; //ArrayList<ArrayList<Integer>> adj = new ArrayList<>(n+1); //Map<Integer, List<Integer>> adj = new HashMap<>(); ArrayList<ArrayList<Edge>> graph= new ArrayList<>(); for(int i=0;i<n+1;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<n-1;i++){ int u = reader.nextInt(); int v = reader.nextInt(); order[u]++; order[v]++; graph.get(u).add(new Edge(v, i)); graph.get(v).add(new Edge(u, i)); if(order[u]==3 || order[v]==3){ flag = 1; } } if(flag == 1) System.out.println(-1); else{ for(int i=1;i<=n;i++){ if(order[i] == 1){ int[] vis = new int[n+1]; int[] weight = new int[n]; dfs(i, graph, vis, 0, weight); for(int j =0;j<n-1;j++){ System.out.print(weight[j] + " "); } break; } } System.out.println(); } } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.concurrent.ThreadLocalRandom; public class A { private static void sport(List<Integer>[] g, Map<W, Integer> map) { int n = g.length; for (int i = 0; i < n; i++) { if (g[i].size() > 2) { System.out.println(-1); return; } } int[] ans = new int[n - 1]; //dfs(new C(-1, 0), g, ans, 3, new HashSet<>()); Queue<int[]> queue = new LinkedList<>(); Set<Integer> seen = new HashSet<>(); int val = 3; for (Integer integer : g[0]) { Integer idx = map.get(new W(0, integer)); ans[idx] = val; queue.add(new int[]{val, integer}); seen.add(integer); val = val == 2 ? 3 : 2; } seen.add(0); while (!queue.isEmpty()) { int[] poll = queue.poll(); for (Integer u : g[poll[1]]) { if (!seen.contains(u)) { seen.add(u); int curr = poll[0] == 2 ? 3 : 2; Integer integer = map.get(new W(poll[1], u)); ans[integer] = curr; queue.add(new int[]{curr, u}); } } } for (int an : ans) { System.out.print(an + " "); } System.out.println(); } static class W { int u; int v; public W(int u, int v) { this.u = u; this.v = v; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; W w = (W) o; return u == w.u && v == w.v; } @Override public int hashCode() { return Objects.hash(u, v); } } static void dfs(C v, List<C>[] g, int[] ans, int prev, Set<Integer> seen) { if (v.i != -1) { ans[v.i] = prev == 2 ? 3 : 2; } seen.add(v.v); int next = prev == 2 ? 3 : 2; for (C c : g[v.v]) { if (!seen.contains(c.v)) { dfs(c, g, ans, next, seen); } next = next == 2 ? 3 : 2; } } static class C { int i; int v; public C(int i, int v) { this.i = i; this.v = v; } } public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); List<Integer>[] g = new ArrayList[n]; for (int j = 0; j < n; j++) { g[j] = new ArrayList<>(); } Map<W, Integer> map = new HashMap<>(); for (int j = 0; j < n - 1; j++) { int u = sc.nextInt() - 1; int v = sc.nextInt() - 1; g[u].add(v); g[v].add(u); map.put(new W(u, v), j); map.put(new W(v, u), j); } sport(g, map); } } static void shuffleArray(int[] ar) { // If running on Java 6 or older, use `new Random()` on RHS here Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap int a = ar[index]; ar[index] = ar[i]; ar[i] = a; } } 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()); } void nextLine() throws IOException { br.readLine(); } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } int[] readArrayInt(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
0
Non-plagiarised
a368f345
e5d02e93
import java.util.*; import java.io.*; public class codeforces { static class Pair { char type; int L; int R; Pair(char type, int L, int R) { this.type = type; this.L = L; this.R = R; } } static FastReader fr; static StringBuilder res; static class FastReader { BufferedReader br; StringTokenizer st; BufferedWriter bw; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(System.out)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } char nextChar() { return next().charAt(0); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } void write(String str) { try { bw.write(str); } catch (IOException e) { e.printStackTrace(); } } void close() { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } 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 { fr = new FastReader(); boolean fixed = false; int t = !fixed ? fr.nextInt() : 1; res = new StringBuilder(); while (t-- > 0) { solve(); } fr.write(res.toString()); fr.close(); } static int M = 1000008; static boolean[] primes = new boolean[M]; static int[] dp = new int[M]; public static void sieve() { for (int i = 2; i * i <= M; i++) { if (!primes[i]) { for (int j = i * i; j < M; j += i) { primes[j] = true; } } } for (int i = 5; i < 1000001; i++) { if (!primes[i] && !primes[i - 2]) dp[i] = dp[i - 1] + 1; else dp[i] = dp[i - 1]; } } public static void solve() { int n = fr.nextInt(); String[] arr = new String[n]; for (int i = 0; i < n; i++) { arr[i] = fr.nextLine(); } char[] chars = { 'a', 'b', 'c', 'd', 'e' }; int ans = 0; for (int i = 0; i < 5; i++) { char ch = chars[i]; List<Integer> a = new ArrayList<>(); for (int j = 0; j < n; j++) { String s = arr[j]; int c = 0; for (int k = 0; k < s.length(); k++) { if (s.charAt(k) == ch) c++; } a.add(2 * c - s.length()); } // System.out.println(a); Collections.sort(a, Collections.reverseOrder()); int sum = 0, len = 0; for (int k = 0; k < a.size(); k++) { if (sum + a.get(k) <= 0) { break; } else { len++; sum += a.get(k); } } ans = Math.max(ans, len); } res.append(ans + "\n"); } public static int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = fr.nextInt(); } return arr; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static List<Integer> readArrayList(int n) { List<Integer> arr = new ArrayList<>(); for (int i = 0; i < n; i++) { arr.add(fr.nextInt()); } return arr; } }
import java.util.*; import java.io.*; public class C_1551 { 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(); int[] a = new int[n]; int[] b = new int[n]; int[] c = new int[n]; int[] d = new int[n]; int[] e = new int[n]; for(int i = 0; i < n; i++) { String s = sc.next(); int[] cnt = new int[5]; for(int j = 0; j < s.length(); j++) cnt[s.charAt(j) - 'a']++; a[i] = 2 * cnt[0] - s.length(); b[i] = 2 * cnt[1] - s.length(); c[i] = 2 * cnt[2] - s.length(); d[i] = 2 * cnt[3] - s.length(); e[i] = 2 * cnt[4] - s.length(); } Arrays.sort(a); Arrays.sort(b); Arrays.sort(c); Arrays.sort(d); Arrays.sort(e); int max = 0; int sum = 0; for(int i = n - 1; i >= 0; i--) { sum += a[i]; if(sum <= 0) break; max = n - i; } sum = 0; for(int i = n - 1; i >= 0; i--) { sum += b[i]; if(sum <= 0) break; max = Math.max(max, n - i); } sum = 0; for(int i = n - 1; i >= 0; i--) { sum += c[i]; if(sum <= 0) break; max = Math.max(max, n - i); } sum = 0; for(int i = n - 1; i >= 0; i--) { sum += d[i]; if(sum <= 0) break; max = Math.max(max, n - i); } sum = 0; for(int i = n - 1; i >= 0; i--) { sum += e[i]; if(sum <= 0) break; max = Math.max(max, n - i); } pw.println(max); } 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); } } }
0
Non-plagiarised
5766f176
e7a997b5
import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { // write your code here boolean readFromLocal = true; //readFromLocal = false; String filepath = "src/input.txt"; //FileInputStrviseam fileInputStream = new FileInputStream(filepath); InputReader inputReader = new InputReader(System.in); Solve s = new Solve(); s.solve(inputReader); } } class Solve { public void solve(InputReader inputReader) { int t,n; t = inputReader.nextInt(); while (t>0) { t--; n = inputReader.nextInt(); Graph g = new Graph(n); g.res = new int[n-1]; for(int i=1;i<n;i++){ g.addEdge(inputReader.nextInt(), inputReader.nextInt(),i-1,true); } if (g.hasDegreeMoreThanTwo()){ System.out.println(-1); }else { int minDegree = 2,node = 1; for(int i=1;i<=n;i++){ if (g.adj[i].size()<minDegree){ node = i; minDegree = g.adj[i].size(); } } g.dfs(node,-1,2); for(int i=0;i<n-1;i++){ System.out.print(g.res[i] + " "); } System.out.println(); } } } } class PairComp implements Comparator<Pair> { @Override public int compare(Pair o1, Pair o2) { return (int) (o1.first - o2.first); } } class InputReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } 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 nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return readLine(); } else { return readLine0(); } } public BigInteger readBigInteger() { try { return new BigInteger(nextString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) { read(); } return value == -1; } public String next() { return nextString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public int[] nextSortedIntArray(int n) { int[] array = nextIntArray(n); Arrays.sort(array); return array; } public int[] nextSumIntArray(int n) { int[] array = new int[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; ++i) array[i] = nextLong(); return array; } public long[] nextSumLongArray(int n) { long[] array = new long[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextSortedLongArray(int n) { long[] array = nextLongArray(n); Arrays.sort(array); return array; } } class Pair implements Comparable<Pair> { long first, second; public Pair(long first, long second) { this.first = first; this.second = second; } @Override public int compareTo(Pair o) { return (int) (this.second - o.second); } } class Utils { static void swap(int[] res, int i, int j) { int temp = res[i]; res[i] = res[j]; res[j] = temp; } static long pow(long num, int n, long mod) { long res = num % mod; while (n > 0) { if ((n & 1) != 0) { res = (res * num) % mod; } num = (num * num) % mod; n >>= 1; } return res; } } class Graph { public ArrayList<int[]>[] adj; int size; boolean[] vis; public int [] res; Graph(int n){ this.size = n; this.adj = new ArrayList[n+1]; this.vis = new boolean[n+1]; for (int i = 0; i <=n; i++) { adj[i] = new ArrayList<>(); } } public void addEdge(int a, int b, int edge,boolean biDirectional){ adj[a].add(new int[]{b, edge}); if (biDirectional) { adj[b].add(new int[]{a, edge}); } } public void dfs(int node,int parent,int prime){ for (int[] nodePair: adj[node]) { if (nodePair[0]!=parent){ res[nodePair[1]] = prime; dfs(nodePair[0],node,prime^1); } } } boolean hasDegreeMoreThanTwo() { for (int i = 0; i <= size; i++) { if (adj[i].size()>2){ return true; } } return false; } }
import java.io.*; import java.util.*; public class c { public static void main(String[] args){ FastScanner sc = new FastScanner(); int t = sc.nextInt(); while(t-- > 0){ int n = sc.nextInt(); ArrayList<ArrayList<Edge>> graph = new ArrayList<>(); for(int i=0; i<n; i++){ graph.add(new ArrayList<Edge>()); } for(int i=0; i<n-1; i++){ int u = sc.nextInt(); int v = sc.nextInt(); Edge e = new Edge(u-1, v-1, i+1); Edge e2 = new Edge(v-1, u-1, i+1); graph.get(u-1). add(e); graph.get(v-1).add(e2); } int edges[] = new int[n]; int indegree1count = 0; int indegree2count = 0; for(ArrayList<Edge> list : graph){ if(list.size() == 1){ indegree1count++; } else if(list.size() == 2){ indegree2count++; } } if(indegree1count == 2 && indegree1count+indegree2count==n){ for(int i=0; i<graph.size(); i++){ ArrayList<Edge> list = graph.get(i); if(list.size() == 1){ dfs(graph, edges, false, -1, i) ; } } for(int i=1; i<edges.length; i++){ System.out.print(edges[i] + " "); } System.out.println(); } else{ System.out.println(-1); } } } public static void dfs(ArrayList<ArrayList<Edge>> graph, int[] edges, boolean isprev2, int parent, int current){ for(Edge e : graph.get(current)){ if(e.v == parent){ continue; } edges[e.id] = isprev2 ? 5 : 2; dfs(graph, edges, !isprev2, current, e.v); } } } class Edge { int u; int v; int id; public Edge(int u, int v, int id) { this.u = u; this.v = v; this.id = id; } } class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
0
Non-plagiarised
3f6f1267
cdb7a891
import java.io.*; import java.util.*; public class T4 { public static void main(String[] args) { FastScanner s = new FastScanner(); T4 main = new T4(); int n = 1; while (n-- > 0) { main.MainPrint(s); } } long mod = 998244353L; private void MainPrint(FastScanner s) { int n = s.nextInt(); k = s.nextInt(); TreeMap<Integer, int[]> map = new TreeMap<>(); int l, r; int[] get; for (int i = 0; i < n; i++) { l = s.nextInt(); r = s.nextInt(); if (!map.containsKey(l)) { get = new int[2]; get[0]++; map.put(l, get); } else { get = map.get(l); get[0]++; } if (!map.containsKey(r + 1)) { get = new int[2]; get[1]++; map.put(r + 1, get); } else { get = map.get(r + 1); get[1]++; } } int count = 0; long ans = 0; precomp(); for (int i : map.keySet()) { get = map.get(i); count += get[0] - get[1]; ans += rem[count] - rem[count-get[0]]; ans %= mod; } if (ans<0) ans+=mod; System.out.println(ans%mod); } long mul(long a, long b) { return a*b%mod; } 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)); } long modInv(long x) { return exp(x, mod-2); } long[] facts; long[] factInvs; long[] rem; int k; void precomp() { facts=new long[300_001]; factInvs=new long[300_001]; rem = new long[300_001]; 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); for (int i = k;i<rem.length;i++){ rem[i] =mul(facts[i], mul(factInvs[k], factInvs[i-k])); } } 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.io.PrintWriter; import java.util.*; import java.lang.*; public class Practice { public static long mod = 998244353; public static long mod2 = 998244353; public static long tt = 0; public static int[] ttt = new int[2]; public static long[] fac = new long[500000]; public static ArrayList<Integer> prime; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); // int t = Integer.parseInt(br.readLine()); // while (t-- > 0) { fac(); String[] s1 = br.readLine().split(" "); int n = Integer.parseInt(s1[0]); int k = Integer.parseInt(s1[1]); Long[][] arr = new Long[n][2]; Long[] st = new Long[n]; Long[] end = new Long[n]; for (int i = 0; i < n; i++) { String str = (br.readLine()); String[] s2 = str.split(" "); arr[i][0] = Long.parseLong(s2[0]); st[i] = arr[i][0]; arr[i][1] = Long.parseLong(s2[1]); end[i] = arr[i][1]; } Arrays.sort(st); Arrays.sort(end); long pp = (power(fac[k], mod - 2)); long[] comb = new long[n + 1]; for (int i = 1; i <= n; i++) { if (i < k) { continue; } // System.out.println(i + " " + fac[i] + " " + power(fac[k], mod - 2) + " " + // fac[k]); comb[i] = (fac[i] * pp) % mod; comb[i] = (comb[i] * (power(fac[i - k], mod - 2))) % mod; } // for (int i = 0; i <= n; i++) { // System.out.print(comb[i] + " "); // } HashMap<Long, Integer> map1 = new HashMap<Long, Integer>(); HashMap<Long, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { if (map.containsKey(arr[i][0])) { map.put(arr[i][0], map.getOrDefault(arr[i][0], 0) + 1); continue; } int a = getLower(arr[i][0] - 1, st); int b = getLower(arr[i][0] - 1, end); map.put(arr[i][0], map.getOrDefault(arr[i][0], 0) + 1); map1.put(arr[i][0], map1.getOrDefault(arr[i][0], 0)); if (a != -1 || b != -1) { if (b == -1) { int curr = a + 1; map1.put(arr[i][0], map1.getOrDefault(arr[i][0], 0) + curr); continue; } // System.out.println(arr[i][0] + " " + b + " " + a); int curr = a - b; map1.put(arr[i][0], map1.getOrDefault(arr[i][0], 0) + curr); } } long ans = 0; for (long a : map.keySet()) { ans = (ans + comb[map.get(a) + map1.get(a)] + mod - comb[map1.get(a)]) % mod; } // System.out.println(map + " " + map1); pw.println(ans); // } pw.close(); } private static long power(long a, long p) { // TODO Auto-generated method stub long res = 1; while (p > 0) { if (p % 2 == 1) { res = (res * a) % mod; } p = p / 2; a = (a * a) % mod; } return res; } private static void fac() { fac[0] = 1; // TODO Auto-generated method stub for (int i = 1; i < fac.length; i++) { if (i == 1) { fac[i] = 1; } else { fac[i] = i * fac[i - 1]; } if (fac[i] > mod) { fac[i] = fac[i] % mod; } } } private static int getLower(Long long1, Long[] st) { // TODO Auto-generated method stub int left = 0, right = st.length - 1; int ans = -1; while (left <= right) { int mid = (left + right) / 2; if (st[mid] <= long1) { ans = mid; left = mid + 1; } else { right = mid - 1; } } return ans; } } // private static long getGCD(long l, long m) { // // long t1 = Math.min(l, m); // long t2 = Math.max(l, m); // while (true) { // long temp = t2 % t1; // if (temp == 0) { // return t1; // } // t2 = t1; // t1 = temp; // } // }
0
Non-plagiarised
bdfe8110
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 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()); } } }
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
8d6f1bf5
dc281165
//package Div2.C; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Menorah { 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()); String source = br.readLine(); String destination = br.readLine(); int sameStatusOnes = 0; int sameStatusZeros = 0; int diffStatusOnes = 0; int diffStatusZeros = 0; for (int i = 0; i < n; i++) { char c1 = source.charAt(i); char c2 = destination.charAt(i); if (c1 == c2) { if (c1 == '0') { sameStatusZeros += 1; } else { sameStatusOnes += 1; } } else { if (c1 == '0') { diffStatusZeros += 1; } else { diffStatusOnes += 1; } } } int sameStatus = sameStatusOnes + sameStatusZeros; int diffStatus = diffStatusOnes + diffStatusZeros; //first case if (sameStatus == n) { System.out.println(0); } else if (diffStatus == n) { //second case if (diffStatus % 2 == 0 && diffStatusOnes == (n + 1) / 2) System.out.println(n); else System.out.println(-1); } else { int op1 = -1; int op2 = -1; if (sameStatus % 2 != 0 && sameStatusOnes == (sameStatus + 1) / 2) op1 = sameStatus; if (diffStatus % 2 == 0 && diffStatusOnes == (diffStatus + 1) / 2) op2 = diffStatus; if (op1 != -1 && op2 != -1) System.out.println(Integer.min(op1, op2)); else if (op1 != -1) System.out.println(op1); else if (op2 != -1) System.out.println(op2); else System.out.println(-1); } t--; } } }
/** * 12/24/21 morning * https://codeforces.com/contest/1615/problem/C */ // package codeforce.global.r18; import java.util.*; import java.io.*; public class C { static PrintWriter pw; void solve(int n, char[] a, char[] b) { int same = 0, diff = 0; int[] sameCnt = {0, 0}, diffCnt = {0, 0}; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { same++; sameCnt[a[i] - '0']++; } else { diff++; diffCnt[a[i] - '0']++; } } // tr(same, sameCnt, diff, diffCnt); int res = Integer.MAX_VALUE; if (same % 2 != 0 && sameCnt[1] - sameCnt[0] == 1) res = Math.min(res, same); if (diff % 2 == 0 && diffCnt[1] == diffCnt[0]) res = Math.min(res, diff); pr(res == Integer.MAX_VALUE ? -1 : res); } private void run() { // read_write_file(); // comment this before submission FastScanner fs = new FastScanner(); int t = fs.nextInt(); while (t-- > 0) { int n = fs.nextInt(); char[] a = fs.next().toCharArray(), b = fs.next().toCharArray(); solve(n, a, b); } } private final String INPUT = "input.txt"; private final String OUTPUT = "output.txt"; void read_write_file() { FileInputStream instream = null; PrintStream outstream = null; try { instream = new FileInputStream(INPUT); outstream = new PrintStream(new FileOutputStream(OUTPUT)); System.setIn(instream); System.setOut(outstream); } catch (Exception e) { } } public static void main(String[] args) { pw = new PrintWriter(System.out); new C().run(); pw.close(); } void pr(int num) { pw.println(num); } void pr(long num) { pw.println(num); } void pr(double num) { pw.println(num); } void pr(String s) { pw.println(s); } void pr(char c) { pw.println(c); } class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } void tr(Object... o) { pw.println(Arrays.deepToString(o)); } }
0
Non-plagiarised
ccc8ef27
d6fb3b9e
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 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
90dc2b20
d8a171a3
import java.util.*; import java.io.*; public class C1615{ static FastScanner fs = null; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); while (t-->0) { int n = fs.nextInt(); String a = fs.next(); String b = fs.next(); char ch1[] = a.toCharArray(); char ch2[] = b.toCharArray(); int c00 = 0; int c01 = 0; int c10 = 0; int c11 = 0; for(int i=0;i<n;i++){ if(ch1[i]=='0'){ if(ch2[i]=='0'){ c00+=1; } else{ c01+=1; } } else{ if(ch2[i]=='0'){ c10+=1; } else{ c11+=1; } } } int ans = -1; if((c11-c00)==1 || c10==c01){ int s1 = (int)1e7; int s2 = (int)1e7; if((c11-c00)==1){ s1 = c11+c00; } if(c10==c01) s2 = c10+c01; ans = Math.min(s1,s2); } out.println(ans); } 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.*; import java.util.*; public class B { public static void main(String[] args)throws IOException { FastScanner scan = new FastScanner(); PrintWriter output = new PrintWriter(System.out); int t = scan.nextInt(); for(int tt = 0;tt<t;tt++) { int n = scan.nextInt(); char initial[] = scan.next().toCharArray(); char desired[] = scan.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(ans, count); } 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) ans = -1; output.println(ans); } output.flush(); } public static int[] sort(int arr[]) { List<Integer> list = new ArrayList<>(); for(int i:arr) list.add(i); Collections.sort(list); for(int i = 0;i<list.size();i++) arr[i] = list.get(i); return arr; } public static int gcd(int a, int b) { if(a == 0) return b; return gcd(b%a, a); } public static void printArray(int arr[]) { for(int i:arr) System.out.print(i+" "); System.out.println(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
0
Non-plagiarised