F1
stringlengths
8
8
F2
stringlengths
8
8
text_1
stringlengths
606
8.03k
text_2
stringlengths
592
5.48k
label
int64
0
1
2470b521
f6ca6fc8
import com.sun.security.jgss.GSSUtil; import javax.swing.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; //ankit singh public class Main { static int inf=Integer.MAX_VALUE/2; public static void main(String[] args) { Scanner input=new Scanner(System.in); int nn=input.nextInt(); int a[]=new int[nn]; ArrayList<Integer> one=new ArrayList<>(); ArrayList<Integer> zero=new ArrayList<>(); for (int i = 0; i <nn ; i++) { a[i]=input.nextInt(); if(a[i]==1){ one.add(i); }else zero.add(i); } if (one.size()==0) { System.out.println(0); return; } int n=one.size(); int m=zero.size(); int dp[][]=new int[n+1][m+1]; int ans=0; for (int i = 1; i <=n ; i++) { dp[i][0]=inf; for (int j = 1; j <=m ; j++) { dp[i][j]=inf; //take the current dp[i][j]=Math.min(dp[i][j],dp[i-1][j-1]+Math.abs(one.get(i-1)-zero.get(j-1))); //take previos dp[i][j]=Math.min(dp[i][j],dp[i][j-1]); } } ans=inf; for (int i = 1; i <=m ; i++) { ans=Math.min(ans,dp[n][i]); } System.out.println(ans); } }
import java.util.*; import java.io.*; public class D { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n = sc.nextInt(); ArrayList<Integer> o=new ArrayList<Integer>(); ArrayList<Integer> e=new ArrayList<Integer>(); 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()]); } }
0
a8e2cefc
ee270b2a
import java.io.*; import java.sql.SQLOutput; import java.util.*; public class airconditioner { public static class compare implements Comparator<int[]> { public int compare(int[] o1, int[] o2) { return o1[0] - o2[0]; } } public static void main(String[] args) throws IOException { FastReader input = new FastReader(); int testcases = input.nextInt(); for (int t = 0; t < testcases; t++) { int length = input.nextInt(); int conditioners = input.nextInt(); int[] array = new int[length]; int[][] conpositions = new int[conditioners][2]; //0 = position, 1 = value for (int i = 0; i < length; i++) { array[i] = 2000000069; } for (int i = 0; i < conditioners; i++) { conpositions[i][0] = input.nextInt() - 1; } for (int i = 0; i < conditioners; i++) { conpositions[i][1] = input.nextInt(); array[conpositions[i][0]] = conpositions[i][1]; } Arrays.sort(conpositions, new compare()); int position = conditioners - 2; int counter = conpositions[conditioners - 1][1]; for (int i = conpositions[conditioners - 1][0]; i >= 0; i--) { //i = arrayposition array[i] = counter; if (position >= 0 && i == conpositions[position][0]) { if (conpositions[position][1] < counter) { counter = conpositions[position][1]; array[i] = counter; } position--; } counter++; } position = 1; counter = conpositions[0][1]; for (int i = conpositions[0][0]; i < length; i++) { if (counter < array[i]) { array[i] = counter; } if (position < conditioners && i == conpositions[position][0]) { if (conpositions[position][1] < counter) { counter = conpositions[position][1]; if (counter < array[i]) { array[i] = counter; } } position++; } counter++; } for (int i = 0; i < length; i++) { System.out.print(array[i] + " "); } System.out.println(); } } }
import java.util.*; public class D{ private static Scanner scanner = new Scanner(System.in); public static void main(String[] args){ int q = scanner.nextInt(); while(q-- > 0){ int n = scanner.nextInt(), k = scanner.nextInt(); int[] a = new int[k]; for(int i=0;i<k;i++){ a[i] = scanner.nextInt(); } int[] t = new int[k]; for(int j=0;j<k;j++){ t[j] = scanner.nextInt(); } long[] L = new long[n]; long[] R = new long[n]; for(int i=0;i<n;i++){ L[i] = Integer.MAX_VALUE; R[i] = Integer.MAX_VALUE; } for(int i=0;i<k;i++){ L[a[i]-1] = t[i]; R[a[i]-1] = t[i]; } long min = Integer.MAX_VALUE; for(int i=0;i<n;i++){ L[i] = Math.min(min+1,L[i]); min = L[i]; } min = Integer.MAX_VALUE; for(int i=n-1;i>=0;i--){ R[i] = Math.min(min+1,R[i]); min = R[i]; } for(int i=0;i<n;i++){ System.out.print(Math.min(L[i],R[i]) + " "); } System.out.println(); } } }
0
90f01508
e00b1794
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 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<>(); 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; } } }
0
16857116
f3d7ce08
import javax.swing.plaf.IconUIResource; import java.lang.reflect.Array; import java.text.CollationElementIterator; import java.util.*; import java.io.*; //Timus judge id- 323935JJ public class Main { //---------------------------------------------------------------------------------------------- public static int mod = (int) (1e9 + 7); static int ans = Integer.MAX_VALUE; public static void main(String hi[]) throws Exception { FastReader sc = new FastReader(); int t =sc.nextInt(); while(t-->0) { int n =sc.nextInt(); String a = sc.nextLine(),b=sc.nextLine(); int count1=0,count2=0,count3=0,count4=0; for(int i=0;i<n;i++) { if(a.charAt(i)=='0'&&b.charAt(i)=='0') count1++; else if(a.charAt(i)=='1'&&b.charAt(i)=='1') count2++; else if(a.charAt(i)=='1'&&b.charAt(i)=='0') count3++; else if(a.charAt(i)=='0'&&b.charAt(i)=='1') count4++; } int ans=Integer.MAX_VALUE; if(count3==count4) ans=Math.min(count3*2,ans); if(count2==count1+1) ans=Math.min(ans,2*count1+1); if(count2==1&&count1==0) ans=1; if(count3==0&&count4==0) ans=0; if(ans==Integer.MAX_VALUE) System.out.println(-1); else System.out.println(ans); } } // method to return LCM of two numbers // method to return LCM of two numbers }
import javax.swing.plaf.IconUIResource; import java.lang.reflect.Array; import java.text.CollationElementIterator; import java.util.*; import java.io.*; //Timus judge id- 323935JJ public class Main { //---------------------------------------------------------------------------------------------- public static int mod = (int) (1e9 + 7); static int ans = Integer.MAX_VALUE; public static void main(String hi[]) throws Exception { FastReader sc = new FastReader(); int t =sc.nextInt(); while(t-->0) { int n =sc.nextInt(); String a = sc.nextLine(),b=sc.nextLine(); int count1=0,count2=0,count3=0,count4=0; for(int i=0;i<n;i++) { if(a.charAt(i)=='0'&&b.charAt(i)=='0') count1++; else if(a.charAt(i)=='1'&&b.charAt(i)=='1') count2++; else if(a.charAt(i)=='1'&&b.charAt(i)=='0') count3++; else if(a.charAt(i)=='0'&&b.charAt(i)=='1') count4++; } int ans=Integer.MAX_VALUE; if(count3==count4) ans=Math.min(count3*2,ans); if(count2==count1+1) ans=Math.min(ans,2*count1+1); if(ans==Integer.MAX_VALUE) System.out.println(-1); else System.out.println(ans); } } // method to return LCM of two numbers // method to return LCM of two numbers }
1
51151974
c23278ec
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 }
import java.io.*; import java.sql.Array; import java.util.*; public class P1551C { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } static class Task { public void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); String[] s = new String[n]; int[][] freq = new int[n][6]; for (int i = 0; i < n; i++) { s[i] = in.nextLine(); for (char c : s[i].toCharArray()) { freq[i][c - 'a']++; freq[i][5]++; } } int totmax = 0; for (int i = 0; i < 5; i++) { List<Integer> diffs = new ArrayList<>(n); for (int j = 0; j < n; j++) { diffs.add(2 * freq[j][i] - freq[j][5]); } Collections.sort(diffs); Collections.reverse(diffs); int curr = 0, cnt = 0; while (cnt < diffs.size() && curr + diffs.get(cnt) > 0) { curr += diffs.get(cnt++); } totmax = Math.max(totmax, cnt); } out.println(totmax); } } } }
0
00f79486
ce0b2178
import java.util.*; import java.io.*; public class Main { PrintWriter out; FastReader sc; StringBuilder sb; /****************************************************************************************** *****************************************************************************************/ public void sol() { int n=ni(); int[] ar=new int[n]; for(int i=0;i<n;i++)ar[i]=ni(); char[] s=rl(); PriorityQueue<Integer> red=new PriorityQueue<>(Collections.reverseOrder()),blue=new PriorityQueue<>(); for(int i=0;i<n;i++) { if(s[i]=='R')red.add(ar[i]); else blue.add(ar[i]); } int a=n; while(red.size()>0) { if(red.poll()>a) { no();return; }a--; }a=1; while(blue.size()>0) { if(blue.poll()<a) { no();return; }a++; }yes(); } public static void main(String[] args) { Main g=new Main(); g.out=new PrintWriter(System.out); g.sc=new FastReader(); int t=1; t=g.ni(); while(t-->0) g.sol(); g.out.flush(); } /**************************************************************************************** *****************************************************************************************/ public int ni(){ return sc.nextInt(); }public char[] rl(){ return sc.nextLine().toCharArray(); } public void pl(Object s){ out.println(s); } public String next(){ return sc.next(); } void yes(){ pl("YES"); }void no(){ pl("NO"); } }
import java.io.*; import java.util.*; public class D { static class Pair implements Comparable<Pair>{ int a; char b; public Pair(int a, char b) { this.a = a; this.b = 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(); int arr[] = scan.readArray(n); char line[] = scan.next().toCharArray(); ArrayList<Pair> pairs = new ArrayList<Pair>(); for(int i = 0;i<n;i++) { pairs.add(new Pair(arr[i], line[i])); } Collections.sort(pairs); boolean possible = true; for(int i = 1;i<=n;i++) { if(pairs.get(i-1).a == i) { continue; } else if(pairs.get(i-1).a < i && pairs.get(i-1).b == 'R') { continue; } else if(pairs.get(i-1).a > i && pairs.get(i-1).b == 'B') { continue; } else { possible = false; break; } } output.println(possible == true ? "YES" : "NO"); } 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; } }
0
976fe834
da54dec3
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.*; /* -> Give your 100%, that's it! -> Feynman Rule To Solve Any Problem: 1. Read the problem. 2. Think About It. 3. Solve it! */ public class Template { static int mod = 1000000007; public static void main(String[] args){ FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int yo = sc.nextInt(); while (yo-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); int[][] arr = new int[n][m]; for(int i = 0; i < n; i++){ String s = sc.next(); for(int j = 0; j < m; j++){ arr[i][j] = s.charAt(j)-'0'; } } List<String> list = new ArrayList<>(); for(int i = 0; i < n-1; i++){ for(int j = 0; j < m-1; j++){ check(list,i,j,n,m,arr); } } // for(int i = 0; i < n; i++){ // for(int j = 0; j < m; j++){ // out.print(arr[i][j] + " "); // } // out.println(); // } out.println(list.size()/3); for(int i = 0; i < list.size(); i+=3){ out.println(list.get(i) + " " + list.get(i+1) + " " + list.get(i+2)); } // out.println(list.size()); // for(int i = 0; i < list.size(); i++){ // out.println(list.get(i)); // } } out.close(); } static void check(List<String> list, int i, int j, int n, int m, int[][] arr){ int[][] a = new int[2][2]; a[0][0] = arr[i][j]; a[0][1] = arr[i][j+1]; a[1][0] = arr[i+1][j]; a[1][1] = arr[i+1][j+1]; int ones = 0; for(int x = 0; x < 2; x++){ for(int y = 0; y < 2; y++){ if(a[x][y] == 1) ones++; } } if(ones == 0) return; if(ones == 1){ ones(i,j,list,a); twos(i,j,list,a); threes(i,j,list,a); } else if(ones == 2){ twos(i,j,list,a); threes(i,j,list,a); } else if(ones == 3){ threes(i,j,list,a); } else if(ones == 4){ fours(i,j,list,a); // for(int x = 0; x < 2; x++){ // for(int y = 0; y < 2; y++){ // out.print(a[x][y] + " "); // } // out.println(); // } ones(i,j,list,a); // for(int x = 0; x < 2; x++){ // for(int y = 0; y < 2; y++){ // out.print(a[x][y] + " "); // } // out.println(); // } twos(i,j,list,a); // for(int x = 0; x < 2; x++){ // for(int y = 0; y < 2; y++){ // out.print(a[x][y] + " "); // } // out.println(); // } threes(i,j,list,a); // for(int x = 0; x < 2; x++){ // for(int y = 0; y < 2; y++){ // out.print(a[x][y] + " "); // } // out.println(); // } } arr[i][j] = a[0][0]; arr[i+1][j] = a[1][0]; arr[i][j+1] = a[0][1]; arr[i+1][j+1] = a[1][1]; } public static void twos(int i, int j, List<String> list, int[][] a){ int count = 0; for(int x = 0; x < 2; x++){ for(int y = 0; y < 2; y++){ if(a[x][y] == 1 && count < 1) { count++; a[x][y] = 0; list.add(get(x,y,i,j)); } else if(a[x][y] == 0){ list.add(get(x,y,i,j)); a[x][y] = 1; } } } } public static void threes(int i, int j, List<String> list, int[][] a){ int count = 0; for(int x = 0; x < 2; x++){ for(int y = 0; y < 2; y++){ if(a[x][y] == 1) { a[x][y] = 0; list.add(get(x,y,i,j)); } } } } public static void fours(int i, int j, List<String> list, int[][] a){ int count = 0; for(int x = 0; x < 2; x++){ for(int y = 0; y < 2; y++){ if(a[x][y] == 1 && count < 3) { count++; a[x][y] = 0; list.add(get(x,y,i,j)); } } } } public static void ones(int i, int j, List<String> list, int[][] a){ int count = 0; for(int x = 0; x < 2; x++){ for(int y = 0; y < 2; y++){ if(a[x][y] == 1) { a[x][y] = 0; list.add(get(x,y,i,j)); } else if(a[x][y] == 0 && count < 2){ a[x][y] = 1; count++; list.add(get(x,y,i,j)); } } } } public static String get(int x, int y, int i, int j){ if(x == 0 && y == 0) { return (i+1) + " " + (j+1); } else if(x == 0 && y == 1){ return (i+1) + " " + (j+2); } else if(x == 1 && y == 0){ return (i+2) + " " + (j+1); } else { return (i+2) + " " + (j+2); } } /* Random stuff to try when stuck: -if it's 2C then it's dp -for combo/probability problems, expand the given form we're interested in -make everything the same then build an answer (constructive, make everything 0 then do something) -something appears in parts of 2 --> model as graph -assume a greedy then try to show why it works -find way to simplify into one variable if multiple exist -treat it like fmc (note any passing thoughts/algo that could be used so you can revisit them) -find lower and upper bounds on answer -figure out what ur trying to find and isolate it -see what observations you have and come up with more continuations -work backwards (in constructive, go from the goal to the start) -turn into prefix/suffix sum argument (often works if problem revolves around adjacent array elements) -instead of solving for answer, try solving for complement (ex, find n-(min) instead of max) -draw something -simulate a process -dont implement something unless if ur fairly confident its correct -after 3 bad submissions move on to next problem if applicable -do something instead of nothing and stay organized -write stuff down Random stuff to check when wa: -if code is way too long/cancer then reassess -switched N/M -int overflow -switched variables -wrong MOD -hardcoded edge case incorrectly Random stuff to check when tle: -continue instead of break -condition in for/while loop bad Random stuff to check when rte: -switched N/M -long to int/int overflow -division by 0 -edge case for empty list/data structure/N=1 */ // For Input.txt and Output.txt // FileInputStream in = new FileInputStream("input.txt"); // FileOutputStream out = new FileOutputStream("output.txt"); // PrintWriter pw = new PrintWriter(out); // Scanner sc = new Scanner(in); }
import java.util.*; import java.math.*; public class Solution { final static int MOD = 1000000007; public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = Integer.parseInt(in.next()); for(int tt = 0; tt < t; tt++) { int n = Integer.parseInt(in.next()); int m = Integer.parseInt(in.next()); char[][] grid = new char[n][m]; List<Integer> ans = new ArrayList<>(); for(int i = 0; i < n; i++) { grid[i] = in.next().toCharArray(); } int iii = 0; int jjj = 0; if(n % 2 != 0) { iii = 1; for(int j = 0; j + 1 < m; j++) { if(grid[0][j] == '1') { grid[0][j] = '0'; grid[1][j] = (grid[1][j] == '1') ? '0' : '1'; grid[1][j + 1] = (grid[1][j + 1] == '1') ? '0' : '1'; ans.add(1); ans.add(j + 1); ans.add(2); ans.add(j + 1); ans.add(2); ans.add(j + 2); } if(grid[0][j + 1] == '1') { grid[0][j + 1] = '0'; grid[1][j] = (grid[1][j] == '1') ? '0' : '1'; grid[1][j + 1] = (grid[1][j + 1] == '1') ? '0' : '1'; ans.add(1); ans.add(j + 2); ans.add(2); ans.add(j + 1); ans.add(2); ans.add(j + 2); } } } if(m % 2 != 0) { jjj = 1; for(int i = (n % 2 == 0) ? 0 : 1; i + 1 < n; i++) { if(grid[i][0] == '1') { grid[i][0] = '0'; grid[i][1] = (grid[i][1] == '1') ? '0' : '1'; grid[i + 1][1] = (grid[i + 1][1] == '1') ? '0' : '1'; ans.add(i + 1); ans.add(1); ans.add(i + 1); ans.add(2); ans.add(i + 2); ans.add(2); } if(grid[i + 1][0] == '1') { grid[i + 1][0] = '0'; grid[i][1] = (grid[i][1] == '1') ? '0' : '1'; grid[i + 1][1] = (grid[i + 1][1] == '1') ? '0' : '1'; ans.add(i + 2); ans.add(1); ans.add(i + 1); ans.add(2); ans.add(i + 2); ans.add(2); } } } for(int i = iii; i < n; i += 2) { for(int j = jjj; j < m; j += 2) { int cnt = count(grid, i, j); while(cnt > 0) { List<G> g = new ArrayList<>(); for(int k = i; k < i + 2; k++) { for(int l = j; l < j + 2; l++) { g.add(new G(grid[k][l], k, l)); } } Collections.sort(g); int l = 0; int r = 0; if(cnt == 1 || cnt == 3) { l = 1; r = 3; }else { l = 0; r = 2; } for(int k = l; k <= r; k++) { int ii = g.get(k).i; int jj = g.get(k).j; grid[ii][jj] = (grid[ii][jj] == '1') ? '0' : '1'; ans.add(ii + 1); ans.add(jj + 1); } cnt = count(grid, i, j); } } } System.out.println(ans.size() / 6); for(int i = 0; i < ans.size(); i += 6) { for(int j = i; j < i + 6; j++) { System.out.print(ans.get(j) + " " ); } System.out.println(); } } } public static int count(char[][] grid, int i, int j) { int cnt = 0; for(int k = i; k < i + 2; k++) { for(int l = j; l < j + 2; l++) { if(grid[k][l] == '1') cnt += 1; } } return cnt; } } class G implements Comparable <G> { Character val; int i; int j; public G(char val, int i, int j) { this.val = val; this.i = i; this.j = j; } }
0
8d6f1bf5
e90a198b
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--; } } }
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class Template { static int mod = 1000000007; public static void main(String[] args){ FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int yo = sc.nextInt(); while (yo-- > 0) { int n = sc.nextInt(); String s = sc.next(); String t = sc.next(); int op1 = cal(s,t,n); int op2 = helper(s,t,n,'0'); int op3 = helper(s,t,n,'1'); int ans = min(min(op1,op2),op3); if(ans == Integer.MAX_VALUE){ out.println("-1"); } else { out.println(ans); } } out.close(); } static int helper(String str1, String str2, int n, int ch){ char[] s = str1.toCharArray(); char[] t = str2.toCharArray(); int idx = -1; for(int i = 0; i < n; i++){ if(s[i] == '1' && t[i] == ch){ idx = i; break; } } if(idx == -1){ return Integer.MAX_VALUE; } for(int i = 0; i < n; i++){ if(i == idx) continue; if(s[i] == '1') s[i] = '0'; else s[i] = '1'; } int ans = cal(String.valueOf(s),String.valueOf(t),n); if(ans == Integer.MAX_VALUE) return ans; return 1 + ans; } static int cal(String s, String t, int n){ int op01 = 0; int op10 = 0; for(int i = 0; i < n; i++){ if(s.charAt(i) != t.charAt(i)){ if(s.charAt(i) == '1') op10++; else op01++; } } if(op10 != op01){ return Integer.MAX_VALUE; } return op01 + op10; } /* Source: hu_tao Random stuff to try when stuck: - use bruteforcer - check for n = 1, n = 2, so on -if it's 2C then it's dp -for combo/probability problems, expand the given form we're interested in -make everything the same then build an answer (constructive, make everything 0 then do something) -something appears in parts of 2 --> model as graph -assume a greedy then try to show why it works -find way to simplify into one variable if multiple exist -treat it like fmc (note any passing thoughts/algo that could be used so you can revisit them) -find lower and upper bounds on answer -figure out what ur trying to find and isolate it -see what observations you have and come up with more continuations -work backwards (in constructive, go from the goal to the start) -turn into prefix/suffix sum argument (often works if problem revolves around adjacent array elements) -instead of solving for answer, try solving for complement (ex, find n-(min) instead of max) -draw something -simulate a process -dont implement something unless if ur fairly confident its correct -after 3 bad submissions move on to next problem if applicable -do something instead of nothing and stay organized -write stuff down Random stuff to check when wa: -if code is way too long/cancer then reassess -switched N/M -int overflow -switched variables -wrong MOD -hardcoded edge case incorrectly Random stuff to check when tle: -continue instead of break -condition in for/while loop bad Random stuff to check when rte: -switched N/M -long to int/int overflow -division by 0 -edge case for empty list/data structure/N=1 */ // For Input.txt and Output.txt // FileInputStream in = new FileInputStream("input.txt"); // FileOutputStream out = new FileOutputStream("output.txt"); // PrintWriter pw = new PrintWriter(out); // Scanner sc = new Scanner(in); }
0
9291ca83
d61f51c5
import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; public class Practice { static HashMap<String, Integer> map = new HashMap<>(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-->0) { int n = sc.nextInt(); int[][] occurances = new int[5][n]; for(int i=0;i<n;i++){ String s = sc.next(); int[] count = new int[5]; int len = s.length(); for(int j=0;j<s.length();j++){ count[s.charAt(j)-'a']++; } for(int j=0;j<5;j++){ occurances[j][i] = count[j] - (len-count[j]); } } int ans = 0; for(int i=0;i<5;i++){ Arrays.sort(occurances[i]); int tmpAns = 0; int tmpSum=0; for(int j=n-1;j>=0;j--){ tmpSum+=occurances[i][j]; if(tmpSum>0) tmpAns++; else break; } ans = Math.max(ans, tmpAns); } System.out.println(ans); } } }
import java.util.*; 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; } } }
0
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; 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(); } }
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; //Count the number of coprime's upto N // Returns nCr % p using Fermat's little theorem. //Modular Operations for Addition and Multiplication. /* * * >= <= 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 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
0fd5b95a
6490bbe8
import java.io.PrintWriter; import java.util.*; public class codeforces { static int dp[][]=new int[5001][5001]; public static void main(String[] args) { Scanner s=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int t=1; for(int tt=0;tt<t;tt++) { int n=s.nextInt(); int a[]=new int[n]; ArrayList<Integer> z=new ArrayList<>(); ArrayList<Integer> o=new ArrayList<>(); for(int i=0;i<n;i++) { a[i]=s.nextInt(); if(a[i]==1) { o.add(i); }else { z.add(i); } } for(int i=0;i<5001;i++) { Arrays.fill(dp[i], -1); } System.out.println(sol(0,0,z,o)); } out.close(); s.close(); } public static int sol(int i,int j,ArrayList<Integer> z,ArrayList<Integer> o) { if(j==o.size()) { return 0; } int h=z.size()-i; int l=o.size()-j; if(i==z.size()) { return 10000000; } if(dp[i][j]!=-1) { //System.out.println(i+" "+j); return dp[i][j]; } int ans1=sol(i+1,j,z,o); int ans2=sol(i+1,j+1,z,o)+Math.abs(z.get(i)-o.get(j)); dp[i][j]=Math.min(ans1, ans2); return dp[i][j]; } }
import java.util.*; // import java.lang.*; import java.io.*; // THIS TEMPLATE MADE BY AKSH BANSAL. public class Solution { private static boolean[] isPrime; // static ArrayList<Integer>[] adj; // static void getAdj(int n,int q, FastReader sc){ // adj = new ArrayList[n+1]; // for(int i=1;i<=n;i++){ // adj[i] = new ArrayList<>(); // } // for(int i=0;i<q;i++){ // int a = sc.nextInt(); // int b = sc.nextInt(); // adj[a].add(b); // adj[b].add(a); // } // } public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); // primes(); // ________________________________ // int t = sc.nextInt(); // StringBuilder output = new StringBuilder(); // while (t-- > 0) { // output.append(solver()).append("\n"); // } // out.println(output); // _______________________________ int n = sc.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); } out.println(solver(n, arr)); // ________________________________ out.flush(); } public static long solver(int n, int[] arr) { ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); for(int i=0;i<n;i++){ if(arr[i] ==1){ a.add(i); } else{ b.add(i); } } // System.out.println("__"+ a); // System.out.println("__"+ b); long inf = (long)1e10; int aLen = a.size(), bLen = b.size(); long[][] dp = new long[bLen+1][aLen+1]; for(int i=0;i<bLen+1;i++)Arrays.fill(dp[i],inf); // dp[0][0] = 0; for(int i=0;i<=bLen;i++){ dp[i][0] = 0; } for(int i=1;i<=bLen;i++){ for(int j=1;j<=i && j<=aLen;j++){ int aa = a.get(j-1); int bb = b.get(i-1); // System.out.println((i-1)+" "+(j-1)+"__"+ aa+" "+bb); dp[i][j] = Math.min( Math.abs(aa-bb)+dp[i-1][j-1], dp[i-1][j] ); // System.out.println((i-1)+" "+(j-1)+"__"+ dp[i][j]); } } // for(int i=0;i<=bLen;i++){ // for(int j=0;j<=aLen;j++){ // System.out.print(dp[i][j]+" "); // } // System.out.println("__" ); // } return dp[bLen][aLen]==inf?0:dp[bLen][aLen]; } }
0
8d9871a9
eb6cfca7
import java.util.*; public class Main { static Scanner scan = new Scanner(System.in); static int[] readArray(int[] x) { for(int i=0; i<x.length; ++i) x[i] = scan.nextInt(); return x; } static long[] readArray(long[] x) { for(int i=0; i<x.length; ++i) x[i] = scan.nextLong(); return x; } public static void main(String[] args) { int t = scan.nextInt(); for(int it=0; it<t; ++it) { int n = scan.nextInt(); long[] aa = readArray(new long[n]); long minEven = aa[0]; long minOdd = aa[1]; long sum = aa[0]+aa[1]; long best = n*minEven + n*minOdd; int numOdd = 1; int numEven = 1; for(int i=2; i<n; ++i) { if(i%2 == 0) { minEven = Math.min(aa[i], minEven); numEven++; }else { minOdd = Math.min(aa[i], minOdd); numOdd++; } sum += aa[i]; long score = sum; score += minEven*(n-numEven); score += minOdd*(n-numOdd); best = Math.min(best, score); } System.out.println(best); } } }
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.List; import java.util.Map.Entry; import java.util.TreeMap; public class Main { public Main() throws FileNotFoundException { // File file = Paths.get("input.txt").toFile(); // if (file.exists()) { // System.setIn(new FileInputStream(file)); // } long t = System.currentTimeMillis(); InputReader reader = new InputReader(); int ttt = reader.nextInt(); for (int tt = 0; tt < ttt; tt++) { int n=reader.nextInt(); long[] s=new long[n]; for(int i=0;i<n;i++) { s[i]=reader.nextLong(); } long smallest1=s[0]; long smallest2=s[1]; long val=n*s[0]+n*s[1]; int left1=n-1; int left2=n-1; long base=s[0]+s[1]; for(int i=2;i<n;i++) { if(i%2==0) { //left1 val=Math.min(val, base+left2*smallest2+left1*s[i]); base+=s[i]; smallest1=Math.min(smallest1, s[i]); left1--; }else { val=Math.min(val, base+left1*smallest1+left2*s[i]); base+=s[i]; smallest2=Math.min(smallest2, s[i]); left2--; //left2 } } System.out.println(val); } } public static void main(String[] args) throws FileNotFoundException { new Main(); } }
0
8c591975
b6724dd9
import java.io.DataInputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class Main { private static void run() throws IOException { int n, k; n = in.nextInt(); k = in.nextInt(); int[] a = new int[n]; long[] t = new long[k]; for (int i = 0; i < n; i++) { a[i] = -1; } for (int i = 0; i < k; i++) { a[in.nextInt() - 1] = i; } for (int i = 0; i < k; i++) { t[i] = in.nextLong(); } long[] dp = new long[n]; long now; now = 2000000000; for (int i = 0; i < n; i++) { now++; if (a[i] != -1) { now = Math.min(now, t[a[i]]); } dp[i] = now; } now = 2000000000; for (int i = n - 1; i >= 0; i--) { now++; if (a[i] != -1) { now = Math.min(now, t[a[i]]); } dp[i] = Math.min(dp[i], now); } for (int i = 0; i < n; i++) { out.printf("%d ", dp[i]); } out.println(); } 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(); } static final long mod = 1000000007; @SuppressWarnings("FieldCanBeLocal") private static Reader in; private static PrintWriter out; }
import java.io.*; import java.util.*; public class AirConditioners { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub // System.in and System.out are input and output streams, respectively. InputStream inputStream = System.in; InputReader in = new InputReader(inputStream); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t = in.nextInt(); while (t-- != 0) { int n = in.nextInt(); int k = in.nextInt(); HashMap<Integer, Integer> hm = new HashMap<>(); int[] ac = in.readArray(k); for (int idx = 0; idx < ac.length; idx++) { hm.put(ac[idx], in.nextInt()); } Arrays.sort(ac); long[] lmin = new long[n]; long val = (long) Integer.MAX_VALUE; int acdx = 0; for (int idx = 0; idx < lmin.length; idx++) { if (acdx < ac.length && ac[acdx] - 1 == idx) { val = (long) Math.min(val, hm.get(ac[acdx])); acdx++; } lmin[idx] = val; val++; } acdx = ac.length - 1; val = Integer.MAX_VALUE; for (int idx = lmin.length-1; idx >= 0; idx--) { if (acdx >= 0 && ac[acdx] - 1 == idx) { val = (long) Math.min(val, hm.get(ac[acdx])); acdx--; } lmin[idx] = Math.min(lmin[idx], val); val++; } for (long x : lmin) { out.write(x + " "); } out.newLine(); } out.close(); } }
0
11373c16
7011024d
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { 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 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
3c667d4f
a7894e0b
import java.util.*; public class j { public static void main(String args[]) { Scanner in=new Scanner(System.in); int n=in.nextInt(); while(n-->0) { int len=in.nextInt(); int t=in.nextInt(); int pos[]=new int[t]; int temp[]=new int[t]; for(int i=0;i<t;i++) pos[i]=in.nextInt(); for(int i=0;i<t;i++) temp[i]=in.nextInt(); long range[]=new long[len]; Arrays.fill(range,Long.MAX_VALUE-10000); for(int i=0;i<t;i++) range[pos[i]-1]=temp[i]; for(int i=1;i<len;i++) { range[i]=Math.min(range[i],1+range[i-1]); } for(int i=len-2;i>=0;i--) { range[i]=Math.min(range[i+1]+1,range[i]); } for(int i=0;i<len;i++) { System.out.print(range[i]+" "); }System.out.println(); } } }
import java.io.*; import java.math.*; import java.util.*; public class test { public static void main(String args[]) throws Exception { Scanner sc = new Scanner(); StringBuilder res = new StringBuilder(); int tc = sc.nextInt(); while(tc-->0) { int n=sc.nextInt(); int k=sc.nextInt(); int[] ac=new int[k]; long[] ans=new long[n]; Arrays.fill(ans, Integer.MAX_VALUE/2); for(int i=0;i<k;i++) { ac[i]=sc.nextInt()-1; } for(int i=0;i<k;i++) { long x = sc.nextLong(); ans[ac[i]] = x; } for(int i=1;i<n;i++) { ans[i]=Math.min(ans[i], ans[i-1]+1); } for(int i=n-2;i>=0;i--) { ans[i]=Math.min(ans[i], ans[i+1]+1); } for(int i=0;i<n;i++) { res.append(ans[i]+" "); } res.append("\n"); } System.out.println(res); } }
1
2fdcf0ad
d20e861b
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main { static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static int[][] map; static int n, m; static ArrayList<String> actions = new ArrayList<>(); static String[] move = new String[]{"", "0110", "1001", "1101", "1001", "1110", "1011", "0000", "0110", "0111", "1101", "0000", "0111", "0000", "0000", "0001"}; static int[] xdif = new int[]{0, 0, 1, 1}, ydif = new int[]{0, 1, 0, 1}; public static void main(String[] args) throws IOException { int tests = sc.nextInt(); for (int t = 0; t < tests; t++) solve(); pw.close(); } static void solve() throws IOException { n = sc.nextInt(); m = sc.nextInt(); map = new int[n][m]; actions.clear(); for (int i = 0; i < n; i++) { String line = sc.next(); for (int j = 0; j < m; j++) map[i][j] = line.charAt(j) - '0'; } if (n % 2 + m % 2 == 2) removeCorner(); if (n % 2 == 1) for (int y = 0; y + 1 < m; y += 2) removeDown(y); if (m % 2 == 1) for (int x = 0; x + 1 < n; x += 2) removeRight(x); for (int x = 0; x + 1 < n; x += 2) for (int y = 0; y + 1 < m; y += 2) square(x, y); pw.println(actions.size()); for (String action : actions) pw.println(action); } static void square(int x, int y) { String type = "" + map[x][y] + map[x][y + 1] + map[x + 1][y] + map[x + 1][y + 1]; if (map[x][y] + map[x + 1][y] + map[x][y + 1] + map[x + 1][y + 1] > 0) { int[][] poses = new int[3][]; int index = 0; for (int t = 0; t < 4; t++) { if (map[x + xdif[t]][y + ydif[t]] != move[Integer.parseInt(type, 2)].charAt(t) - '0') poses[index++] = new int[]{x + xdif[t], y + ydif[t]}; } action(poses[0][0], poses[0][1], poses[1][0], poses[1][1], poses[2][0], poses[2][1]); square(x, y); } } static void invert(int x, int y) { map[x][y] = 1 - map[x][y]; } static void removeCorner() { if (map[n - 1][m - 1] == 1) action(n - 1, m - 1, n - 2, m - 1, n - 1, m - 2); } static void removeRight(int x) { int y = m - 1; if (map[x][y] + map[x + 1][y] == 2) action(x, y, x + 1, y, x, y - 1); else if (map[x][y] == 1) action(x, y, x, y - 1, x + 1, y - 1); else if (map[x + 1][y] == 1) action(x + 1, y, x, y - 1, x + 1, y - 1); } static void removeDown(int y) { int x = n - 1; if (map[x][y] + map[x][y + 1] == 2) action(x, y, x, y + 1, x - 1, y); else if (map[x][y] == 1) action(x, y, x - 1, y, x - 1, y + 1); else if (map[x][y + 1] == 1) action(x, y + 1, x - 1, y, x - 1, y + 1); } static void action(int x1, int y1, int x2, int y2, int x3, int y3) { actions.add((x1 + 1) + " " + (y1 + 1) + " " + (x2 + 1) + " " + (y2 + 1) + " " + (x3 + 1) + " " + (y3 + 1)); invert(x1, y1); invert(x2, y2); invert(x3, y3); } }
import java.util.*; public class CodeForces{ // 4 // -3 -5 -2 1 // k 2 4 0 3 static ArrayList<int[]> ans = new ArrayList<>(); public static void add(int a,int b,int c,int d,int e,int f){ int[] tem=new int[6]; tem[0]=a;tem[1]=b;tem[2]=c;tem[3]=d;tem[4]=e;tem[5]=f; ans.add(tem); } public static void helper(int[][] arr,int r,int c){ for(int k=0;k<12;k++){ if(arr[r][c]==1){ add(r,c,r+1,c,r,c+1); arr[r][c]=1-arr[r][c]; arr[r+1][c]=1-arr[r+1][c]; arr[r][c+1]=1-arr[r][c+1]; } if(arr[r][c+1]==1){ add(r,c+1,r+1,c+1,r,c); arr[r][c+1]=1-arr[r][c+1]; arr[r+1][c+1]=1-arr[r+1][c+1]; arr[r][c]=1-arr[r][c]; } if(arr[r+1][c]==1){ add(r+1,c+1,r,c,r+1,c); arr[r+1][c+1]=1-arr[r+1][c+1]; arr[r][c]=1-arr[r][c]; arr[r+1][c]=1-arr[r+1][c]; } if(arr[r+1][c+1]==1){ add(r+1,c+1,r+1,c,r,c+1); arr[r+1][c+1]=1-arr[r+1][c+1]; arr[r+1][c]=1-arr[r+1][c]; arr[r][c+1]=1-arr[r][c+1]; } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int m = sc.nextInt(); ans.clear(); int[][] arr=new int[n][m]; for(int i=0;i<n;i++){ String str = sc.next(); for(int j=0;j<m;j++){ arr[i][j]=str.charAt(j)-'0'; } } for(int i=0;i<n;i+=2){ for(int j=0;j<m;j+=2){ if(i==n-1&&j==m-1){ helper(arr,i-1,j-1); }else if(i==n-1){ helper(arr,i-1,j); }else if(j==m-1){ helper(arr,i,j-1); }else{ helper(arr,i,j); } } } System.out.println(ans.size()); for(int[] move:ans){ for(int i=0;i<6;i++){ System.out.print(move[i]+1 +" "); } System.out.println(); } // for(int i=0;i<n;i++){ // for(int j=0;j<m;j++){ // System.out.print(arr[i][j] +" "); // } // System.out.println(); // } } } }
0
6018974f
bdf7bfb2
import java.util.*; public class contestC { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-->0){ int n = scanner.nextInt(); ArrayList<String> list = new ArrayList<>(); for(int i=0;i<n;++i){ String s = scanner.next(); list.add(s); } int ans = 0; for(char a='a';a<='e';++a ){ ArrayList<Integer> num = new ArrayList<>(); for(String s : list){ int cnt = 0; for(int i=0;i<s.length();++i){ if(s.charAt(i)==a) cnt++; else cnt--; } num.add(cnt); } Collections.sort(num,Collections.reverseOrder()); int sum = 0; for(int i=0;i<num.size();++i){ sum+=num.get(i); if(sum<=0){ ans = Math.max(ans,i); break; } } if(sum > 0) { ans = n; break; } } System.out.println(ans); } } }
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(); } }
0
829d2024
9852706b
import java.util.*; import java.io.*; public class Main{ public static class Element implements Comparable<Element>{ public int key; public int value; Element(int k, int v) { key=k; value=v; } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int m=sc.nextInt(); int x=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;++i) arr[i]=sc.nextInt(); PriorityQueue<Element> pq=new PriorityQueue<>(); for(int i=1;i<=m;++i) { pq.add(new Element(i,0)); } System.out.println("YES"); for(int j=0;j<n;j++) { Element cur = pq.poll(); System.out.print(cur.key+" "); cur.value+= arr[j]; pq.add(cur); } System.out.println(); } } }
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static int modulo=998244353; public static void main(String[] args) { FastScanner in = new FastScanner(); int test=in.nextInt(); while(test-->0){ int n=in.nextInt(); int m=in.nextInt(); int x=in.nextInt(); PriorityQueue<pair> pq=new PriorityQueue<>(); int arr[]=new int[n]; for(int i=1;i<=m;i++){ pq.add(new pair(i,0)); } System.out.println("YES"); for(int i=0;i<n;i++){ pair p=pq.poll(); int a=in.nextInt(); p.y+=a; pq.add(p); System.out.print(p.x+" "); } System.out.println(); } // int sum=in.nextInt(); // // boolean dp[][]=new boolean[n+1][sum+1]; // // for(int i=0;i<=n;i++){ // for(int j=0;j<=sum;j++){ // if(i==0) // dp[i][j]=false; // if(j==0) // dp[i][j]=true; // // } // } // // for(int i=1;i<=n;i++){ // for(int j=1;j<=sum;j++){ // if(arr[i-1]<=j) // dp[i][j]=(dp[i-1][j])||(dp[i-1][j-arr[i-1]]); // else // dp[i][j]=dp[i-1][j]; // // } // } // boolean flag=false; // for(int i=0;i<=n;i++){ // if(dp[i][sum]==true){ // flag=true; // System.out.println(1); // break; // } // // } // if(!flag) // System.out.println(0); } } class pair implements Comparable<pair> { int x; int y; pair(int x, int y) { this.x = x; this.y = y; } }
1
0b04b41e
4da08761
import java.io.BufferedReader; import java.io.*; import java.util.*; public class josph { static BufferedReader br; // 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=Math.max(max,cnt); } System.out.println(max); } } public static String cin() throws Exception { return br.readLine(); } public static Integer cinI() throws Exception { return Integer.parseInt(br.readLine()); } }
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Solution { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Main solver = new Main(); boolean multipleTC = true; int testCount = multipleTC ? Integer.parseInt(in.next()) : 1; for (int i = 1; i <= testCount; i++) solver.solve(in, out, i); out.close(); } static class Main { PrintWriter out; InputReader in; public void solve(InputReader in, PrintWriter out, int test) { this.out = out; this.in = in; int n = ni(); String[] arr = new String[n]; int[][] freq = new int[n][5]; int[][] rem = new int[n][5]; for(int i = 0; i < n; i++){ arr[i] = n(); for(int j = 0; j < arr[i].length(); j++) freq[i][arr[i].charAt(j) - 'a']++; for(int j = 0; j < 5; j++) rem[i][j] = arr[i].length() - freq[i][j]; } int ans = 0; for(int i = 0; i < 5; i++){ int[] vals = new int[n]; for(int j = 0; j < n; j++) vals[j] = freq[j][i] - rem[j][i]; Arrays.sort(vals); int sum = 0, x = 0; for(int j = n - 1; j >= 0; j--){ if(sum + vals[j] > 0){ x++; sum += vals[j]; } else { break; } } if(x > ans) { ans = x; } } System.out.println(ans); } String n(){ return in.next(); } int ni() { return in.nextInt(); } } }
0
94b3b86d
efe594c3
import java.io.*; import java.util.*; public class D { public static ArrayList<Integer> adj[]; public static int node; public static int dist = 0; public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); int T = sc.nextInt(); PrintWriter out = new PrintWriter(System.out); for(int t = 0; t < T; t++){ int N = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int da = sc.nextInt(); int db = sc.nextInt(); adj = new ArrayList[N+1]; for(int i = 0; i <= N; i++){ adj[i] = new ArrayList<Integer>(); } for(int i = 0; i < N-1; i++){ int v = sc.nextInt(); int u = sc.nextInt(); adj[v].add(u); adj[u].add(v); } if(db > 2*da){ dfs1(a, 0, b, 0); if(dist <= da){ out.println("Alice"); } else{ node = 0; dist = 0; dfs(1, 0, 0); dfs(node, 0, 0); if(dist > 2*da){ out.println("Bob"); } else{ out.println("Alice"); } } } else{ out.println("Alice"); } } out.close(); } public static void dfs1(int a, int p, int b, int d){ if(a == b){ dist = d; } for(int next : adj[a]){ if(next != p){ dfs1(next, a, b, d+1); } } } public static void dfs(int i, int p, int d){ if(d > dist){ node = i; dist = d; } for(int next : adj[i]){ if(next != p){ dfs(next, i, d+1); } } } }
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.LinkedList; import java.util.StringTokenizer; public class problemD { static ArrayList<Integer>[] adj; static boolean found = false; static int[] D = new int[212345]; private static void solve() throws Exception { int n = fs.nextInt(); int a = fs.nextInt(); int b = fs.nextInt(); int da = fs.nextInt(); int db = fs.nextInt(); adj = new ArrayList[n+1]; for (int i = 0 ; i <= n ; i ++ ) adj[i] = new ArrayList<Integer>(); for (int i = 0 ; i < n-1; i ++ ) { int u = fs.nextInt(); int v = fs.nextInt(); adj[u].add(v); adj[v].add(u); } found = false; dfs(a, 0, 0, da, b); if (found) { out.println("Alice"); return; } dfs(b,0,0); int maxD = -1; int node = -1; for (int i = 1; i <= n; i ++ ) { if (D[i] > maxD) { maxD = D[i]; node = i; } } dfs(node, 0, 0); maxD = -1; for (int i = 1; i <= n; i ++ ) { if (D[i] > maxD) { maxD = D[i]; } } boolean bob = db > da * 2 && maxD > da * 2; out.println(bob ? "Bob" : "Alice"); } private static void dfs(int node, int parent, int dist) { D[node] = dist; for (int x: adj[node]) { if (x != parent) { dfs(x, node, dist+1); } } } private static void dfs(int node, int parent, int dist, int da, int b) { if (node == b) { found = true; return; } for (int x: adj[node]) { if (x != parent) { if (dist+1 <= da) { dfs(x, node, dist + 1, da, b); } } } } private static FastScanner fs = new FastScanner(); private static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws Exception { int T = 1; T = fs.nextInt(); for (int t = 0; t < T; t++) { solve(); } out.close(); } }
0
1230c43e
ce0b2178
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { static boolean[] primecheck = new boolean[1000002]; public static void main(String[] args) throws IOException { OutputStream outputStream = System.out; FastReader in = new FastReader(); PrintWriter out = new PrintWriter(outputStream); PROBLEM solver = new PROBLEM(); int t = 1; t = in.nextInt(); for (int i = 0; i < t; i++) { solver.solve(in, out); } out.close(); } static class PROBLEM { public void solve(FastReader in, PrintWriter out) { int n = in.nextInt(); int[] a = in.readArray(n); char[] c = in.nextLine().toCharArray(); int cur = 1; ArrayList<Pair> p = new ArrayList<>(); for (int i = 0; i < n; i++) { p.add(new Pair(c[i], a[i])); } Collections.sort(p); // for (int i = 0; i < n; i++) { // out.println(p.get(i).x + " " + p.get(i).y); // } for (int i = 0; i < n; i++) { if(p.get(i).x == 'B' && p.get(i).y < i+1){ out.println("NO"); return; } if(p.get(i).x == 'R' && p.get(i).y > i+1){ out.println("NO"); return; } } out.println("YES"); // int n = in.nextInt(), m = in.nextInt(); // char[] s = in.nextLine().toCharArray(); // // int rl = 0, ud = 0, r = 0 , l = 0, rlf = 0, udf = 0; // int lmax = 0, rmax = 0, umax = 0, dmax = 0; // // for (int i = 0; i < s.length; i++) { // if(s[i] == 'L'){ // if(rlf == 0) rlf = -1; // rl--; // l--; // if(rl == 0) l = 0; // if(rl < 0 && lmax+rmax <= m) lmax = Math.max(Math.abs(l), lmax); // } // if(s[i] == 'R'){ // if(rlf == 0) rlf = 1; // rl++; // r++; // // if(rl > 0 && lmax+rmax <= m) rmax = Math.max(r, rmax); // } // if(s[i] == 'U'){ // if(udf == 0) udf = 1; // ud++; // r = Math.max(Math.abs(ud), r); // if(ud > 0 && umax+dmax <= n) umax = Math.max(Math.abs(ud), umax); // } // if(s[i] == 'D'){ // if(udf == 0) udf = -1; // ud--; // r = Math.max(Math.abs(ud), r); // if(ud < 0 && umax+dmax <= n) dmax = Math.max(Math.abs(ud), dmax); // } // } // // int ansc = 0, ansr = 0; // // out.println(rlf + " lmx = " + lmax + " rmax" + rmax); // // if(rlf == 1) ansc = m-rmax; // else if(rlf == -1) ansc = 1+lmax; // else ansc = 1; // // if(udf == 1) ansr = 1+umax; // else if(udf == -1) ansr = m-dmax; // else ansr = 1; // // out.println(ansr + " " + ansc); } } static class Pair implements Comparable<Pair>{ char x; int y; Pair(char x, int y){ this.x = x; this.y = y; } } // 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 int compareToY(Pair<U, V> b) { // int cmpU = y.compareTo(b.y); // return cmpU != 0 ? cmpU : x.compareTo(b.x); // } // // public String toString() { // return String.format("(%s, %s)", x.toString(), y.toString()); // } // // } }
import java.io.*; import java.util.*; public class D { static class Pair implements Comparable<Pair>{ int a; char b; public Pair(int a, char b) { this.a = a; this.b = 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(); int arr[] = scan.readArray(n); char line[] = scan.next().toCharArray(); ArrayList<Pair> pairs = new ArrayList<Pair>(); for(int i = 0;i<n;i++) { pairs.add(new Pair(arr[i], line[i])); } Collections.sort(pairs); boolean possible = true; for(int i = 1;i<=n;i++) { if(pairs.get(i-1).a == i) { continue; } else if(pairs.get(i-1).a < i && pairs.get(i-1).b == 'R') { continue; } else if(pairs.get(i-1).a > i && pairs.get(i-1).b == 'B') { continue; } else { possible = false; break; } } output.println(possible == true ? "YES" : "NO"); } 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; } }
0
8f31b279
d2f74dfc
import java.util.*; import java.io.*; public class JavaTract { static class Pair implements Comparable<Pair>{ int first; int second; Pair(int x,int y){ this.first=x; this.second=y; } } public static void main (String[] args) { Scanner scan=new Scanner(System.in); int t=scan.nextInt(); while(t-->0){ int n=scan.nextInt(); int m=scan.nextInt(); int x=scan.nextInt(); int[]arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=scan.nextInt(); } // TreeSet<Pair> set = new TreeSet<>(); Queue<Pair> set = new PriorityQueue<>(); for(int i=1;i<=m;i++){ set.add(new Pair(0,i)); } System.out.println("YES"); for(int i=0;i<n;i++){ Pair temp=set.poll(); int first = temp.first; int second = temp.second; System.out.print(second+" "); set.add(new Pair(first+arr[i],second)); } System.out.println(); } } }
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 m=sc.nextInt(); int x=sc.nextInt(); int[] a=new int[n]; int[] c=new int[n]; Map<Integer,Stack<Integer>> mp=new HashMap<>(); for(int i=0;i<n;i++){ a[i]=sc.nextInt(); //c[i]=a[i]; if(mp.containsKey(a[i])){ Stack<Integer> l=mp.get(a[i]); l.push(i); mp.put(a[i],l); } else{ Stack<Integer> l=new Stack<>(); l.push(i); mp.put(a[i],l); } } Arrays.sort(a); int[] b=new int[n]; int cn=1; for(int i=0;i<n;i++){ cn=cn%m; Stack<Integer> li=mp.get(a[i]); if(!li.isEmpty()){ int val=li.peek(); b[val]=cn+1; c[i]+=a[i]; li.pop(); mp.put(a[i],li); } cn++; } int mv=c[0]-c[n-1]; if(Math.abs(mv)>x) System.out.println("NO"); else { System.out.println("YES"); for(int i=0;i<n;i++){ System.out.print(b[i]+" "); } System.out.println(); } } } }
0
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); } } } } }
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
48c5f745
4d25818e
import java.io.*; import java.util.*; public class E { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Reader sc=new Reader(); PrintWriter pw=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int k=sc.nextInt(); int[] idxes=new int[k]; int[] temps=new int[k]; for(int i=0;i<k;i++) { idxes[i]=sc.nextInt()-1; } for(int i=0;i<k;i++) { temps[i]=sc.nextInt(); } int[] ans=new int[n]; Arrays.fill(ans, Integer.MAX_VALUE/2); for(int i=0;i<k;i++) { ans[idxes[i]]=temps[i]; } for(int i=1;i<n;i++) { ans[i]=Math.min(ans[i], ans[i-1]+1); } for(int i=n-2;i>=0;i--) { ans[i]=Math.min(ans[i], ans[i+1]+1); } for(int i=0;i<n;i++) { pw.print(ans[i]+" "); } pw.println(); } pw.flush(); sc.close(); } }
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 k= sc.nextInt(); int ac[] = new int[k]; int temp[] = new int[k]; int ans[] = new int[n+1]; for(int i=0;i<k;i++) ac[i] = sc.nextInt(); for(int i=0;i<k;i++) temp[i] = sc.nextInt(); Arrays.fill(ans,Integer.MAX_VALUE); for(int i=0;i<k;i++) ans[ac[i]] = temp[i]; for(int i=2;i<n+1;i++) ans[i]=Math.min(ans[i],ans[i-1]==Integer.MAX_VALUE ? ans[i-1] : ans[i-1]+1); for(int i=n-1;i>0;i--) ans[i]=Math.min(ans[i],ans[i+1]+1); for(int i=1;i<n+1;i++) System.out.print(ans[i]+" "); System.out.println(" "); } } }
1
1f27b677
81fb6415
import java.util.*; import java.io.*; public class Codeforces { static long accurateFloor(long a, long b) { long val = a/b; while(val*b > a) val--; return val; } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); 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()); Long[] a = new Long[n]; st = new StringTokenizer(br.readLine()); long[] sum = new long[n]; for(int i = 0; i < n; i++) { a[i] = Long.parseLong(st.nextToken()); } Arrays.sort(a); sum[0] = a[0]; for(int i = 1; i < n; i++) { sum[i] += sum[i-1]+a[i]; } long ans = Long.MAX_VALUE; for(int y = 0; y < n; y++) { // long z = k-sum[n-y-1]+a[0]; // if(z > 0) z = z/(y+1); // else { // if(z%(y+1) == 0) z = z/(y+1); // else { // z = z/(y+1); // z -= 1; // } // } long x = a[0]-accurateFloor(k-sum[n-y-1]+a[0], y+1); x = Math.max(0, x); ans = Math.min(ans, y+x); } writer.println(ans); } writer.close(); br.close(); } }
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(); } }
0
a7894e0b
fadc1365
import java.io.*; import java.math.*; import java.util.*; public class test { public static void main(String args[]) throws Exception { Scanner sc = new Scanner(); StringBuilder res = new StringBuilder(); int tc = sc.nextInt(); while(tc-->0) { int n=sc.nextInt(); int k=sc.nextInt(); int[] ac=new int[k]; long[] ans=new long[n]; Arrays.fill(ans, Integer.MAX_VALUE/2); for(int i=0;i<k;i++) { ac[i]=sc.nextInt()-1; } for(int i=0;i<k;i++) { long x = sc.nextLong(); ans[ac[i]] = x; } for(int i=1;i<n;i++) { ans[i]=Math.min(ans[i], ans[i-1]+1); } for(int i=n-2;i>=0;i--) { ans[i]=Math.min(ans[i], ans[i+1]+1); } for(int i=0;i<n;i++) { res.append(ans[i]+" "); } res.append("\n"); } System.out.println(res); } }
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(); } }
1
6e207cbf
dc281165
import java.util.*; import java.lang.*; import java.io.*; public class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int sm, n; while(t > 0) { t--; n = sc.nextInt(); String s1,s2; s1 = sc.next(); s2 = sc.next(); int a[] = new int[4]; a[0] = 0; a[1] = 0; a[2] = 0; a[3] = 0; for(int i = 0 ; i < n ; i++) { if(s1.charAt(i) == '0'&& s2.charAt(i) == '1') a[0]++; else if(s1.charAt(i) == '1'&& s2.charAt(i) == '0') a[1]++; else if(s1.charAt(i) == '1'&& s2.charAt(i) == '1') a[2]++; else a[3]++; } // System.out.println(a[0] + " " + a[1] + " " + a[2] + " " + a[3]); int n1 = Integer.MAX_VALUE, n2 = Integer.MAX_VALUE, n3 = Integer.MAX_VALUE; if (a[0] == a[1]) { n1 = 2*a[0]; } if((a[2] - 1) == a[3]) { // System.out.println(a[3] + 1); n2 = 2*a[3] + 1; } if((a[3] + 1) == a[2]) { // System.out.println(a[2] + 1); n3 = 2*a[2] + 1; } int ans = Math.min(n1, Math.min(n2,n3)); if(ans == Integer.MAX_VALUE) { System.out.println("-1"); } else { System.out.println(ans); } } } }
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"; 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); } }
0
764484c3
9028caf7
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 Armchairs { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); Vector<Integer> a = new Vector(); Vector<Integer> b = new Vector(); for (int i=0; i<n; ++i){ int input = sc.nextInt(); if(input==1) a.add(i); else b.add(i); } n = a.size(); int m = b.size(); if(n==0) out.print(0); else{ int [][]dp = new int [n][m]; for (int i=0; i<n; ++i) for (int j=0; j<m; ++j) dp[i][j] = 25000000; dp[0][0] = Math.abs(a.get(0) - b.get(0)); for (int i=0; i<n; ++i) for (int j=1; j<m; ++j){ if(i==0) dp[i][j] = Math.min(Math.abs(a.get(i) - b.get(j)),dp[i][j-1]); else dp[i][j] = Math.min(Math.abs(a.get(i) - b.get(j)) + dp[i-1][j-1],dp[i][j-1]); } out.print(dp[n-1][m-1]); } out.close(); } /*Methods*/ }
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=1; while(T-->0) { int n=input.nextInt(); int a[]=new int[n]; ArrayList<Integer> list=new ArrayList<>(); ArrayList<Integer> space=new ArrayList<>(); for(int i=0;i<n;i++) { a[i]=input.nextInt(); if(a[i]==1) { list.add(i); } else { space.add(i); } } int pre[]=new int[space.size()]; for(int i=0;i<list.size();i++) { if(i==0) { int min=Integer.MAX_VALUE; for(int j=0;j<space.size();j++) { pre[j]=Math.abs(list.get(i)-space.get(j)); min=Math.min(min,pre[j]); pre[j]=min; } } else { int arr[]=new int[space.size()]; for(int j=0;j<i;j++) { arr[j]=Integer.MAX_VALUE; } int min=Integer.MAX_VALUE; for(int j=i;j<space.size();j++) { int v=Math.abs(list.get(i)-space.get(j)); v+=pre[j-1]; arr[j]=v; min=Math.min(min,v); arr[j]=min; } for(int j=0;j<space.size();j++) { pre[j]=arr[j]; } } } out.println(pre[space.size()-1]); } out.close(); } }
0
29d865c3
5cfa2861
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { while (N-- > 0) { solve(); } out.close(); } public static void solve() { int M = sc.nextInt(); String a = sc.nextLine(); String b = sc.nextLine(); int d = 0; int al = 0; int bl = 0; for (int i = 0; i < M; i++) { if (a.charAt(i) != b.charAt(i)) { d++; } if (a.charAt(i) == '1') { al++; } if (b.charAt(i) == '1') { bl++; } } if ((al == bl && d % 2 == 0) || (M - al + 1 == bl && (M - d) % 2 == 1)) { if ((al == bl && d % 2 == 0) && (M - al + 1 == bl && (M - d) % 2 == 1)) { out.println(Math.min(d, M - d)); } else if (al == bl && d % 2 == 0) { out.println(d); } else { out.println(M - d); } } else { out.println(-1); return; } } private static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); private static MyScanner sc = new MyScanner(); private static int N = sc.nextInt(); }
import java.util.*; import java.io.*; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.Math.max; public class EdB { static long[] mods = {1000000007, 998244353, 1000000009}; static long mod = mods[0]; public static MyScanner sc; public static PrintWriter out; public static void main(String[] largewang) throws Exception{ // TODO Auto-generated method stub sc = new MyScanner(); out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-->0) { int n = sc.nextInt(); char[] a = sc.next().toCharArray(); char[] b = sc.next().toCharArray(); int counta = 0; int countb = 0; for (char j : a) { if (j == '1') counta++; } for (char j : b) { if (j == '1') countb++; } long ans = 0; if (counta == countb && n-counta+1 == countb){ for (int j = 0;j<n;j++){ if (b[j] == '1' && a[j] == '0') { ans += 2; } } long tempans = 0; int j=0; for(j = 0;j<n;j++){ if (a[j] == '1' && b[j] == '1') break; } for(int i = 0;i<n;i++){ if (i == j) continue; if (a[i] == '1') a[i] = '0'; else a[i] = '1'; } tempans += 1; for (j = 0;j<n;j++){ if (b[j] == '1' && a[j] == '0') { tempans += 2; } } ans = min(ans, tempans); } else if (counta == countb) { for (int j = 0;j<n;j++){ if (b[j] == '1' && a[j] == '0') { ans += 2; } } } else if (n-counta+1 == countb) { int j=0; for(j = 0;j<n;j++){ if (a[j] == '1' && b[j] == '1') break; } for(int i = 0;i<n;i++){ if (i == j) continue; if (a[i] == '1') a[i] = '0'; else a[i] = '1'; } ans += 1; for (j = 0;j<n;j++){ if (b[j] == '1' && a[j] == '0') { ans += 2; } } } else { ans = -1; } out.println(ans); } out.close(); } }
0
53196443
e496d229
import java.lang.*; import java.util.*; import java.io.*; public class Main { static FastScanner in = new FastScanner(); static void solve() { int n = in.nextInt(); long[] a = new long[n], odd = new long[n], even = new long[n]; long[] sum = new long[n]; long m1 = Long.MAX_VALUE, m2 = Long.MAX_VALUE; long st = 0; for (int i = 0; i < n; ++i) { a[i] = in.nextLong(); if (i % 2 == 0 && a[i] < m1) m1 = a[i]; if (i % 2 == 1 && a[i] < m2) m2 = a[i]; st += a[i]; odd[i] = m1; even[i] = m2; sum[i] = st; } long ans = Long.MAX_VALUE; for (int i = 1; i < n; ++i) { long aux = sum[i] + odd[i] * (n - i - 1 + (i + 1) / 2) + even[i] * (n - (i + 1) / 2); if (aux < ans && aux > 0) ans = aux; } System.out.println(ans); } public static void main(String[] args) { int T = in.nextInt(); while (T-- > 0) solve(); } }
import java.util.*; //import java.util.Stack; 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(); long[] a=new long[n]; long[] odd=new long[n]; long[] even=new long[n]; long[] sum=new long[n]; long m1=Long.MAX_VALUE; long m2=Long.MAX_VALUE; long st=(long)0; for(int i=0;i<n;i++){ a[i]=s.nextLong(); if(i%2==0&&m1>a[i]) m1=a[i]; if(i%2==1&&m2>a[i]) m2=a[i]; st+=a[i]; odd[i]=m1; even[i]=m2; sum[i]=st; } long minc=Long.MAX_VALUE; for(int i=1;i<n;i++){ long c=sum[i]+odd[i]*(n-i-1+(i+1)/2)+even[i]*(n-(i+1)/2); if(c<minc&&c>0) minc=c; } System.out.println(minc); } } }
1
4241f473
4685c420
import java.io.*; import java.util.*; public class C { public static void main(String[] args)throws IOException { FastScanner scan = new FastScanner(); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); int t = scan.nextInt(); for(int tt = 0;tt<t;tt++) { int n = scan.nextInt(); ArrayList<String> arr = new ArrayList<>(); for(int i = 0;i<n;i++) arr.add(scan.next()); int max = -1; for(int cases = 0;cases<5;cases++) { ArrayList<Integer> list = new ArrayList<>(); char ch = (char)('a'+cases); for(int i = 0;i<n;i++) { String s = arr.get(i); int countch = 0, countTotal = 0; for(int j = 0;j<s.length();j++) { if(s.charAt(j)==ch) countch++; else countTotal++; } list.add(countch-countTotal); } Collections.sort(list); int sum = 0, count = 0; for(int i = n-1;i>=0;i--) { sum+=list.get(i); if(sum>0) count++; else break; } max = Math.max(max, count); } output.write(max+"\n"); } 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; } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; public class ProblemC { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringBuilder print=new StringBuilder(); int test=Integer.parseInt(br.readLine()); while(test--!=0){ int n=Integer.parseInt(br.readLine()); ArrayList<Story> stories[]=new ArrayList[5]; for(int i=0;i<5;i++){ stories[i]=new ArrayList<>(); } for(int i=1;i<=n;i++){ char c[]=br.readLine().toCharArray(); int freq[]=new int[5]; for(int j=0;j<c.length;j++){ freq[c[j]-97]++; } for(int j=0;j<5;j++){ stories[j].add(new Story(freq[j],c.length-freq[j])); } } for(int i=0;i<5;i++){ Collections.sort(stories[i]); } int max=0; for(int i=0;i<5;i++){ int def=0; int count=0; for(Story story:stories[i]){ int diff=story.diff; if(def+diff<=0){ break; } else{ def+=diff; count++; } } max=Math.max(max,count); } print.append(max+"\n"); } System.out.print(print); } } class Story implements Comparable<Story>{ int x,y,diff; public Story(int x,int y){ this.x=x; this.y=y; this.diff=this.x-this.y; } }
0
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(); } }
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(); } } //----------------------------------------------------------------------------------// }
1
40cc90ae
9399204d
import javax.swing.text.html.parser.Entity; import java.io.*; import java.util.*; import java.math.BigInteger; public class Main{ public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } // main solver static class Task{ double eps= 0.00000001; static final int MAXN = 1010; static final int MOD= 1000000007; // stores smallest prime factor for every number static int spf[] = new int[MAXN]; static boolean[] prime; // Calculating SPF (Smallest Prime Factor) for every // number till MAXN. // Time Complexity : O(nloglogn) // function to find first index >= x // function to find last index <= y // function to count elements within given range public int add(int a, int b){ a+=b; if(a>=MOD) a-=MOD; else if(a<0) a+=MOD; return a; } int[] fact= new int[MAXN]; int[] inv= new int[MAXN]; /** * return VALUE of lower bound for unsorted array */ /** * return VALUE of upper bound for unsorted array */ InputReader in; PrintWriter out; static int mod = 1000000007; int ans = 0; ArrayList<Integer>g[] = new ArrayList[101]; int cnt[][] = new int[101][101]; int dp[][] = new int[101][101]; public void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- > 0){ int n = in.nextInt(); String[] str = new String[n]; ArrayList<Integer> diff[] = new ArrayList[5]; for (int i = 0; i < 5; i++) diff[i] = new ArrayList<>(); for (int i = 0; i < n; i++){ str[i] = in.nextToken(); int[] cnt = new int[5]; for (int j = 0; j < str[i].length(); j++){ cnt[str[i].charAt(j) - 'a']++; } for (int j = 0; j < 5; j++){ diff[j].add(cnt[j] * 2 - str[i].length()); } } int ans = 0; for (int i = 0; i < 5; i++){ Collections.sort(diff[i]); Collections.reverse(diff[i]); int cur = 0, x = 0; for (int j = 0; j < diff[i].size(); j++){ cur+=diff[i].get(j); if (cur <= 0){ break; } x++; } ans = Math.max(ans, x); } out.println(ans); } } } // public static class compareL implements Comparator<Tuple>{ // @Override // public int compare(Tuple t1, Tuple t2) { // return t2.l - t1.l; // } // } // fast input reader class; }
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 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 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 int i() { return in.nextInt(); } } 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; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader
0
51d857bc
c392efe7
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Round659 { static int rec = 0; static int X[] = { -1, 0, 0, 1 }; static int Y[] = { 0, -1, 1, 0 }; static long mod = 1000000007; static int last=0; static int maxDepth=0; public static List<Integer>[] initAdjacency(int n, int e, Reader scan, boolean type) throws IOException { List<Integer> adj[] = new ArrayList[n + 1]; for (int i = 0; i < e; i++) { int u = scan.nextInt(); int v = scan.nextInt(); if (adj[u] == null) adj[u] = new ArrayList<>(); if (type && adj[v] == null) adj[v] = new ArrayList<>(); adj[u].add(v); if (type) adj[v].add(u); } return adj; } public static void main(String[] args) throws IOException { Reader scan = new Reader(); // Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while (t-- > 0) { D(scan); } } public static void D(Reader scan) throws IOException { int n=scan.nextInt(); int a=scan.nextInt(); int b=scan.nextInt(); int da=scan.nextInt(); int db=scan.nextInt(); List<Integer> adj[]=initAdjacency(n, n-1, scan, true); int d= dist(a, 0, adj, b); if(d<=da) { System.out.println("Alice"); return; } if(db<=2*da) { System.out.println("Alice"); return; } MyPair far1= farthest(1, 0, adj); MyPair far2=farthest(far1.weight, 0, adj); int diameter= far2.value-1; if(diameter<=2*da) { System.out.println("Alice"); }else { System.out.println("Bob"); } // int val=d-2*a; // // if(val>0) { // System.out.println("Bob"); // return; // } // // int dep=0; // for(Integer x: adj[a]) { // if(x==last) continue; // // dep=Math.max(getDepth(x, a, adj), dep); // } // // if(dep>=val) { // System.out.println("Bob"); // }else { // System.out.println("Alice"); // } } public static MyPair farthest(int i, int parent, List<Integer> adj[]) { int dist=0; int node= i; for(Integer x: adj[i]) { if(x==parent) continue; MyPair recAns= farthest(x, i, adj); if(recAns.value>dist) { dist= recAns.value; node= recAns.weight; } } return new MyPair(dist+1, node); } public static int dist(int i, int parent, List<Integer> adj[], int target) { if(i==target) return 0; for(Integer x: adj[i]) { if(x==parent) continue; int recAns= dist(x, i, adj, target); if(recAns!=-1) { last=x; return recAns+1; } } return -1; } } class MyPair { int value; int weight; public MyPair(int value, int w) { this.value = value; weight = w; } }
import java.util.*; import java.io.*; import java.math.*; /** * * @Har_Har_Mahadev */ /** * Main , Solution , Remove Public */ public class D { static int visited[]; static int distance[]; private static ArrayList<Integer>[] adj; private static void BFS(int node) { visited[node] = 1; Queue<Integer> q = new LinkedList<Integer>(); q.offer(node); distance[node] = 0; while(!q.isEmpty()) { int curr = q.poll(); for(int child : adj[curr]) { if(visited[child] == 0) { q.offer(child); distance[child] = distance[curr] + 1; visited[child] = 1; } } } } public static void process() throws IOException { int n = sc.nextInt(),a = sc.nextInt(),b = sc.nextInt(), da = sc.nextInt(),db = sc.nextInt(); adj = new ArrayList[n+1]; distance = new int[n+1]; visited = new int[n+1]; for(int i = 0; i<=n; i++)adj[i] = new ArrayList<Integer>(); for(int i =1; i<n; i++) { int u = sc.nextInt(),v = sc.nextInt(); adj[u].add(v); adj[v].add(u); } BFS(a); if(distance[b] <= da || db-da<=da) { System.out.println("Alice"); return; } ArrayList<Pair> lis = new ArrayList<D.Pair>(); for(int i = 1; i<=n; i++)lis.add(new Pair(distance[i], i)); Collections.sort(lis); Pair e = lis.get(n-1); distance = new int[n+1]; visited = new int[n+1]; BFS(e.y); int max = 0; for(int i = 1; i<=n; i++)max = Math.max(max,distance[i]); if(max-da<=da) { System.out.println("Alice"); return; } System.out.println("Bob"); } //============================================================================= //--------------------------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(); 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 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; // } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////// }
0
0f09b843
45e41c6d
import java.io.*; import java.util.*; import static java.lang.Math.*; public class C { public static void main(String[] args) throws Exception { InputReader in = new InputReader(System.in); PrintWriter pw = new PrintWriter(System.out); char[] keys = {'a', 'b', 'c', 'd', 'e'}; int test = in.nextInt(); for (int t = 1; t <= test; t++) { int n = in.nextInt(); String[] words = new String[n]; for (int i = 0; i < n; i++) { words[i] = in.next(); } int res = 0; for (char c : keys) { res = max(res, getMaxCount(n, words, c)); } pw.println(res); } pw.close(); } static int getMaxCount(int n, String[] words, char c) { Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = 2 * getCount(words[i], c) - words[i].length(); } Arrays.sort(arr, Comparator.reverseOrder()); int sum = 0, count = 0; for (int i : arr) { sum += i; if (sum <= 0) { break; } count++; } return count; } static int getCount(String s, char c) { int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == c) { count++; } } return count; } }
import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class C { static char[] alphabet = {'a', 'b', 'c', 'd', 'e'}; 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(); String[] words = new String[n]; for (int i = 0; i < n; i++) { words[i] = scn.next(); } int maxWords = 0; for (char target : alphabet) { Integer[] diff = new Integer[n]; for (int i = 0; i < n; i++) { String word = words[i]; diff[i] = wordDiff(word, target); } Arrays.sort(diff, Collections.reverseOrder()); int pointer = 0; int sum = 0; for (; pointer < n && sum > -diff[pointer]; pointer++) { sum += diff[pointer]; } maxWords = Math.max(pointer, maxWords); } System.out.println(maxWords); } } static int wordDiff(String a, char target) { int ans = 0; for (char c : a.toCharArray()) { if (c == target) ans++; } return ans * 2 - a.length(); } }
0
1f748faf
851fafb6
import java.util.*; import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction; import java.io.*; import java.math.*; import java.sql.Array;; public class Main { static long mod = (long)(1e9 + 7); /* ***************************************************************************************************************************************************/ static FR sc = new FR(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) { int tc = 1; tc = sc.nextInt(); while(tc-->0) { TEST_CASE(); } System.out.println(sb); } static void TEST_CASE() { int n = sc.nextInt(); ; int[] arr = new int[n]; for(int i =0 ; i<n;i++) { arr[i] = sc.nextInt(); } boolean cond = false; Set<Integer> set = new HashSet<>(); set.add(0); for(int i =0 ; i<n ; i++) { ArrayList<Integer> al = new ArrayList<>(set); for(int e:al) { int num = e+arr[i]; if(set.contains(num)) { cond = true; break; } set.add(num); } } if(cond) System.out.println("YES"); else System.out.println("NO"); } }
import java.io.PrintWriter; import java.util.*; public class D { // MUST SEE BEFORE SUBMISSION // check whether int part would overflow or not, especially when it is a * b!!!! public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); // int t = 1; for (int i = 0; i < t; i++) { solve(sc, pw); } pw.close(); } static void solve(Scanner in, PrintWriter out){ int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } Set<Integer> set = new HashSet<>(); for(int x : arr) { if (x == 0){ out.println("YES"); return; }else if (set.contains(x) || set.contains(-x)){ out.println("YES"); return; } set.add(x); } for (int i = 0; i < n; i++) { if (dfs(0, i, arr, 0)){ out.println("YES"); return; } } out.println("NO"); } static boolean dfs(int idx, int need, int[] arr, int cur){ if (cur == arr[need]) return true; if (idx == arr.length) return false; if (idx == need) return dfs(idx + 1, need, arr, cur); return dfs(idx + 1, need, arr, cur + arr[idx]) | dfs(idx + 1, need, arr, cur - arr[idx]) | dfs(idx + 1, need, arr, cur); } }
0
5d175166
d1391025
import java.io.*; import java.util.*; public class A { //--------------------------INPUT READER---------------------------------// //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// //--------------------------VARIABLES------------------------------------// static FastReader sc = new FastReader(); static OutputStream outputStream = System.out; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { sc = new FastReader(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); } static int[] res; static List<List<Integer>> graph; static boolean two = true; static HashMap<pr<Integer, Integer>, Integer> hm; static void solve() throws IOException { int n = sc.ni(); graph = new ArrayList<>(); res = new int[n-1]; hm = new HashMap<>(); for(int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } boolean f = false; for(int i = 0; i < n-1; i++) { int a = sc.ni(), b = sc.ni(); graph.get(a).add(b); graph.get(b).add(a); if(graph.get(a).size() > 2 || graph.get(b).size() > 2) f = true; hm.put(new pr<>(a, b), i); hm.put(new pr<>(b, a), i); } if(f) { System.out.println(-1); return; } int one = 0; for(int i = 0; i < n; i++) { if(graph.get(i).size() == 1) { one = i; break; } } dfs(one, -1); for(int i: res) { System.out.println(i+" "); } System.out.println(); } static void dfs(int at, int pt) { List<Integer> li = graph.get(at); if(pt != -1) { res[hm.get(new pr<>(at, pt))] = two?2:3; two = !two; } for(int to: li) { if(to == pt) continue; dfs(to, at); } } static class pr <T, V> { T a; V b; public pr(T a, V b) { this.a = a; this.b = b; } } }
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class C { static class scanner { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException {return Integer.parseInt(next());} } static class Pair {int first, second;public Pair(int first, int second) {this.first = first;this.second = second;}} public static void main(String[] args) throws IOException { scanner.init(System.in); int t = 1; t = scanner.nextInt(); while (t-- > 0) { solve(); } } static void solve() throws IOException { int n = scanner.nextInt(); List<List<Pair>> tree = new ArrayList<>(); for (int i = 0; i < n; i++) { tree.add(new ArrayList<>()); } for (int i = 0; i < n-1; i++) { int u = scanner.nextInt()-1; int v = scanner.nextInt()-1; tree.get(u).add(new Pair(v, i)); tree.get(v).add(new Pair(u, i)); } int start = -1; for (int i = 0; i < n; i++) { if(tree.get(i).size() > 2) { System.out.println(-1); return; } else if(tree.get(i).size() == 1) { start = i; } } int[] res = new int[n-1]; Queue<Integer> q = new LinkedList<>(); q.add(start); int weight = 2, prev = -1; while (!q.isEmpty()) { int u = q.poll(); for(Pair v : tree.get(u)) { if(v.first != prev) { q.add(v.first); res[v.second] = weight; weight = 5 - weight; } } prev = u; } for(int i : res) { System.out.print(i + " "); } System.out.println(); } }
0
0588b869
69b2fd22
import java.util.*; import java.io.*; public class Solution { static final long mod=(long)1e9+7; /*static class Pair implements Comparable<Pair> { int v,l; Pair(int v,int l) { this.v=v; this.l=l; } public int compareTo(Pair p) { return l-p.l; } }*/ public static void main(String args[])throws Exception { FastReader fs=new FastReader(); PrintWriter pw=new PrintWriter(System.out); //int tc=fs.nextInt(); int n=fs.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=fs.nextInt(); ArrayList<Integer> o=new ArrayList<>(); ArrayList<Integer> z=new ArrayList<>(); for(int i=0;i<n;i++) { if(a[i]==1)o.add(i); else z.add(i); } int ans[][]=new int[o.size()+1][z.size()+1]; for(int i=1;i<=o.size();i++) { for(int j=i;j<=z.size();j++) { if(i==j)ans[i][j]=ans[i-1][j-1]+(int)Math.abs(o.get(i-1)-z.get(j-1)); else ans[i][j]=Math.min(ans[i][j-1],ans[i-1][j-1]+(int)Math.abs(o.get(i-1)-z.get(j-1))); } } pw.println(ans[o.size()][z.size()]); pw.flush(); pw.close(); } }
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=1; while(T-->0) { int n=input.nextInt(); int a[]=new int[n]; ArrayList<Integer> list=new ArrayList<>(); ArrayList<Integer> space=new ArrayList<>(); for(int i=0;i<n;i++) { a[i]=input.nextInt(); if(a[i]==1) { list.add(i); } else { space.add(i); } } int pre[]=new int[space.size()]; for(int i=0;i<list.size();i++) { if(i==0) { int min=Integer.MAX_VALUE; for(int j=0;j<space.size();j++) { pre[j]=Math.abs(list.get(i)-space.get(j)); min=Math.min(min,pre[j]); pre[j]=min; } } else { int arr[]=new int[space.size()]; for(int j=0;j<i;j++) { arr[j]=Integer.MAX_VALUE; } int min=Integer.MAX_VALUE; for(int j=i;j<space.size();j++) { int v=Math.abs(list.get(i)-space.get(j)); v+=pre[j-1]; arr[j]=v; min=Math.min(min,v); arr[j]=min; } for(int j=0;j<space.size();j++) { pre[j]=arr[j]; } } } out.println(pre[space.size()-1]); } out.close(); } }
0
343cc8e7
7f69a9e8
import java.io.*; import java.util.*; public class D669b{ public static void main(String[] args)throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int n = Integer.parseInt(f.readLine()); StringTokenizer st = new StringTokenizer(f.readLine()); int[] array = new int[n]; for(int k = 0; k < n; k++){ array[k] = Integer.parseInt(st.nextToken()); } ArrayList<HashSet<Integer>> adj = new ArrayList<HashSet<Integer>>(n); for(int k = 0; k < n; k++) adj.add(new HashSet<Integer>()); for(int k = 0; k < n-1; k++){ adj.get(k).add(k+1); } //closest number before that is < Stack<Num> stk = new Stack<Num>(); stk.add(new Num(array[0],0)); for(int k = 1; k < n; k++){ while(!stk.isEmpty() && stk.peek().x > array[k]){ stk.pop(); } if(!stk.isEmpty()){ adj.get(stk.peek().i).add(k); } stk.add(new Num(array[k],k)); } //closest number after that is < stk = new Stack<Num>(); stk.add(new Num(array[n-1],n-1)); for(int k = n-2; k >= 0; k--){ while(!stk.isEmpty() && stk.peek().x > array[k]){ stk.pop(); } if(!stk.isEmpty()){ adj.get(k).add(stk.peek().i); } stk.add(new Num(array[k],k)); } //closest number before that is > stk = new Stack<Num>(); stk.add(new Num(array[0],0)); for(int k = 1; k < n; k++){ while(!stk.isEmpty() && stk.peek().x < array[k]){ stk.pop(); } if(!stk.isEmpty()){ adj.get(stk.peek().i).add(k); } stk.add(new Num(array[k],k)); } //closest number after that is > stk = new Stack<Num>(); stk.add(new Num(array[n-1],n-1)); for(int k = n-2; k >= 0; k--){ while(!stk.isEmpty() && stk.peek().x < array[k]){ stk.pop(); } if(!stk.isEmpty()){ adj.get(k).add(stk.peek().i); } stk.add(new Num(array[k],k)); } int[] path = new int[n]; Arrays.fill(path,Integer.MAX_VALUE); path[0] = 0; for(int k = 0; k < n; k++){ for(int nei : adj.get(k)){ path[nei] = Math.min(path[nei],path[k]+1); } } int answer = path[n-1]; out.println(answer); out.close(); } public static class Num{ int x; int i; public Num(int a, int b){ x = a; i = b; } } }
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; import java.util.ArrayList; import java.util.Vector; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DDiscreteCentrifugalJumps solver = new DDiscreteCentrifugalJumps(); solver.solve(1, in, out); out.close(); } static class DDiscreteCentrifugalJumps { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); ArrayList<Integer> jumps[] = new ArrayList[n]; int h[] = new int[n]; for (int i = 0; i < n; i++) { h[i] = in.nextInt(); jumps[i] = new ArrayList<>(); } Stack<int[]> s = new Stack<>(); for (int i = 0; i < n; i++) { while (s.size() > 0 && s.peek()[0] > h[i]) { s.pop(); } if (s.size() > 0) { jumps[i].add(s.peek()[1]); } s.add(new int[]{h[i], i}); } s.clear(); for (int i = 0; i < n; i++) { while (s.size() > 0 && s.peek()[0] < h[i]) { s.pop(); } if (s.size() > 0) { jumps[i].add(s.peek()[1]); } s.add(new int[]{h[i], i}); } s.clear(); for (int i = n - 1; i >= 0; i--) { while (s.size() > 0 && s.peek()[0] < h[i]) { s.pop(); } if (s.size() > 0) { jumps[s.peek()[1]].add(i); } s.add(new int[]{h[i], i}); } s.clear(); for (int i = n - 1; i >= 0; i--) { while (s.size() > 0 && s.peek()[0] > h[i]) { s.pop(); } if (s.size() > 0) { jumps[s.peek()[1]].add(i); } s.add(new int[]{h[i], i}); } s.clear(); int dp[] = new int[n]; dp[0] = 0; for (int i = 1; i < n; i++) { dp[i] = Integer.MAX_VALUE; for (int x : jumps[i]) { dp[i] = Math.min(dp[i], dp[x] + 1); } } out.print(dp[n - 1]); } } }
0
68cdd55d
c18048bf
import java.io.*; import java.util.*; public class Main { static PrintWriter out; static FastReader sc; public static void main(String[] args) throws FileNotFoundException { boolean t = true; boolean f = false; if (f) { out = new PrintWriter("output.txt"); sc = new FastReader("input.txt"); } else { out = new PrintWriter((System.out)); sc = new FastReader(); } int tt = 1; tt = sc.nextInt(); while (tt-- > 0) { solve(); } out.flush(); out.close(); } static boolean flag = false; static List<int[]>[] ans; public static void dfs(int index, boolean[] visited, int parent, int val, List<Integer>[] ll){ if(visited[index]){ return; } List<Integer> l1 = ll[index]; // out.println(" == " + l1 + " " + index); if(l1.size() > 2){ flag = true; return; } visited[index] = true; for(int k: l1){ if(visited[k] == false){ // out.println(visited[k] + " " + index + " " + k + " " + val); ans[index].add(new int[]{k, val}); ans[k].add(new int[]{index, val}); dfs(k, visited, index, (val == 2) ? 5 : 2, ll); } } } public static void solve() { int n = sc.nextInt(); List<Integer>[] ll = new List[n + 1]; // out.println(" mm " + n); for (int i = 0; i <= n; i++) { ll[i] = new ArrayList<>(); } int[][] store = new int[n][2]; for (int i = 0; i < n - 1; i++) { int u = sc.nextInt(); int v = sc.nextInt(); store[i][0] = u; store[i][1] = v; // out.println(" === " + u + " " + v); int mm = Math.min(u, v); int mx = Math.max(u, v); // out.println(" u " + v + " " + u); ll[mm].add(mx); ll[mx].add(mm); // ll[v].add(u) } int two = 2; int prime = 5; flag = false; ans = new List[n+1]; int[] arr = new int[n + 1]; for(int i = 0; i <= n; i++){ ans[i] = new ArrayList<>(); } boolean[] visited = new boolean[n+1]; List<Integer> one = ll[1]; if(one.size() > 2){ out.println(-1); return; } // out.println(" -- " + one); visited[1] = true; for(int i = 0; i < one.size(); i++){ if(i == 0){ ans[1].add(new int[]{one.get(i), 2}); ans[one.get(i)].add(new int[]{1, 2}); dfs(one.get(i), visited, -1, 5, ll); }else{ ans[1].add(new int[]{one.get(i), 5}); ans[one.get(i)].add(new int[]{1, 5}); dfs(one.get(i), visited, -1, 2, ll); } } if (flag) { out.println(-1); return; } for(int i = 0; i < n-1; i++){ // out.println(" -- "); int u = store[i][0]; int v = store[i][1]; // out.println(" uu " + u + " " + v); if(u == 0 || v == 0){ continue; } int mm = Math.min(u, v); int mx = Math.max(u, v); List<int[]> vv = ans[mm]; // for(int[] ii:vv){ // out.println(Arrays.toString(ii)); // } if(vv == null){ continue; } for(int[] j: vv){ if(j[0] == mx){ out.print(j[1]+ " "); break; } } } out.println(); } }
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 int mod = 998244353 ; // static int N = 200005; // static long factorial_num_inv[] = new long[N+1]; // static long natual_num_inv[] = new long[N+1]; // static long fact[] = new long[N+1]; // static void InverseofNumber() //{ // natual_num_inv[0] = 1; // natual_num_inv[1] = 1; // for (int i = 2; i <= N; i++) // natual_num_inv[i] = natual_num_inv[mod % i] * (mod - mod / i) % mod; //} //static void InverseofFactorial() //{ // factorial_num_inv[0] = factorial_num_inv[1] = 1; // for (int i = 2; i <= N; i++) // factorial_num_inv[i] = (natual_num_inv[i] * factorial_num_inv[i - 1]) % mod; //} //static long nCrModP(long N, long R) //{ // long ans = ((fact[(int)N] * factorial_num_inv[(int)R]) % mod * factorial_num_inv[(int)(N - R)]) % mod; // return ans%mod; //} //static boolean prime[]; //static void sieveOfEratosthenes(int n) //{ // prime = new boolean[n+1]; // for (int i = 0; i <= n; i++) // prime[i] = true; // for (int p = 2; p * p <= n; p++) // { // // If prime[p] 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; // } // } //} static int visited[]; static HashMap<Pair,Integer> hm; public static void main (String[] args) throws java.lang.Exception { // InverseofNumber(); // InverseofFactorial(); // fact[0] = 1; // for (long i = 1; i <= 2*100000; i++) // { // fact[(int)i] = (fact[(int)i - 1] * i) % mod; // } FastReader scan = new FastReader(); PrintWriter pw = new PrintWriter(System.out); int t = scan.nextInt(); while(t-->0){ int n = scan.nextInt(); List<List<Integer>> a = new ArrayList<List<Integer>>(); for(int i=0;i<=n;i++){ a.add(new ArrayList<Integer>()); } Pair edge[] = new Pair[n-1]; for(int i=0;i<n-1;i++){ int x = scan.nextInt(); int y = scan.nextInt(); edge[i] = new Pair(x,y); a.get(x).add(y); a.get(y).add(x); } int flag=0; int start = -1; for(int i=1;i<=n;i++){ if(a.get(i).size()>2) flag = 1; if(a.get(i).size()==1) start = i; } if(flag==1) pw.println(-1); else{ visited = new int[n+1]; hm = new HashMap<Pair,Integer>(); dfs(a,start,2); for(int i=0;i<n-1;i++){ int x = edge[i].x; int y = edge[i].y; pw.print(hm.get(new Pair(x,y))+" "); } pw.println(); } pw.flush(); } } static void dfs(List<List<Integer>> a,int start,int parent){ if(visited[start]==0){ visited[start] = 1; List<Integer> temp = a.get(start); int len = temp.size(); for(int i=0;i<len;i++){ int end = temp.get(i); if(parent==2){ hm.put(new Pair(start,end),3); hm.put(new Pair(end,start),3); dfs(a,end,3); } else{ hm.put(new Pair(start,end),2); hm.put(new Pair(end,start),2); dfs(a,end,2); } } } } //static long bin_exp_mod(long a,long n){ // long res = 1; // if(a==0) // return 0; // while(n!=0){ // if(n%2==1){ // res = ((res)*(a)); // } // n = n/2; // a = ((a)*(a)); // } // return res; //} //static long bin_exp_mod(long a,long n){ // long mod = 1000000007; // long res = 1; // a = a%mod; // if(a==0) // return 0; // while(n!=0){ // if(n%2==1){ // res = ((res%mod)*(a%mod))%mod; // } // n = n/2; // a = ((a%mod)*(a%mod))%mod; // } // res = res%mod; // return res; //} // 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/gcd(a,b))*b; // } } class Pair{ Integer x,y; Pair(int x,int y){ this.x = x; this.y = y; } }
0
53d782a0
5769b7b3
import java.io.*; import java.util.*; public class B { //--------------------------INPUT READER---------------------------------// //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static FastReader sc = new FastReader(); //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { sc = new FastReader(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); } static void solve() throws IOException { int n = sc.ni(); long k = sc.nl(); Long[] arr = new Long[n]; for(int i = 0; i < n; i++) arr[i] = sc.nl(); Arrays.sort(arr); long sum = 0; for(int i = 0; i < n; i++) { sum += arr[i]; } if(sum <= k) { System.out.println(0); return; } long cont = 0; long min = sum-k; for(int i = n-1; i >= 0; i--) { cont += arr[i]; long psum = sum-cont; if(psum <= k) { long extra = k-psum; if(arr[0]*(n-i)<=extra) { min = Math.min(min, n-i); continue; } if(i == 0) { long q = k/n; long ans = arr[0]-q+n-1; min = Math.min(min, ans); continue; } extra += arr[0]; long q = extra/(n-i+1); long ans = arr[0]-q+n-i; min = Math.min(min, ans); } else { long toRem = psum-k-arr[0]; long q = (long)Math.ceil(toRem/(n-i+1.0)); long ans = q+arr[0]+n-i; min = Math.min(min, ans); } } System.out.println(min); } }
import java.io.*; import java.util.*; public class B { //--------------------------INPUT READER---------------------------------// //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static FastReader sc = new FastReader(); static OutputStream outputStream = System.out; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { sc = new FastReader(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(); long k = sc.nl(); Long[] arr = new Long[n]; for(int i = 0; i < n; i++) arr[i] = sc.nl(); Arrays.sort(arr); long sum = 0; for(int i = 0; i < n; i++) { sum += arr[i]; } if(sum <= k) { System.out.println(0); return; } long cont = 0; long min = sum-k; for(int i = n-1; i >= 0; i--) { cont += arr[i]; long psum = sum-cont; if(psum <= k) { long extra = k-psum; if(arr[0]*(n-i)<=extra) { min = Math.min(min, n-i); continue; } if(i == 0) { long q = k/n; long ans = arr[0]-q+n-1; min = Math.min(min, ans); continue; } extra += arr[0]; long q = extra/(n-i+1); long ans = arr[0]-q+n-i; min = Math.min(min, ans); } else { long toRem = psum-k-arr[0]; long q = (long)Math.ceil(toRem/(n-i+1.0)); long ans = q+arr[0]+n-i; min = Math.min(min, ans); } } System.out.println(min); } }
1
69b2fd22
e6b7a899
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=1; while(T-->0) { int n=input.nextInt(); int a[]=new int[n]; ArrayList<Integer> list=new ArrayList<>(); ArrayList<Integer> space=new ArrayList<>(); for(int i=0;i<n;i++) { a[i]=input.nextInt(); if(a[i]==1) { list.add(i); } else { space.add(i); } } int pre[]=new int[space.size()]; for(int i=0;i<list.size();i++) { if(i==0) { int min=Integer.MAX_VALUE; for(int j=0;j<space.size();j++) { pre[j]=Math.abs(list.get(i)-space.get(j)); min=Math.min(min,pre[j]); pre[j]=min; } } else { int arr[]=new int[space.size()]; for(int j=0;j<i;j++) { arr[j]=Integer.MAX_VALUE; } int min=Integer.MAX_VALUE; for(int j=i;j<space.size();j++) { int v=Math.abs(list.get(i)-space.get(j)); v+=pre[j-1]; arr[j]=v; min=Math.min(min,v); arr[j]=min; } for(int j=0;j<space.size();j++) { pre[j]=arr[j]; } } } out.println(pre[space.size()-1]); } out.close(); } }
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()]); } }
0
680ba922
76ad805a
import java.util.*; import java.io.*; public class Solution { // Updates a node in Binary Index Tree (BITree) // at given index in BITree. The given value // 'val' is added to BITree[i] and all of // its ancestors in tree. static int dp[][]; public static void main(String []args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); sc.nextLine(); String a = sc.nextLine(); String b = sc.nextLine(); int same = 0 , zo = 0 , oz = 0 , oo = 0 , zz = 0; for(int i = 0 ; i < n ; i++) { if(a.charAt(i) == '0' && b.charAt(i) == '1') oz++; else if(a.charAt(i) == '1' && b.charAt(i) == '0') zo++; else if(a.charAt(i) == '1' && b.charAt(i) == '1') oo++; else zz++; } if(oz == zo || (zz == oo-1)) { int mx = Integer.MAX_VALUE; if(oz == zo) mx = Math.min(mx,2*oz); if(oo-1 == zz) mx = Math.min(mx,zz+oo); System.out.println(mx); } else { System.out.println(-1); } } } }
import java.util.Arrays; import java.util.Scanner; public class First { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); String a = sc.next(); String b = sc.next(); char ch1[] = a.toCharArray(); char ch2[] = b.toCharArray(); int zz = 0; int oz = 0; int zo = 0; int oo = 0; for (int i = 0; i < n; i++) { if (ch1[i] == '0') { if (ch2[i] == '0') { zz += 1; } else { oz += 1; } } else { if (ch2[i] == '0') { zo += 1; } else { oo += 1; } } } int ans = -1; if ((oo - zz) == 1 || zo == oz) { int s1 = (int) 1e7; int s2 = (int) 1e7; if ((oo - zz) == 1) { s1 = oo + zz; } if (zo == oz) s2 = zo + oz; ans = Math.min(s1, s2); } System.out.println(ans); } } }
0
8a729537
abd16ff0
import java.io.*; import java.util.*; public class test3 { public static void main(String[] args) throws IOException { FastReader f = new FastReader(); int t = f.nextInt(); while(t-->0) { int n = f.nextInt(); int C[] = new int[n]; long ans=Long.MAX_VALUE,pre = 0; PriorityQueue<Integer> epq = new PriorityQueue<Integer>(); PriorityQueue<Integer> opq = new PriorityQueue<Integer>(); for(int i = 0;i<n;i++) { C[i] = f.nextInt(); if(i%2==0)epq.add(C[i]); else opq.add(C[i]); pre+=C[i]; if(i>0) { ans = Math.min(ans,pre+ ((long)n-(long)epq.size())*(long)epq.peek() + ((long)n-(long)opq.size())*(long)opq.peek()); } } System.out.println(ans); } } }
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); } } }
1
74b5ad88
dc575a6c
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.StringTokenizer; public class A { static int[][] A; static long[][] B; static ArrayList<ArrayList<Integer>> ar; public static void main(String[] args) { CP sc =new CP(); int tt = sc.nextInt(); while (tt-- > 0) { int n = sc.nextInt(); ar = new ArrayList<>(); for(int i=0;i<n+1;i++) ar.add(new ArrayList<>()); A = new int[n+1][2]; B = new long[n+1][2]; for(int i=1;i<n+1;i++) { A[i][0] = sc.nextInt(); A[i][1] = sc.nextInt(); } for(int i=0;i<n-1;i++){ int u= sc.nextInt(); int v = sc.nextInt(); ar.get(u).add(v); ar.get(v).add(u); } // for(int i=0;i<n;i++) { // for (int j = 0; j < 2; j++) // System.out.print(A[i][j] + " "); // System.out.println(); // } // for(int i=0;i<ar.size();i++) // System.out.println(ar.get(i)); //System.out.println(ar.get(1).get(0)); //System.out.println(ar.get(3).size()); DFS(1, -1); System.out.println(Math.max(B[1][0], B[1][1])); } } static long max = Long.MIN_VALUE; static void DFS(int curr, int parent){ B[curr][0] =0; B[curr][1] = 0; for(int i : ar.get(curr)){ if(parent!=i) { DFS(i, curr); B[curr][0] += Math.max(B[i][0] + Math.abs(A[curr][0] - A[i][0]), B[i][1] + Math.abs(A[curr][0] - A[i][1])); B[curr][1] += Math.max(B[i][0] + Math.abs(A[curr][1] - A[i][0]), B[i][1] + Math.abs(A[curr][1] - A[i][1])); } } //System.out.println(B[curr][0]+" "+B[curr][1]); } /*****************************************************************************/ /*****************************************************************************/ }
import java.util.*; import java.io.*; public class Main { static long[][]dp; static ArrayList<Integer>[]adj; static int[][]arr; public static void main(String[] args) { FastScanner sc=new FastScanner(); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); arr=new int[n][2]; adj=new ArrayList[n]; for(int i=0;i<n;i++){ arr[i][0]=sc.nextInt(); arr[i][1]=sc.nextInt(); adj[i]=new ArrayList<>(); } for(int i=0;i<n-1;i++){ int a=sc.nextInt()-1; int b=sc.nextInt()-1; adj[a].add(b); adj[b].add(a); } dp=new long[n][2]; dfs(0,-1); System.out.println(Math.max(dp[0][0],dp[0][1])); } } public static void dfs(int v,int parent){ for(int u:adj[v]){ if(u!=parent){ dfs(u,v); } } long ans1=0; long ans2=0; for(int u:adj[v]){ if(u!=parent){ long a=Math.abs(arr[v][0]-arr[u][0])+dp[u][0]; long b=Math.abs(arr[v][0]-arr[u][1])+dp[u][1]; long a1=Math.abs(arr[v][1]-arr[u][0])+dp[u][0]; long b1=Math.abs(arr[v][1]-arr[u][1])+dp[u][1]; ans1+=Math.max(a,b); ans2+=Math.max(a1,b1); } } dp[v][0]=ans1; dp[v][1]=ans2; } }
0
3afcc566
3dd65549
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); } } }
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 long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
0
baa8e497
cc9230d3
import java.util.*; import java.io.*; public class Solution { public static void main(String str[]) throws IOException{ Reader sc = new Reader(); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); ArrayList<Tree> tt = new ArrayList<>(); for(int i=0;i<n;i++){ Tree temp = new Tree(sc.nextInt(), sc.nextInt(), i); tt.add(temp); } for(int i=0;i<n-1;i++){ int x = sc.nextInt()-1; int y = sc.nextInt()-1; tt.get(x).al.add(tt.get(y)); tt.get(y).al.add(tt.get(x)); } Pair p = traversal(tt.get(0), null); System.out.println(Math.max(p.av, p.bv)); } } static Pair traversal(Tree node, Tree Parent){ if(node.al.size()==1 && Parent!=null){ Pair p = new Pair(0,0); return p; } long lt = 0; long rt = 0; for(Tree t: node.al){ if(t!=Parent){ Pair p = traversal(t, node); long ll = Math.abs(node.l- t.l) + p.av; ll = Math.max(ll, Math.abs(node.l-t.r)+p.bv); long rr = Math.abs(node.r- t.l) + p.av; rr = Math.max(rr, Math.abs(node.r-t.r)+p.bv); lt+=ll; rt+=rr; } } Pair p = new Pair(lt, rt); return p; } } class Pair{ long av; long bv; Pair(long av, long bv){ this.av = av; this.bv = bv; } } class Tree{ int l; int r; int i; ArrayList<Tree> al = new ArrayList<>(); Tree(int l, int r, int i){ this.l = l; this.r = r; this.i = i; } }
import java.io.BufferedReader; import java.util.StringTokenizer; import java.io.InputStreamReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; public class C { static int[][] lr = new int[2][(int)2e5+10]; static long[][] dp = new long[2][(int)2e5+10]; static ArrayList<ArrayList<Integer>> g; public static void main(String[] args) { FastReader fr = new FastReader(); PrintWriter out = new PrintWriter(System.out, true); int cases = fr.nextInt(); for(int c = 0; c < cases; c++) { int nodes = fr.nextInt(); g = new ArrayList<ArrayList<Integer>>(); for(int i = 1; i <= nodes; i++) { lr[0][i] = fr.nextInt(); lr[1][i] = fr.nextInt(); } for(int i = 0; i <= nodes; i++) { g.add(new ArrayList<Integer>()); } for(int i = 0; i < nodes-1; i++) { int f = fr.nextInt(); int t = fr.nextInt(); g.get(f).add(t); g.get(t).add(f); } DFS(1, -1); out.write(Math.max(dp[0][1], dp[1][1]) + "\n"); } out.close(); } static void DFS(int v, int p) { dp[0][v] = dp[1][v] = 0; for(Integer u : g.get(v)) { if (u == p) continue; DFS(u, v); dp[0][v] += Math.max(Math.abs(lr[0][v] - lr[1][u]) + dp[1][u], dp[0][u] + Math.abs(lr[0][v] - lr[0][u])); dp[1][v] += Math.max(Math.abs(lr[1][v] - lr[1][u]) + dp[1][u], dp[0][u] + Math.abs(lr[1][v] - lr[0][u])); } } }
0

No dataset card yet

New: Create and edit this dataset card directly on the website!

Contribute a Dataset Card
Downloads last month
0
Add dataset card