F1
stringlengths
8
8
F2
stringlengths
8
8
text_1
stringlengths
607
24.4k
text_2
stringlengths
591
24.4k
label
int64
0
1
2470b521
f6ca6fc8
// package com.company; import com.sun.security.jgss.GSSUtil; import javax.swing.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; //ankit singh public class Main { static int inf=Integer.MAX_VALUE/2; public static void main(String[] args) { Scanner input=new Scanner(System.in); int nn=input.nextInt(); int a[]=new int[nn]; ArrayList<Integer> one=new ArrayList<>(); ArrayList<Integer> zero=new ArrayList<>(); for (int i = 0; i <nn ; i++) { a[i]=input.nextInt(); if(a[i]==1){ one.add(i); }else zero.add(i); } if (one.size()==0) { System.out.println(0); return; } int n=one.size(); int m=zero.size(); int dp[][]=new int[n+1][m+1]; int ans=0; for (int i = 1; i <=n ; i++) { dp[i][0]=inf; for (int j = 1; j <=m ; j++) { dp[i][j]=inf; //take the current dp[i][j]=Math.min(dp[i][j],dp[i-1][j-1]+Math.abs(one.get(i-1)-zero.get(j-1))); //take previos dp[i][j]=Math.min(dp[i][j],dp[i][j-1]); } } ans=inf; for (int i = 1; i <=m ; i++) { ans=Math.min(ans,dp[n][i]); } System.out.println(ans); } }
import java.util.*; import java.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 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() throws java.io.IOException { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static class 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.DataInputStream; // import java.io.FileInputStream; // import java.io.IOException; import java.io.*; import java.util.*; public class one { static Scanner sc=new Scanner(System.in); boolean prime[]; static int prev=-1; static int dp[][]; public static int[] input(int size){ int arr[]=new int[size]; for(int i=0;i<size;i++) arr[i]=sc.nextInt(); return arr; } public static void main(String[] args) { //int testcase=1; int testcase=sc.nextInt(); //System.out.println("HI"); while(testcase-->0){ // int x=sc.nextInt(); // int y=sc.nextInt(); //String str[]=new String[size]; solve(); System.out.println(); } } public static void solve(){ HashMap<Integer,Integer> map=new HashMap<Integer,Integer>(); int size=sc.nextInt(); int arr[][]=new int[size-1][2]; for(int i=0;i<size-1;i++){ arr[i][0]=sc.nextInt(); arr[i][1]=sc.nextInt(); } for(int x[]:arr){ map.put(x[0],map.getOrDefault(x[0], 0)+1); map.put(x[1],map.getOrDefault(x[1], 0)+1); if(map.get(x[0])>2||map.get(x[1])>2){ System.out.println(-1); return; } } List<List<Integer>> adj=new ArrayList<>(); for(int i=0;i<=size;i++) adj.add(new ArrayList<Integer>()); for(int x[]:arr){ adj.get(x[0]).add(x[1]); adj.get(x[1]).add(x[0]); } //System.out.println(adj); int vist[]=new int[size+1]; HashMap<String,Integer> ans=new HashMap<String,Integer>(); for(int i=1;i<=size;i++){ if(vist[i]==0){ dfs(i,vist,adj,ans,2); } } //System.out.println(ans); for(int x[]:arr){ //System.out.print(map.get(x[0])); int a=Math.min(x[0],x[1]); int b=Math.max(x[0],x[1]); String s=a+" "+b; System.out.print(ans.get(s)+" "); } // map=new HashMap<Integer,Integer>(); // for(int x[]:arr){ // if(map.containsKey(x[0])){ // int val=13-map.get(x[0]); // map.put(x[1],val); // System.out.print(val+" "); // }else if(map.containsKey(x[1])){ // int val=13-map.get(x[1]); // map.put(x[0],val); // System.out.print(val+" "); // }else{ // System.out.print(2+" "); // map.put(x[0],2); // map.put(x[1],2); // } // } } public static void dfs(int node,int vist[],List<List<Integer>> adj,HashMap<String,Integer> ans,int val){ vist[node]=1; for(int i:adj.get(node)){ if(vist[i]==1) continue; int x=Math.min(i, node); int y=Math.max(i, node); ans.put(x+" "+y,val); dfs(i,vist,adj,ans,5-val); val=5-val; } } }
import java.util.*; import java.io.*; public class Main{ static final Random random=new Random(); static long mod=1000000007L; static HashMap<String,Integer>map=new HashMap<>(); static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null || !st.hasMoreTokens()){ try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } int[] readIntArray(int n){ int[] res=new int[n]; for(int i=0;i<n;i++)res[i]=nextInt(); return res; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } public static void main(String[] args) { try { FastReader in=new FastReader(); FastWriter out = new FastWriter(); int testCases=in.nextInt(); //out.println("hee"); //int testCases=1; while(testCases-- > 0){ solve(in); } out.close(); } catch (Exception e) { return; } } public static void solve( FastReader in){ int n=in.nextInt(); List<List<Pair>> graph=new ArrayList<>(); for(int i=0;i<n;i++){ graph.add(new ArrayList<>()); } //out.println("hee"); for(int i=0;i<n-1;i++){ int u=in.nextInt(); int v=in.nextInt(); u--;v--; graph.get(u).add(new Pair(v,i)); graph.get(v).add(new Pair(u,i)); } int start=0; for(int i=0;i<n;i++){ if(graph.get(i).size()>2){ System.out.println(""+-1); return; } else if(graph.get(i).size()==1){ start=i; } } int[] ans=new int[n-1]; int[] vis=new int[n+1]; vis[start]=1; int w=2; Queue<Integer> q=new LinkedList<>(); q.add(start); while(!q.isEmpty()){ int x=q.poll(); for(Pair p:graph.get(x)){ if(vis[p.node]==0){ vis[p.node]=1; q.add(p.node); int idx=p.eg_indx; ans[idx]=w; if(w==2)w=5; else w=2; } } } for(int i:ans){ System.out.print(i+" "); } System.out.println(""); } static class Pair{ int node; int eg_indx; Pair(int node,int eg_indx){ this.node=node; this.eg_indx=eg_indx; } } static int gcd(int a,int b){ if(b==0){ return a; } return gcd(b,a%b); } }
0
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 class Pair implements Comparable<Pair> { int x=0,y=0; int z=0; public Pair(int a, int b) { this.x=a; this.y=b; } public int compareTo(Pair o) { return this.x - o.x; } } public static int mod = (int) (1e9 + 7); static int ans = Integer.MAX_VALUE; public static void main(String hi[]) throws Exception { FastReader sc = new FastReader(); int t =sc.nextInt(); while(t-->0) { int n =sc.nextInt(); String a = sc.nextLine(),b=sc.nextLine(); int count1=0,count2=0,count3=0,count4=0; for(int i=0;i<n;i++) { if(a.charAt(i)=='0'&&b.charAt(i)=='0') count1++; else if(a.charAt(i)=='1'&&b.charAt(i)=='1') count2++; else if(a.charAt(i)=='1'&&b.charAt(i)=='0') count3++; else if(a.charAt(i)=='0'&&b.charAt(i)=='1') count4++; } int ans=Integer.MAX_VALUE; if(count3==count4) ans=Math.min(count3*2,ans); if(count2==count1+1) ans=Math.min(ans,2*count1+1); if(count2==1&&count1==0) ans=1; if(count3==0&&count4==0) ans=0; if(ans==Integer.MAX_VALUE) System.out.println(-1); else System.out.println(ans); } } static int find(int[] a,int x) { if(a[x]==-1) return x; else return find(a,a[x]); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // method to return LCM of two numbers static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcdl(long a, long b) { if (a == 0) return b; return gcdl(b % a, a); } // method to return LCM of two numbers static long lcml(long a, long b) { return (a / gcdl(a, b)) * b; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
import javax.swing.plaf.IconUIResource; import java.lang.reflect.Array; import java.text.CollationElementIterator; import java.util.*; import java.io.*; //Timus judge id- 323935JJ public class Main { //---------------------------------------------------------------------------------------------- public static class Pair implements Comparable<Pair> { int x=0,y=0; int z=0; public Pair(int a, int b) { this.x=a; this.y=b; } public int compareTo(Pair o) { return this.x - o.x; } } public static int mod = (int) (1e9 + 7); static int ans = Integer.MAX_VALUE; public static void main(String hi[]) throws Exception { FastReader sc = new FastReader(); int t =sc.nextInt(); while(t-->0) { int n =sc.nextInt(); String a = sc.nextLine(),b=sc.nextLine(); int count1=0,count2=0,count3=0,count4=0; for(int i=0;i<n;i++) { if(a.charAt(i)=='0'&&b.charAt(i)=='0') count1++; else if(a.charAt(i)=='1'&&b.charAt(i)=='1') count2++; else if(a.charAt(i)=='1'&&b.charAt(i)=='0') count3++; else if(a.charAt(i)=='0'&&b.charAt(i)=='1') count4++; } int ans=Integer.MAX_VALUE; if(count3==count4) ans=Math.min(count3*2,ans); if(count2==count1+1) ans=Math.min(ans,2*count1+1); if(ans==Integer.MAX_VALUE) System.out.println(-1); else System.out.println(ans); } } static int find(int[] a,int x) { if(a[x]==-1) return x; else return find(a,a[x]); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // method to return LCM of two numbers static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcdl(long a, long b) { if (a == 0) return b; return gcdl(b % a, a); } // method to return LCM of two numbers static long lcml(long a, long b) { return (a / gcdl(a, b)) * b; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
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 static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } }return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }return str; } } }
import java.io.*; import java.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); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } } }
0
00f79486
ce0b2178
/* In_The_Name_Of_Allah_The_Merciful */ import java.util.*; import java.io.*; public class Main { class Pair implements Comparable<Pair>{ int f,s; Pair(int x,int y){ f=x; s=y; } @Override public int compareTo(Pair a) { if(a.f!=this.f)return Integer.compare(f,a.f); else return Integer.compare(s,a.s); } } PrintWriter out; FastReader sc; long[] m1= {(long)(1e9+7),998244353}; long mod=m1[1]; long maxlong=Long.MAX_VALUE; long minlong=Long.MIN_VALUE; 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(); } /**************************************************************************************** *****************************************************************************************/ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public int ni(){ return sc.nextInt(); }public long nl(){ return sc.nextLong(); }public double nd(){ return sc.nextDouble(); }public char[] rl(){ return sc.nextLine().toCharArray(); }public String rl1(){ return sc.nextLine(); } public void pl(Object s){ out.println(s); } public void pr(Object s){ out.print(s); }public String next(){ return sc.next(); }public long abs(long x){ return Math.abs(x); } public int abs(int x){ return Math.abs(x); } public double abs(double x){ return Math.abs(x); }public long min(long x,long y){ return (long)Math.min(x,y); } public int min(int x,int y){ return (int)Math.min(x,y); } public double min(double x,double y){ return Math.min(x,y); }public long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); }public long lcm(long a, long b) { return (a / gcd(a, b)) * b; } void sort1(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) { l.add(i); } Collections.sort(l,Collections.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort(char[] a) { ArrayList<Character> l = new ArrayList<>(); for (char i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort1(char[] a) { ArrayList<Character> l = new ArrayList<>(); for (char i : a) { l.add(i); } Collections.sort(l,Collections.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort1(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) { l.add(i); } Collections.sort(l,Collections.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } void sort(double[] a) { ArrayList<Double> l = new ArrayList<>(); for (double i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }long pow(long a,long b){ if(b==0){ return 1; }long p=pow(a,b/2); if(b%2==0) return mMultiplication(p,p)%mod; else return (mMultiplication(mMultiplication(p,p),a))%mod; } int swap(int a,int b){ return a; }long swap(long a,long b){ return a; }double swap(double a,double b){ return a; } boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0); }boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0); }public long max(long x,long y){ return (long)Math.max(x,y); } public int max(int x,int y){ return (int)Math.max(x,y); } public double max(double x,double y){ return Math.max(x,y); }long sqrt(long x){ return (long)Math.sqrt(x); }int sqrt(int x){ return (int)Math.sqrt(x); }void input(int[] ar,int n){ for(int i=0;i<n;i++)ar[i]=ni(); }void input(long[] ar,int n){ for(int i=0;i<n;i++)ar[i]=nl(); }void fill(int[] ar,int k){ Arrays.fill(ar,k); }void yes(){ pl("YES"); }void no(){ pl("NO"); } long c2(long n) { return (n*(n-1))/2; } long[] sieve(int n) { long[] k=new long[n+1]; boolean[] pr=new boolean[n+1]; for(int i=1;i<=n;i++){ k[i]=i; pr[i]=true; }for(int i=2;i<=n;i++){ if(pr[i]){ for(int j=i+i;j<=n;j+=i){ pr[j]=false; if(k[j]==j){ k[j]=i; } } } }return k; } int strSmall(int[] arr, int target) { int start = 0, end = arr.length-1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } int strSmall(ArrayList<Integer> arr, int target) { int start = 0, end = arr.size()-1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr.get(mid) > target) { start = mid + 1; ans=start; } else { end = mid - 1; } } return ans; }long mMultiplication(long a,long b) { long res = 0; a %= mod; while (b > 0) { if ((b & 1) > 0) { res = (res + a) % mod; } a = (2 * a) % mod; b >>= 1; } return res; }long nCr(int n, int r ,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i %p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; }long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; }long modInverse(long n, long p) { return power(n, p - 2, p); } int[][] floydWarshall(int graph[][],int INF,int V) { int dist[][] = new int[V][V]; int i, j, k; for (i = 0; i < V; i++) for (j = 0; j < V; j++) dist[i][j] = graph[i][j]; for (k = 0; k < V; k++) { for (i = 0; i < V; i++) { for (j = 0; j < V; j++) { if (dist[i][k] + dist[k][j] < dist[i][j]) dist[i][j] = dist[i][k] + dist[k][j]; } } }return dist; } class minque { Deque<Long> q; minque(){ q=new ArrayDeque<Long>(); }public void add(long p){ while(!q.isEmpty()&&q.peekLast()>p)q.pollLast(); q.addLast(p); }public void remove(long p) { if(!q.isEmpty()&&q.getFirst()==p)q.removeFirst(); }public long min() { return q.getFirst(); } } int find(subset[] subsets, int i) { if (subsets[i].parent != i) subsets[i].parent = find(subsets, subsets[i].parent); return subsets[i].parent; }void Union(subset[] subsets, int x, int y) { int xroot = find(subsets, x); int yroot = find(subsets, y); if (subsets[xroot].rank < subsets[yroot].rank) { subsets[xroot].parent = yroot; } else if (subsets[yroot].rank < subsets[xroot].rank) { subsets[yroot].parent = xroot; } else { subsets[xroot].parent = yroot; subsets[yroot].rank++; } }class subset { int parent; int rank; } }
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; } @Override public int compareTo(Pair p) { if(this.b == p.b) return this.a - p.a; return this.b - p.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; } public static int gcd(int a, int b) { if(a == 0) return b; return gcd(b%a, a); } static boolean isPrime(int n) { if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.sqrt(n); i += 2) if (n % i == 0) return false; return true; } public static void printArray(int arr[]) { for(int i:arr) System.out.print(i+" "); System.out.println(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
0
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 */ public static class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } public static void sort(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for (int x : arr) ls.add(x); Collections.sort(ls); for (int i = 0; i < arr.length; i++) arr[i] = ls.get(i); } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] sieve(int N) { boolean[] sieve = new boolean[N + 1]; for (int i = 2; i <= N; i++) sieve[i] = true; for (int i = 2; i <= N; i++) { if (sieve[i]) { for (int j = 2 * i; j <= N; j += i) { sieve[j] = false; } } } return sieve; } public static long power(long x, long y, long p) { long res = 1L; x = x % p; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y >>= 1; x = (x * x) % p; } return res; } public static void print(int[] arr) { //for debugging only for (int x : arr) out.print(x + " "); out.println(); } static class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] readInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] readLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] readDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } // For Input.txt and Output.txt // FileInputStream in = new FileInputStream("input.txt"); // FileOutputStream out = new FileOutputStream("output.txt"); // PrintWriter pw = new PrintWriter(out); // Scanner sc = new Scanner(in); }
import java.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; } @Override public int compareTo(G obj) { return this.val.compareTo(obj.val); } }
0
8d6f1bf5
e90a198b
//package Div2.C; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Menorah { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while (t > 0) { int n = Integer.parseInt(br.readLine()); String source = br.readLine(); String destination = br.readLine(); int sameStatusOnes = 0; int sameStatusZeros = 0; int diffStatusOnes = 0; int diffStatusZeros = 0; for (int i = 0; i < n; i++) { char c1 = source.charAt(i); char c2 = destination.charAt(i); if (c1 == c2) { if (c1 == '0') { sameStatusZeros += 1; } else { sameStatusOnes += 1; } } else { if (c1 == '0') { diffStatusZeros += 1; } else { diffStatusOnes += 1; } } } int sameStatus = sameStatusOnes + sameStatusZeros; int diffStatus = diffStatusOnes + diffStatusZeros; //first case if (sameStatus == n) { System.out.println(0); } else if (diffStatus == n) { //second case if (diffStatus % 2 == 0 && diffStatusOnes == (n + 1) / 2) System.out.println(n); else System.out.println(-1); } else { int op1 = -1; int op2 = -1; if (sameStatus % 2 != 0 && sameStatusOnes == (sameStatus + 1) / 2) op1 = sameStatus; if (diffStatus % 2 == 0 && diffStatusOnes == (diffStatus + 1) / 2) op2 = diffStatus; if (op1 != -1 && op2 != -1) System.out.println(Integer.min(op1, op2)); else if (op1 != -1) System.out.println(op1); else if (op2 != -1) System.out.println(op2); else System.out.println(-1); } t--; } } }
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 */ public static class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } public static void sort(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for (int x : arr) ls.add(x); Collections.sort(ls); for (int i = 0; i < arr.length; i++) arr[i] = ls.get(i); } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] sieve(int N) { boolean[] sieve = new boolean[N + 1]; for (int i = 2; i <= N; i++) sieve[i] = true; for (int i = 2; i <= N; i++) { if (sieve[i]) { for (int j = 2 * i; j <= N; j += i) { sieve[j] = false; } } } return sieve; } public static long power(long x, long y, long p) { long res = 1L; x = x % p; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y >>= 1; x = (x * x) % p; } return res; } public static void print(int[] arr, PrintWriter out) { //for debugging only for (int x : arr) out.print(x + " "); out.println(); } public static int log2(int a){ return (int)(Math.log(a)/Math.log(2)); } public static long ceil(long x, long y){ return (x + 0l + y - 1) / y; } static class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] readInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] readLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] readDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } // For Input.txt and Output.txt // FileInputStream in = new FileInputStream("input.txt"); // FileOutputStream out = new FileOutputStream("output.txt"); // PrintWriter pw = new PrintWriter(out); // Scanner sc = new Scanner(in); }
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; } } static int BS(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] < element) { low = mid + 1; } else { high = mid; } } if (arr[low] == element) { return low; } else if (arr[high] == element) { return high; } return -1; } static int lower_bound(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] < element) { low = mid + 1; } else { high = mid; } } if (arr[low] >= element) { return low; } else if (arr[high] >= element) { return high; } return -1; } static int upper_bound(int[] arr, int l, int r, int element) { int low = l; int high = r; while (high - low > 1) { int mid = low + (high - low) / 2; if (arr[mid] <= element) { low = mid + 1; } else { high = mid; } } if (arr[low] > element) { return low; } else if (arr[high] > element) { return high; } return -1; } public static long gcd_long(long a,long b){ //a/b,a-> dividant b-> divisor if(b==0)return a; return gcd_long(b, a%b); } public static int gcd_int(int a,int b){ //a/b,a-> dividant b-> divisor if(b==0)return a; return gcd_int(b, a%b); } public static int lcm(int a,int b){ int gcd=gcd_int(a, b); return (a*b)/gcd; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } String nextLine() { String s = ""; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return s; } Long nextLong() { return Long.parseLong(next()); } } } /* * public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){ * return true; } if(n<1 || m<1 || k<0){ return false; } boolean * tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ return true; } * return false; } */
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; class Pair{ public int day; int count; Pair(int d, int c){ day=d; count=c; } } static class Solver { public void solve(InputReader in, PrintWriter out) { int tt=in.nextInt(); //int tt=1; for(int cases=1;cases<=tt;cases++){ int n= in.nextInt(); long odd_min= Long.MAX_VALUE; long even_min= Long.MAX_VALUE; long ans= Long.MAX_VALUE; int odd_length=0; int even_length=0; long total=0; for(int segment=1;segment<=n;segment++){ int cost= in.nextInt(); total+=cost; if(segment%2==1){ odd_min = Math.min(odd_min, cost); odd_length++; } else{ even_min = Math.min(even_min, cost); even_length++; } if(segment>1){ { long value=total-(odd_min+even_min); value+= odd_min * (n-odd_length+1); value+= even_min * (n-even_length+1); ans= Math.min(value, ans); } } } System.out.println(ans); } } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solver solver = new Solver(); solver.solve(in, out); out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.PrintStream; import java.io.PrintWriter; public class A { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable<Pair> { int first, second; public Pair(int aa, int bb) { first = aa; second = bb; } public int compareTo(Pair o) { if(this.second < o.second) return -1; if(this.second > o.second) return +1; return this.first - o.first; } } static class Tuple implements Comparable<Tuple>{ long first, second,third; public Tuple(long first, long second, long third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int[] parent; int[] rank; //Size of the trees is used as the rank public DSU(int n) { parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) //finding through path compression { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public boolean union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return false; //if they are already connected we exit by returning false. // if a's parent is less than b's parent if(rank[a] < rank[b]) { //then move a under b parent[a] = b; } //else if rank of j's parent is less than i's parent else if(rank[a] > rank[b]) { //then move b under a parent[b] = a; } //if both have the same rank. else { //move a under b (it doesnt matter if its the other way around. parent[b] = a; rank[a] = 1 + rank[a]; } return true; } } static class Reader { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) throws IOException { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] longReadArray(int n) throws IOException { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static boolean isPrime(long n) { if(n == 1) { return false; } for(int i = 2;i*i<=n;i++) { if(n%i == 0) { return false; } } return true; } public static List<Integer> SieveList(int n) { boolean prime[] = new boolean[n+1]; Arrays.fill(prime, true); ArrayList<Integer> l = new ArrayList<>(); for (int p=2; p*p<=n; p++) { if (prime[p] == true) { for(int i=p*p; i<=n; i += p) { prime[i] = false; } } } for (int p=2; p<=n; p++) { if (prime[p] == true) { l.add(p); } } return l; } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long modPow(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return modPow(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)(n)] * modInverse(fac[(int)(r)], p) % p * modInverse(fac[(int)(n - r)], p) % p) % p; } public static long fact(long n) { long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (long i = 1; i <= n; i++) fac[(int)(i)] = fac[(int)(i - 1)] * i; return fac[(int)(n)]; } public static long nCr(int n, int k) { long ans = 1; for(int i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } public static void Sort(int[] a) { List<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); // Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void ssort(char[] a) { List<Character> l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } //Modular Operations for Addition and Multiplication. public static long perfomMod(long x) { return ((x%M + M)%M); } public static long addMod(long a, long b) { return perfomMod(perfomMod(a)+perfomMod(b)); } public static long subMod(long a, long b) { return perfomMod(perfomMod(a)-perfomMod(b)); } public static long mulMod(long a, long b) { return perfomMod(perfomMod(a)*perfomMod(b)); } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(int a[], int x) { int l=-1, r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { long INF = 1000000000000000007L; int n = sc.nextInt(); long[] c = new long[n+1]; for(int i = 1;i<=n;i++) c[i] = sc.nextLong(); long ans = INF; long mo = INF, so = 0, co = 0; long me = INF, se = 0, ce = 0; for(int i=1;i<=n;i++) { if(i%2 == 1) { mo = min(mo,c[i]); so += c[i]; co++; } else { me = min(me,c[i]); se += c[i]; ce++; } if(i>=2) { long x = so + (n - co) * mo + se + (n - ce) * me; ans=min(ans,x); } } fout.println(ans); } fout.close(); } }
0
0fd5b95a
6490bbe8
//package codeforces; import java.io.PrintWriter; import java.util.*; public class codeforces { static int dp[][]=new int[5001][5001]; public static void main(String[] args) { Scanner s=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int t=1; for(int tt=0;tt<t;tt++) { int n=s.nextInt(); int a[]=new int[n]; ArrayList<Integer> z=new ArrayList<>(); ArrayList<Integer> o=new ArrayList<>(); for(int i=0;i<n;i++) { a[i]=s.nextInt(); if(a[i]==1) { o.add(i); }else { z.add(i); } } for(int i=0;i<5001;i++) { Arrays.fill(dp[i], -1); } System.out.println(sol(0,0,z,o)); } out.close(); s.close(); } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sortcol(int a[][],int c) { Arrays.sort(a, (x, y) -> { if (x[c] != y[c]) return(int)( x[c] - y[c]); return (int)-(x[1]+x[2] - y[1]-y[2]); }); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static int sol(int i,int j,ArrayList<Integer> z,ArrayList<Integer> o) { if(j==o.size()) { return 0; } int h=z.size()-i; int l=o.size()-j; if(i==z.size()) { return 10000000; } if(dp[i][j]!=-1) { //System.out.println(i+" "+j); return dp[i][j]; } int ans1=sol(i+1,j,z,o); int ans2=sol(i+1,j+1,z,o)+Math.abs(z.get(i)-o.get(j)); dp[i][j]=Math.min(ans1, ans2); return dp[i][j]; } }
import java.util.*; // import java.lang.*; import java.io.*; // THIS TEMPLATE MADE BY AKSH BANSAL. public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static boolean[] isPrime; private static void primes(){ int num = (int)1e6; // PRIMES FROM 1 TO NUM isPrime = new boolean[num]; for (int i = 2; i< isPrime.length; i++) { isPrime[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(isPrime[i] == true) { for(int j = (i*i); j<num; j = j+i) { isPrime[j] = false; } } } } private static long gcd(long a, long b){ if(b==0)return a; return gcd(b,a%b); } private static long pow(long x,long y){ if(y==0)return 1; long temp = pow(x, y/2); if(y%2==1){ return x*temp*temp; } else{ return temp*temp; } } // static ArrayList<Integer>[] adj; // static void getAdj(int n,int q, FastReader sc){ // adj = new ArrayList[n+1]; // for(int i=1;i<=n;i++){ // adj[i] = new ArrayList<>(); // } // for(int i=0;i<q;i++){ // int a = sc.nextInt(); // int b = sc.nextInt(); // adj[a].add(b); // adj[b].add(a); // } // } public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); // primes(); // ________________________________ // int t = sc.nextInt(); // StringBuilder output = new StringBuilder(); // while (t-- > 0) { // output.append(solver()).append("\n"); // } // out.println(output); // _______________________________ int n = sc.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); } out.println(solver(n, arr)); // ________________________________ out.flush(); } public static long solver(int n, int[] arr) { ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); for(int i=0;i<n;i++){ if(arr[i] ==1){ a.add(i); } else{ b.add(i); } } // System.out.println("__"+ a); // System.out.println("__"+ b); long inf = (long)1e10; int aLen = a.size(), bLen = b.size(); long[][] dp = new long[bLen+1][aLen+1]; for(int i=0;i<bLen+1;i++)Arrays.fill(dp[i],inf); // dp[0][0] = 0; for(int i=0;i<=bLen;i++){ dp[i][0] = 0; } for(int i=1;i<=bLen;i++){ for(int j=1;j<=i && j<=aLen;j++){ int aa = a.get(j-1); int bb = b.get(i-1); // System.out.println((i-1)+" "+(j-1)+"__"+ aa+" "+bb); dp[i][j] = Math.min( Math.abs(aa-bb)+dp[i-1][j-1], dp[i-1][j] ); // System.out.println((i-1)+" "+(j-1)+"__"+ dp[i][j]); } } // for(int i=0;i<=bLen;i++){ // for(int j=0;j<=aLen;j++){ // System.out.print(dp[i][j]+" "); // } // System.out.println("__" ); // } return dp[bLen][aLen]==inf?0:dp[bLen][aLen]; } }
0
8d9871a9
eb6cfca7
import java.util.*; public class Main { static Scanner scan = new Scanner(System.in); static int[] readArray(int[] x) { for(int i=0; i<x.length; ++i) x[i] = scan.nextInt(); return x; } static long[] readArray(long[] x) { for(int i=0; i<x.length; ++i) x[i] = scan.nextLong(); return x; } public static void go() { } public static void main(String[] args) { int t = scan.nextInt(); for(int it=0; it<t; ++it) { int n = scan.nextInt(); long[] aa = readArray(new long[n]); long minEven = aa[0]; long minOdd = aa[1]; long sum = aa[0]+aa[1]; long best = n*minEven + n*minOdd; int numOdd = 1; int numEven = 1; for(int i=2; i<n; ++i) { if(i%2 == 0) { minEven = Math.min(aa[i], minEven); numEven++; }else { minOdd = Math.min(aa[i], minOdd); numOdd++; } sum += aa[i]; long score = sum; score += minEven*(n-numEven); score += minOdd*(n-numOdd); best = Math.min(best, score); } System.out.println(best); } } }
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.List; import java.util.Map.Entry; import java.util.TreeMap; public class Main { public Main() throws FileNotFoundException { // File file = Paths.get("input.txt").toFile(); // if (file.exists()) { // System.setIn(new FileInputStream(file)); // } long t = System.currentTimeMillis(); InputReader reader = new InputReader(); int ttt = reader.nextInt(); for (int tt = 0; tt < ttt; tt++) { int n=reader.nextInt(); long[] s=new long[n]; for(int i=0;i<n;i++) { s[i]=reader.nextLong(); } long smallest1=s[0]; long smallest2=s[1]; long val=n*s[0]+n*s[1]; int left1=n-1; int left2=n-1; long base=s[0]+s[1]; for(int i=2;i<n;i++) { if(i%2==0) { //left1 val=Math.min(val, base+left2*smallest2+left1*s[i]); base+=s[i]; smallest1=Math.min(smallest1, s[i]); left1--; }else { val=Math.min(val, base+left1*smallest1+left2*s[i]); base+=s[i]; smallest2=Math.min(smallest2, s[i]); left2--; //left2 } } System.out.println(val); } } static class InputReader { private byte[] buf = new byte[16384]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static void main(String[] args) throws FileNotFoundException { new Main(); } }
0
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(); } private static int gcd(int a, int b) { if (a == 0 || b == 0) return 0; while (b != 0) { int tmp; tmp = a % b; a = b; b = tmp; } return a; } static final long mod = 1000000007; static long pow_mod(long a, long b) { long result = 1; while (b != 0) { if ((b & 1) != 0) result = (result * a) % mod; a = (a * a) % mod; b >>= 1; } return result; } private static long multiplied_mod(long... longs) { long ans = 1; for (long now : longs) { ans = (ans * now) % mod; } return ans; } @SuppressWarnings("FieldCanBeLocal") private static Reader in; private static PrintWriter out; private static int[] read_int_array(int len) throws IOException { int[] a = new int[len]; for (int i = 0; i < len; i++) { a[i] = in.nextInt(); } return a; } private static long[] read_long_array(int len) throws IOException { long[] a = new long[len]; for (int i = 0; i < len; i++) { a[i] = in.nextLong(); } return a; } private static void print_array(int[] array) { for (int now : array) { out.print(now); out.print(' '); } out.println(); } private static void print_array(long[] array) { for (long now : array) { out.print(now); out.print(' '); } out.println(); } static class Reader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { final byte[] buf = new byte[1024]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextSign() throws IOException { byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() throws IOException { int b; // noinspection ALL while ((b = read()) != -1 && isSpaceChar(b)) { ; } return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } }
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(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } } }
0
11373c16
7011024d
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef {static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main (String[] args) throws java.lang.Exception { FastReader scan = new FastReader(); PrintWriter pw = new PrintWriter(System.out); int n = scan.nextInt(); ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); for(int i=0;i<n;i++){ int x = scan.nextInt(); if(x==1) a.add(i); else b.add(i); } int x = a.size(); if(x==0){ pw.println(0); pw.flush(); return; } int y = b.size(); int dp[][] = new int[x][y]; int min = Integer.MAX_VALUE; for(int i=0;i<y;i++){ min = Math.min(Math.abs(a.get(0) - b.get(i)),min); dp[0][i] = min; } for(int i=1;i<x;i++){ min = Integer.MAX_VALUE; for(int j=i;j<y;j++){ min = Math.min(Math.abs(a.get(i)-b.get(j))+dp[i-1][j-1],min); dp[i][j] = min; } } pw.println(dp[x-1][y-1]); pw.flush(); } }
import java.util.*; public class D { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); ArrayList<Integer> occupied = new ArrayList<>(); ArrayList<Integer> vacant = new ArrayList<>(); for (int i = 0; i < n; i++) { int x = scanner.nextInt(); if (x == 1) occupied.add(i); else vacant.add(i); } Solution Solution = new Solution(occupied, vacant); // System.out.println(Solution.tabulation()); System.out.println(Solution.memoization()); } } class Solution { ArrayList<Integer> occupied, vacant; int x, y; public Solution(ArrayList<Integer> occupied, ArrayList<Integer> vacant) { this.occupied = occupied; this.vacant = vacant; x = occupied.size(); y = vacant.size(); } int tabulation() { return tabulation(x, y); } int tabulation(int x, int y) { int[][] dp = new int[x+1][y+1]; for (int i = 0; i <= x; i++) { Arrays.fill(dp[i], Integer.MAX_VALUE/2); } for (int i = 0; i <= x; i++) { dp[i][0] = 0; } for (int i = 0; i <= y; i++) { dp[0][i] = 0; } for (int i = 1; i <= x; i++) { for (int j = 1; j <= y; j++) { if(i == j) { dp[i][j] = dp[i-1][j-1] + Math.abs(occupied.get(i-1) - vacant.get(j-1)); } else { dp[i][j] = Math.min(dp[i][j-1], dp[i-1][j-1] + Math.abs(occupied.get(i-1) - vacant.get(j-1))); } } } return dp[x][y]; } int memoization() { int[][] dp = new int[x][y]; for (int i = 0; i < x; i++) { Arrays.fill(dp[i], -1); } return memoization(dp, x-1, y-1); } int memoization(int[][] dp, int n, int m) { if(n < 0) { return 0; } if(m < n) { return Integer.MAX_VALUE; } if(dp[n][m] != -1) { return dp[n][m]; } int first = memoization(dp, n, m-1); int second = memoization(dp, n-1, m-1) + Math.abs(occupied.get(n) - vacant.get(m)); dp[n][m] = Math.min(first, second); return dp[n][m]; } }
0
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 { static class Pair{ long x; long y; Pair(long x,long y){ this.x = x; this.y = y; } } static class Sort implements Comparator<Pair> { @Override public int compare(Pair a, Pair b) { if(a.x!=b.x) { return (int)(a.x - b.x); } else { return (int)(a.y-b.y); } } } static class Compare { void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.x!=p2.x) { return (int)(p1.x - p2.x); } else { return (int)(p1.y - p2.y); } } }); // for (int i = 0; i < n; i++) { // System.out.print(arr[i].x + " " + arr[i].y + " "); // } // System.out.println(); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } 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); } } class Scanner { static BufferedReader br; static StringTokenizer st = new StringTokenizer(""); public Scanner(InputStream inputStream) { br = new BufferedReader(new InputStreamReader(inputStream)); } public Scanner(String fileName) throws FileNotFoundException { br = new BufferedReader(new FileReader(fileName)); } public boolean hasNext() throws IOException { if (st.hasMoreTokens()) return true; String line; while ((line = br.readLine()) != null) { st = new StringTokenizer(line); if (st.hasMoreTokens()) return true; } return false; } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public String next() throws IOException { hasNext(); return st.nextToken(); } }
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(); } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader f) { br = new BufferedReader(f); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] nextIntArr(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(next()); } return arr; } } }
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; } @Override public int compareTo(Element o) { if(this.value<o.value) return -1; if(this.value>o.value) return 1; return 0; } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int m=sc.nextInt(); int x=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;++i) arr[i]=sc.nextInt(); PriorityQueue<Element> pq=new PriorityQueue<>(); for(int i=1;i<=m;++i) { pq.add(new Element(i,0)); } System.out.println("YES"); for(int j=0;j<n;j++) { Element cur = pq.poll(); System.out.print(cur.key+" "); cur.value+= arr[j]; pq.add(cur); } System.out.println(); } } }
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); } public static void solve1(int n,int arr[]){ int b[]=new int[2*n+1]; for(int i=0;i<n;i++){ b[arr[i]]=i+1; } int count=0; for(int i=3;i<2*n;i++){ for(int j=1;j*j<i;j++){ if(i%j==0 && i!=j*j){ if(b[j]+b[i/j]==i){ count++; } } } } System.out.println(count); } public static long modpow(long a, long b){ if(b==0){ return 1; } long x = modpow(a, b/2); x = (x*x)%modulo; if(b%2==1){ return (x*a)%modulo; } return x; } public static long factorial(long n){ if(n==1) return 1; if(n==2) return 2; if(n==3) return 6; return n*factorial(n-1); } public static long benches(int n){ return ncr(n,5)*n*(n-1)*(n-2)*(n-3)*(n-4); } public static long ncr(int n,int r){ long dp[][]=new long[n+1][r+1]; for(int i=0;i<=n;i++){ for(int j=0;j<=Math.min(i,r);j++){ if(j==0 ||j==i) dp[i][j]=1; else{ dp[i][j]=dp[i-1][j]+dp[i-1][j-1]; } } } return dp[n][r]; } public static long select(int n){ return ncr(n,5)+ncr(n,6)+ncr(n,7); } public static long ncr1(int n,int r){ long k=Math.max(r,n-r); long num=1; long den=1; long j=1; for(long i=n;i>k;i--){ num*=i; den*=j; j++; } return num/den; } public static int countDiff(int arr[],int n,int diff){ int sum=0; for(int i=0;i<n;i++){ sum+=arr[i]; } int max=sum/2; boolean dp[][]=new boolean[n+1][max+1]; for(int i=0;i<=n;i++){ for(int j=0;j<=max;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<=max;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]; } } int min=0; int count=0; for(int j=0;j<=max;j++){ min=0; if(dp[n][j]==true){ min=sum-2*j; if(min==diff) count++; } } return count; } public static int minimumDiff(int arr[],int n){ int sum=0; for(int i=0;i<n;i++){ sum+=arr[i]; } int max=sum/2; boolean dp[][]=new boolean[n+1][max+1]; for(int i=0;i<=n;i++){ for(int j=0;j<=max;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<=max;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]; } } int min=Integer.MAX_VALUE; for(int j=0;j<=max;j++){ if(dp[n][j]==true){ min=Math.min(min,sum-2*j); } } return min; } public static int findTargetSumWays(int[] nums, int target) { int n=nums.length; int dp[][]=new int[n+1][target+1]; for(int i=0;i<=n;i++){ for(int j=0;j<=target;j++){ if(i==0) dp[i][j]=0; if(j==0) dp[i][j]=1; } } for(int i=1;i<=n;i++){ for(int j=1;j<=target;j++){ if(nums[i-1]<=j) dp[i][j]=dp[i-1][j]+dp[i-1][j-nums[i-1]]; else dp[i][j]=dp[i-1][j]; } } // for(int i=0;i<=n;i++){ // for(int j=0;j<=target;j++){ // System.out.print(dp[i][j]+" "); // } // System.out.println(); // } return dp[n][target]; } static int equalPartition(int n, int arr[],int sum) { int max=sum; boolean dp[][]=new boolean[n+1][max+1]; for(int i=0;i<=n;i++){ for(int j=0;j<=max;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<=max;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]; } } } for(int i=0;i<=n;i++){ for(int j=0;j<=max;j++){ System.out.print(dp[i][j]+" "); } System.out.println(); } int count=0; for(int i=0;i<=n;i++){ if(dp[i][max]==true){ count++; } } return count; } public static long power(long base,long power){ long res=1; while(power!=0){ if(power%2==0){ power/=2; base=(base*base); } else{ power--; res=(res*base); } } return res; } } class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } class pair implements Comparable<pair> { int x; int y; pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(pair o) { return (int)(y-o.y); } }
1
0b04b41e
4da08761
import java.io.BufferedReader; import java.io.*; import java.util.*; public class josph { static BufferedReader br; static long mod = 1000000000 + 7; static HashSet<Integer> p = new HashSet<>(); static boolean debug =true; // Arrays.sort(time , (a1,a2) -> (a1[0]-a2[0])); 2d array sort lamda public static void main(String[] args) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pr = new PrintWriter(System.out); int tc = 1; tc= cinI(); while(tc-->0){ int n =cinI(); String[] a= new String[n]; int[][] f =new int[10][n]; for(int i=0;i<n;i++){ a[i]=cin(); char[] x = a[i].toCharArray(); for(char c:x){ int index = c-'a'; f[index][i]+=1; } for(int j=0;j<10;j++){ int rem =x.length-f[j][i]; f[j][i]-=rem; } } int max=0; for(int j=0;j<10;j++){ Arrays.sort(f[j]); int cnt=0; int sum=0; for(int i=n-1;i>=0;i--){ sum+=f[j][i]; if(sum>0){ cnt+=1; } else{ break; } } max=max(max,cnt); } System.out.println(max); } } public static <E> void print(String var ,E e){ if(debug==true){ System.out.println(var +" "+e); } } private static long[] sort(long[] e){ ArrayList<Long> x=new ArrayList<>(); for(long c:e){ x.add(c); } Collections.sort(x); long[] y = new long[e.length]; for(int i=0;i<x.size();i++){ y[i]=x.get(i); } return y; } public static void printDp(long[][] dp) { int n = dp.length; for (int i = 0; i < n; i++) { for (int j = 0; j < dp[0].length; j++) { System.out.print(dp[i][j] + " "); } System.out.println(); } } private static long gcd(long a, long b) { if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a % b, b); return gcd(a, b % a); } public static long min(long a, long b) { return Math.min(a, b); } public static int min(int a, int b) { return Math.min(a, b); } public static void sieve() { int[] pf = new int[100000000 + 1]; //0 prime //1 not prime pf[0] = 1; pf[1] = 1; for (int j = 2; j <= 10000; j++) { if (pf[j] == 0) { p.add(j); for (int k = j * j; k < pf.length; k += j) { pf[k] = 1; } } } } public static int[] readArray(int n, int x, int z) throws Exception { int[] arr = new int[n]; String[] ar = cinA(); for (int i = x; i < n + x; i++) { arr[i] = getI(ar[i - x]); } return arr; } public static long[] readArray(int n, int x) throws Exception { long[] arr = new long[n]; String[] ar = cinA(); for (int i = x; i < n + x; i++) { arr[i] = getL(ar[i - x]); } return arr; } public static void arrinit(String[] a, long[] b) throws Exception { for (int i = 0; i < a.length; i++) { b[i] = Long.parseLong(a[i]); } } public static HashSet<Integer>[] Graph(int n, int edge, int directed) throws Exception { HashSet<Integer>[] tree = new HashSet[n]; for (int j = 0; j < edge; j++) { String[] uv = cinA(); int u = getI(uv[0]); int v = getI(uv[1]); if (directed == 0) { tree[v].add(u); } tree[u].add(v); } return tree; } public static void arrinit(String[] a, int[] b) throws Exception { for (int i = 0; i < a.length; i++) { b[i] = Integer.parseInt(a[i]); } } static double findRoots(int a, int b, int c) { // If a is 0, then equation is not //quadratic, but linear int d = b * b - 4 * a * c; double sqrt_val = Math.sqrt(Math.abs(d)); // System.out.println("Roots are real and different \n"); return Math.max((double) (-b + sqrt_val) / (2 * a), (double) (-b - sqrt_val) / (2 * a)); } public static String cin() throws Exception { return br.readLine(); } public static String[] cinA() throws Exception { return br.readLine().split(" "); } public static String[] cinA(int x) throws Exception { return br.readLine().split(""); } public static String ToString(Long x) { return Long.toBinaryString(x); } public static void cout(String s) { System.out.println(s); } public static Integer cinI() throws Exception { return Integer.parseInt(br.readLine()); } public static int getI(String s) throws Exception { return Integer.parseInt(s); } public static long getL(String s) throws Exception { return Long.parseLong(s); } public static long max(long a, long b) { return Math.max(a, b); } public static int max(int a, int b) { return Math.max(a, b); } public static void coutI(int x) { System.out.println(String.valueOf(x)); } public static void coutI(long x) { System.out.println(String.valueOf(x)); } public static Long cinL() throws Exception { return Long.parseLong(br.readLine()); } public static void arrInit(String[] arr, int[] arr1) throws Exception { for (int i = 0; i < arr.length; i++) { arr1[i] = getI(arr[i]); } } }
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Solution { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Main solver = new Main(); boolean multipleTC = true; int testCount = multipleTC ? Integer.parseInt(in.next()) : 1; for (int i = 1; i <= testCount; i++) solver.solve(in, out, i); out.close(); } static class Main { PrintWriter out; InputReader in; public void solve(InputReader in, PrintWriter out, int test) { this.out = out; this.in = in; int n = ni(); String[] arr = new String[n]; int[][] freq = new int[n][5]; int[][] rem = new int[n][5]; for(int i = 0; i < n; i++){ arr[i] = n(); for(int j = 0; j < arr[i].length(); j++) freq[i][arr[i].charAt(j) - 'a']++; for(int j = 0; j < 5; j++) rem[i][j] = arr[i].length() - freq[i][j]; } int ans = 0; for(int i = 0; i < 5; i++){ int[] vals = new int[n]; for(int j = 0; j < n; j++) vals[j] = freq[j][i] - rem[j][i]; Arrays.sort(vals); int sum = 0, x = 0; for(int j = n - 1; j >= 0; j--){ if(sum + vals[j] > 0){ x++; sum += vals[j]; } else { break; } } if(x > ans) { ans = x; } } pn(ans); } int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } String n(){ return in.next(); } int ni() { return in.nextInt(); } long nl() { return in.nextLong(); } void pn(long zx) { out.println(zx); } void pn(String sz) { out.println(sz); } void pn(double dx){ out.println(dx); } class Tuple { long x; long y; Tuple(long a, long b) { x = a; y = b; } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new UnknownError(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
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); } } } static class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } static void ASSERT(boolean assertion, String message) { if (!assertion) throw new AssertionError(message); } }
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(); } static void debug(Object... O) { System.err.print("DEBUG "); System.err.println(Arrays.deepToString(O)); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextString() { return next(); } } }
0
1230c43e
ce0b2178
//package com.company; 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 HashMap<Integer, Integer> initializeMap(int n){ HashMap<Integer,Integer> hm = new HashMap<>(); for (int i = 0; i <= n; i++) { hm.put(i, 0); } return hm; } static boolean isRegular(char[] c){ Stack<Character> s = new Stack<>(); for (char value : c) { if (s.isEmpty() && value == ')') return false; if (value == '(') s.push(value); else s.pop(); } return s.isEmpty(); } static ArrayList<ArrayList<Integer>> createAdj(int n, int e){ FastReader in = new FastReader(); ArrayList<ArrayList<Integer>> adj = new ArrayList<>(); for (int i = 0; i < n + 1; i++) { adj.add(new ArrayList<>()); } for (int i = 0; i < e; i++) { int a = in.nextInt(), b = in.nextInt(); System.out.println(a); adj.get(a).add(b); adj.get(b).add(a); } return adj; } static int binarySearch(int[] a, int l, int r, int x){ if(r>=l){ int mid = l + (r-l)/2; if(a[mid] == x) return mid; if(a[mid] > x) return binarySearch(a, l, mid-1, x); else return binarySearch(a,mid+1, r, x); } return -1; } static boolean isPalindromeI(int n){ int d = 0; int y = n; while(y>0){ d++; y/=10; } int[] a = new int[d]; for (int i = 0; i < d; i++) { a[i] = n%10; n/=10; } System.out.println(Arrays.toString(a)); for (int i = 0; i < d / 2; i++) { if(a[i] != a[d-i-1]) return false; } return true; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static boolean isSquare(double a) { boolean isSq = false; double b = Math.sqrt(a); double c = Math.sqrt(a) - Math.floor(b); if (c == 0) isSq = true; return isSq; } static int exponentMod(int A, int B, int C) { // Base cases if (A == 0) return 0; if (B == 0) return 1; // If B is even long y; if (B % 2 == 0) { y = exponentMod(A, B / 2, C); y = (y * y) % C; } // If B is odd else { y = A % C; y = (y * exponentMod(A, B - 1, C) % C) % C; } return (int) ((y + C) % C); } static class Pair implements Comparable<Pair>{ char x; int y; Pair(char x, int y){ this.x = x; this.y = y; } public int compareTo(Pair o){ int ans = Integer.compare(x, o.x); if(o.x == x) ans = Integer.compare(y, o.y); return ans; } } static class Tuple implements Comparable<Tuple>{ int x, y, id; Tuple(int x, int y, int id){ this.x = x; this.y = y; this.id = id; } public int compareTo(Tuple o){ int ans = Integer.compare(x, o.x); if(o.x == x) ans = Integer.compare(y, o.y); return ans; } } // 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()); // } // // } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return next().charAt(0); } boolean nextBoolean() { return !(nextInt() == 0); } // boolean nextBoolean(){return Boolean.parseBoolean(next());} String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int[] readArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } } private static int[] mergeSort(int[] array) { //array.length replaced with ctr int ctr = array.length; if (ctr <= 1) { return array; } int midpoint = ctr / 2; int[] left = new int[midpoint]; int[] right; if (ctr % 2 == 0) { right = new int[midpoint]; } else { right = new int[midpoint + 1]; } for (int i = 0; i < left.length; i++) { left[i] = array[i]; } for (int i = 0; i < right.length; i++) { right[i] = array[i + midpoint]; } int[] result = new int[array.length]; left = mergeSort(left); right = mergeSort(right); result = merge(left, right); return result; } private static int[] merge(int[] left, int[] right) { int[] result = new int[left.length + right.length]; int leftPointer = 0, rightPointer = 0, resultPointer = 0; while (leftPointer < left.length || rightPointer < right.length) { if (leftPointer < left.length && rightPointer < right.length) { if (left[leftPointer] < right[rightPointer]) { result[resultPointer++] = left[leftPointer++]; } else { result[resultPointer++] = right[rightPointer++]; } } else if (leftPointer < left.length) { result[resultPointer++] = left[leftPointer++]; } else { result[resultPointer++] = right[rightPointer++]; } } return result; } public static void Sieve(int n) { Arrays.fill(primecheck, true); primecheck[0] = false; primecheck[1] = false; for (int i = 2; i * i < n + 1; i++) { if (primecheck[i]) { for (int j = i * 2; j < n + 1; j += i) { primecheck[j] = false; } } } } }
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; } @Override public int compareTo(Pair p) { if(this.b == p.b) return this.a - p.a; return this.b - p.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; } public static int gcd(int a, int b) { if(a == 0) return b; return gcd(b%a, a); } static boolean isPrime(int n) { if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.sqrt(n); i += 2) if (n % i == 0) return false; return true; } public static void printArray(int arr[]) { for(int i:arr) System.out.print(i+" "); System.out.println(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
0
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; } @Override public int compareTo(Pair x){ return this.first-x.first; } } 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); } } } } } class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
import java.io.*; import java.util.*; public class GFG { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); String a=sc.next(); String b=sc.next(); int i; int zero=0,one=0; int x=0,y=0,x1=0,y1=0; for(i=0;i<n;i++){ if(a.charAt(i)=='0') zero++; else one++; } if(one==0){ if(a.equals(b)) System.out.println("0"); else System.out.println("-1"); } else{ int same=0,diff=0; for(i=0;i<n;i++){ if(a.charAt(i)==b.charAt(i)){ same++; x++; if(a.charAt(i)=='1'){ x1++; } } else{ diff++; y++; if(a.charAt(i)=='1'){ y1++; } } } int ans=Integer.MAX_VALUE; if(x%2!=0&&(x+1)/2==x1){ ans=x; } if(y%2==0&&(y/2)==y1){ ans=Math.min(ans,y); } if(ans==Integer.MAX_VALUE){ System.out.println("-1"); }else{ System.out.println(ans); } } } } }
0
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(); } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
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 { static class Pair{ long x; long y; Pair(long x,long y){ this.x = x; this.y = y; } } static class Sort implements Comparator<Pair> { @Override public int compare(Pair a, Pair b) { if(a.x!=b.x) { return (int)(a.x - b.x); } else { return (int)(a.y-b.y); } } } static class Compare { void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.x!=p2.x) { return (int)(p1.x - p2.x); } else { return (int)(p1.y - p2.y); } } }); // for (int i = 0; i < n; i++) { // System.out.print(arr[i].x + " " + arr[i].y + " "); // } // System.out.println(); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } 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(); } static boolean isPrime(int n){ if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= Math.sqrt(n); i += 2){ if (n % i == 0) return false; } return true; } static void sort(long [] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sort(char [] a) { ArrayList<Character> l=new ArrayList<>(); for (char i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sort(int [] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static int gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b); } static void sortcol(int a[][],int c) { Arrays.sort(a, (x, y) -> { if(x[c]>y[c]) { return 1; }else { return -1; } }); } public static void printb(boolean ans) { if(ans) { System.out.println("Yes"); }else { System.out.println("No"); } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } double nextDouble() { return Double.parseDouble(next()); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static class Pair implements Comparable<Pair>{ int a , b; Pair(int x , int y){ a=x; b=y; } public int compareTo(Pair o) { return a != o.a ? a - o.a : b - o.b; } } }
1
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); } } } }
/** * 12/24/21 morning * https://codeforces.com/contest/1615/problem/C */ // package codeforce.global.r18; import java.util.*; import java.io.*; public class C { static PrintWriter pw; void solve(int n, char[] a, char[] b) { int same = 0, diff = 0; int[] sameCnt = {0, 0}, diffCnt = {0, 0}; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { same++; sameCnt[a[i] - '0']++; } else { diff++; diffCnt[a[i] - '0']++; } } // tr(same, sameCnt, diff, diffCnt); int res = Integer.MAX_VALUE; if (same % 2 != 0 && sameCnt[1] - sameCnt[0] == 1) res = Math.min(res, same); if (diff % 2 == 0 && diffCnt[1] == diffCnt[0]) res = Math.min(res, diff); pr(res == Integer.MAX_VALUE ? -1 : res); } private void run() { // read_write_file(); // comment this before submission FastScanner fs = new FastScanner(); int t = fs.nextInt(); while (t-- > 0) { int n = fs.nextInt(); char[] a = fs.next().toCharArray(), b = fs.next().toCharArray(); solve(n, a, b); } } private final String INPUT = "input.txt"; private final String OUTPUT = "output.txt"; void read_write_file() { FileInputStream instream = null; PrintStream outstream = null; try { instream = new FileInputStream(INPUT); outstream = new PrintStream(new FileOutputStream(OUTPUT)); System.setIn(instream); System.setOut(outstream); } catch (Exception e) { } } public static void main(String[] args) { pw = new PrintWriter(System.out); new C().run(); pw.close(); } void pr(int num) { pw.println(num); } void pr(long num) { pw.println(num); } void pr(double num) { pw.println(num); } void pr(String s) { pw.println(s); } void pr(char c) { pw.println(c); } class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } void tr(Object... o) { pw.println(Arrays.deepToString(o)); } }
0
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*/ static int min(int... ar){int min=Integer.MAX_VALUE;for(int i:ar)min=Math.min(min, i);return min;} static long min(long... ar){long min=Long.MAX_VALUE;for(long i:ar)min=Math.min(min, i);return min;} static int max(int... ar) {int max=Integer.MIN_VALUE;for(int i:ar)max=Math.max(max, i);return max;} static long max(long... ar) {long max=Long.MIN_VALUE;for(long i:ar)max=Math.max(max, i);return max;} static void reverse(int a[]){for(int i=0;i<a.length>>1;i++){int tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}} static void reverse(long a[]){for(int i=0;i<a.length>>1;i++){long tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}} static String reverse(String s){StringBuilder sb=new StringBuilder(s);sb.reverse();return sb.toString();} }
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=1; while(T-->0) { int n=input.nextInt(); int a[]=new int[n]; ArrayList<Integer> list=new ArrayList<>(); ArrayList<Integer> space=new ArrayList<>(); for(int i=0;i<n;i++) { a[i]=input.nextInt(); if(a[i]==1) { list.add(i); } else { space.add(i); } } int pre[]=new int[space.size()]; for(int i=0;i<list.size();i++) { if(i==0) { int min=Integer.MAX_VALUE; for(int j=0;j<space.size();j++) { pre[j]=Math.abs(list.get(i)-space.get(j)); min=Math.min(min,pre[j]); pre[j]=min; } } else { int arr[]=new int[space.size()]; for(int j=0;j<i;j++) { arr[j]=Integer.MAX_VALUE; } int min=Integer.MAX_VALUE; for(int j=i;j<space.size();j++) { int v=Math.abs(list.get(i)-space.get(j)); v+=pre[j-1]; arr[j]=v; min=Math.min(min,v); arr[j]=min; } for(int j=0;j<space.size();j++) { pre[j]=arr[j]; } } } out.println(pre[space.size()-1]); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
0
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(); @SuppressWarnings("unused") private static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
/** * check out my youtube channel sh0rkyboy * https://tinyurl.com/zdxe2y4z * I do screencasts, solutions, and other fun stuff in the future */ 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(); } public static void sort(int[] array){ ArrayList<Integer> copy = new ArrayList<>(); for (int i : array) copy.add(i); Collections.sort(copy); for(int i = 0;i<array.length;i++) array[i] = copy.get(i); } static String[] readArrayString(int n){ String[] array = new String[n]; for(int j =0 ;j<n;j++) array[j] = sc.next(); return array; } static int[] readArrayInt(int n){ int[] array = new int[n]; for(int j = 0;j<n;j++) array[j] = sc.nextInt(); return array; } static int[] readArrayInt1(int n){ int[] array = new int[n+1]; for(int j = 1;j<=n;j++){ array[j] = sc.nextInt(); } return array; } static long[] readArrayLong(int n){ long[] array = new long[n]; for(int j =0 ;j<n;j++) array[j] = sc.nextLong(); return array; } static double[] readArrayDouble(int n){ double[] array = new double[n]; for(int j =0 ;j<n;j++) array[j] = sc.nextDouble(); return array; } static int minIndex(int[] array){ int minValue = Integer.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(long[] array){ long minValue = Long.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(double[] array){ double minValue = Double.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } public static int power(long x, long y, long mod){ long ans = 1; while(y>0){ if (y%2==1) ans = (ans*x)%mod; x = (x*x)%mod; y/=2; } return (int)(ans); } static void verdict(boolean a){ out.println(a ? "YES" : "NO"); } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } //StringJoiner sj = new StringJoiner(" "); //sj.add(strings) //sj.toString() gives string of those stuff w spaces or whatever that sequence is
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(); } static class Pair<X, Y> { X x; Y y; public Pair(X x, Y y) { this.x = x; this.y = y; } } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
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; } public static int gcd(int a, int b) { if(a == 0) return b; return gcd(b%a, a); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
// package Div3_734; 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; } @Override public int compareTo(Story o) { return -this.diff+o.diff; } }
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(); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class D { public static void main(String[] args) { FastScanner fs = new FastScanner(); int cases = fs.nextInt(); while(cases-->0){ int n = fs.nextInt(), k = fs.nextInt(); int[] positions = fs.readArray(k), temps = fs.readArray(k); int[] forced = new int[n]; Arrays.fill(forced, Integer.MAX_VALUE/2); for(int i=0; i<k; i++) forced[positions[i]-1] = temps[i]; for(int i=1; i<n; i++) forced[i] = Math.min(forced[i],forced[i-1]+1); for(int i=n-2; i>=0; i--) forced[i] = Math.min(forced[i], forced[i+1]+1); for(int i=0; i<n; i++) System.out.print(forced[i] + " "); System.out.println(); } } //----------------------------------------------------------------------------------// static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } char nextChar(){ return next().charAt(0); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static class Debug{ public static void debug(long a){ System.out.println("--> "+a); } public static void debug(long a, long b){ System.out.println("--> "+ a +" + " + b); } public static void debug(char a, char b){ System.out.println("--> "+ a +" + " + b); } public static void debug(int[] array){ System.out.print("Array--> "); System.out.println(Arrays.toString(array)); } public static void debug(char[] array){ System.out.print("Array--> "); System.out.println(Arrays.toString(array)); } public static void debug(HashMap<Integer,Integer> map){ System.out.print("Map--> "+map.toString()); } } }
1
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) public void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) // marking smallest prime factor for every // number to be itself. spf[i] = i; // separately marking spf for every even // number as 2 for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { // checking if i is prime if (spf[i] == i) { // marking SPF for all numbers divisible by i for (int j=i*i; j<MAXN; j+=i) // marking spf[j] if it is not // previously marked if (spf[j]==j) spf[j] = i; } } } void sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. prime= new boolean[n + 1]; for (int i = 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; } } } // function to find first index >= x public int lowerIndex(List<Integer> arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) >= x) h = mid - 1; else l = mid + 1; } return l; } public int lowerIndex(int[] arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] >= x) h = mid - 1; else l = mid + 1; } return l; } // function to find last index <= y public int upperIndex(List<Integer> arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) <= y) l = mid + 1; else h = mid - 1; } return h; } public int upperIndex(int[] arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } // function to count elements within given range public int countInRange(List<Integer> arr, int n, int x, int y) { // initialize result int count = 0; count = upperIndex(arr, n, y) - lowerIndex(arr, n, x) + 1; return count; } public int _gcd(int a, int b) { if(b == 0) { return a; } else { return _gcd(b, a % b); } } public int add(int a, int b){ a+=b; if(a>=MOD) a-=MOD; else if(a<0) a+=MOD; return a; } public int mul(int a, int b){ long res= (long)a*(long)b; return (int)(res%MOD); } public int power(int a, int b) { int ans=1; while(b>0){ if((b&1)!=0) ans= mul(ans,a); b>>=1; a= mul(a,a); } return ans; } int[] fact= new int[MAXN]; int[] inv= new int[MAXN]; public int Ckn(int n, int k){ if(k<0 || n<0) return 0; return mul(mul(fact[n],inv[k]),inv[n-k]); } public int inverse(int a){ return power(a,MOD-2); } public void preprocess() { fact[0]=1; for(int i=1;i<MAXN;i++) fact[i]= mul(fact[i-1],i); inv[MAXN-1]= inverse(fact[MAXN-1]); for(int i=MAXN-2;i>=0;i--){ inv[i]= mul(inv[i+1],i+1); } } /** * return VALUE of lower bound for unsorted array */ public int lowerBoundNormalArray(int[] arr, int x){ TreeSet<Integer> set= new TreeSet<>(); for(int num: arr) set.add(num); return set.lower(x); } /** * return VALUE of upper bound for unsorted array */ public int upperBoundNormalArray(int[] arr, int x){ TreeSet<Integer> set= new TreeSet<>(); for(int num: arr) set.add(num); return set.higher(x); } public void debugArr(int[] arr){ for(int i: arr) out.print(i+" "); out.println(); } public int rand(){ int min=0, max= MAXN; int random_int = (int)Math.floor(Math.random()*(max-min+1)+min); return random_int; } 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]; int Add(int a, int b){ a+=b; while (a>=mod)a-=mod; return a; } int Mul(int a, int b){ return (int)(((long)a * b) % mod); } void dfs(int node, int par, int root, int depth){ cnt[root][depth]++; for (int v : g[node]){ if (v != par){ dfs(v, node, root, depth+1); } } } 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); } } class SEG { int n; int[] segs; public SEG (int[] a){ this.n= a.length; segs= new int[4*this.n]; build(a,0,0,this.n-1); } public void build(int[] a, int root, int l, int r){ if(l==r){ segs[root]=a[l]; return; } int m= (l+r)/2; build(a,2*root+1,l,m); build(a,2*root+2,m+1,r); segs[root]= _gcd(segs[2*root+1], segs[2*root+2]); } public int query(int root, int l, int r, int lq, int rq){ if(lq<=l && rq>=r) return segs[root]; if(lq>r || rq<l) return 0; int m= (l+r)/2; int left= query(2*root+1, l, m, lq, rq); int right= query(2*root+2, m+1, r, lq, rq); return _gcd(left,right); } } } static class Venice{ public Map<Long,Long> m= new HashMap<>(); public long base=0; public long totalValue=0; private int M= 1000000007; private long addMod(long a, long b){ a+=b; if(a>=M) a-=M; return a; } public void reset(){ m= new HashMap<>(); base=0; totalValue=0; } public void update(long add){ base= base+ add; } public void add(long key, long val){ long newKey= key-base; m.put(newKey, addMod(m.getOrDefault(newKey,(long)0),val)); } } static class Tuple implements Comparable<Tuple>{ int x, y, z; public Tuple(int x, int y, int z){ this.x= x; this.y= y; this.z=z; } @Override public int compareTo(Tuple o){ return this.x-o.x; } } static class Pair implements Comparable<Pair>{ public int x; public int y; public Pair(int x, int y){ this.x= x; this.y= y; } @Override public int compareTo(Pair o) { return this.x-o.x; } } // public static class compareL implements Comparator<Tuple>{ // @Override // public int compare(Tuple t1, Tuple t2) { // return t2.l - t1.l; // } // } // fast input reader class; static class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } public String nextToken() { while (st == null || !st.hasMoreTokens()) { String line = null; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble(){ return Double.parseDouble(nextToken()); } public long nextLong(){ return Long.parseLong(nextToken()); } public int[] nextIntArr(int n){ int[] arr= new int[n]; for(int i=0;i<n;i++) arr[i]= nextInt(); return arr; } public long[] nextLongArr(int n){ long[] arr= new long[n]; for(int i=0;i<n;i++) arr[i]= nextLong(); return arr; } public List<Integer> nextIntList(int n){ List<Integer> arr= new ArrayList<>(); for(int i=0;i<n;i++) arr.add(nextInt()); return arr; } public int[][] nextIntMatArr(int n, int m){ int[][] mat= new int[n][m]; for(int i=0;i<n;i++) for(int j=0;j<m;j++) mat[i][j]= nextInt(); return mat; } public List<List<Integer>> nextIntMatList(int n, int m){ List<List<Integer>> mat= new ArrayList<>(); for(int i=0;i<n;i++){ List<Integer> temp= new ArrayList<>(); for(int j=0;j<m;j++) temp.add(nextInt()); mat.add(temp); } return mat; } public char[] nextStringCharArr(){ return nextToken().toCharArray(); } } }
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; public final class A { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<Integer> g[]; static long mod=(long)1e9+7,INF=Long.MAX_VALUE; static boolean set[],col[]; static int par[],tot[],partial[]; static int D[],P[][]; static long dp[][],sum=0,max=0,size[]; // static node1 seg[]; //static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)}; public static void main(String args[])throws IOException { int T=i(); outer:while(T-->0) { int N=i(); int size[]=new int[N]; PriorityQueue<node1> q[]=new PriorityQueue[6]; for(int i=0; i<5; i++)q[i]=new PriorityQueue<node1>(); for(int i=0; i<N; i++) { char X[]=in.next().toCharArray(); int s=X.length; size[i]=s; int f[]=new int[5]; for(char x:X)f[x-'a']++; for(int j=0; j<5; j++)q[j].add(new node1(f[j],i,s)); } int max=0; for(int i=0; i<5; i++) { PriorityQueue<node1> q_new=new PriorityQueue<>(); q_new=q[i]; int c=0; long f=0; while(q_new.size()>0) { node1 x=q_new.remove(); // System.out.println(x.f+" "+x.size+" "+x.a); f+=x.a; if(f>0) { c++; max=Math.max(max, c); } else break; } } out.println(max); } out.close(); } static int OR(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("OR "+i+" "+j); return i(); } static int AND(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("AND "+i+" "+j); return i(); } static int XOR(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("XOR "+i+" "+j); return i(); } static boolean f1(int l,char X[]) { int i=0; for(; l<X.length; l++) { if(X[i]!=X[l])return false; i++; } return true; } static int f(int a) { for(int i=a+1; a>0; a++) { if(GCD(i,a)==1)return i; } return 0; } static int min(char X[],char str[],int N) { int s=0; for(int i=0; i<N; i++) { int it=i%3; if(X[i]!=str[it])s++; // ans.append(str[it]); } return s; } static char f(int i,char X[]) { int a=0,b=0,c=0; for(; i<X.length; i+=3) { if(X[i]=='R')a++; if(X[i]=='B')b++; if(X[i]=='G')c++; } if(a>=b && a>=c)return 'R'; if(b>=a && b>=c)return 'B'; return 'G'; } static void f1(int n,int p,long sum,int N) { for(int c:g[n]) { if(c==p)continue; long s=sum+N-2*size[c]; f1(c,n,s,N); max=Math.max(max, s); } } static long f(int i,int j,ArrayList<Integer> A) { if(i+1==A.size()) { return j; } int moves=1+A.get(i); if(j==1)return 1+f(i+1,moves,A); if(j>0 && dp[i][j-1]==0)f(i,j-1,A); return dp[i][j]=dp[i][j-1]+f(i+1,j+moves,A); } // static void build(int v,int tl,int tr,long A[]) // { // if(tl==tr) // { // seg[v]=new node1(A[tl],A[tr],1,true); // return ; // } // int tm=(tl+tr)/2; // build(2*v,tl,tm,A); // build(2*v+1,tm+1,tr,A); // seg[v]=merge(seg[2*v],seg[2*v+1]); // } // static node1 ask(int v,int tl,int tr,int l,int r) // { // if(l>r)return new node1(0,0,0,false);//verify true or false // if(tl==l && tr==r)return seg[v]; // int tm=(tl+tr)/2; // node1 a=ask(v*2,tl,tm,l,Math.min(tm, r)); // node1 b=ask(v*2+1,tm+1,tr,Math.max(tm+1, l),r); // return merge(a,b); // } // static node1 merge(node1 a,node1 b) // { // long s=0; // long l1=a.L,r1=a.R,c1=a.cnt; // long l2=b.L,r2=b.R,c2=b.cnt; // long g=GCD(l2,r1); s=c1+c2; // if(g==1) // { // s--; // g=(l2*r1)/g; // if(c1==1) // { // l1=g; // } // if(c2==1)r2=g; // return new node1(l1,r2,s,true); // } // return new node1(l1,r2,s,a.leaf^b.leaf); // } static long f(long l,long r,long a,long b,long N) { while(r-l>1) { long m=(l+r)/2; long x=m*b; if(N<x) { r=m; continue; } if((N-x)%a==0)r=m; else l=m; } return r; } static long f1(long a,long A[],long bits[],long sum) { long s=A.length; s=mul(s,a); s=(s+(sum%mod))%mod; long p=1L; for(long x:bits) { if((a&p)!=0)s=((s-mul(x,p))+mod)%mod; p<<=1; } return s; } static long f2(long a,long A[],long bits[]) { long s=0; long p=1L; for(long x:bits) { if((a&p)!=0) { s=(s+mul(p,x))%mod; } p<<=1; } return s; } static long f(long x1,long y1,long x2,long y2) { return Math.abs(x1-x2)+Math.abs(y1-y2); } static long f(long x,long max,long s) { long l=-1,r=(x/s)+1; while(r-l>1) { long m=(l+r)/2; if(x-m*s>max)l=m; else r=m; } return l+1; } static int f(long A[],long x) { int l=-1,r=A.length; while(r-l>1) { int m=(l+r)/2; if(A[m]>=x)r=m; else l=m; } return r; } static int bin(int x,ArrayList<Integer> A) { int l=0,r=A.size()-1; while(l<=r) { int m=(l+r)/2; int a=A.get(m); if(a==x)return m; if(a<x)l=m+1; else r=m-1; } return 0; } static int left(int x,ArrayList<Integer> A) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; int a=A.get(m); if(a<=x)l=m; else r=m; } return l; } static int right(int x,ArrayList<Integer> A) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; int a=A.get(m); if(a<x)l=m; else r=m; } return r; } static boolean equal(long A[],long B[]) { for(int i=0; i<A.length; i++)if(A[i]!=B[i])return false; return true; } static int max(int a ,int b,int c,int d) { a=Math.max(a, b); c=Math.max(c,d); return Math.max(a, c); } static int min(int a ,int b,int c,int d) { a=Math.min(a, b); c=Math.min(c,d); return Math.min(a, c); } static HashMap<Integer,Integer> Hash(int A[]) { HashMap<Integer,Integer> mp=new HashMap<>(); for(int a:A) { int f=mp.getOrDefault(a,0)+1; mp.put(a, f); } return mp; } static long mul(long a, long b) { return ( a %mod * 1L * b%mod )%mod; } static void swap(int A[],int a,int b) { int t=A[a]; A[a]=A[b]; A[b]=t; } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { a=find(a); b=find(b); if(a!=b) { par[a]+=par[b]; par[b]=a; } } static boolean isSorted(int A[]) { for(int i=1; i<A.length; i++) { if(A[i]<A[i-1])return false; } return true; } static boolean isDivisible(StringBuilder X,int i,long num) { long r=0; for(; i<X.length(); i++) { r=r*10+(X.charAt(i)-'0'); r=r%num; } return r==0; } static int lower_Bound(int A[],int low,int high, int x) { if (low > high) if (x >= A[high]) return A[high]; int mid = (low + high) / 2; if (A[mid] == x) return A[mid]; if (mid > 0 && A[mid - 1] <= x && x < A[mid]) return A[mid - 1]; if (x < A[mid]) return lower_Bound( A, low, mid - 1, x); return lower_Bound(A, mid + 1, high, x); } static String f(String A) { String X=""; for(int i=A.length()-1; i>=0; i--) { int c=A.charAt(i)-'0'; X+=(c+1)%2; } return X; } static void sort(long[] a) //check for long { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static String swap(String X,int i,int j) { char ch[]=X.toCharArray(); char a=ch[i]; ch[i]=ch[j]; ch[j]=a; return new String(ch); } static int sD(long n) { if (n % 2 == 0 ) return 2; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0 ) return i; } return (int)n; } static void setGraph(int N) { tot=new int[N+1]; partial=new int[N+1]; D=new int[N+1]; P=new int[N+1][(int)(Math.log(N)+10)]; set=new boolean[N+1]; g=new ArrayList[N+1]; for(int i=0; i<=N; i++) { g[i]=new ArrayList<>(); D[i]=Integer.MAX_VALUE; //D2[i]=INF; } } static long pow(long a,long b) { //long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } } class node1 implements Comparable<node1> { int index,f,size; long a; node1(int f,int i,int size) { this.f=f; this.index=i; this.size=size; a=2*f-size; } public int compareTo(node1 x) { if(this.a==x.a)return 0; else if(this.a<x.a)return 1; else return -1; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
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; static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static long[] initArray(int n, Reader scan) throws IOException { long arr[] = new long[n]; for (int i = 0; i < n; i++) { arr[i] = scan.nextLong(); } return arr; } public static long sum(long arr[]) { long sum = 0; for (long i : arr) { sum += (long) i; } return sum; } public static long max(long arr[]) { long max = Long.MIN_VALUE; for (long i : arr) { max = Math.max(i, max); } return max; } public static long min(long arr[]) { long min = Long.MAX_VALUE; for (long i : arr) { min = Math.min(i, min); } return min; } 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 getDepth(int i, int parent, List<Integer> adj[]) { int max=0; for(Integer x: adj[i]) { if(x==parent) continue; max=Math.max(max, getDepth(x, i, adj)); } return max+1; } 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; } public static void C(int n, String s, int k) { char arr[]= s.toCharArray(); for(int i=0;i<n;i++) { if(i+k>=n) break; if(arr[i]=='0'&&arr[i+k]=='1') { System.out.println("NO"); return; } if(arr[i]=='1'&&arr[i+k]=='0') { System.out.println("NO"); return; } if(arr[i]=='0'||arr[i]=='1') { if(arr[i+k]=='?') { arr[i+k]=arr[i]; } continue; } if(arr[i+k]=='0'||arr[i+k]=='1') { if(arr[i]=='?') { arr[i]=arr[i+k]; } continue; } } int c0[]=new int[n]; int c1[]=new int[n]; int last0=0; int last1=0; for(int i=0;i<n;i++) { if(arr[i]=='0') { last0++; }else if(arr[i]=='1'){ last1++; } c0[i]=last0; c1[i]=last1; } for(int i=k-1;i<n;i++) { int count0= c0[i]- ((i-k<0)?0: c0[i-k]); int count1= c1[i]- ((i-k<0)?0: c1[i-k]); if(count0>k/2||count1>k/2) { System.out.println("NO"); return; } } System.out.println("YES"); } public static void A(int n, long arr[]) { for(int i=n-1;i>=0;i--) { System.out.print(arr[i]+" "); } System.out.println(); } public static void B(int n, long arr[]) { for(int i=1;i<n;i++) { if(arr[i-1]>0) { arr[i]+= arr[i-1]; arr[i-1]=0; } } // System.out.println(Arrays.toString(arr)); long ans=0; for(Long x: arr) { if(x<0) ans+= -x; } System.out.println(ans); } } 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 = max(max,distance[i]); if(max-da<=da) { System.out.println("Alice"); return; } System.out.println("Bob"); } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= private static void google(int tt) { System.out.print("Case #" + (tt) + ": "); } static FastScanner sc; static PrintWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new PrintWriter(System.out); } else { sc = new FastScanner(100); out = new PrintWriter("output.txt"); } int t = 1; t = sc.nextInt(); int TTT = 1; while (t-- > 0) { // google(TTT++); process(); } out.flush(); out.close(); } static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Integer.compare(this.x, o.x); } // @Override // public boolean equals(Object o) { // if (this == o) return true; // if (!(o instanceof Pair)) return false; // Pair key = (Pair) o; // return x == key.x && y == key.y; // } // // @Override // public int hashCode() { // int result = x; // result = 31 * result + y; // return result; // } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static void debug(Object o) { System.out.println(o); } static void debug(Object x, Object y) { System.out.println("(" + x + " , " + y + ")"); } static void println(Object o) { out.println(o); } static void println(int[] o) { for (int e : o) print(e + " "); println(); } static void println(long[] o) { for (long e : o) print(e + " "); println(); } static void println() { out.println(); } static void print(Object o) { out.print(o); } static void pflush(Object o) { out.println(o); out.flush(); } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int abs(int x) { return Math.abs(x); } static long abs(long x) { return Math.abs(x); } static long sqrt(long z) { long sqz = (long) Math.sqrt(z); while (sqz * 1L * sqz < z) { sqz++; } while (sqz * 1L * sqz > z) { sqz--; } return sqz; } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static int lower_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(System.in)); } FastScanner(int a) throws FileNotFoundException { br = new BufferedReader(new FileReader("input.txt")); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) throws IOException { int[] A = new int[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextInt(); } return A; } long[] readArrayLong(int n) throws IOException { long[] A = new long[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextLong(); } return A; } } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void reverseArray(int[] a) { int n = a.length; int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } static void reverseArray(long[] a) { int n = a.length; long arr[] = new long[n]; for (int i = 0; i < n; i++) arr[i] = a[n - i - 1]; for (int i = 0; i < n; i++) a[i] = arr[i]; } }
0
0f09b843
45e41c6d
//Created by Aminul on 7/23/2021. 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; } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() throws Exception { while (tokenizer == null || !tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(next()); } } }
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
/* JAI MATA DI */ import java.util.*; import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction; import java.io.*; import java.math.*; import java.sql.Array;; public class Main { static class FR{ BufferedReader br; StringTokenizer st; public FR() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long mod = (long)(1e9 + 7); static void sort(long[] arr ) { ArrayList<Long> al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr ) { ArrayList<Integer> al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static int UB(long[] arr , long find , int l , int r) { while(l<=r) { int m = (l+r)/2; if(arr[m]<find) l = m+1; else r = m-1; } return l; } static int LB(long[] arr , long find,int l ,int r ) { while(l<=r) { int m = (l+r)/2; if(arr[m] > find) r = m-1; else l = m+1; } return r; } static int UB(int[] arr , long find , int l , int r) { while(l<=r) { int m = (l+r)/2; if(arr[m]<find) l = m+1; else r = m-1; } return l; } static int LB(int[] arr , long find,int l ,int r ) { while(l<=r) { int m = (l+r)/2; if(arr[m] > find) r = m-1; else l = m+1; } return r; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i); } static long[][] ncr(int n, int k) { long C[][] = new long[n + 1][k + 1]; int i, j; // Calculate value of Binomial // Coefficient in bottom up manner for (i = 0; i <= n; i++) { for (j = 0; j <= Math.min(i, k); j++) { // Base Cases if (j == 0 || j == i) C[i][j] = 1; // Calculate value using // previously stored values else C[i][j] = (C[i - 1][j - 1] + C[i - 1][j])%mod; } } return C; } static long modInverse(long a, long m) { long g = gcd(a, m); return power(a, m - 2, m); } static long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (int)((p * (long)p) % m); if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); } /* ***************************************************************************************************************************************************/ static FR sc = new FR(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) { int tc = 1; tc = sc.nextInt(); while(tc-->0) { TEST_CASE(); } System.out.println(sb); } static void TEST_CASE() { int n = sc.nextInt(); ; int[] arr = new int[n]; for(int i =0 ; i<n;i++) { arr[i] = sc.nextInt(); } boolean cond = false; Set<Integer> set = new HashSet<>(); set.add(0); for(int i =0 ; i<n ; i++) { ArrayList<Integer> al = new ArrayList<>(set); for(int e:al) { int num = e+arr[i]; if(set.contains(num)) { cond = true; break; } set.add(num); } } if(cond) System.out.println("YES"); else System.out.println("NO"); } }
// package codeforce.cfGR15; import java.io.PrintWriter; import java.util.*; public class D { // MUST SEE BEFORE SUBMISSION // check whether int part would overflow or not, especially when it is a * b!!!! public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); // int t = 1; for (int i = 0; i < t; i++) { solve(sc, pw); } pw.close(); } static void solve(Scanner in, PrintWriter out){ int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } Set<Integer> set = new HashSet<>(); for(int x : arr) { if (x == 0){ out.println("YES"); return; }else if (set.contains(x) || set.contains(-x)){ out.println("YES"); return; } set.add(x); } for (int i = 0; i < n; i++) { if (dfs(0, i, arr, 0)){ out.println("YES"); return; } } out.println("NO"); } static boolean dfs(int idx, int need, int[] arr, int cur){ if (cur == arr[need]) return true; if (idx == arr.length) return false; if (idx == need) return dfs(idx + 1, need, arr, cur); return dfs(idx + 1, need, arr, cur + arr[idx]) | dfs(idx + 1, need, arr, cur - arr[idx]) | dfs(idx + 1, need, arr, cur); } }
0
5d175166
d1391025
import java.io.*; import java.util.*; public class A { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static 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) { w.p(-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) { w.pr(i+" "); } w.pl(); } 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; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof pr)) return false; pr<?, ?> pr = (pr<?, ?>) o; return a.equals(pr.a) && b.equals(pr.b); } @Override public int hashCode() { return Objects.hash(a, 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 double nextDouble() throws IOException {return Double.parseDouble(next());} static long nextLong() throws IOException {return Long.parseLong(next());} } static long min(long a, long b) {return Math.min(a, b);} static long min(long a, long b, long c) {return min(a, min(b, c));} static int min(int a, int b) {return Math.min(a, b);} static int min(int a, int b, int c) {return min(a, min(b, c));} static long max(long a, long b) {return Math.max(a, b);} static long max(long a, long b, long c) {return max(a, max(b, c));} static int max(int a, int b) {return Math.max(a, b);} static int max(int a, int b, int c) {return max(a, max(b, c));} static int abs(int x) {return Math.abs(x);} static long abs(long x) {return Math.abs(x);} static long ceil(double x) {return (long) Math.ceil(x);} static long floor(double x) {return (long) Math.floor(x);} static int ceil(int x) {return (int) Math.ceil(x);} static int floor(int x) {return (int) Math.floor(x);} static double sqrt(double x) {return Math.sqrt(x);} static double cbrt(double x) {return Math.cbrt(x);} static long sqrt(long x) {return (long) Math.sqrt(x);} static long cbrt(long x) {return (long) Math.cbrt(x);} static int gcd(int a, int b) {if(b == 0)return a;return gcd(b, a % b);} static long gcd(long a, long b) {if(b == 0)return a;return gcd(b, a % b);} static double pow(double n, double power) {return Math.pow(n, power);} static long pow(long n, double power) {return (long) Math.pow(n, power);} 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 class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static final long mod=(long)1e9+7; public static long pow(long a,int p) { long res=1; while(p>0) { if(p%2==1) { p--; res*=a; res%=mod; } else { a*=a; a%=mod; p/=2; } } return res; } static class Pair { int u,v,w; Pair(int u,int v,int w) { this.u=u; this.v=v; this.w=w; } } /*static class Pair implements Comparable<Pair> { int v,l; Pair(int v,int l) { this.v=v; this.l=l; } public int compareTo(Pair p) { return l-p.l; } }*/ static int gcd(int a,int b) { if(b%a==0) return a; return gcd(b%a,a); } public static void dfs(int u,int dist[],int sub[],int mxv[],int par[],ArrayList<Integer> edge[]) { sub[u]=1; for(int v:edge[u]) { if(dist[v]==-1) { par[v]=u; dist[v]=dist[u]+1; dfs(v,dist,sub,mxv,par,edge); if(sub[v]+1>sub[u]) { sub[u]=sub[v]+1; mxv[u]=v; } } } } public static void main(String args[])throws Exception { FastReader fs=new FastReader(); PrintWriter pw=new PrintWriter(System.out); //int tc=fs.nextInt(); int n=fs.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=fs.nextInt(); ArrayList<Integer> o=new ArrayList<>(); ArrayList<Integer> z=new ArrayList<>(); for(int i=0;i<n;i++) { if(a[i]==1)o.add(i); else z.add(i); } int ans[][]=new int[o.size()+1][z.size()+1]; for(int i=1;i<=o.size();i++) { for(int j=i;j<=z.size();j++) { if(i==j)ans[i][j]=ans[i-1][j-1]+(int)Math.abs(o.get(i-1)-z.get(j-1)); else ans[i][j]=Math.min(ans[i][j-1],ans[i-1][j-1]+(int)Math.abs(o.get(i-1)-z.get(j-1))); } } pw.println(ans[o.size()][z.size()]); pw.flush(); pw.close(); } }
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=1; while(T-->0) { int n=input.nextInt(); int a[]=new int[n]; ArrayList<Integer> list=new ArrayList<>(); ArrayList<Integer> space=new ArrayList<>(); for(int i=0;i<n;i++) { a[i]=input.nextInt(); if(a[i]==1) { list.add(i); } else { space.add(i); } } int pre[]=new int[space.size()]; for(int i=0;i<list.size();i++) { if(i==0) { int min=Integer.MAX_VALUE; for(int j=0;j<space.size();j++) { pre[j]=Math.abs(list.get(i)-space.get(j)); min=Math.min(min,pre[j]); pre[j]=min; } } else { int arr[]=new int[space.size()]; for(int j=0;j<i;j++) { arr[j]=Integer.MAX_VALUE; } int min=Integer.MAX_VALUE; for(int j=i;j<space.size();j++) { int v=Math.abs(list.get(i)-space.get(j)); v+=pre[j-1]; arr[j]=v; min=Math.min(min,v); arr[j]=min; } for(int j=0;j<space.size();j++) { pre[j]=arr[j]; } } } out.println(pre[space.size()-1]); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
0
343cc8e7
7f69a9e8
//make sure to make new file! 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]); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
0
68cdd55d
c18048bf
import java.io.*; import java.util.*; public class Main { static PrintWriter out; static Kioken sc; public static void main(String[] args) throws FileNotFoundException { boolean t = true; boolean f = false; if (f) { out = new PrintWriter("output.txt"); sc = new Kioken("input.txt"); } else { out = new PrintWriter((System.out)); sc = new Kioken(); } 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(); } public static long gcd(long a, long b) { while (b != 0) { long rem = a % b; a = b; b = rem; } return a; } public static long leftShift(long a) { return (long) Math.pow(2, a); } public static void reverse(int[] arr) { Arrays.sort(arr); int n = arr.length; for (int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[n - 1 - i]; arr[n - 1 - i] = temp; } return; } public static int lower_bound(ArrayList<Integer> ar, int k) { int s = 0, e = ar.size(); while (s != e) { int mid = s + e >> 1; if (ar.get(mid) <= k) { s = mid + 1; } else { e = mid; } } return Math.abs(s) - 1; } public static int upper_bound(ArrayList<Integer> ar, int k) { int s = 0; int e = ar.size(); while (s != e) { int mid = s + e >> 1; if (ar.get(mid) < k) { s = mid + 1; } else { e = mid; } } if (s == ar.size()) { return -1; } return s; } static class Kioken { // FileInputStream br = new FileInputStream("input.txt"); BufferedReader br; StringTokenizer st; Kioken(String filename) { try { FileReader fr = new FileReader(filename); br = new BufferedReader(fr); st = new StringTokenizer(""); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } Kioken() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); } return null; } public boolean hasNext() { String next = null; try { next = br.readLine(); } catch (Exception e) { } if (next == null || next.length() == 0) { return false; } st = new StringTokenizer(next); return true; } } }
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef {static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // 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; } public boolean equals(Object obj) { // TODO Auto-generated method stub if(obj instanceof Pair) { Pair temp = (Pair) obj; if(this.x.equals(temp.x) && this.y.equals(temp.y)) return true; } return false; } @Override public int hashCode() { // TODO Auto-generated method stub return (this.x.hashCode() + this.y.hashCode()); } } //class Compar implements Comparator<Pair>{ // public int compare(Pair p1,Pair p2){ // if(p1.x==p2.x) // return 0; // else if(p1.x<p2.x) // return -1; // else // return 1; // } //} //class Node{ // int src,dest,weight; // Node(int src,int dest,int weight){ // this.src = src; // this.dest = dest; // this.weight = weight; // } //}
0
53d782a0
5769b7b3
import java.io.*; import java.util.*; public class B { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(); long k = sc.nl(); Long[] arr = new Long[n]; for(int i = 0; i < n; i++) arr[i] = sc.nl(); Arrays.sort(arr); long sum = 0; for(int i = 0; i < n; i++) { sum += arr[i]; } if(sum <= k) { w.p(0); return; } long cont = 0; long min = sum-k; for(int i = n-1; i >= 0; i--) { cont += arr[i]; long psum = sum-cont; if(psum <= k) { long extra = k-psum; if(arr[0]*(n-i)<=extra) { min = Math.min(min, n-i); continue; } if(i == 0) { long q = k/n; long ans = arr[0]-q+n-1; min = Math.min(min, ans); continue; } extra += arr[0]; long q = extra/(n-i+1); long ans = arr[0]-q+n-i; min = Math.min(min, ans); } else { long toRem = psum-k-arr[0]; long q = (long)Math.ceil(toRem/(n-i+1.0)); long ans = q+arr[0]+n-i; min = Math.min(min, ans); } } w.p(min); } }
import java.io.*; import java.util.*; public class B { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(); long k = sc.nl(); Long[] arr = new Long[n]; for(int i = 0; i < n; i++) arr[i] = sc.nl(); Arrays.sort(arr); long sum = 0; for(int i = 0; i < n; i++) { sum += arr[i]; } if(sum <= k) { w.p(0); return; } long cont = 0; long min = sum-k; for(int i = n-1; i >= 0; i--) { cont += arr[i]; long psum = sum-cont; if(psum <= k) { long extra = k-psum; if(arr[0]*(n-i)<=extra) { min = Math.min(min, n-i); continue; } if(i == 0) { long q = k/n; long ans = arr[0]-q+n-1; min = Math.min(min, ans); continue; } extra += arr[0]; long q = extra/(n-i+1); long ans = arr[0]-q+n-i; min = Math.min(min, ans); } else { long toRem = psum-k-arr[0]; long q = (long)Math.ceil(toRem/(n-i+1.0)); long ans = q+arr[0]+n-i; min = Math.min(min, ans); } } w.p(min); } }
1
69b2fd22
e6b7a899
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=1; while(T-->0) { int n=input.nextInt(); int a[]=new int[n]; ArrayList<Integer> list=new ArrayList<>(); ArrayList<Integer> space=new ArrayList<>(); for(int i=0;i<n;i++) { a[i]=input.nextInt(); if(a[i]==1) { list.add(i); } else { space.add(i); } } int pre[]=new int[space.size()]; for(int i=0;i<list.size();i++) { if(i==0) { int min=Integer.MAX_VALUE; for(int j=0;j<space.size();j++) { pre[j]=Math.abs(list.get(i)-space.get(j)); min=Math.min(min,pre[j]); pre[j]=min; } } else { int arr[]=new int[space.size()]; for(int j=0;j<i;j++) { arr[j]=Integer.MAX_VALUE; } int min=Integer.MAX_VALUE; for(int j=i;j<space.size();j++) { int v=Math.abs(list.get(i)-space.get(j)); v+=pre[j-1]; arr[j]=v; min=Math.min(min,v); arr[j]=min; } for(int j=0;j<space.size();j++) { pre[j]=arr[j]; } } } out.println(pre[space.size()-1]); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.*; public class Main { static final long MOD=1000000007; static final long MOD1=998244353; static long ans=0; //static ArrayList<Integer> ans=new ArrayList<>(); public static void main(String[] args){ PrintWriter out = new PrintWriter(System.out); InputReader sc=new InputReader(System.in); int N = sc.nextInt(); int[] A = sc.nextIntArray(N); ArrayList<Integer> a1 = new ArrayList<Integer>(); ArrayList<Integer> a2 = new ArrayList<Integer>(); for (int i = 0; i < A.length; i++) { if (A[i]==0) { a1.add(i); }else { a2.add(i); } } int[][] dp = new int[a1.size()+1][a2.size()+1]; for (int i = 0; i < dp.length; i++) { Arrays.fill(dp[i], Integer.MAX_VALUE/2); } dp[0][0] = 0; for (int i = 1; i <= a1.size() ; i++) { int pos1 = a1.get(i-1); for (int j = 0; j <= a2.size(); j++) { dp[i][j] = dp[i-1][j]; if (j-1>=0) { int pos2 = a2.get(j-1); dp[i][j] = Math.min(dp[i][j], dp[i-1][j-1] + Math.abs(pos1-pos2)); } } } System.out.println(dp[a1.size()][a2.size()]); } static class InputReader { private InputStream in; private byte[] buffer = new byte[1024]; private int curbuf; private int lenbuf; public InputReader(InputStream in) { this.in = in; this.curbuf = this.lenbuf = 0; } public boolean hasNextByte() { if (curbuf >= lenbuf) { curbuf = 0; try { lenbuf = in.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return false; } return true; } private int readByte() { if (hasNextByte()) return buffer[curbuf++]; else return -1; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private void skip() { while (hasNextByte() && isSpaceChar(buffer[curbuf])) curbuf++; } public boolean hasNext() { skip(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public double[] nextDoubleArray(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[][] nextCharMap(int n, int m) { char[][] map = new char[n][m]; for (int i = 0; i < n; i++) map[i] = next().toCharArray(); return map; } } }
0
680ba922
76ad805a
import java.util.*; import java.io.*; public class Solution { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static int parent(int a , int p[]) { if(a == p[a]) return a; return p[a] = parent(p[a],p); } static void union(int a , int b , int p[] , int size[]) { a = parent(a,p); b = parent(b,p); if(a == b) return; if(size[a] < size[b]) { int temp = a; a = b; b = temp; } p[b] = a; size[a] += size[b]; } static long getSum(int index , long BITree[]) { long sum = 0; // Iniialize result // index in BITree[] is 1 more than // the index in arr[] // index = index + 1; // Traverse ancestors of BITree[index] while(index>0) { // Add current element of BITree // to sum sum += BITree[index]; // Move index to parent node in // getSum View index -= index & (-index); } return sum; } // Updates a node in Binary Index Tree (BITree) // at given index in BITree. The given value // 'val' is added to BITree[i] and all of // its ancestors in tree. public static void updateBIT(int n, int index, long val , long BITree[]) { // index in BITree[] is 1 more than // the index in arr[] // index = index + 1; // Traverse all ancestors and add 'val' while(index <= n) { // Add 'val' to current node of BIT Tree BITree[index] += val; // Update index to that of parent // in update View index += index & (-index); } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int dp[][]; static int f(int pos , int take , int arr[]) { if(pos == -1) { if(take == 0) return 0; return -10000000; } if(dp[pos][take] != -1) return dp[pos][take]; if(pos+1-take == arr[pos]) dp[pos][take] = Math.max(dp[pos][take],1 + f(pos-1,take,arr)); dp[pos][take] = Math.max(dp[pos][take],f(pos-1,take,arr)); if(take > 0) dp[pos][take] = Math.max(dp[pos][take],f(pos-1,take-1,arr)); return dp[pos][take]; } public static void main(String []args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); sc.nextLine(); String a = sc.nextLine(); String b = sc.nextLine(); int same = 0 , zo = 0 , oz = 0 , oo = 0 , zz = 0; for(int i = 0 ; i < n ; i++) { if(a.charAt(i) == '0' && b.charAt(i) == '1') oz++; else if(a.charAt(i) == '1' && b.charAt(i) == '0') zo++; else if(a.charAt(i) == '1' && b.charAt(i) == '1') oo++; else zz++; } if(oz == zo || (zz == oo-1)) { int mx = Integer.MAX_VALUE; if(oz == zo) mx = Math.min(mx,2*oz); if(oo-1 == zz) mx = Math.min(mx,zz+oo); System.out.println(mx); } else { System.out.println(-1); } } } }
import java.util.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); } } } //9 //001011011 //011010101 3 2 2 2 //9 //100010111 //101101100 2 1 3 3
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); } } static boolean isPrime(int n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static void print(int x,int y,int d,int n) { int i = 0; System.out.print(x+" "+y+" "); for(int j = x+d;j<y;j+=d) { System.out.print(j+" "); i++; } for(int j = x-d;j>0;j-=d) { if(i==n)return; System.out.print(j+" "); i++; } for(int j = y+d;j<1000000000;j+=d) { if(i==n)return; System.out.print(j+" "); i++; } } static int prime(int n){ int ret = 0; while(n%2==0){ ret++; n/=2; } for(int i=3;i<=Math.sqrt(n);i+=2){ while(n%i==0){ ret++; n/=i; } } if(n>2)ret++; return ret; } static long nCr(int n, int r) { if(n<r) { return 0; } long[] C=new long[r+1]; C[0] = 1; for (int i = 1; i <= n; i++) { for (int j = Math.min(i, r); j > 0; j--) C[j] = (C[j] + C[j-1]); } return C[r]; } static int power(int a,int n, int p) { int res = 1; a = a % p; while (n > 0) { if ((n & 1) == 1) res = (res * a) % p; n = n >> 1; a = (a * a) % p; } return res; } static boolean isPrime(int n, int k) { if (n <= 1 || n == 4) return false; if (n <= 3) return true; while (k > 0) { int a = 2 + (int)(Math.random() % (n - 4)); if (power(a, n - 1, n) != 1) return false; k--; } return true; } static long GCD(long a,long b) { if(a%b==0)return b; else return GCD(b,a%b); } static ArrayList<Integer> readArray(FastReader f,int size){ ArrayList<Integer> ret = new ArrayList<Integer>(); for(int i=0;i<size;i++) { ret.add(f.nextInt()); }return ret; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
import java.util.*; 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]); } /*****************************************************************************/ static class CP { BufferedReader bufferedReader; StringTokenizer stringTokenizer; public CP() { bufferedReader = new BufferedReader(new InputStreamReader(System.in)); } int nextInt() { return Integer.parseInt(NNNN()); } long nextLong() { return Long.parseLong(NNNN()); } double nextDouble() { return Double.parseDouble(NNNN()); } String NNNN() { while (stringTokenizer == null || !stringTokenizer.hasMoreElements()) { try { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return stringTokenizer.nextToken(); } String nextLine() { String spl = ""; try { spl = bufferedReader.readLine(); } catch (IOException e) { e.printStackTrace(); } return spl; } } /*****************************************************************************/ }
// "static void main" must be defined in a public class. import java.util.*; import java.io.*; public class Main { static long[][]dp; static ArrayList<Integer>[]adj; static int[][]arr; static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } 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
//package codeforces.globalround18; import java.io.*; import java.util.*; import static java.lang.Math.*; //Think through the entire logic before jump into coding! //If you are out of ideas, take a guess! It is better than doing nothing! //Read both C and D, it is possible that D is easier than C for you! //Be aware of integer overflow! //If you find an answer and want to return immediately, don't forget to flush before return! public class C { static InputReader in; static PrintWriter out; public static void main(String[] args) { //initReaderPrinter(true); initReaderPrinter(false); solve(in.nextInt()); //solve(1); } static void solve(int testCnt) { for (int testNumber = 0; testNumber < testCnt; testNumber++) { int n = in.nextInt(); char[] a = in.next().toCharArray(), b = in.next().toCharArray(); int match0 = 0, match1 = 0, mismatch10 = 0, mismatch01 = 0; for(int i = 0; i < n; i++) { if(a[i] == b[i]) { if(a[i] == '0') match0++; else match1++; } else { if(a[i] == '0') mismatch01++; else mismatch10++; } } if(mismatch01 + mismatch10 == 0) out.println(0); else { if(match1 - match0 == 1 && mismatch01 == mismatch10) { out.println(min(match0 + match1, mismatch01 + mismatch10)); } else if(match1 - match0 == 1) { out.println(match0 + match1); } else if(mismatch01 == mismatch10) { out.println(mismatch01 + mismatch10); } else out.println(-1); } } out.close(); } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream("src/input.in")); out = new PrintWriter(new FileOutputStream("src/output.out")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitive(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitiveOneIndexed(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } Long[] nextLongArray(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitive(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitiveOneIndexed(int n) { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } String[] nextStringArray(int n) { String[] g = new String[n]; for (int i = 0; i < n; i++) g[i] = next(); return g; } List<Integer>[] readGraphOneIndexed(int n, int m) { List<Integer>[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); adj[u].add(v); adj[v].add(u); } return adj; } List<Integer>[] readGraphZeroIndexed(int n, int m) { List<Integer>[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; adj[u].add(v); adj[v].add(u); } return adj; } /* A more efficient way of building a graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]] = end2[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]] = end1[i]; idxForEachNode[end2[i]]++; } return adj; } } }
import java.util.*; import java.io.*; public class codeforces { public static void main(String[] args) throws Exception { int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); char[] a=sc.next().toCharArray(); char[] b=sc.next().toCharArray(); int e0=0; int e1=0; int o0=0; int o1=0; for(int i=0;i<n;i++) { if(a[i]!=b[i]) { if(a[i]=='1') { e1++; }else { e0++; } }else { if(a[i]=='1') { o1++; }else { o0++; } } } int ans=Integer.MAX_VALUE; if(e1==e0) { ans=Math.min(ans, e1+e0); } if(o1==o0+1) { ans=Math.min(ans, o1+o0); } // pw.println(e0+" "+e1+" "+o0+" "+o1); pw.println(ans==Integer.MAX_VALUE?-1:ans); } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } static long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
0
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; } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } } 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])); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { this.br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try{ st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
0
6e7cd58b
7279bcb3
import java.util.*; import java.io.*; public class CodeforcesRound734 { static FastReader sc = new FastReader(); public static void main(String[] args) throws IOException { try { int t = sc.nextInt(); while (t-- > 0) { // A(); // B1(); // B2(); C(); } } catch (Exception e) { // return; e.printStackTrace(); } } static void C() { int n = sc.nextInt(); int a[][] = new int[5][n]; for (int i = 0; i < n; i++) { String s = sc.next(); for (int j = 0; j < s.length(); j++) { if (s.charAt(j) == 'a') a[0][i]++; else a[0][i]--; if (s.charAt(j) == 'b') a[1][i]++; else a[1][i]--; if (s.charAt(j) == 'c') a[2][i]++; else a[2][i]--; if (s.charAt(j) == 'd') a[3][i]++; else a[3][i]--; if (s.charAt(j) == 'e') a[4][i]++; else a[4][i]--; } } for (int x[] : a) { Arrays.sort(x); } int ans = 0; for (int i = 0; i < 5; i++) { int temp = 0, cnt = 0; for (int j = n - 1; j >= 0; j--) { temp += a[i][j]; if (temp <= 0) break; else cnt++; } ans = Math.max(ans, cnt); } System.out.println(ans); } static void B2() { int n = sc.nextInt(); int k = sc.nextInt(); ArrayList<ArrayList<Integer>> al = new ArrayList<>(); for (int i = 0; i <= n; i++) { al.add(new ArrayList<>()); } int ans[] = new int[n + 1]; for (int i = 0; i < n; i++) { int x = sc.nextInt(); al.get(x).add(i + 1); } ArrayList<Integer> temp = new ArrayList<>(); for (ArrayList<Integer> ind : al) { if (ind.size() >= k) { for (int i = 0; i < k; i++) { ans[ind.get(i)] = i + 1; } } else { temp.addAll(ind); } } int color = 0; for (int i = 0; i < temp.size() / k; i++) { for (int j = i * k; j < i * k + k; j++) { ans[temp.get(j)] = ++color; if (color == k) color = 0; } } for (int i = 1; i <= n; i++) { System.out.print(ans[i] + " "); } System.out.println(); } static void B1() { String s = sc.next(); HashMap<Character, Integer> map = new HashMap<>(); for (char ch : s.toCharArray()) { map.put(ch, map.getOrDefault(ch, 0) + 1); } if (s.length() < 2) { System.out.println(0); return; } int s1 = 0; for (int v : map.values()) { if (v > 2) { s1 += 2; } else { s1 += v; } } System.out.println(s1 / 2); } static void A() { int n = sc.nextInt(); if (n % 3 == 0) { System.out.println(n / 3 + " " + n / 3); } else if (n % 3 == 1) { System.out.println((n / 3 + 1) + " " + (n / 3)); } else { System.out.println((n / 3) + " " + (n / 3 + 1)); } } static boolean[] seiveOfEratosthenes(int n) { boolean[] isPrime = new boolean[n + 1]; Arrays.fill(isPrime, true); for (int i = 2; i * i <= n; i++) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } return isPrime; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static boolean isPrime(long n) { if (n < 2) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(next()); } return a; } void printIntArray(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } long[] readLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = Long.parseLong(next()); } return a; } void printLongArray(long a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
import java.util.*; public class main { public static final Scanner sc = new Scanner(System.in); public static int gcd(int a,int b) { return b==0?a:gcd(b, a%b); } public static int max(int a,int b) { if(a>b) return a; else return b; } public static void solve(){ int n=sc.nextInt(); int[][] a=new int[5][n]; for(int i=0;i<n;i++){ String x=sc.next(); for(int j=0;j<x.length();j++){ if(x.charAt(j)=='a') a[0][i]++; else a[0][i]--; if(x.charAt(j)=='b') a[1][i]++; else a[1][i]--; if(x.charAt(j)=='c') a[2][i]++; else a[2][i]--; if(x.charAt(j)=='d') a[3][i]++; else a[3][i]--; if(x.charAt(j)=='e') a[4][i]++; else a[4][i]--; } } for(int j=0;j<5;j++) Arrays.sort(a[j]); int ans=0; for(int j=0;j<5;j++){ int tmp=0,cnt=0; for(int i=n-1;i>=0;i--){ tmp+=a[j][i]; if(tmp<=0) break; else cnt++; } ans=max(ans,cnt); } System.out.println(ans); } public static void main(String[] args) { int T=1; T=sc.nextInt(); while(--T>=0) solve(); } }
1
1500a4fa
d7a8434f
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); } } } } }
import java.util.*; import java.io.*; public class C { public static void main(String[] args) { FastScanner sc = new FastScanner(); int T = sc.nextInt(); while(T-->0) { int n = sc.nextInt(); char[] s = new char[n]; char[] t = new char[n]; s = sc.next().toCharArray(); t = sc.next().toCharArray(); int a = 0, b = 0, c = 0, d = 0; for(int i = 0; i < n; i++) { if(s[i] == '0' && t[i] == '0') a++; if(s[i] == '1' && t[i] == '0') b++; if(s[i] == '0' && t[i] == '1') c++; if(s[i] == '1' && t[i] == '1') d++; } int res = Integer.MAX_VALUE; if(b == c || b+1 == c) { if((b + c) % 2 == 0) { res = Math.min(res, b + c); } } if(a == d || a+1 == d) { if((a + d) % 2 == 1) { res = Math.min(res, a + d); } } if(res == Integer.MAX_VALUE) System.out.println(-1); else System.out.println(res); } } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
0
25151980
3b5cec19
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class C { public static void main(String[] args) { FastScanner fs=new FastScanner(); int T=fs.nextInt(); for (int tt=1; tt<=T; tt++) { int n=fs.nextInt(); int m=fs.nextInt(); int x=fs.nextInt(); int arr[]=fs.readArray(n); if(m>n) { System.out.println("NO"); continue; } int copy[]=new int [n]; for(int i=0;i<n;i++) { copy[i]=arr[i]; } Arrays.sort(copy); ArrayList<Integer> container[]= new ArrayList[x+1]; for(int i=1;i<=x;i++) { container[i]=new ArrayList<Integer>(); } for(int i=0;i<m;i++) { for(int j=i;j<n;j+=m) { container[copy[j]].add(i+1); } } int counter[]=new int [n]; for(int i=0;i<n;i++) { counter[i]=container[arr[i]].get(0); container[arr[i]].remove(0); } System.out.println("YES"); for(int i=0;i<n;i++) { System.out.print(counter[i]+" "); } System.out.println(); } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } int[][] readArray(int n,int m) { int[][] a=new int[n][m]; for (int i=0; i<n; i++) for(int j=0; j<m; j++) a[i][j]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
import java.util.*; import java.io.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class Test{ static class Pair implements Comparable<Pair>{ int wt; int idx; Pair(int x,int y){ this.wt=x; this.idx=y; } @Override public int compareTo(Pair x){ return this.wt-x.wt; } public String toString(){ return "("+wt+" "+idx+")"; } } public static void main (String[] args) throws java.lang.Exception{ FastReader scan=new FastReader(); 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(); } Queue<Pair>q=new PriorityQueue<>(); for(int i=1;i<=m;i++){ q.add(new Pair(0,i)); } System.out.println("YES"); for(int i=0;i<n;i++){ Pair temp=q.poll(); int wt=temp.wt; int idx=temp.idx; System.out.print(idx+" "); //System.out.println(temp); q.add(new Pair(wt+arr[i],idx)); } System.out.println(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
0
56a90e79
b505f95e
import java.util.Scanner; public class fifth { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); for (int i = 0; i < t; i++) { int n = scn.nextInt(); int k = scn.nextInt(); int[] a = new int[k]; int[] b = new int[k]; for (int j = 0; j < k; j++) a[j] = scn.nextInt(); for (int j = 0; j < k; j++) b[j] = scn.nextInt(); fun(n, a, b); } } public static void fun(int n, int[] a, int[] b) { int[] ans = new int[n]; for (int i = 0; i < n; i++) ans[i] = Integer.MAX_VALUE; for (int i = 0; i < a.length; i++) ans[a[i] - 1] = b[i]; for (int i = 1; i < n; i++) if (ans[i - 1] != Integer.MAX_VALUE) 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 ele : ans) System.out.print(ele + " "); System.out.println(); } }
import java.util.*; import java.io.*; public class E { public static void main(String[] args) throws IOException { setIO(); int INF = 2000000000; int Q = ni(); while (Q-- > 0) { rl(); st = nl(); int N = ni(st), K = ni(st); int[] P = nia(K); int[] T = nia(K); int[] A = new int[N]; Arrays.fill(A, INF); for (int i = 0; i < K; i++) { A[P[i]-1] = T[i]; } int[] R = new int[N]; R[N-1] = A[N-1]; for (int i = N-2; i >= 0; i--) { R[i] = Math.min(R[i+1] + 1, A[i]); } int[] L = new int[N]; L[0] = A[0]; for (int i = 1; i < N; i++) { L[i] = Math.min(L[i-1] + 1, A[i]); } for (int i = 0; i < N; i++) { out.print(Math.min(L[i], R[i]) + " "); } out.println(); } f.close(); out.close(); } static BufferedReader f; static PrintWriter out; static StringTokenizer st; static String rl() throws IOException { return f.readLine(); } static int ni(StringTokenizer st) { return Integer.parseInt(st.nextToken()); } static long nlg(StringTokenizer st) { return Long.parseLong(st.nextToken()); } static int ni() throws IOException { return Integer.parseInt(rl()); } static long nlg() throws IOException { return Long.parseLong(rl()); } static StringTokenizer nl() throws IOException { return new StringTokenizer(rl()); } static int[] nia(int N) throws IOException { StringTokenizer st = nl(); int[] A = new int[N]; for (int i = 0; i < N; i++) A[i] = ni(st); return A; } static void setIn(String s) throws IOException { f = new BufferedReader(new FileReader(s)); } static void setOut(String s) throws IOException { out = new PrintWriter(new FileWriter(s)); } static void setIn() { f = new BufferedReader(new InputStreamReader(System.in)); } static void setOut() { out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); } static void setIO(String s) throws IOException { setIn(s + ".in"); setOut(s + ".out"); } static void setTextIO(String s) throws IOException { setIn(s + ".txt"); setOut(s + "_out.txt"); } static void setIO() { setIn(); setOut(); } }
0
df594a00
fcbe7917
import java.io.*; import java.util.*; public class Codeforces { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int cases = Integer.parseInt(br.readLine()); while(cases-- > 0) { br.readLine(); String[] str = br.readLine().split(" "); int n = Integer.parseInt(str[0]); int k = Integer.parseInt(str[1]); int[] a = new int[k]; int[] t = new int[k]; str = br.readLine().split(" "); for(int i=0; i<k; i++) { a[i] = Integer.parseInt(str[i]) - 1; } str = br.readLine().split(" "); for(int i=0; i<k; i++) { t[i] = Integer.parseInt(str[i]); } int[] temp = new int[n]; Arrays.fill(temp, Integer.MAX_VALUE); int[] left = new int[n]; int[] right = new int[n]; Arrays.fill(left, Integer.MAX_VALUE); Arrays.fill(right, Integer.MAX_VALUE); int ind = 0; for(int i=0; i<k; i++) { left[a[i]] = t[i]; right[a[i]] = t[i]; } int minleft = Integer.MAX_VALUE; for(int i=0; i<n; i++) { left[i] = Math.min(left[i], minleft); minleft = left[i] == Integer.MAX_VALUE ? Integer.MAX_VALUE : left[i]+1; } int minright = Integer.MAX_VALUE; for(int i=n-1; i>=0; i--) { right[i] = Math.min(right[i], minright); minright = right[i] == Integer.MAX_VALUE ? Integer.MAX_VALUE : right[i]+1; } for(int i=0; i<n; i++) { temp[i] = Math.min(right[i], left[i]); System.out.print(temp[i]+" "); } System.out.println(); } } }
import java.io.*; import java.util.*; public class E_Air_Conditioners{ public static void main(String Args[]) throws Exception{ BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); int t= Integer.parseInt(br.readLine()); StringTokenizer st; while(t-->0){ String gap=br.readLine(); st=new StringTokenizer(br.readLine()); int n= Integer.parseInt(st.nextToken()); int k= Integer.parseInt(st.nextToken()); int pos[]=new int[k]; st=new StringTokenizer(br.readLine()); for(int i=0;i<k;i++){ pos[i]=Integer.parseInt(st.nextToken())-1; } int temp[]=new int[k]; st=new StringTokenizer(br.readLine()); int ans[]=new int[n]; int l[]=new int[n]; int r[]=new int[n]; Arrays.fill(ans,Integer.MAX_VALUE); for(int i=0;i<k;i++){ temp[i]=Integer.parseInt(st.nextToken()); ans[pos[i]]=temp[i]; } int min=Integer.MAX_VALUE; for(int i=0;i<n;i++){ if(min==Integer.MAX_VALUE){ min=ans[i]; } else{ min=Math.min(min+1,ans[i]); } l[i]=min; } min=Integer.MAX_VALUE; for(int i=n-1;i>=0;i--){ if(min==Integer.MAX_VALUE){ min=ans[i]; } else{ min=Math.min(min+1,ans[i]); } r[i]=min; } for(int i=0;i<n;i++){ System.out.print(Math.min(l[i],r[i])+" "); } System.out.println(); } } }
0
1dab88fb
b7de5c19
import java.util.*; public class Main { static class Edge{ public int node; public int index; public Edge(int n, int i){ node=n; index=i; } } static Scanner sc=new Scanner(System.in); public static void main(String[] args) { int test=sc.nextInt(); while(test-->0){ solve(); } } static void solve(){ int n=sc.nextInt(); ArrayList<ArrayList<Edge>> graph= new ArrayList<ArrayList<Edge>>(); for(int i=0;i<n;i++){ graph.add(new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { int u = sc.nextInt(); int v = sc.nextInt(); u--; v--; graph.get(u).add(new Edge(v, i)); graph.get(v).add(new Edge(u, i)); } int start = 0; for (int i = 0; i < n; i++) { if (graph.get(i).size() > 2) { System.out.println("-1"); return; } else if (graph.get(i).size() == 1) { start = i; } } int[] weight = new int[n - 1]; int prevNode = -1; int curNode = start; int curWeight = 2; while (true) { ArrayList<Edge> edges = graph.get(curNode); Edge next = edges.get(0); if (next.node == prevNode) { if (edges.size() == 1) { break; } else { next = edges.get(1); } } weight[next.index] = curWeight; prevNode = curNode; curNode = next.node; curWeight = 5 - curWeight; } for (int i = 0; i < n - 1; i++) { System.out.print(weight[i]); System.out.print(" "); } System.out.println(); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); for (int i = 0; i < t; i++) { int n = scan.nextInt(); ArrayList<ArrayList<Pair>> graph = new ArrayList<>(); for (int j = 0; j < n; j++) { graph.add(new ArrayList<>()); } for (int j = 0; j < n - 1; j++) { int u; int v; u = scan.nextInt(); v = scan.nextInt(); u--; v--; graph.get(u).add(new Pair(v, j)); graph.get(v).add(new Pair(u, j)); } boolean soluble = true; int curV = 0; int prevV = -1; int[] ans = new int[n]; int prime = 2; for (int j = 0; j < n; j++) { ArrayList<Pair> list = graph.get(j); if (list.size() > 2) { soluble = false; } else if (list.size() == 1) { curV = j; } } if (soluble) { for (int j = 0; j < n - 1; j++) { ArrayList<Pair> list = graph.get(curV); for (int z = 0; z < list.size(); z++) { if (list.get(z).vertex != prevV) { ans[list.get(z).numberOfEdge] = prime; prime = changePrime(prime); prevV = curV; curV = list.get(z).vertex; break; } } } for (int j = 0; j < n - 1; j++) { System.out.print(ans[j] + " "); } System.out.println(); } else { System.out.println(-1); } } } public static int changePrime(int prime) { if (prime == 2) { prime = 3; } else { prime = 2; } return prime; } } class Pair { int vertex; int numberOfEdge; public Pair(int vertex, int numberOfEdge) { this.vertex = vertex; this.numberOfEdge = numberOfEdge; } }
0
28c2d81a
fa484fdd
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class D { public static void main(String[] args) { FastScanner fs = new FastScanner(); int cases = fs.nextInt(); while(cases-->0){ int n = fs.nextInt(), k = fs.nextInt(); int[] positions = fs.readArray(k), temps = fs.readArray(k); int[] forced = new int[n]; Arrays.fill(forced, Integer.MAX_VALUE/2); for(int i=0; i<k; i++) forced[positions[i]-1] = temps[i]; for(int i=1; i<n; i++) forced[i] = Math.min(forced[i],forced[i-1]+1); for(int i=n-2; i>=0; i--) forced[i] = Math.min(forced[i], forced[i+1]+1); for(int i=0; i<n; i++) System.out.print(forced[i] + " "); System.out.println(); } } //----------------------------------------------------------------------------------// static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } char nextChar(){ return next().charAt(0); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static class Debug{ public static void debug(long a){ System.out.println("--> "+a); } public static void debug(long a, long b){ System.out.println("--> "+ a +" + " + b); } public static void debug(char a, char b){ System.out.println("--> "+ a +" + " + b); } public static void debug(int[] array){ System.out.print("Array--> "); System.out.println(Arrays.toString(array)); } public static void debug(char[] array){ System.out.print("Array--> "); System.out.println(Arrays.toString(array)); } public static void debug(HashMap<Integer,Integer> map){ System.out.print("Map--> "+map.toString()); } } }
//import java.io.IOException; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class AirConditioners { static InputReader inputReader=new InputReader(System.in); static void solve() { int n=inputReader.nextInt(); int k=inputReader.nextInt(); int pos[]=inputReader.nextIntArray(k); int power[]=inputReader.nextIntArray(k); int answer[]=new int[n]; Arrays.fill(answer,(int)(2e9)); for (int i=0;i<k;i++) { answer[pos[i]-1]=power[i]; } for (int i=1;i<n;i++) { answer[i]=Math.min(answer[i],answer[i-1]+1); } for (int i=n-2;i>=0;i--) { answer[i]=Math.min(answer[i],answer[i+1]+1); } for (int i=0;i<n;i++) { out.print(answer[i]+" "); } } static PrintWriter out=new PrintWriter((System.out)); public static void main(String args[])throws IOException { int t=inputReader.nextInt(); while(t-->0) { solve(); out.println(); } out.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
1
6f02c6d9
c9159d9c
import java.io.*; import java.util.*; public class Main { static long mod = 1000000007; static long inv(long a, long b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; } static long mi(long a) { return inv(a, mod); } static InputReader sc = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] A = new int[n]; for (int i = 0; i < A.length; i++) { A[i] = sc.nextInt(); } String word = sc.next(); ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for (int i = 0; i < word.length(); i++) { if (word.charAt(i) == 'R') { red.add(A[i]); } else { blue.add(A[i]); } } Collections.sort(blue); Collections.sort(red); boolean possible = true; int a = 1; for (int i = 0; i < blue.size(); i++, a++) { if (blue.get(i) < a) { possible = false; break; } } for (int i = 0; i < red.size(); i++, a++) { if (red.get(i) > a) { possible = false; break; } } if (possible) out.println("YES"); else out.println("NO"); } out.flush(); out.close(); } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return str; } } public static void shuffle(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } } public static void shuffle(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } }
import java.util.*; public class SolutionB { public static long gcd(long a, long b){ if(b==0){ return a; } return gcd(b, a%b); } public static long gcdSum(long b){ long a = 0; long temp = b; while(temp!=0){ a = a + temp%10; temp = temp/10; } return gcd(a,b); } public static class Pair{ Long a; Long b; public Pair(Long a, Long b) { this.a = a; this.b = b; } } public static long factorial (long n){ if(n==0) return 1; else if(n==1) return n; return n * factorial(n-1); } public static long lcm (long n){ if(n<3) return n; return lcmForBig(n,n-1); } private static long lcmForBig(long n, long l) { if(l==1) return n; n = (n * l) / gcd(n, l); return lcmForBig(n, l-1); } public static void main(String[] args){ Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int i =0;i<t;i++) { int n = s.nextInt(); int arr [] = new int[n]; ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for(int j=0;j<n;j++){ int num = s.nextInt(); arr[j]=num; } String color = s.next(); for(int j=0;j<n;j++){ if(color.charAt(j)=='B'){ blue.add(arr[j]); } else{ red.add(arr[j]); } } Collections.sort(blue); String ans = "YES"; int counter = 0; for(int j=0;j<blue.size();j++){ int current = blue.get(j); if (current<1){ ans="NO"; break; } if(current>counter){ counter++; } else{ ans="NO"; break; } } if(ans=="NO"){ System.out.println(ans); } else{ int tempCounter = n+1; Collections.sort(red); for(int j=red.size()-1;j>=0;j--){ int current = red.get(j); if(current>=tempCounter){ ans="NO"; break; } else{ tempCounter--; } } if(tempCounter-counter!=1) System.out.println("NO"); else System.out.println(ans); } } return; } }
0
548ffb07
921b6e4a
import java.io.*; import java.util.*; public class D_Java { public static final int MOD = 998244353; public static int mul(int a, int b) { return (int)((long)a * (long)b % MOD); } int[] f; int[] rf; public int C(int n, int k) { return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n-k], rf[k])); } public static int pow(int a, int n) { int res = 1; while (n != 0) { if ((n & 1) == 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } static void shuffleArray(int[] a) { Random rnd = new Random(); for (int i = a.length-1; i > 0; i--) { int index = rnd.nextInt(i + 1); int tmp = a[index]; a[index] = a[i]; a[i] = tmp; } } public static int inv(int a) { return pow(a, MOD-2); } public void doIt() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tok.nextToken()); int k = Integer.parseInt(tok.nextToken()); f = new int[n+42]; rf = new int[n+42]; f[0] = rf[0] = 1; for (int i = 1; i < f.length; ++i) { f[i] = mul(f[i-1], i); rf[i] = mul(rf[i-1], inv(i)); } int[] events = new int[2*n]; for (int i = 0; i < n; ++i) { tok = new StringTokenizer(in.readLine()); int le = Integer.parseInt(tok.nextToken()); int ri = Integer.parseInt(tok.nextToken()); events[i] = le*2; events[i + n] = ri*2 + 1; } shuffleArray(events); Arrays.sort(events); int ans = 0; int balance = 0; for (int r = 0; r < 2*n;) { int l = r; while (r < 2*n && events[l] == events[r]) { ++r; } int added = r - l; if (events[l] % 2 == 0) { // Open event ans += C(balance + added, k); if (ans >= MOD) ans -= MOD; ans += MOD - C(balance, k); if (ans >= MOD) ans -= MOD; balance += added; } else { // Close event balance -= added; } } in.close(); System.out.println(ans); } public static void main(String[] args) throws IOException { (new D_Java()).doIt(); } }
import java.io.*; import java.util.*; public class D_Java { public static final int MOD = 998244353; public static int mul(int a, int b) { return (int)((long)a * (long)b % MOD); } int[] f; int[] rf; public int C(int n, int k) { return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n-k], rf[k])); } public static int pow(int a, int n) { int res = 1; while (n != 0) { if ((n & 1) == 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } static void shuffleArray(int[] a) { Random rnd = new Random(); for (int i = a.length-1; i > 0; i--) { int index = rnd.nextInt(i + 1); int tmp = a[index]; a[index] = a[i]; a[i] = tmp; } } public static int inv(int a) { return pow(a, MOD-2); } public void doIt() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tok.nextToken()); int k = Integer.parseInt(tok.nextToken()); f = new int[n+42]; rf = new int[n+42]; f[0] = rf[0] = 1; for (int i = 1; i < f.length; ++i) { f[i] = mul(f[i-1], i); rf[i] = mul(rf[i-1], inv(i)); } int[] events = new int[2*n]; for (int i = 0; i < n; ++i) { tok = new StringTokenizer(in.readLine()); int le = Integer.parseInt(tok.nextToken()); int ri = Integer.parseInt(tok.nextToken()); events[i] = le*2; events[i + n] = ri*2 + 1; } shuffleArray(events); Arrays.sort(events); int ans = 0; int balance = 0; for (int r = 0; r < 2*n;) { int l = r; while (r < 2*n && events[l] == events[r]) { ++r; } int added = r - l; if (events[l] % 2 == 0) { // Open event ans += C(balance + added, k); if (ans >= MOD) ans -= MOD; ans += MOD - C(balance, k); if (ans >= MOD) ans -= MOD; balance += added; } else { // Close event balance -= added; } } in.close(); System.out.println(ans); } public static void main(String[] args) throws IOException { (new D_Java()).doIt(); } }
1
5766f176
e7a997b5
import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { // write your code here boolean readFromLocal = true; //readFromLocal = false; String filepath = "src/input.txt"; //FileInputStrviseam fileInputStream = new FileInputStream(filepath); InputReader inputReader = new InputReader(System.in); Solve s = new Solve(); s.solve(inputReader); } } class Solve { public void solve(InputReader inputReader) { int t,n; t = inputReader.nextInt(); while (t>0) { t--; n = inputReader.nextInt(); Graph g = new Graph(n); g.res = new int[n-1]; for(int i=1;i<n;i++){ g.addEdge(inputReader.nextInt(), inputReader.nextInt(),i-1,true); } if (g.hasDegreeMoreThanTwo()){ System.out.println(-1); }else { int minDegree = 2,node = 1; for(int i=1;i<=n;i++){ if (g.adj[i].size()<minDegree){ node = i; minDegree = g.adj[i].size(); } } g.dfs(node,-1,2); for(int i=0;i<n-1;i++){ System.out.print(g.res[i] + " "); } System.out.println(); } } } } class PairComp implements Comparator<Pair> { @Override public int compare(Pair o1, Pair o2) { return (int) (o1.first - o2.first); } } class InputReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) { return -1; } } return buf[curChar]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return readLine(); } else { return readLine0(); } } public BigInteger readBigInteger() { try { return new BigInteger(nextString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) { read(); } return value == -1; } public String next() { return nextString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public int[] nextSortedIntArray(int n) { int[] array = nextIntArray(n); Arrays.sort(array); return array; } public int[] nextSumIntArray(int n) { int[] array = new int[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; ++i) array[i] = nextLong(); return array; } public long[] nextSumLongArray(int n) { long[] array = new long[n]; array[0] = nextInt(); for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt(); return array; } public long[] nextSortedLongArray(int n) { long[] array = nextLongArray(n); Arrays.sort(array); return array; } } class Pair implements Comparable<Pair> { long first, second; public Pair(long first, long second) { this.first = first; this.second = second; } @Override public int compareTo(Pair o) { return (int) (this.second - o.second); } } class Utils { static void swap(int[] res, int i, int j) { int temp = res[i]; res[i] = res[j]; res[j] = temp; } static long pow(long num, int n, long mod) { long res = num % mod; while (n > 0) { if ((n & 1) != 0) { res = (res * num) % mod; } num = (num * num) % mod; n >>= 1; } return res; } } class Graph { public ArrayList<int[]>[] adj; int size; boolean[] vis; public int [] res; Graph(int n){ this.size = n; this.adj = new ArrayList[n+1]; this.vis = new boolean[n+1]; for (int i = 0; i <=n; i++) { adj[i] = new ArrayList<>(); } } public void addEdge(int a, int b, int edge,boolean biDirectional){ adj[a].add(new int[]{b, edge}); if (biDirectional) { adj[b].add(new int[]{a, edge}); } } public void dfs(int node,int parent,int prime){ for (int[] nodePair: adj[node]) { if (nodePair[0]!=parent){ res[nodePair[1]] = prime; dfs(nodePair[0],node,prime^1); } } } boolean hasDegreeMoreThanTwo() { for (int i = 0; i <= size; i++) { if (adj[i].size()>2){ return true; } } return false; } }
import java.io.*; import java.util.*; public class c { public static void main(String[] args){ FastScanner sc = new FastScanner(); int t = sc.nextInt(); while(t-- > 0){ int n = sc.nextInt(); ArrayList<ArrayList<Edge>> graph = new ArrayList<>(); for(int i=0; i<n; i++){ graph.add(new ArrayList<Edge>()); } for(int i=0; i<n-1; i++){ int u = sc.nextInt(); int v = sc.nextInt(); Edge e = new Edge(u-1, v-1, i+1); Edge e2 = new Edge(v-1, u-1, i+1); graph.get(u-1). add(e); graph.get(v-1).add(e2); } int edges[] = new int[n]; int indegree1count = 0; int indegree2count = 0; for(ArrayList<Edge> list : graph){ if(list.size() == 1){ indegree1count++; } else if(list.size() == 2){ indegree2count++; } } if(indegree1count == 2 && indegree1count+indegree2count==n){ for(int i=0; i<graph.size(); i++){ ArrayList<Edge> list = graph.get(i); if(list.size() == 1){ dfs(graph, edges, false, -1, i) ; } } for(int i=1; i<edges.length; i++){ System.out.print(edges[i] + " "); } System.out.println(); } else{ System.out.println(-1); } } } public static void dfs(ArrayList<ArrayList<Edge>> graph, int[] edges, boolean isprev2, int parent, int current){ for(Edge e : graph.get(current)){ if(e.v == parent){ continue; } edges[e.id] = isprev2 ? 5 : 2; dfs(graph, edges, !isprev2, current, e.v); } } } class Edge { int u; int v; int id; public Edge(int u, int v, int id) { this.u = u; this.v = v; this.id = id; } } class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
0
2b7b2d45
a0b406e6
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class D_1552 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]) { FastReader fs = new FastReader(); int tc = fs.nextInt(); while(tc-- > 0) { boolean flag=false; int n=fs.nextInt(); int[] ar=new int[n]; for(int i=0;i<n;i++) {ar[i]=fs.nextInt();} for(int i=1;i<Math.pow(3, n);i++) { int copy=i; int sum=0; for(int j=0;j<n;j++) { int rem=copy%3; sum+=rem==0?0:rem==1?ar[j]:-ar[j]; copy=copy/3; } if(sum==0) {System.out.println("yes"); flag=true; break;} } if(flag==false) System.out.println("no"); } } }
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); int tcCnt = in.nextInt(); for (int tc = 1; tc <= tcCnt; tc++) solver.solve(tc, in, out); out.close(); } static class Task { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } for (int mask = 1; mask < (int)(Math.pow(3, n)); mask++) { int copy = mask; int sum = 0; for (int idx = 0; idx < n; idx++) { int digit = copy % 3; sum += digit == 0 ? 0 : digit == 1 ? a[idx] : -a[idx]; copy = copy / 3; } if (sum == 0) { out.println("YES"); return; } } out.println("NO"); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public String nextToken() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(nextLine()); } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public int[] nextIntArray(int size) { int[] arr = new int[size]; for (int i = 0; i < size; i++) { arr[i] = nextInt(); } return arr; } } }
1
1071f735
cd835290
//package com.company; //import java.math.BigInteger; import java.util.*; //import java.util.Stack; import java.io.BufferedReader; import java.io.IOException; //import java.io.*; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static class obj{ long a1; long a2; //int a3; obj(long a1,long a2){ this.a1=a1; this.a2=a2; // this.a3=a3; } } static class sortby implements Comparator<obj>{ public int compare(obj o1,obj o2){ return o1.a1>o2.a1?1:-1; } } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static long power(long x,long y,long p){ String b1=""+y; long d=p; long reminderb=0; for(int i=0;i<b1.length();i++){ reminderb=(reminderb*10+b1.charAt(i)-'0')%d; } y=reminderb; long res=1; x=x%p; while(y>0){ if((y&1)>0) res=(res*x)%p; y=y>>1; x=(x*x)%p; } return res; } static boolean isComposite(int n) { // Corner cases if (n <= 1) System.out.println("False"); if (n <= 3) System.out.println("False"); // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return true; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return true; return false; } //static ArrayList<Integer>[] a; // static int[] vis; //static int[] col; /* static boolean dfs(int i,int c){ vis[i]=1; col[i]=c; //System.out.println(i+" "+c); for(int j:a[i]){ if(vis[j]==0) { if (dfs(j, c ^ 1) == false) return false; } else if(col[i]==col[j]) return false; } return true; }*/ static int[] m; static int solve(int p,long d){ if(p<1) return 0; if(m[p]!=0) return m[p]; if(p==1) return 1; long to=0; for(int i=1;i<=p-1;i++){ to+=solve(i,d); to=to%d; } for(int i=p;i>=2;i--){ to+=solve(p/i,d); to=to%d; } m[p]=(int)to; return m[p]; } static long[][] dis; // static long d=1000000007; public static long solve(long[] arr,int i,int j){ if(i>j){ return 0; } if(dis[i][j]!=-1) { return dis[i][j]; } if(i==j){ return arr[i]; } long c=0; c=Math.max(arr[i]+Math.min(arr[j-1]+solve(arr,i+1,j-1),arr[i+1]+solve(arr,i+2,j)),arr[j]+Math.min(solve(arr,i+1,j-1),solve(arr,i,j-2))); dis[i][j]=c; return c; } public static void main(String[] args) { FastReader s = new FastReader(); //InputStream inputStream = System.in; OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); //Scanner s=new Scanner(System.in); int t = s.nextInt(); while(t-->0) { int n = s.nextInt(); int m = s.nextInt(); int x=s.nextInt(); long[] a = new long[n]; for(int i=0;i<n;i++){ a[i]=s.nextInt(); } out.println("YES"); PriorityQueue<obj> p = new PriorityQueue<>(new sortby()); for (int i = 0; i <m; i++) { p.add(new obj(a[i],i+1)); out.print(i+1+" "); } for(int i=m;i<n;i++){ obj o=p.poll(); p.add(new obj(o.a1+a[i],o.a2)); out.print(o.a2+" "); } out.println(); } out.close(); }}
import java.util.*; public class CP { public static int[] swap(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; return arr; } static int isPowerOfTwo(int n) { if(n == 0 ) return 0; while(n != 1) { if(n % 2 != 0) return 0; n = n / 2; } return 1; } public static void printArr(int[] a){ for(int i=0; i<a.length; i++){ System.out.print(a[i]+1+" "); } System.out.println(); } public static class Pair implements Comparable<Pair> { int val; int idx; Pair(int a, int b){ this.val = a; this.idx = b; } public int compareTo(Pair o){ return this.val - o.val; } } public static void main(String[] args) { // CF1 // Scanner sc = new Scanner(System.in); // int t = sc.nextInt(); // for (int i = 0; i < t; i++) { // int n = sc.nextInt(); // int x = sc.nextInt(); // int[] arr = new int[n]; // int sum = 0; // for (int j = 0; j < n; j++) { // arr[j] = sc.nextInt(); // sum += arr[j]; // } // if (x == sum) { // System.out.println("NO"); // } else if (x > sum) { // System.out.println("YES"); // for (int j = 0; j < n; j++) { // System.out.print(arr[j] + " "); // } // System.out.println(); // } else {// x<sum // Arrays.sort(arr); // int curr = 0; // int k = 0; // while ((curr != x) && (k < n)) { // curr += arr[k]; // k++; // } // if(k==n) { // System.out.println("YES"); // for (int j = 0; j < n; j++) { // System.out.print(arr[j] + " "); // } // System.out.println(); // } // else if (curr == x) { // arr = swap(arr, k-1, n - 1); // System.out.println("YES"); // for (int j = 0; j < n; j++) { // System.out.print(arr[j] + " "); // } // System.out.println(); // }else{ // System.out.println("NO"); // } // } // } Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int m=sc.nextInt(); int x = sc.nextInt(); int[] arr = new int[n]; int[] ans = new int[n]; for (int j = 0; j < n; j++) { arr[j] = sc.nextInt(); } PriorityQueue<Pair> pq=new PriorityQueue<>(); for (int j = 0; j < m; j++) { pq.add(new Pair(arr[j],j)); ans[j]=j; } for (int j = m; j < n; j++) { Pair p=pq.remove(); pq.add(new Pair(p.val+arr[j],p.idx)); ans[j]=p.idx; } System.out.println("YES"); printArr(ans); } } }
1
8a041ef5
bdf7bfb2
// package com.company; import java.util.*; import java.io.*; import java.lang.*; import java.util.jar.JarEntry; public class Main{ /* || श्री गणेशाय नमः || @𝐚𝐮𝐭𝐡𝐨𝐫 𝐉𝐢𝐠𝐚𝐫 𝐍𝐚𝐢𝐧𝐮𝐣𝐢 𝐒𝐕𝐍𝐈𝐓- 𝐒𝐮𝐫𝐚𝐭 */ public static void main(String args[]){ InputReader in=new InputReader(System.in); TASK solver = new TASK(); int t=1; t = in.nextInt(); for(int i=1;i<=t;i++) { solver.solve(in,i); } } static class TASK { void solve(InputReader in, int testNumber) { int n = in.nextInt(); int a[][] = new int[n][5]; for(int i=0;i<n;i++) { char[] s = in.next().toCharArray(); for(int j=0;j<s.length;j++) { a[i][s[j]-'a']++; } } int max=0; int x[] = new int[n]; for(int j=0;j<=4;j++) { for(int i=0;i<n;i++) { x[i]=2*a[i][j]; for(int k=0;k<5;k++) { x[i]-=a[i][k]; } } Arrays.sort(x); int c=0,sum=0; for(int i=n-1;i>=0;i--) { sum+=x[i]; if(sum<=0) break; c++; } max=Math.max(max,c); } System.out.println(max); } } static class pair{ int x ; int y; pair(int x,int y) { this.x=x; this.y=y; } } static class Maths { static long gcd(long a, long b) { if (a < b) return gcd(b,a); if (b == 0) return a; else return gcd(b,a%b); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0){ int n = sc.nextInt(); String[] s = new String[n]; for(int i=0; i<n; i++) s[i] = sc.next(); int MAX = 0; for(char c = 'a'; c <= 'e'; c++){ PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); //Big comes in top; for(int i=0; i<n; ++i) { int curChar = 0; int otherChar = 0; for(int j=0; j<s[i].length(); j++) { if(s[i].charAt(j) == c) curChar++; else otherChar++; } int diff = curChar - otherChar; pq.add(diff); } int cur = 0; int numberOfWords = 0; while(!pq.isEmpty()){ if(cur + pq.peek() > 0){ cur += pq.poll(); numberOfWords++; }else{ break; } } MAX = Math.max(MAX, numberOfWords); } pw.println(MAX); } pw.close(); } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader f) { br = new BufferedReader(f); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] nextIntArr(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(next()); } return arr; } } }
0
2eb89317
5f0a176e
import java.util.*; import java.io.*; ////*************************************************************************** /* public class E_Gardener_and_Tree implements Runnable{ public static void main(String[] args) throws Exception { new Thread(null, new E_Gardener_and_Tree(), "E_Gardener_and_Tree", 1<<28).start(); } public void run(){ WRITE YOUR CODE HERE!!!! JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!! } } */ /////************************************************************************** public class C_Menorah{ public static void main(String[] args) { FastScanner s= new FastScanner(); //PrintWriter out=new PrintWriter(System.out); //end of program //out.println(answer); //out.close(); StringBuilder res = new StringBuilder(); int t=s.nextInt(); int p=0; while(p<t){ int n=s.nextInt(); String str1=s.nextToken(); String str2=s.nextToken(); if(str1.equals(str2)){ res.append("0 \n"); } else{ long count1=0; long count0=0; for(int i=0;i<n;i++){ char ch=str1.charAt(i); if(ch=='1'){ count1++; } } count0=n-count1; if(count1==0){ res.append("-1 \n"); } else{ long nice1=0; long nice0=0; for(int i=0;i<n;i++){ char ch=str2.charAt(i); if(ch=='1'){ nice1++; } } nice0=(n-nice1); int check1=0; int check2=0; if((count1==nice1)&&(count0==nice0)){ check1=1; } long yo1=(1+count0); long yo0=(count1-1); if((yo1==nice1)&&(yo0==nice0)){ check2=1; } if(check1==0 && check2==0){ res.append("-1 \n"); } else{ //System.out.println("here"); long correct=0; long wrong=0; long correct1=0; long correct0=0; long wrong1=0; long wrong0=0; for(int i=0;i<n;i++){ char ch1=str1.charAt(i); char ch2=str2.charAt(i); if(ch1==ch2){ correct++; if(ch1=='1'){ correct1++; } else{ correct0++; } } else{ wrong++; if(ch1=='1'){ wrong1++; } else{ wrong0++; } } } long ans1= solve(correct1,correct0,wrong1,wrong0,1); long ans2= solve(correct1,correct0,wrong1,wrong0,0); long ans=Math.min(ans1,ans2); if(ans>=Integer.MAX_VALUE){ ans=-1; } res.append(ans+" \n"); } } } p++; } System.out.println(res); } private static long solve( long correct1, long correct0, long wrong1, long wrong0,long a) { long op1=Integer.MAX_VALUE; long op2=Integer.MAX_VALUE; if(wrong1==0 && wrong0==0){ return 0; } if(a==1){ { // using correct1 if(correct1>0){ long newcorrect1=1+wrong0; long newcorrect0=wrong1; long newwrong1=correct0; long newwrong0=correct1-1; op1=(1+solve(newcorrect1,newcorrect0,newwrong1,newwrong0,0)); } } } else{ { //using wrong1 { if(wrong1>0){ long newcorrect1=wrong0; long newcorrect0=wrong1-1; long newwrong1=1+correct0; long newwrong0=correct1; op2=(1+solve(newcorrect1,newcorrect0,newwrong1,newwrong0,1)); } } } } long ans=Math.min(op1,op2); return ans; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } static long modpower(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // SIMPLE POWER FUNCTION=> static long power(long x, long y) { long res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = res * x; // y must be even now y = y >> 1; // y = y/2 x = x * x; // Change x to x^2 } return res; } }
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.*; import java.util.concurrent.ThreadLocalRandom; public class a729 { public static void main(String[] args) throws IOException { // try { BufferedWriter out = new BufferedWriter( new OutputStreamWriter(System.out)); BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); PrintWriter pt = new PrintWriter(System.out); FastReader sc = new FastReader(); int t = sc.nextInt(); for(int o = 0 ; o<t;o++){ int n = sc.nextInt(); String s1 = sc.next(); String s2 = sc.next(); if(s1.equals(s2)) { System.out.println(0); continue; } int f = 0; for(int i = 0 ; i<n;i++) { if(s1.charAt(i) == '1') { f = 1; break; } } if(f == 0) { System.out.println(-1); continue; } int c = 0; int c1 = 0; int c2 = 0; int c3 = 0; int c4 = 0; for(int i = 0; i<n;i++) { if(s1.charAt(i)!=s2.charAt(i)) { c++; if(s1.charAt(i) == '1') { c1++; }else { c2++; } }else { if(s1.charAt(i) == '1') { c3++; }else { c4++; } } } int g = 0; int h = 0; int v1 = Integer.MAX_VALUE; int v2 = Integer.MAX_VALUE; if(c%2==1) { g = 1; } int e = n-c; if(e%2==0) { h = 1; } //System.out.println(Math.min(v1, v2)); if(c2!=c/2){ g = 1; } if(c4!=e/2){ h = 1; } if(g == 0){ v1 = c; } if(h == 0){ v2 = e; } int ans = Math.min(v1, v2); if(ans == Integer.MAX_VALUE) { System.out.println(-1); }else { System.out.println(ans); } } // }catch(Exception e) { // return; // } } //------------------------------------------------------------------------------------------------------------------------------------------------ public static boolean check(int[][] arr , int m , int n , int val){ HashSet<Integer>set = new HashSet<Integer>(); int f = 0; int id = 0; ArrayList<Integer> al = new ArrayList<Integer>(); for(int i = 0 ; i<m;i++) { for(int j = 0 ; j<n;j++) { if(arr[i][j]>=val) { // System.out.println(arr[i][j] + " " + i + " " + j); set.add(j); al.add(arr[i][j]); } } id = i; if(set.size()>=2) { f = 1; break; }else { set.clear(); } } if(f == 0) { return false; } // System.out.println(set); // System.out.println(al); // int g = 1; for(int i = 0 ; i<n;i++) { if(set.contains(i)) { continue; } int h= 0; for(int j = 0 ; j<m;j++) { if(j == id) { continue; } if(arr[j][i]>=val) { h = 1; break; } } if(h == 0) { return false; } } return true; } static int cntDivisors(int n){ int cnt = 0; for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { if (n/i == i) cnt++; else cnt+=2; } } return cnt; } public static long findgcd(long[] arr,int v) { int n = arr.length; long g = arr[v]; for(int i = v ; i<n;i+=2) { g = gcd(g, arr[i]); } return g; } public static long ncr(long[] fac, int n , int r , long m) { return fac[n]*(modInverse(fac[r], m))%m *(modInverse(fac[n-r], m))%m; } public static void build(int [][] seg,char []arr,int idx, int lo , int hi) { if(lo == hi) { // seg[idx] = arr[lo]; seg[idx][(int)arr[lo]-'a'] = 1; return; } int mid = (lo + hi)/2; build(seg,arr,2*idx+1, lo, mid); build(seg,arr,idx*2+2, mid +1, hi); //seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]); for(int i = 0 ; i<27;i++) { seg[idx][i] = seg[2*idx+1][i] + seg[2*idx + 2][i]; } } //for f/inding minimum in range public static void query(int[][]seg,int[]ans,int idx , int lo , int hi , int l , int r) { if(lo>=l && hi<=r) { for(int i = 0 ; i<27;i++) { ans[i]+= seg[idx][i]; } return ; } if(hi<l || lo>r) { return; } int mid = (lo + hi)/2; query(seg,ans,idx*2 +1, lo, mid, l, r); query(seg,ans,idx*2 + 2, mid + 1, hi, l, r); //return Math.min(left, right); } //// for sum // public static void update(int[][]seg,char[]arr,int idx, int lo , int hi , int node , char val) { if(lo == hi) { // seg[idx] += val; seg[idx][val-'a']++; seg[idx][arr[node]-'a']--; }else { int mid = (lo + hi )/2; if(node<=mid && node>=lo) { update(seg,arr, idx * 2 +1, lo, mid, node, val); }else { update(seg,arr, idx*2 + 2, mid + 1, hi, node, val); } //seg[idx] = seg[idx*2 + 1] + seg[idx*2 + 2]; for(int i = 0 ; i<27;i++) { seg[idx][i] = seg[2*idx+1][i] + seg[2*idx + 2][i]; } } } public static int lower_bound(int array[], int low, int high, int key){ // Base Case if (low > high) { return low; } // Find the middle index int mid = low + (high - low) / 2; // If key is lesser than or equal to // array[mid] , then search // in left subarray if (key <= array[mid]) { return lower_bound(array, low, mid - 1, key); } // If key is greater than array[mid], // then find in right subarray return lower_bound(array, mid + 1, high, key); } public static int upper_bound(int arr[], int low, int high, int key){ // Base Case if (low > high || low == arr.length) return low; // Find the value of middle index int mid = low + (high - low) / 2; // If key is greater than or equal // to array[mid], then find in // right subarray if (key >= arr[mid]) { return upper_bound(arr, mid + 1, high, key); } // If key is less than array[mid], // then find in left subarray return upper_bound(arr, low, mid - 1, key); } // ----------------------------------------------------------------------------------------------------------------------------------------------- public static long gcd(long a, long b){ if (a == 0) return b; return gcd(b % a, a); } //-------------------------------------------------------------------------------------------------------------------------------------------------------- public static long modInverse(long a, long m){ long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient long q = a / m; long t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } //----------------------------------------------------------------------------------------------------------------------------------- //segment tree //for finding minimum in range // public static void build(int [] seg,int []arr,int idx, int lo , int hi) { // if(lo == hi) { // seg[idx] = arr[lo]; // return; // } // int mid = (lo + hi)/2; // build(seg,arr,2*idx+1, lo, mid); // build(seg,arr,idx*2+2, mid +1, hi); // seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]); // } ////for finding minimum in range //public static int query(int[]seg,int idx , int lo , int hi , int l , int r) { // if(lo>=l && hi<=r) { // return seg[idx]; // } // if(hi<l || lo>r) { // return Integer.MAX_VALUE; // } // int mid = (lo + hi)/2; // int left = query(seg,idx*2 +1, lo, mid, l, r); // int right = query(seg,idx*2 + 2, mid + 1, hi, l, r); // return Math.min(left, right); //} // // for sum // //public static void update(int[]seg,int idx, int lo , int hi , int node , int val) { // if(lo == hi) { // seg[idx] += val; // }else { //int mid = (lo + hi )/2; //if(node<=mid && node>=lo) { // update(seg, idx * 2 +1, lo, mid, node, val); //}else { // update(seg, idx*2 + 2, mid + 1, hi, node, val); //} //seg[idx] = seg[idx*2 + 1] + seg[idx*2 + 2]; // //} //} //--------------------------------------------------------------------------------------------------------------------------------------- static void shuffleArray(long[] ar) { // If running on Java 6 or older, use `new Random()` on RHS here Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap long a = ar[index]; ar[index] = ar[i]; ar[i] = a; } } //----------------------------------------------------------------------------------------------------------------------------------------------------------- } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //------------------------------------------------------------------------------------------------------------------------------------------------------------ class SegmentTree{ int n; public SegmentTree(int[] arr,int n) { this.arr = arr; this.n = n; } int[] arr = new int[n]; // int n = arr.length; int[] seg = new int[4*n]; void build(int idx, int lo , int hi) { if(lo == hi) { seg[idx] = arr[lo]; return; } int mid = (lo + hi)/2; build(2*idx+1, lo, mid); build(idx*2+2, mid +1, hi); seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]); } int query(int idx , int lo , int hi , int l , int r) { if(lo<=l && hi>=r) { return seg[idx]; } if(hi<l || lo>r) { return Integer.MAX_VALUE; } int mid = (lo + hi)/2; int left = query(idx*2 +1, lo, mid, l, r); int right = query(idx*2 + 2, mid + 1, hi, l, r); return Math.min(left, right); } } //----------------------------------------------------------------------------------------------------------------------------------------------------------- class coup{ char a; int b; public coup(char a , int b) { this.a = a; this.b = b; } } class trip{ int a , b, c; public trip(int a , int b, int c) { this.a = a; this.b = b; this.c = c; } } //---------------------------------------------------------------------------------------------------------------------------------------------------------------
0
1adac634
a7e7f371
import java.io.*; import java.util.*; public class Main { static int i, j, k, n, m, t, y, x, sum = 0; static long mod = 998244353; static FastScanner fs = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); static String str; static long ans; public static void main(String[] args) { t = fs.nextInt(); while (t-- > 0) { n = fs.nextInt(); int [] k = fs.readArray(n); int [] h = fs.readArray(n); List<Pair> arr = new ArrayList<>(); for(i=0;i<n;i++){ int a = k[i]-h[i]+1; arr.add(new Pair(a,k[i])); } Collections.sort(arr); ans=0; List<Pair> comp = new ArrayList<>(); int temp = arr.get(0).x; int tempy = arr.get(0).y; for(i=1;i<n;i++){ if(arr.get(i).x > tempy){ comp.add(new Pair(temp, tempy)); temp = arr.get(i).x; tempy = arr.get(i).y; } else tempy = Math.max(tempy, arr.get(i).y); } comp.add(new Pair(temp, tempy)); for(i=0;i<comp.size();i++){ long a = comp.get(i).y - comp.get(i).x +1 ; ans+=(((a)*(a+1))/2); } out.println(ans); } out.close(); } /*static long nck(int n , int k){ long a = fact[n]; long b = modInv(fact[k]); b*= modInv(fact[n-k]); b%=mod; return (a*b)%mod; } static void populateFact(){ fact[0]=1; fact[1] = 1; for(i=2;i<300005;i++){ fact[i]=i*fact[i-1]; fact[i]%=mod; } } */ static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long exp(long base, long pow) { if (pow == 0) return 1; long half = exp(base, pow / 2); if (pow % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long mul(long a, long b) { return ((a % mod) * (b % mod)) % mod; } static long add(long a, long b) { return ((a % mod) + (b % mod)) % mod; } static long modInv(long x) { return exp(x, mod - 2); } static void ruffleSort(int[] a) { //ruffle int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n), temp = a[i]; a[i] = a[oi]; a[oi] = temp; } //then sort Arrays.sort(a); } static void ruffleSort(long[] a) { //ruffle int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n); long temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static class Pair implements Comparable<Pair> { public int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { if (x == o.x) return Integer.compare(y, o.y); return Integer.compare(x, o.x); } } }
import java.io.*; import java.util.*; public class cp { static int mod=(int)1e9+7; // static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static int[] sp; static int size=(int)1e6; static int[] arInt; static long[] arLong; public static void main(String[] args) throws IOException { long tc=sc.nextLong(); // Scanner sc=new Scanner(System.in); // int tc=1; // primeSet=new HashSet<>(); // sieveOfEratosthenes((int)1e6+5); while(tc-->0) { int n=sc.nextInt(); int k[]=new int[n]; int h[]=new int[n]; for(int i=0;i<n;i++) k[i]=sc.nextInt(); for(int i=0;i<n;i++) h[i]=sc.nextInt(); ArrayList<Pair> interval=new ArrayList<Pair>(); ArrayList<Pair> act=new ArrayList<Pair>(); for(int i=0;i<n;i++) interval.add(new Pair(k[i]-h[i]+1,k[i])); Collections.sort(interval); // out.println(interval); act.add(interval.get(0)); for(int i=1;i<n;i++) { Pair p=act.get(act.size()-1); if(p.y<interval.get(i).x) act.add(interval.get(i)); else p.y=Math.max(p.y, interval.get(i).y); } // out.println(act); long mana=0; for(Pair p: act) { long x=p.y-p.x+1; mana+=(x*(x+1))/2; } out.println(mana); // int n=sc.nextInt(); // long days[]=new long[n]; // long power[]=new long[n]; // for (int i = 0; i < power.length; i++) { // days[i]=sc.nextLong(); // } // for (int i = 0; i < power.length; i++) { // power[i]=sc.nextLong(); // // } // // long ans=0; // for(int i=0;i<n;i++) // { // if(i==0) // { // ans+=power[i]*(power[i]+1L)/2L; // continue; // } // // long temp=power[i]*(power[i]+1)/2L; // long temp2=(power[i-1]+days[i]-days[i-1])*(power[i-1]+days[i]-days[i-1]+1L)/2L; // temp2-=power[i-1]*(power[i-1]+1L)/2L; // ans+=Math.min(temp, temp2); //// if(days[i]-days[i-1]<=power[i]) //// { //// ans+=power[i]*(power[i]+1)/2; //// } //// else { //// ans+=power[i]*(power[i]+1)/2; //// ans-=power[i-1]*(power[i-1]+1)/2; //// } // // // } // // out.println(ans); } out.flush(); out.close(); System.gc(); } /* ...SOLUTION ENDS HERE...........SOLUTION ENDS HERE... */ static int util(char a,char b) { int A=a-'0'; int B=b-'0'; return A+B; } static boolean check(int x,int[] rich,int[] poor) { int cnt=0; for(int i=0;i<rich.length;i++) { if(x-1-rich[i]<=cnt && cnt<=poor[i]) cnt++; } return cnt>=x; } static void arrInt(int n) throws IOException { arInt=new int[n]; for (int i = 0; i < arInt.length; i++) { arInt[i]=sc.nextInt(); } } static void arrLong(int n) throws IOException { arLong=new long[n]; for (int i = 0; i < arLong.length; i++) { arLong[i]=sc.nextLong(); } } static ArrayList<Integer> add(int id,int c) { ArrayList<Integer> newArr=new ArrayList<>(); for(int i=0;i<id;i++) newArr.add(arInt[i]); newArr.add(c); for(int i=id;i<arInt.length;i++) { newArr.add(arInt[i]); } return newArr; } // function to find last index <= y static int upper(ArrayList<Integer> arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) <= y) l = mid + 1; else h = mid - 1; } return h; } static int lower(ArrayList<Integer> arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) >= x) h = mid - 1; else l = mid + 1; } return l; } static int N = 501; // Array to store inverse of 1 to N static long[] factorialNumInverse = new long[N + 1]; // Array to precompute inverse of 1! to N! static long[] naturalNumInverse = new long[N + 1]; // Array to store factorial of first N numbers static long[] fact = new long[N + 1]; // Function to precompute inverse of numbers public static void InverseofNumber(int p) { naturalNumInverse[0] = naturalNumInverse[1] = 1; for(int i = 2; i <= N; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse[0] = factorialNumInverse[1] = 1; // Precompute inverse of natural numbers for(int i = 2; i <= N; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to N public static void factorial(int p) { fact[0] = 1; // Precompute factorials for(int i = 1; i <= N; i++) { fact[i] = (fact[i - 1] * (long)i) % p; } } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R, int p) { // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p; return ans; } static String tr(String s) { int now = 0; while (now + 1 < s.length() && s.charAt(now)== '0') ++now; return s.substring(now); } static ArrayList<Integer> ans; static void dfs(int node,Graph gg,int cnt,int k,ArrayList<Integer> temp) { if(cnt==k) return; for(Integer each:gg.list[node]) { if(each==0) { temp.add(each); ans=new ArrayList<>(temp); temp.remove(temp.size()-1); continue; } temp.add(each); dfs(each,gg,cnt+1,k,temp); temp.remove(temp.size()-1); } return; } static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static ArrayList<Integer> commDiv(int a, int b) { // find gcd of a, b int n = gcd(a, b); // Count divisors of n. ArrayList<Integer> Div=new ArrayList<>(); for (int i = 1; i <= Math.sqrt(n); i++) { // if 'i' is factor of n if (n % i == 0) { // check if divisors are equal if (n / i == i) Div.add(i); else { Div.add(i); Div.add(n/i); } } } return Div; } static HashSet<Integer> factors(int x) { HashSet<Integer> a=new HashSet<Integer>(); for(int i=2;i*i<=x;i++) { if(x%i==0) { a.add(i); a.add(x/i); } } return a; } static class Node { int vertex; HashSet<Node> adj; boolean rem; Node(int ver) { vertex=ver; rem=false; adj=new HashSet<Node>(); } @Override public String toString() { return vertex+" "; } } static class Tuple{ int a; int b; int c; public Tuple(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } } //function to find prime factors of n static HashMap<Long,Long> findFactors(long n2) { HashMap<Long,Long> ans=new HashMap<>(); if(n2%2==0) { ans.put(2L, 0L); // cnt++; while((n2&1)==0) { n2=n2>>1; ans.put(2L, ans.get(2L)+1); // } } for(long i=3;i*i<=n2;i+=2) { if(n2%i==0) { ans.put((long)i, 0L); // cnt++; while(n2%i==0) { n2=n2/i; ans.put((long)i, ans.get((long)i)+1); } } } if(n2!=1) { ans.put(n2, ans.getOrDefault(n2, (long) 0)+1); } return ans; } //fenwick tree implementaion static class fwt { int n; long BITree[]; fwt(int n) { this.n=n; BITree=new long[n+1]; } fwt(int arr[], int n) { this.n=n; BITree=new long[n+1]; for(int i = 0; i < n; i++) updateBIT(n, i, arr[i]); } long getSum(int index) { long sum = 0; index = index + 1; while(index>0) { sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(int n, int index,int val) { index = index + 1; while(index <= n) { BITree[index] += val; index += index & (-index); } } void print() { for(int i=0;i<n;i++) out.print(getSum(i)+" "); out.println(); } } class sparseTable{ int n; long[][]dp; int log2[]; int P; void buildTable(long[] arr) { n=arr.length; P=(int)Math.floor(Math.log(n)/Math.log(2)); log2=new int[n+1]; log2[0]=log2[1]=0; for(int i=2;i<=n;i++) { log2[i]=log2[i/2]+1; } dp=new long[P+1][n]; for(int i=0;i<n;i++) { dp[0][i]=arr[i]; } for(int p=1;p<=P;p++) { for(int i=0;i+(1<<p)<=n;i++) { long left=dp[p-1][i]; long right=dp[p-1][i+(1<<(p-1))]; dp[p][i]=Math.max(left, right); } } } long maxQuery(int l,int r) { int len=r-l+1; int p=(int)Math.floor(log2[len]); long left=dp[p][l]; long right=dp[p][r-(1<<p)+1]; return Math.max(left, right); } } //Function to find number of set bits static int setBitNumber(long n) { if (n == 0) return 0; int msb = 0; n = n / 2; while (n != 0) { n = n / 2; msb++; } return msb; } static int getFirstSetBitPos(long n) { return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1; } static ArrayList<Integer> primes; static HashSet<Integer> primeSet; static boolean prime[]; static void sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. prime= new boolean[n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) primeSet.add(i); } } static long mod(long a, long b) { long c = a % b; return (c < 0) ? c + b : c; } static void swap(long arr[],int i,int j) { long temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } static boolean util(int a,int b,int c) { if(b>a)util(b, a, c); while(c>=a) { c-=a; if(c%b==0) return true; } return (c%b==0); } static void flag(boolean flag) { out.println(flag ? "YES" : "NO"); out.flush(); } static void print(int a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print(long a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print_int(ArrayList<Integer> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void print_long(ArrayList<Long> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void printYesNo(boolean condition) { if (condition) { out.println("YES"); } else { out.println("NO"); } } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int lowerIndex(int arr[], int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] >= x) h = mid - 1; else l = mid + 1; } return l; } // function to find last index <= y static int upperIndex(int arr[], int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int upperIndex(long arr[], int n, long y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static int UpperBound(long a[], long x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static class DisjointUnionSets { int[] rank, parent; int n; // Constructor public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } // Creates n sets with single item in each void makeSet() { for (int i = 0; i < n; i++) parent[i] = i; } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } // Unites the set that includes x and the set // that includes x void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else // if ranks are the same { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } // if(xRoot!=yRoot) // parent[y]=x; } int connectedComponents() { int cnt=0; for(int i=0;i<n;i++) { if(parent[i]==i) cnt++; } return cnt; } } static class Graph { int v; ArrayList<Integer> list[]; Graph(int v) { this.v=v; list=new ArrayList[v+1]; for(int i=1;i<=v;i++) list[i]=new ArrayList<Integer>(); } void addEdge(int a, int b) { this.list[a].add(b); } } // static class GraphMap{ // Map<String,ArrayList<String>> graph; // GraphMap() { // // TODO Auto-generated constructor stub // graph=new HashMap<String,ArrayList<String>>(); // // } // void addEdge(String a,String b) // { // if(graph.containsKey(a)) // this.graph.get(a).add(b); // else { // this.graph.put(a, new ArrayList<>()); // this.graph.get(a).add(b); // } // } // } // static void dfsMap(GraphMap g,HashSet<String> vis,String src,int ok) // { // vis.add(src); // // if(g.graph.get(src)!=null) // { // for(String each:g.graph.get(src)) // { // if(!vis.contains(each)) // { // dfsMap(g, vis, each, ok+1); // } // } // } // // cnt=Math.max(cnt, ok); // } static double sum[]; static long cnt; // static void DFS(Graph g, boolean[] visited, int u) // { // visited[u]=true; // // for(int i=0;i<g.list[u].size();i++) // { // int v=g.list[u].get(i); // // if(!visited[v]) // { // cnt1=cnt1*2; // DFS(g, visited, v); // // } // // } // // // } static class Pair implements Comparable<Pair> { int x; int y; Pair(int x,int y) { this.x=x; this.y=y; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return this.x-o.x; } } static long sum_array(int a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static long sum_array(long a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void reverse_array(int a[]) { int n=a.length; int i,t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static void reverse_array(long a[]) { int n=a.length; int i; long t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } // static long modInverse(long a, long m) // { // long g = gcd(a, m); // // return power(a, m - 2, m); // // } static long power(long x, long y) { long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static int power(int x, int y) { int res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static long gcd(long a, long b) { if (a == 0) return b; //cnt+=a/b; return gcd(b%a,a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static PrintWriter out=new PrintWriter(System.out); static int int_max=Integer.MAX_VALUE; static int int_min=Integer.MIN_VALUE; static long long_max=Long.MAX_VALUE; static long long_min=Long.MIN_VALUE; }
0
18e2441c
b728ba1d
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.Map; import java.util.HashMap; public class cf1515 { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } static class Task { public void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- != 0) { int n = in.nextInt(); int m = in.nextInt(); int x = in.nextInt(); TreeMap<Integer, ArrayList<Integer>> map = new TreeMap<>(); for (int i = 0; i < n; i++) { int j = in.nextInt(); if (!map.containsKey(j)) { map.put(j, new ArrayList<Integer>()); } map.get(j).add(i); } out.println("YES"); int[] ans = new int[n]; int sta = 0; for (int s : map.keySet()) { for (int i = 0; i < map.get(s).size(); i++) { ans[map.get(s).get(i)] = (sta++) % m + 1; } } for(int i=0;i<n;i++) { out.print(ans[i]+" "); } out.println(); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreElements()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.util.Objects; import java.util.Collections; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CPhoenixAndTowers solver = new CPhoenixAndTowers(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class CPhoenixAndTowers { public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(), m = in.nextInt(), k = in.nextInt(); ArrayList<Pair<Integer, Integer>> a = new ArrayList<>(); for (int i = 0; i < n; ++i) { a.add(new Pair<>(in.nextInt(), i)); } Collections.sort(a); int[] ans = new int[n]; int[] sum = new int[m]; int j = 1; for (int i = 0; i < n; ++i) { ans[a.get(i).y] = j; sum[j - 1] += a.get(i).x; j++; if (j == m + 1) j = 1; } for (int i = 1; i < m; ++i) { if (Math.abs(sum[i - 1] - sum[i]) > k) { out.println("NO"); } } out.println("YES"); for (int e : ans) { out.print(e + " "); } out.println(); } } static class Pair<U, V> implements Comparable<Pair<U, V>> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } public int compareTo(Pair<U, V> o) { int value = ((Comparable<U>) x).compareTo(o.x); if (value != 0) return value; return ((Comparable<V>) y).compareTo(o.y); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return x.equals(pair.x) && y.equals(pair.y); } public int hashCode() { return Objects.hash(x, y); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
0
0df4050e
d9199dfd
import java.io.*; import java.util.*; public class MainClass { public static void main(String[] args) { Reader in = new Reader(System.in); int t = in.nextInt(); StringBuilder stringBuilder = new StringBuilder(); while (t-- > 0) { ArrayList<Integer> reds = new ArrayList<>(); ArrayList<Integer> blue = new ArrayList<>(); int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt() - 1; } char[] s = in.next().toCharArray(); for (int i = 0; i < n; i++) { if (s[i] == 'R') { reds.add(a[i]); } else { blue.add(a[i]); } } Collections.sort(reds, Collections.reverseOrder()); Collections.sort(blue); boolean ff = true; int start = 0; for (int i = 0; i < blue.size(); i++) { if (blue.get(i) < start) { ff = false; break; } start++; } start = n - 1; for (int i = 0; i < reds.size(); i++) { if (reds.get(i) > start) { ff = false; break; } start--; } stringBuilder.append(ff?"YES":"NO").append("\n"); } System.out.println(stringBuilder); } } class Reader { public BufferedReader reader; public StringTokenizer tokenizer; public Reader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Scanner; public class Simple{ public static void main(String args[]){ //System.out.println("Hello Java"); Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t>0){ int n = s.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = s.nextInt(); } String str = s.next(); //Arrays.sort(arr); ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for(int i=0;i<n;i++){ if(str.charAt(i)=='R'){ red.add(arr[i]); } else{ blue.add(arr[i]); } } Collections.sort(red); Collections.sort(blue); int start =1; boolean bool =true; for(int i=0;i<blue.size();i++){ if(blue.get(i)<start){ bool = false; break; } start++; } if(!bool){ System.out.println("NO"); } else{ for(int i=0;i<red.size();i++){ if(red.get(i)>start){ bool = false; break; } start++; } if(bool){ System.out.println("YES"); } else{ System.out.println("NO"); } } t--; } s.close(); } }
1
29cb9b0e
c77654b8
import java.util.*; public class Main { public static void solve (int ar[],int n) { if(n%2==1) { int c=ar[2]; int b=ar[1]; int a=ar[0]; if(a+b!=0) { ar[2]=-(a+b); ar[1]=c; ar[0]=c; } else if(b+c!=0) { ar[2]=a; ar[1]=a; ar[0]=-(b+c); } else { ar[2]=b; ar[1]=-(a+c); ar[0]=b; } System.out.print(ar[0]+" "+ar[1]+" "+ar[2]+" "); for(int i=3;i<(n);i=i+2) { System.out.print((-ar[i+1])+" "+ar[i]+" "); } System.out.println(); } // System.out.println(ans[n-1]+ " x"+ans[n-2]+" x"+ans[n-3]); else { for(int i=0;i<(n);i=i+2) { System.out.print((-ar[i+1])+" "+ar[i]+" "); } System.out.println(); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in);int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int ar[]=new int[n]; for(int i=0;i<n;i++) ar[i]=sc.nextInt(); solve(ar,n); } } }
import java.io.PrintWriter; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int tc = sc.nextInt(); while(tc-->0){ int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i<n; i++)arr[i] = sc.nextInt(); if(n % 2 == 0){ for(int i = 0; i<n; i+=2){ pw.print((-arr[i + 1]) + " " + arr[i] + " "); } pw.println(); }else{ if(arr[0] + arr[1] != 0) pw.print(arr[2] + " " + arr[2] + " " + (-(arr[0] + arr[1])) + " "); else if(arr[0] + arr[2] != 0)pw.println(arr[1] + " " + (-(arr[0] + arr[2]))+" " + arr[1]); else pw.println(-(arr[1] + arr[2]) + " " + arr[0]+" " + arr[0]); for(int i = 3; i<n; i+=2){ pw.print((-arr[i + 1]) + " " + arr[i] + " "); } pw.println(); } } pw.flush(); } }
0
73f57af1
9a20c823
/*input 3 2 1 6 3 8 1 2 3 1 3 4 6 7 9 1 2 2 3 6 3 14 12 20 12 19 2 12 10 17 3 17 3 2 6 5 1 5 2 6 4 6 */ import java.util.*; import java.lang.*; import java.io.*; public class Main { static PrintWriter out; static int MOD = 1000000007; static FastReader scan; /*-------- I/O usaing short named function ---------*/ public static String ns(){return scan.next();} public static int ni(){return scan.nextInt();} public static long nl(){return scan.nextLong();} public static double nd(){return scan.nextDouble();} public static String nln(){return scan.nextLine();} public static void p(Object o){out.print(o);} public static void ps(Object o){out.print(o + " ");} public static void pn(Object o){out.println(o);} /*-------- for output of an array ---------------------*/ static void iPA(int arr []){ StringBuilder output = new StringBuilder(); for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output); } static void lPA(long arr []){ StringBuilder output = new StringBuilder(); for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output); } static void sPA(String arr []){ StringBuilder output = new StringBuilder(); for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output); } static void dPA(double arr []){ StringBuilder output = new StringBuilder(); for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output); } /*-------------- for input in an array ---------------------*/ static void iIA(int arr[]){ for(int i=0; i<arr.length; i++)arr[i] = ni(); } static void lIA(long arr[]){ for(int i=0; i<arr.length; i++)arr[i] = nl(); } static void sIA(String arr[]){ for(int i=0; i<arr.length; i++)arr[i] = ns(); } static void dIA(double arr[]){ for(int i=0; i<arr.length; i++)arr[i] = nd(); } /*------------ for taking input faster ----------------*/ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // Method to check if x is power of 2 static boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0);} //Method to return lcm of two numbers static int gcd(int a, int b){return a==0?b:gcd(b % a, a); } //Method to count digit of a number static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);} //Method for sorting static void ruffle_sort(int[] a) { //shandom_ruffle Random r=new Random(); int n=a.length; for (int i=0; i<n; i++) { int oi=r.nextInt(n); int temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //sort Arrays.sort(a); } //Method for checking if a number is prime or not static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long[] l, r; public static void main (String[] args) throws java.lang.Exception { OutputStream outputStream =System.out; out =new PrintWriter(outputStream); scan =new FastReader(); //for fast output sometimes StringBuilder sb = new StringBuilder(); int t = ni(); while(t-->0){ int n = ni(); l = new long[n]; r = new long[n]; for(int i=0; i<n; i++){ l[i] = nl(); r[i] = nl(); } //lPA(l); //lPA(r); ArrayList<Integer> adj[] = new ArrayList[n]; for(int i=0; i<n; i++) adj[i] = new ArrayList<Integer>(); for(int i=0; i<n-1; i++){ int u = ni()-1, v = ni()-1; adj[u].add(v); adj[v].add(u); } dp = new Long[n][2]; visited = new boolean[n]; long ans = Math.max(solve(adj, 0, 0, visited), solve(adj, 0, 1, visited)); pn(ans); } out.flush(); out.close(); } static Long dp[][]; static boolean visited[]; static long solve(ArrayList<Integer> adj[], int vertex, int prev, boolean visited[]){ visited[vertex] = true; if(dp[vertex][prev] != null) return dp[vertex][prev]; long ans = 0; for(int x : adj[vertex]){ if(!visited[x]){ if(prev == 0){ ans += Math.max(Math.abs(l[vertex] - l[x]) + solve(adj, x, 0, visited), Math.abs(l[vertex] - r[x]) + solve(adj, x, 1, visited)); //pn(vertex + " " + x + " " + ans); }else{ ans += Math.max(Math.abs(r[vertex] - l[x]) + solve(adj, x, 0, visited), Math.abs(r[vertex] - r[x]) + solve(adj, x, 1, visited)); //pn(vertex + " " + x + " " + ans); } } } visited[vertex] = false; //pn(ans); return dp[vertex][prev] = ans; } }
import java.util.*; import java.io.*; public class Parsas_Humongous_Tree { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void shuffle(int[] a) { Random r = new Random(); for (int i = 0; i <= a.length - 2; i++) { int j = i + r.nextInt(a.length - i); swap(a, i, j); } } public static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void main(String[] args) { // TODO Auto-generated method stub FastReader t = new FastReader(); PrintWriter o = new PrintWriter(System.out); int test = t.nextInt(); while (test-- > 0) { int n = t.nextInt(); long[][] cost = new long[n][2]; List<Integer>[] graph = new ArrayList[n]; dp = new long[n][2]; for (int i = 0; i < n; ++i) { cost[i][0] = t.nextLong(); cost[i][1] = t.nextLong(); graph[i] = new ArrayList<>(); } for (int i = 0; i < n - 1; ++i) { int x = t.nextInt() - 1; int y = t.nextInt() - 1; graph[x].add(y); graph[y].add(x); } o.println(Math.max(dfs(graph, cost, 0, 0, -1), dfs(graph, cost, 0, 1, -1))); } o.flush(); o.close(); } private static long[][] dp; private static long dfs(List<Integer>[] graph, long[][] cost, int u, int j, int par) { if (dp[u][j] != 0) return dp[u][j]; for (int v : graph[u]) if (v != par) { long c1 = Math.abs(cost[u][j] - cost[v][0]) + dfs(graph, cost, v, 0, u); long c2 = Math.abs(cost[u][j] - cost[v][1]) + dfs(graph, cost, v, 1, u); dp[u][j] += Math.max(c1, c2); } return dp[u][j]; } }
0
76ad805a
f229aa7f
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); } } } //9 //001011011 //011010101 3 2 2 2 //9 //100010111 //101101100 2 1 3 3
import java.util.*; import java.io.*; import java.math.*; public class cf { static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); char[] a = sc.next().toCharArray(); char[] b = sc.next().toCharArray(); int x = 0, y = 0, lit = 0,lit2 = 0; for (int i = 0; i < n; i++) { if (a[i] == '1') lit++; if (b[i] == '1') lit2++; if (a[i] == b[i]) x++; else y++; } if(lit == lit2 || n - lit + 1 == lit2) { if (lit == lit2 && n - lit + 1 == lit2) { pw.println(Math.min(x,y)); }else if(lit == lit2) { pw.println(y); }else { pw.println(x); } }else { pw.println(-1); } } pw.close(); } public static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) return this.z - other.z; return this.y - other.y; } else { return this.x - other.x; } } } public static class pair implements Comparable<pair> { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Integer(x).hashCode() * 31 + new Integer(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } }
0
71c7ac71
8f778337
import java.io.*; import java.util.*; import static java.lang.Math.*; public class E{ public static void main(String[] args) throws IOException { // br = new BufferedReader(new FileReader(".in")); // out = new PrintWriter(new FileWriter(".out")); //new Thread(null, new (), "peepee", 1<<28).start(); int q =readInt(); while(q-->0) { br.readLine(); int n =readInt(); int k =readInt(); int[] ans = new int[n]; Arrays.fill(ans, (int)2e9+1); int[] a = new int[k]; int[] t = new int[k]; int mini = 0, maxi = 0; for (int i = 0 ;i <k; i+=1) { a[i]=readInt()-1; if (a[i] < a[mini]) mini = i; if (a[i] > a[maxi]) maxi = i; } for (int j = 0; j <k; j++) t[j]=readInt(); for (int i = 0; i < k; i++) ans[a[i]]=t[i]; int[] l = new int[n]; int[] r = new int[n]; Arrays.fill(l, (int)2e9); Arrays.fill(r, (int)2e9); int temp = t[mini]; for (int i = a[mini]; i < n; i++) { l[i] = temp = min(temp+1, ans[i]); } temp = t[maxi]; for (int i = a[maxi]; i >= 0; i--) { r[i] = temp = min(temp+1,ans[i]); } for (int i = 0; i < n; i++) out.print(min(l[i],r[i]) + " "); out.println(); } out.close(); } static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static StringTokenizer st = new StringTokenizer(""); static String read() throws IOException{return st.hasMoreTokens() ? st.nextToken():(st = new StringTokenizer(br.readLine())).nextToken();} static int readInt() throws IOException{return Integer.parseInt(read());} static long readLong() throws IOException{return Long.parseLong(read());} static double readDouble() throws IOException{return Double.parseDouble(read());} }
import java.util.*; import java.io.*; public class AirConditioner{ public static void main(String[] args) throws Exception{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t-->0){ br.readLine(); StringTokenizer st=null; st=new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()); int k=Integer.parseInt(st.nextToken()); int[] idx=new int[k]; int[] temp=new int[k]; st=new StringTokenizer(br.readLine()); for(int i=0;i<k;i++){ idx[i]=Integer.parseInt(st.nextToken()); } st=new StringTokenizer(br.readLine()); for(int i=0;i<k;i++){ temp[i]=Integer.parseInt(st.nextToken()); } int[] arr=new int[n]; Arrays.fill(arr,Integer.MAX_VALUE-1); for(int i=0;i<k;i++){ arr[idx[i]-1]=temp[i]; } int[] left=new int[n]; int[] right=new int[n]; left[0]=Math.min(Integer.MAX_VALUE,arr[0]); for(int i=1;i<n;i++){ left[i]=Math.min(left[i-1]+1,arr[i]); } right[n-1]=Math.min(Integer.MAX_VALUE,arr[n-1]); for(int i=n-2;i>=0;i--){ right[i]=Math.min(right[i+1]+1,arr[i]); } for(int i=0;i<n;i++){ arr[i]=Math.min(left[i],right[i]); } for(int tp:arr) System.out.print(tp+" "); System.out.println(); } } }
0
77d22fc3
d3528b2a
import java.io.*; import java.util.*; public class A { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// static int t = 0; //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static long ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// static int tt = 0; //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); t = sc.ni();while(t-->0) { tt++; solve(); } w.close(); } static void solve() throws IOException { int n = sc.ni(); String s1 = sc.ns(); String s2 = sc.ns(); char[] strr = s1.toCharArray(); char[] strr2 = s2.toCharArray(); HashSet<Integer> lia = new HashSet<>(), lib = new HashSet<>(); for(int i = 0; i < n; i++) { if(strr[i]=='1') lia.add(i); else lib.add(i); } HashSet<Integer> liaa = new HashSet<>(); for(int i = 0; i < n; i++) { if(strr2[i]=='1') liaa.add(i); } if(!(lia.size() == liaa.size() || lib.size()+1 == liaa.size())) { w.p(-1); return; } int ac = 0, bc = 0; for(int i: lia) { if(liaa.contains(i)) ac++; } bc = liaa.size()-ac; if(lia.size() == liaa.size() && lia.size() == ac) { w.p(0); return; } int nac = lia.size()-ac; int nbc = lib.size()-bc; long ansa = ac != 0 && (ac-1 == nbc)? Math.abs(ac-1+nbc): ima; long ansb = nac != 0 && (nac-1 == bc-1)? Math.abs(nac-1+bc): ima; if(ansa == ima && ansb == ima) { w.p(-1); return; } w.p(Math.min(ansa, ansb)+1); } }
import java.io.*; import java.util.*; public class A { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// static int t = 0; //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static long ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// static int tt = 0; //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); t = sc.ni();while(t-->0) { tt++; solve(); } w.close(); } static void solve() throws IOException { int n = sc.ni(); String s1 = sc.ns(); String s2 = sc.ns(); char[] strr = s1.toCharArray(); char[] strr2 = s2.toCharArray(); HashSet<Integer> lia = new HashSet<>(), lib = new HashSet<>(); for(int i = 0; i < n; i++) { if(strr[i]=='1') lia.add(i); else lib.add(i); } HashSet<Integer> liaa = new HashSet<>(); for(int i = 0; i < n; i++) { if(strr2[i]=='1') liaa.add(i); } if(!(lia.size() == liaa.size() || lib.size()+1 == liaa.size())) { w.p(-1); return; } int ac = 0, bc = 0; for(int i: lia) { if(liaa.contains(i)) ac++; } bc = liaa.size()-ac; if(lia.size() == liaa.size() && lia.size() == ac) { w.p(0); return; } int nac = lia.size()-ac; int nbc = lib.size()-bc; long ansa = ac != 0 && (ac-1 == nbc)? Math.abs(ac-1+nbc): ima; long ansb = nac != 0 && (nac-1 == bc-1)? Math.abs(nac-1+bc): ima; if(ansa == ima && ansb == ima) { w.p(-1); return; } w.p(Math.min(ansa, ansb)+1); } }
1
a4c98ae1
ee4f7b06
import java.io.*; import java.util.*; public class Main { static ArrayList<Integer> one=new ArrayList<>(); static ArrayList<Integer> zero=new ArrayList<>(); static long dp[][]= new long[5001][5001]; static long solve(int i,int j){ if (i==one.size())return 0; if (j==zero.size())return Integer.MAX_VALUE; if (dp[i][j]!=-1){ return dp[i][j]; } return dp[i][j]=Math.min(solve(i+1,j+1)+Math.abs(one.get(i)-zero.get(j)),solve(i,j+1)); } public static void main(String[] args) { FastScanner sc = new FastScanner(); // int t=sc.nextInt(); // while (t-->=1){ int n=sc.nextInt(); int a[]=sc.readArray(n); for (long i[]:dp){ Arrays.fill(i,-1); } for (int i=0;i<n;i++){ if (a[i]==1)one.add(i); else zero.add(i); } Collections.sort(one); Collections.sort(zero); System.out.println(solve(0,0)); } // out.flush(); //------------------------------------if------------------------------------------------------------------------------------------------------------------------------------------------- //sieve static void primeSieve(int a[]){ //mark all odd number as prime for (int i=3;i<a.length;i+=2){ a[i]=1; } for (long i=3;i<a.length;i+=2){ //if the number is marked then it is prime if (a[(int) i]==1){ //mark all muliple of the number as not prime for (long j=i*i;j<a.length;j+=i){ a[(int)j]=0; } } } a[2]=1; a[1]=a[0]=0; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static String sortString(String s) { char temp[] = s.toCharArray(); Arrays.sort(temp); return new String(temp); } static class Pair implements Comparable<Pair> { String a; int b; public Pair(String a, int b) { this.a = a; this.b = b; } // to sort first part // public int compareTo(Pair other) { // if (this.a == other.a) return other.b > this.b ? -1 : 1; // else if (this.a > other.a) return 1; // else return -1; // } public int compareTo(Pair other) { // if (this.b == other.b) return 0; if (this.b > other.b) return 1; else return -1; } //sort on the basis of first part only // public int compareTo(Pair other) { // if (this.a == other.a) return 0; // else if (this.a > other.a) return 1; // else return -1; // } } static int[] frequency(String s){ int fre[]= new int[26]; for (int i=0;i<s.length();i++){ fre[s.charAt(i)-'a']++; } return fre; } static long LOWERBOUND(long a[],long k){ int s=0,e=a.length-1; long ans=-1; while (s<=e){ int mid=(s+e)/2; if (a[mid]<=k){ ans=mid; s=mid+1; } else { e=mid-1; } } return ans; } static long mod =(long)(1e9+7); static long mod(long x) { return ((x % mod + mod) % mod); } static long div(long a,long b){ return ((a/b)%mod+mod)%mod; } static long add(long x, long y) { return mod(mod(x) + mod(y)); } static long mul(long x, long y) { return mod(mod(x) * mod(y)); } //Fast Power(logN) static int fastPower(long a,long n){ int ans=1; while (n>0){ long lastBit=n&1; if (lastBit==1){ ans=(int)mul(ans,a); } a=(int)mul(a,a); n>>=1; } return ans; } static int[] find(int n, int start, int diff) { int a[] = new int[n]; a[0] = start; for (int i = 1; i < n; i++) a[i] = a[i - 1] + diff; return a; } static void swap(int a, int b) { int c = a; a = b; b = c; } static void printArray(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } static boolean sorted(int a[]) { int n = a.length; boolean flag = true; for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) flag = false; } if (flag) return true; else return false; } public static int findlog(long n) { if (n == 0) return 0; if (n == 1) return 0; if (n == 2) return 1; double num = Math.log(n); double den = Math.log(2); if (den == 0) return 0; return (int) (num / den); } public static long gcd(long a, long b) { if (b % a == 0) return a; return gcd(b % a, a); } public static int gcdInt(int a, int b) { if (b % a == 0) return a; return gcdInt(b % a, a); } static void sortReverse(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); // Collections.sort.(l); Collections.sort(l, Collections.reverseOrder()); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArrayLong(long n) { long[] a = new long[(int) n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }
import java.io.*; import java.util.*; import java.lang.Math; public class Main { static PrintWriter pw; static Scanner sc; static StringBuilder ans; static long mod = 1000000000+7; static void pn(final Object arg) { pw.print(arg); pw.flush(); } /*-------------- for input in an value ---------------------*/ static int ni() { return sc.nextInt(); } static long nl() { return sc.nextLong(); } static double nd() { return sc.nextDouble(); } static String ns() { return sc.next(); } static void ap(int arg) { ans.append(arg); } static void ap(long arg) { ans.append(arg); } static void ap(String arg) { ans.append(arg); } static void ap(StringBuilder arg) { ans.append(arg); } /*-------------- for input in an array ---------------------*/ static void inputIntegerArray(int arr[]){ for(int i=0; i<arr.length; i++)arr[i] = ni(); } static void inputLongArray(long arr[]){ for(int i=0; i<arr.length; i++)arr[i] = nl(); } static void inputStringArray(String arr[]){ for(int i=0; i<arr.length; i++)arr[i] = ns(); } static void inputDoubleArray(double arr[]){ for(int i=0; i<arr.length; i++)arr[i] = nd(); } /*-------------- File vs Input ---------------------*/ static void runFile() throws Exception { sc = new Scanner(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); } static void runIo() throws Exception { pw =new PrintWriter(System.out); sc = new Scanner(System.in); } static void swap(int a, int b) { int t = a; a = b; b = t; } static boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0);} static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);} static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean sv[] = new boolean[1000002]; static void seive() { //true -> not prime // false->prime sv[0] = sv[1] = true; sv[2] = false; for(int i = 0; i< sv.length; i++) { if( !sv[i] && (long)i*(long)i < sv.length ) { for ( int j = i*i; j<sv.length ; j += i ) { sv[j] = true; } } } } static long binpow( long a, long b) { long res = 1; while (b > 0) { if ( (b & 1) > 0){ res = (res * a)%mod; } a = (a * a)%mod; b >>= 1; } return res; } static long factorial(long n) { long res = 1, i; for (i = 2; i <= n; i++){ res = ((res%mod) * (i%mod))%mod; } return res; } static class Pair { int idx; int v; Pair(int idx, int v){ this.idx = idx; this.v = v; } } public static void main(String[] args) throws Exception { // runFile(); runIo(); int t; t = 1; // t = sc.nextInt(); ans = new StringBuilder(); while( t-- > 0 ) { solve(); } pn(ans+""); } static int N ; static int M ; static ArrayList<Integer> f; static ArrayList<Integer> e; static long dp[][]; static long find(int i, int j ) { if( i == N ) return 0; if( j == M ) return Integer.MAX_VALUE; if (dp[i][j] != -1 ) return dp[i][j]; return dp[i][j] = Math.min( find(i, j+1), Math.abs(f.get(i)-e.get(j)) + find(i+1, j+1) ); } public static void solve() { int n = ni(); f = new ArrayList(); e = new ArrayList(); for(int i = 0; i<n; i++) { int v = ni(); if( v == 0 ) { e.add(i); } else f.add(i); } N = f.size(); M = e.size(); dp = new long[N][M]; for(int i = 0; i<N; i++) Arrays.fill(dp[i], -1); ap(find(0, 0)+"\n"); } } // 0 1 2 3 4 5 6 7 // 1 1 0 0 0 1 0 0 // 1s -> { 0, 1, 5 } // 0s -> { 2, 3, 6, 7 }
1
2eb89317
d4a3869e
import java.util.*; import java.io.*; ////*************************************************************************** /* public class E_Gardener_and_Tree implements Runnable{ public static void main(String[] args) throws Exception { new Thread(null, new E_Gardener_and_Tree(), "E_Gardener_and_Tree", 1<<28).start(); } public void run(){ WRITE YOUR CODE HERE!!!! JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!! } } */ /////************************************************************************** public class C_Menorah{ public static void main(String[] args) { FastScanner s= new FastScanner(); //PrintWriter out=new PrintWriter(System.out); //end of program //out.println(answer); //out.close(); StringBuilder res = new StringBuilder(); int t=s.nextInt(); int p=0; while(p<t){ int n=s.nextInt(); String str1=s.nextToken(); String str2=s.nextToken(); if(str1.equals(str2)){ res.append("0 \n"); } else{ long count1=0; long count0=0; for(int i=0;i<n;i++){ char ch=str1.charAt(i); if(ch=='1'){ count1++; } } count0=n-count1; if(count1==0){ res.append("-1 \n"); } else{ long nice1=0; long nice0=0; for(int i=0;i<n;i++){ char ch=str2.charAt(i); if(ch=='1'){ nice1++; } } nice0=(n-nice1); int check1=0; int check2=0; if((count1==nice1)&&(count0==nice0)){ check1=1; } long yo1=(1+count0); long yo0=(count1-1); if((yo1==nice1)&&(yo0==nice0)){ check2=1; } if(check1==0 && check2==0){ res.append("-1 \n"); } else{ //System.out.println("here"); long correct=0; long wrong=0; long correct1=0; long correct0=0; long wrong1=0; long wrong0=0; for(int i=0;i<n;i++){ char ch1=str1.charAt(i); char ch2=str2.charAt(i); if(ch1==ch2){ correct++; if(ch1=='1'){ correct1++; } else{ correct0++; } } else{ wrong++; if(ch1=='1'){ wrong1++; } else{ wrong0++; } } } long ans1= solve(correct1,correct0,wrong1,wrong0,1); long ans2= solve(correct1,correct0,wrong1,wrong0,0); long ans=Math.min(ans1,ans2); if(ans>=Integer.MAX_VALUE){ ans=-1; } res.append(ans+" \n"); } } } p++; } System.out.println(res); } private static long solve( long correct1, long correct0, long wrong1, long wrong0,long a) { long op1=Integer.MAX_VALUE; long op2=Integer.MAX_VALUE; if(wrong1==0 && wrong0==0){ return 0; } if(a==1){ { // using correct1 if(correct1>0){ long newcorrect1=1+wrong0; long newcorrect0=wrong1; long newwrong1=correct0; long newwrong0=correct1-1; op1=(1+solve(newcorrect1,newcorrect0,newwrong1,newwrong0,0)); } } } else{ { //using wrong1 { if(wrong1>0){ long newcorrect1=wrong0; long newcorrect0=wrong1-1; long newwrong1=1+correct0; long newwrong0=correct1; op2=(1+solve(newcorrect1,newcorrect0,newwrong1,newwrong0,1)); } } } } long ans=Math.min(op1,op2); return ans; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } static long modpower(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // SIMPLE POWER FUNCTION=> static long power(long x, long y) { long res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = res * x; // y must be even now y = y >> 1; // y = y/2 x = x * x; // Change x to x^2 } return res; } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); for (int i = 0; i < t; i++) { int n = scan.nextInt(); String a = scan.next(); String b = scan.next(); int[] cChet = new int[n]; int[] cNech = new int[n]; int sumChet = 0; int sumNechet = 0; int numOnesA = 0; int numOnesB = 0; boolean soluble = true; for (int j = 0; j < n; j++) { cChet[j] = (a.charAt(j) - 48) ^ (b.charAt(j) - 48); cNech[j] = cChet[j] ^ 1; sumChet += cChet[j]; sumNechet += cNech[j]; } int INF = 100000000; int ans = INF; if (((sumChet % 2 == 1) && (sumNechet % 2 == 0)) || !(numOnesB == numOnesA || numOnesB == (n - numOnesA + 1))) { ans = -1; } else { if (sumChet % 2 == 0) { int ones = 0; int zeroes = 0; for (int j = 0; j < n; j++) { if (cChet[j] == 1) { if (a.charAt(j) == '1') { ones++; } else { zeroes++; } } } if ((ones - zeroes) == 0 || (ones - zeroes) == 1) { ans = Math.min(sumChet, ans); } } if (sumNechet % 2 == 1) { int ones = 0; int zeroes = 0; for (int j = 0; j < n; j++) { if (cNech[j] == 1) { if (a.charAt(j) == '1') { ones++; } else { zeroes++; } } } if ((ones - zeroes) == 0 || (ones - zeroes) == 1) { ans = Math.min(sumNechet, ans); } } } if (ans != INF) { System.out.println(ans); } else { System.out.println(-1); } } } public static int find(int x, int log) { int pow = (int)Math.pow(2, log); int ans = (x / (pow * 2)) * pow; if (x % (pow * 2) > pow) { ans += pow; } else { ans += (x % (pow * 2)); } return ans; } }
0
3a12e509
e812ee0b
import java.io.*; import java.util.*; public class Practice { // static final long mod=7420738134811L; static int mod=1000000007; static final int size=501; static FastReader sc=new FastReader(System.in); // static Reader sc=new Reader(); static PrintWriter out=new PrintWriter(System.out); static long[] factorialNumInverse; static long[] naturalNumInverse; static int[] sp; static long[] fact; static ArrayList<Integer> pr; public static void main(String[] args) throws IOException { // System.setIn(new FileInputStream("input.txt")); // System.setOut(new PrintStream("output.txt")); // factorial(mod); // InverseofNumber(mod); // InverseofFactorial(mod); // make_seive(); int t=1; t=sc.nextInt(); while(t-->0) solve(); out.close(); out.flush(); } static void solve() throws IOException { int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); String s=sc.next(); ArrayList<Integer> blue=new ArrayList<Integer>(); ArrayList<Integer> red=new ArrayList<Integer>(); for(int i=0;i<n;i++) { if(s.charAt(i)=='B') blue.add(arr[i]); else red.add(arr[i]); } Collections.sort(blue); Collections.sort(red); for(int i=0;i<blue.size();i++) { if(blue.get(i)<i+1) { out.println("NO"); return; } } for(int i=0;i<red.size();i++) { if(red.get(i)>i+1+blue.size()) { out.println("NO"); return; } } out.println("YES"); } static class Pair implements Cloneable, Comparable<Pair> { int x,y; Pair(int a,int b) { this.x=a; this.y=b; } @Override public boolean equals(Object obj) { if(obj instanceof Pair) { Pair p=(Pair)obj; return p.x==this.x && p.y==this.y; } return false; } @Override public int hashCode() { return Math.abs(x)+500*Math.abs(y); } @Override public String toString() { return "("+x+" "+y+")"; } @Override protected Pair clone() throws CloneNotSupportedException { return new Pair(this.x,this.y); } @Override public int compareTo(Pair a) { int t= this.x-a.x; if(t!=0) return t; else return this.y-a.y; } public void swap() { this.y=this.y+this.x; this.x=this.y-this.x; this.y=this.y-this.x; } } static class Tuple implements Cloneable, Comparable<Tuple> { int x,y,z; Tuple(int a,int b,int c) { this.x=a; this.y=b; this.z=c; } public boolean equals(Object obj) { if(obj instanceof Tuple) { Tuple p=(Tuple)obj; return p.x==this.x && p.y==this.y && p.z==this.z; } return false; } @Override public int hashCode() { return (this.x+501*this.y); } @Override public String toString() { return "("+x+","+y+","+z+")"; } @Override protected Tuple clone() throws CloneNotSupportedException { return new Tuple(this.x,this.y,this.z); } @Override public int compareTo(Tuple a) { int x=this.z-a.z; if(x!=0) return x; int X= this.x-a.x; if(X!=0) return X; return a.y-this.y; } } static void arraySort(int arr[]) { ArrayList<Integer> a=new ArrayList<Integer>(); for (int i = 0; i < arr.length; i++) { a.add(arr[i]); } Collections.sort(a); for (int i = 0; i < arr.length; i++) { arr[i]=a.get(i); } } static void arraySort(long arr[]) { ArrayList<Long> a=new ArrayList<Long>(); for (int i = 0; i < arr.length; i++) { a.add(arr[i]); } Collections.sort(a); for (int i = 0; i < arr.length; i++) { arr[i]=a.get(i); } } static HashSet<Integer> primeFactors(int n) { HashSet<Integer> ans=new HashSet<Integer>(); if(n%2==0) { ans.add(2); while((n&1)==0) n=n>>1; } for(int i=3;i*i<=n;i+=2) { if(n%i==0) { ans.add(i); while(n%i==0) n=n/i; } } if(n!=1) ans.add(n); return ans; } static void make_seive() { sp=new int[size]; pr=new ArrayList<Integer>(); for (int i=2; i<size; ++i) { if (sp[i] == 0) { sp[i] = i; pr.add(i); } for (int j=0; j<(int)pr.size() && pr.get(j)<=sp[i] && i*pr.get(j)<size; ++j) sp[i * pr.get(j)] = pr.get(j); } } public static void InverseofNumber(int p) { naturalNumInverse=new long[size]; naturalNumInverse[0] = naturalNumInverse[1] = 1; for(int i = 2; i < size; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse=new long[size]; factorialNumInverse[0] = factorialNumInverse[1] = 1; // pre-compute inverse of natural numbers for(int i = 2; i < size; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to 200001 public static void factorial(int p) { fact=new long[size]; fact[0] = 1; for(int i = 1; i < size; i++) fact[i] = (fact[i - 1] * (long)i) % p; } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R) { if(R<0) return 1; // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % mod * factorialNumInverse[N - R]) % mod; return ans; } static int findXOR(int x) //from 0 to x { if(x<0) return 0; if(x%4==0) return x; if(x%4==1) return 1; if(x%4==2) return x+1; return 0; } static boolean isPrime(long x) { if(x==1) return false; if(x<=3) return true; if(x%2==0 || x%3==0) return false; for(int i=5;i<=Math.sqrt(x);i+=2) if(x%i==0) return false; return true; } static long gcd(long a,long b) { return (b==0)?a:gcd(b,a%b); } static int gcd(int a,int b) { return (b==0)?a:gcd(b,a%b); } static class Node { int vertex; HashSet<Edge> adj; int taxC,taxD; Node(int ver) { vertex=ver; taxD=0; taxC=0; adj=new HashSet<Edge>(); } @Override public String toString() { return vertex+" "; } } static class Edge { Node to; int cost; Edge(Node t,int c) { this.to=t; this.cost=c; } @Override public String toString() { return "("+to.vertex+","+cost+") "; } } static long power(long x, long y) { if(x<=0) return 1; long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static long binomialCoeff(long n, long k) { if(n<k) return 0; long res = 1; // if(k>n) // return 0; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (long i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } static class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } public void printarray(int arr[]) { for (int i = 0; i < arr.length; i++) System.out.print(arr[i]+" "); System.out.println(); } } }
import java.util.*; import java.util.concurrent.Exchanger; public class Codeforces { static int[] mass = new int[200_001]; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); StringBuilder stringBuilder = new StringBuilder(); int t = scanner.nextInt(); for (int z =0 ; z < t ; ++z) { solve(scanner, stringBuilder); } System.out.print(stringBuilder); } private static void solve(Scanner scanner, StringBuilder stringBuilder) { int n = scanner.nextInt(); for (int i = 0 ; i < n ; ++i) { mass[i] = scanner.nextInt(); } String colors = scanner.next(); List<Integer> red = new ArrayList<>(n); List<Integer> blue = new ArrayList<>(n); for (int i = 0 ; i < n ; ++i) { if(colors.charAt(i) == 'R') red.add(mass[i]); else blue.add(mass[i]); } red.sort(Comparator.reverseOrder()); blue.sort(Comparator.naturalOrder()); int creatd = 0; int upper = n; int down = 1; for (Integer integer : red) { if (integer > upper) { stringBuilder.append("NO\n"); return; } else { creatd++; upper--; } } for (Integer integer : blue) { if (integer < down) { stringBuilder.append("NO\n"); return; } else { creatd++; down++; } } if (creatd == n) stringBuilder.append("YES\n"); else stringBuilder.append("NO\n"); } }
0
49b94994
d8654140
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main1582D { public static void main(String[] args) { final FastScanner in = new FastScanner(System.in); final PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); int[] a = new int[n]; for (int j = 0; j < n; j++) { a[j] = in.nextInt(); } int[] b = solution(a, n); for (int j = 0; j < n; j++) { out.print(b[j]); out.print(" "); } out.println(); } out.flush(); out.close(); in.close(); } private static int[] solution(int[] a, int n) { int[] b = new int[n]; int start = 0; if (n % 2 == 1) { if (a[0] + a[1] != 0) { b[0] = -a[2]; b[1] = -a[2]; b[2] = a[0] + a[1]; } else if (a[0] + a[2] != 0) { b[0] = -a[1]; b[1] = a[0] + a[2]; b[2] = -a[1]; } else { b[0] = a[1] + a[2]; b[1] = -a[0]; b[2] = -a[0]; } start = 3; } else { b[0] = -a[1]; b[1] = a[0]; int gcd = gcd(b[0], b[1]); b[0] /= gcd; b[1] /= gcd; start = 2; } for (int i = start; i < n; i += 2) { b[i] = -a[i + 1]; b[i + 1] = a[i]; } return b; } private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream)); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readIntArr(int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = Integer.parseInt(next()); } return result; } long[] readLongArr(int n) { long[] result = new long[n]; for (int i = 0; i < n; i++) { result[i] = Long.parseLong(next()); } return result; } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } long nextLong() { return Long.parseLong(next()); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.text.DecimalFormat; import java.util.*; public class Codeforces { static int mod=1000000007 ; static List<Integer>[] adj; static boolean vst[]; static int dp[]; public static void main(String[] args) throws Exception { PrintWriter out=new PrintWriter(System.out); FastScanner fs=new FastScanner(); int t=fs.nextInt(); while(t-->0) { int n=fs.nextInt(); int arr[]=fs.readArray(n); int ans[]=new int[n]; if(n%2==0) { for(int i=0;i<n;i+=2) { ans[i]=-arr[i+1]; ans[i+1]=arr[i]; } } else { for(int i=3;i<n;i+=2) { ans[i]=-arr[i+1]; ans[i+1]=arr[i]; } int a=0, b=0, c=0; outer:for(int i=0;i<3;i++) { for(int j=i+1;j<3;j++) { if(arr[i]+arr[j]!=0) { b=i; c=j; a= 3-c-b; break outer; } } } ans[a]=arr[b]+arr[c]; ans[b]=-arr[a]; ans[c]=-arr[a]; } for(int i=0;i<n;i++) { out.print(ans[i]+" "); } out.println(); // long sum=0; // for(int i=0;i<n;i++) { // sum+=arr[i]*ans[i]; // } // if(sum!=0) System.out.println(false); } out.close(); } static long pow(long a,long b) { if(b<0) return 1; long res=1; while(b!=0) { if((b&1)!=0) { res*=a; res%=mod; } a*=a; a%=mod; b=b>>1; } return res; } static long gcd(long a,long b) { if(b==0) return a; return gcd(b,a%b); } static long nck(int n,int k) { if(k>n) return 0; long res=1; res*=fact(n); res%=mod; res*=modInv(fact(k)); res%=mod; res*=modInv(fact(n-k)); res%=mod; return res; } static long fact(long n) { // return fact[(int)n]; long res=1; for(int i=2;i<=n;i++) { res*=i; res%=mod; } return res; } static long modInv(long n) { return pow(n,mod-2); } static void sort(int[] a) { //suffle int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n); int temp=a[i]; a[i]=a[oi]; a[oi]=temp; } //then sort Arrays.sort(a); } // Use this to input code since it is faster than a Scanner static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long[] lreadArray(int n) { long a[]=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
0
b2590225
fdcbdf85
//package com.company; import java.util.*; public class P3 { public static class tower implements Comparable<tower>{ ArrayList<Integer> index; int size; tower(){ size = 0; index = new ArrayList<>(); } @Override public int compareTo(tower a) { if(this.size > a.size) { return 1; } else if (this.size < a.size) { return -1; } else { return 0; } } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t>0){ t--; int n = scan.nextInt(); int m = scan.nextInt(); int x = scan.nextInt(); ArrayList<Integer> arr = new ArrayList<>(); HashMap<Integer,Integer> map = new HashMap<>(); for(int i = 0;i<n;i++){ arr.add(scan.nextInt()); map.put(i, arr.get(i)); } System.out.println("YES"); PriorityQueue<tower> towers = new PriorityQueue<>(); for(int i = 0;i<m;i++){ tower curr = new tower(); towers.add(curr); } for(int i = 0;i<n;i++){ tower curr = towers.poll(); curr.size += arr.get(i); curr.index.add(i); towers.add(curr); } int[] ans = new int[n]; int count = 1; while(towers.size() > 0){ tower curr = towers.poll(); for(Integer p : curr.index){ ans[p] = count; } count++; } for(int i = 0;i<n;i++){ System.out.print(ans[i] +" "); } System.out.println(); } } }
import java.io.*; import java.util.*; import java.lang.*; public class B{ public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static long MOD = (long) (1e9 + 7); // static long MOD = 998244353; static long MOD2 = MOD * MOD; static FastReader sc = new FastReader(); static int pInf = Integer.MAX_VALUE; static int nInf = Integer.MIN_VALUE; static long ded = (long)(1e17)+9; public static void main(String[] args) throws Exception { int test = 1; test = sc.nextInt(); for (int i = 1; i <= test; i++){ // out.print("Case #"+i+": "); solve(); } out.flush(); out.close(); } static void solve(){ int n = sc.nextInt(); int m = sc.nextInt(); int diff = sc.nextInt(); ArrayList<Pair> A = new ArrayList<>(); for(int i= 0; i < n; i++){ int x = sc.nextInt(); A.add(new Pair(x,i)); } int[] sum = new int[m]; int[] ans = new int[n]; Collections.sort(A); for(int i= 0; i < n; i++){ int idx = i%m; sum[idx] += A.get(i).x; ans[A.get(i).y] = idx+1; } ruffleSort(sum); if(Math.abs(sum[0]-sum[sum.length-1])>diff){ out.println("NO"); return; } out.println("YES"); for(int i = 0; i < n; i++){ out.print(ans[i]+" "); } out.println(); } static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o){ if(this.x==o.x){ return -1*(o.y-this.y); } return this.x-o.x; } @Override public String toString() { return "Pair{" + "x=" + x + ", y=" + y + '}'; } public boolean equals(Pair o){ return this.x==o.x&&this.y==o.y; } } public static long mul(long a, long b) { return ((a % MOD) * (b % MOD)) % MOD; } public static long add(long a, long b) { return ((a % MOD) + (b % MOD)) % MOD; } public static long c2(long n) { if ((n & 1) == 0) { return mul(n / 2, n - 1); } else { return mul(n, (n - 1) / 2); } } //Shuffle Sort static final Random random = new Random(); static void ruffleSort(int[] a) { int n = a.length;//shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n); int temp= a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } //Brian Kernighans Algorithm static long countSetBits(long n) { if (n == 0) return 0; return 1 + countSetBits(n & (n - 1)); } //Euclidean Algorithm static long gcd(long A, long B) { if (B == 0) return A; return gcd(B, A % B); } //Modular Exponentiation static long fastExpo(long x, long n) { if (n == 0) return 1; if ((n & 1) == 0) return fastExpo((x * x) % MOD, n / 2) % MOD; return ((x % MOD) * fastExpo((x * x) % MOD, (n - 1) / 2)) % MOD; } //AKS Algorithm static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i <= Math.sqrt(n); i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static long modinv(long x) { return modpow(x, MOD - 2); } public static long modpow(long a, long b) { if (b == 0) { return 1; } long x = modpow(a, b / 2); x = (x * x) % MOD; if (b % 2 == 1) { return (x * a) % MOD; } return x; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
0
69b8ffb2
ba468e1f
import java.util.*; //CODE FORCES public class anshulvmc { public static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static int gcd(int a, int b) { if (b==0) return a; return gcd(b, a%b); } public static void google(int t) { System.out.println("Case #"+t+": "); } // public static void gt(int[][] arr,int k) { // int n = arr.length+1; // k = Math.min(k,n+1); // // Node[] nodes = new Node[n]; // for(int i=0;i<n;i++) nodes[i] = new Node(); // for(int i=0;i<n-1;i++) { // int a = arr[i][0]; // int b = arr[i][1]; // System.out.println(a+" "+b); // nodes[a].adj.add(nodes[b]); // nodes[b].adj.add(nodes[a]); // } // // ArrayDeque<Node> bfs = new ArrayDeque<>(); // for(Node nn:nodes) { // if(nn.adj.size()<2) { // bfs.addLast(nn); // nn.dist=0; // } // } // // while(bfs.size()>0) { // Node nn = bfs.removeFirst(); // for(Node a : nn.adj) { // if(a.dist!=-1) continue; // a.usedDegree++; // if(a.adj.size() - a.usedDegree <= 1) { // a.dist = nn.dist+1; // bfs.addLast(a); // } // } // } // // int[] cs = new int[n+1]; // for(Node nn:nodes) { // cs[nn.dist]++; // } // for(int i=1;i<cs.length;i++) cs[i]+=cs[i-1]; // System.out.println(n-cs[k-1]); // } public static class Node{ ArrayList<Node> adj = new ArrayList<>(); int dist = -1; int usedDegree = 0; } public static void cat_mice(int dest,int[] arr) { sort(arr); int time = dest; int timeleft = dest-1; int counter=0; for(int i=arr.length-1;i>=0;i--) { int val = arr[i]; int takes = time - val; if(takes <= timeleft) { timeleft -= takes; counter++; } } System.out.println(counter); } public static void minex(int n,int[] arr) { sort(arr); int ans=arr[0]; for(int i=0;i<arr.length-1;i++) { ans = Math.max(ans,arr[i+1] - arr[i]); } System.out.println(ans); } public static void func(long start,long n) { long x = start; if(n==0){ System.out.println(x); return; } long k=n-1; long c=k/4; long rem=k%4; long ans=x; if(x%2 == 0) { ans -= 1; ans -= (c * 4); if(rem == 1) { ans += n; } else if(rem == 2) { ans += n + n-1; } else if(rem == 3){ ans += (n-2) + (n-1) - n; } } else { ans += 1; ans += (c * 4); if(rem == 1) { ans -= n; } else if(rem == 2) { ans -= n + n-1; } else if(rem == 3) { ans -= n-2 + n-1 - n; } } System.out.println(ans); } public static boolean redblue(int[] num, String chnum) { ArrayList<Integer> blue = new ArrayList<>(); ArrayList<Integer> red = new ArrayList<>(); for(int i=0;i<chnum.length();i++) { char ch = chnum.charAt(i); if(ch == 'B') { blue.add(num[i]); } else { red.add(num[i]); } } Collections.sort(blue); Collections.sort(red); // System.out.println(blue); // System.out.println(red); for(int i=0;i<blue.size();i++) { if(blue.get(i) >= i+1) { } else { return false; } } for(int i=0;i<red.size();i++) { if(red.get(i) > i+1 + blue.size()) { // System.out.println(red.get(i)+" "+(i+1 + blue.size())); return false; } } return true; } public static void main(String args[]) { Scanner scn = new Scanner(System.in); int test = scn.nextInt(); for(int i=0;i<test;i++) { int size = scn.nextInt(); int[] arr = new int[size]; for(int j=0;j<size;j++) { arr[j] = scn.nextInt(); } String str = scn.next(); boolean f = redblue(arr,str); if(f) { System.out.println("YES"); } else { System.out.println("NO"); } } // int n = 1; // int[] dp = new int[2]; // System.out.println(fact(n,dp)); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Codeforces { public static void main(String[] args) { FastReader fastReader = new FastReader(); int t = fastReader.nextInt(); while (t-- > 0) { int n = fastReader.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = fastReader.nextInt(); } ArrayList<Integer> b = new ArrayList<>(); ArrayList<Integer> r = new ArrayList<>(); char c[] = fastReader.next().toCharArray(); for (int i = 0; i < n; i++) { if (c[i] == 'B') { b.add(a[i]); } else { r.add(a[i]); } } Collections.sort(b); Collections.sort(r); int sizeb = b.size(); boolean isValid = true; for (int i = 1 , j = 0; i <=sizeb; i++ , j++){ if (b.get(j) < i){ isValid =false; } } for (int i = sizeb+1 , j = 0; i <=n && j < r.size(); i++ , j++){ if (r.get(j) > i){ isValid =false; } } if (isValid){ System.out.println("YES"); }else{ System.out.println("NO"); } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
0
a368f345
b434c275
import java.util.*; import java.io.*; public class codeforces { static class Pair { char type; int L; int R; Pair(char type, int L, int R) { this.type = type; this.L = L; this.R = R; } } static FastReader fr; static StringBuilder res; static class FastReader { BufferedReader br; StringTokenizer st; BufferedWriter bw; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(System.out)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } char nextChar() { return next().charAt(0); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } void write(String str) { try { bw.write(str); } catch (IOException e) { e.printStackTrace(); } } void close() { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) throws java.lang.Exception { fr = new FastReader(); boolean fixed = false; int t = !fixed ? fr.nextInt() : 1; res = new StringBuilder(); while (t-- > 0) { solve(); } fr.write(res.toString()); fr.close(); } static int M = 1000008; static boolean[] primes = new boolean[M]; static int[] dp = new int[M]; public static void sieve() { for (int i = 2; i * i <= M; i++) { if (!primes[i]) { for (int j = i * i; j < M; j += i) { primes[j] = true; } } } for (int i = 5; i < 1000001; i++) { if (!primes[i] && !primes[i - 2]) dp[i] = dp[i - 1] + 1; else dp[i] = dp[i - 1]; } } public static void solve() { int n = fr.nextInt(); String[] arr = new String[n]; for (int i = 0; i < n; i++) { arr[i] = fr.nextLine(); } char[] chars = { 'a', 'b', 'c', 'd', 'e' }; int ans = 0; for (int i = 0; i < 5; i++) { char ch = chars[i]; List<Integer> a = new ArrayList<>(); for (int j = 0; j < n; j++) { String s = arr[j]; int c = 0; for (int k = 0; k < s.length(); k++) { if (s.charAt(k) == ch) c++; } a.add(2 * c - s.length()); } // System.out.println(a); Collections.sort(a, Collections.reverseOrder()); int sum = 0, len = 0; for (int k = 0; k < a.size(); k++) { if (sum + a.get(k) <= 0) { break; } else { len++; sum += a.get(k); } } ans = Math.max(ans, len); } res.append(ans + "\n"); } public static int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = fr.nextInt(); } return arr; } public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public static List<Integer> readArrayList(int n) { List<Integer> arr = new ArrayList<>(); for (int i = 0; i < n; i++) { arr.add(fr.nextInt()); } return arr; } }
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; public final class A { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<Integer> g[]; static long mod=(long)1e9+7,INF=Long.MAX_VALUE; static boolean set[],col[]; static int par[],tot[],partial[]; static int D[],P[][]; static long dp[][],sum=0,max=0,size[]; // static node1 seg[]; //static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)}; public static void main(String args[])throws IOException { int T=i(); outer:while(T-->0) { int N=i(); int size[]=new int[N]; PriorityQueue<node1> q[]=new PriorityQueue[26]; for(int i=0; i<26; i++)q[i]=new PriorityQueue<node1>(); for(int i=0; i<N; i++) { char X[]=in.next().toCharArray(); int s=X.length; size[i]=s; int f[]=new int[26]; for(char x:X)f[x-'a']++; for(int j=0; j<26; j++)q[j].add(new node1(f[j],i,s)); } int max=0; for(int i=0; i<26; i++) { PriorityQueue<node1> q_new=new PriorityQueue<>(); q_new=q[i]; int c=0; long f=0; while(q_new.size()>0) { node1 x=q_new.remove(); // System.out.println(x.f+" "+x.size+" "+x.a); f+=x.a; if(f>0) { c++; max=Math.max(max, c); } else break; } } out.println(max); } out.close(); } static int OR(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("OR "+i+" "+j); return i(); } static int AND(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("AND "+i+" "+j); return i(); } static int XOR(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("XOR "+i+" "+j); return i(); } static boolean f1(int l,char X[]) { int i=0; for(; l<X.length; l++) { if(X[i]!=X[l])return false; i++; } return true; } static int f(int a) { for(int i=a+1; a>0; a++) { if(GCD(i,a)==1)return i; } return 0; } static int min(char X[],char str[],int N) { int s=0; for(int i=0; i<N; i++) { int it=i%3; if(X[i]!=str[it])s++; // ans.append(str[it]); } return s; } static char f(int i,char X[]) { int a=0,b=0,c=0; for(; i<X.length; i+=3) { if(X[i]=='R')a++; if(X[i]=='B')b++; if(X[i]=='G')c++; } if(a>=b && a>=c)return 'R'; if(b>=a && b>=c)return 'B'; return 'G'; } static void f1(int n,int p,long sum,int N) { for(int c:g[n]) { if(c==p)continue; long s=sum+N-2*size[c]; f1(c,n,s,N); max=Math.max(max, s); } } static long f(int i,int j,ArrayList<Integer> A) { if(i+1==A.size()) { return j; } int moves=1+A.get(i); if(j==1)return 1+f(i+1,moves,A); if(j>0 && dp[i][j-1]==0)f(i,j-1,A); return dp[i][j]=dp[i][j-1]+f(i+1,j+moves,A); } // static void build(int v,int tl,int tr,long A[]) // { // if(tl==tr) // { // seg[v]=new node1(A[tl],A[tr],1,true); // return ; // } // int tm=(tl+tr)/2; // build(2*v,tl,tm,A); // build(2*v+1,tm+1,tr,A); // seg[v]=merge(seg[2*v],seg[2*v+1]); // } // static node1 ask(int v,int tl,int tr,int l,int r) // { // if(l>r)return new node1(0,0,0,false);//verify true or false // if(tl==l && tr==r)return seg[v]; // int tm=(tl+tr)/2; // node1 a=ask(v*2,tl,tm,l,Math.min(tm, r)); // node1 b=ask(v*2+1,tm+1,tr,Math.max(tm+1, l),r); // return merge(a,b); // } // static node1 merge(node1 a,node1 b) // { // long s=0; // long l1=a.L,r1=a.R,c1=a.cnt; // long l2=b.L,r2=b.R,c2=b.cnt; // long g=GCD(l2,r1); s=c1+c2; // if(g==1) // { // s--; // g=(l2*r1)/g; // if(c1==1) // { // l1=g; // } // if(c2==1)r2=g; // return new node1(l1,r2,s,true); // } // return new node1(l1,r2,s,a.leaf^b.leaf); // } static long f(long l,long r,long a,long b,long N) { while(r-l>1) { long m=(l+r)/2; long x=m*b; if(N<x) { r=m; continue; } if((N-x)%a==0)r=m; else l=m; } return r; } static long f1(long a,long A[],long bits[],long sum) { long s=A.length; s=mul(s,a); s=(s+(sum%mod))%mod; long p=1L; for(long x:bits) { if((a&p)!=0)s=((s-mul(x,p))+mod)%mod; p<<=1; } return s; } static long f2(long a,long A[],long bits[]) { long s=0; long p=1L; for(long x:bits) { if((a&p)!=0) { s=(s+mul(p,x))%mod; } p<<=1; } return s; } static long f(long x1,long y1,long x2,long y2) { return Math.abs(x1-x2)+Math.abs(y1-y2); } static long f(long x,long max,long s) { long l=-1,r=(x/s)+1; while(r-l>1) { long m=(l+r)/2; if(x-m*s>max)l=m; else r=m; } return l+1; } static int f(long A[],long x) { int l=-1,r=A.length; while(r-l>1) { int m=(l+r)/2; if(A[m]>=x)r=m; else l=m; } return r; } static int bin(int x,ArrayList<Integer> A) { int l=0,r=A.size()-1; while(l<=r) { int m=(l+r)/2; int a=A.get(m); if(a==x)return m; if(a<x)l=m+1; else r=m-1; } return 0; } static int left(int x,ArrayList<Integer> A) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; int a=A.get(m); if(a<=x)l=m; else r=m; } return l; } static int right(int x,ArrayList<Integer> A) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; int a=A.get(m); if(a<x)l=m; else r=m; } return r; } static boolean equal(long A[],long B[]) { for(int i=0; i<A.length; i++)if(A[i]!=B[i])return false; return true; } static int max(int a ,int b,int c,int d) { a=Math.max(a, b); c=Math.max(c,d); return Math.max(a, c); } static int min(int a ,int b,int c,int d) { a=Math.min(a, b); c=Math.min(c,d); return Math.min(a, c); } static HashMap<Integer,Integer> Hash(int A[]) { HashMap<Integer,Integer> mp=new HashMap<>(); for(int a:A) { int f=mp.getOrDefault(a,0)+1; mp.put(a, f); } return mp; } static long mul(long a, long b) { return ( a %mod * 1L * b%mod )%mod; } static void swap(int A[],int a,int b) { int t=A[a]; A[a]=A[b]; A[b]=t; } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { a=find(a); b=find(b); if(a!=b) { par[a]+=par[b]; par[b]=a; } } static boolean isSorted(int A[]) { for(int i=1; i<A.length; i++) { if(A[i]<A[i-1])return false; } return true; } static boolean isDivisible(StringBuilder X,int i,long num) { long r=0; for(; i<X.length(); i++) { r=r*10+(X.charAt(i)-'0'); r=r%num; } return r==0; } static int lower_Bound(int A[],int low,int high, int x) { if (low > high) if (x >= A[high]) return A[high]; int mid = (low + high) / 2; if (A[mid] == x) return A[mid]; if (mid > 0 && A[mid - 1] <= x && x < A[mid]) return A[mid - 1]; if (x < A[mid]) return lower_Bound( A, low, mid - 1, x); return lower_Bound(A, mid + 1, high, x); } static String f(String A) { String X=""; for(int i=A.length()-1; i>=0; i--) { int c=A.charAt(i)-'0'; X+=(c+1)%2; } return X; } static void sort(long[] a) //check for long { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static String swap(String X,int i,int j) { char ch[]=X.toCharArray(); char a=ch[i]; ch[i]=ch[j]; ch[j]=a; return new String(ch); } static int sD(long n) { if (n % 2 == 0 ) return 2; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0 ) return i; } return (int)n; } static void setGraph(int N) { tot=new int[N+1]; partial=new int[N+1]; D=new int[N+1]; P=new int[N+1][(int)(Math.log(N)+10)]; set=new boolean[N+1]; g=new ArrayList[N+1]; for(int i=0; i<=N; i++) { g[i]=new ArrayList<>(); D[i]=Integer.MAX_VALUE; //D2[i]=INF; } } static long pow(long a,long b) { //long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } } class node1 implements Comparable<node1> { int index,f,size; long a; node1(int f,int i,int size) { this.f=f; this.index=i; this.size=size; a=2*f-size; } public int compareTo(node1 x) { if(this.a==x.a)return 0; else if(this.a<x.a)return 1; else return -1; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
0
1adac634
ee0bc145
import java.io.*; import java.util.*; public class Main { static int i, j, k, n, m, t, y, x, sum = 0; static long mod = 998244353; static FastScanner fs = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); static String str; static long ans; public static void main(String[] args) { t = fs.nextInt(); while (t-- > 0) { n = fs.nextInt(); int [] k = fs.readArray(n); int [] h = fs.readArray(n); List<Pair> arr = new ArrayList<>(); for(i=0;i<n;i++){ int a = k[i]-h[i]+1; arr.add(new Pair(a,k[i])); } Collections.sort(arr); ans=0; List<Pair> comp = new ArrayList<>(); int temp = arr.get(0).x; int tempy = arr.get(0).y; for(i=1;i<n;i++){ if(arr.get(i).x > tempy){ comp.add(new Pair(temp, tempy)); temp = arr.get(i).x; tempy = arr.get(i).y; } else tempy = Math.max(tempy, arr.get(i).y); } comp.add(new Pair(temp, tempy)); for(i=0;i<comp.size();i++){ long a = comp.get(i).y - comp.get(i).x +1 ; ans+=(((a)*(a+1))/2); } out.println(ans); } out.close(); } /*static long nck(int n , int k){ long a = fact[n]; long b = modInv(fact[k]); b*= modInv(fact[n-k]); b%=mod; return (a*b)%mod; } static void populateFact(){ fact[0]=1; fact[1] = 1; for(i=2;i<300005;i++){ fact[i]=i*fact[i-1]; fact[i]%=mod; } } */ static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long exp(long base, long pow) { if (pow == 0) return 1; long half = exp(base, pow / 2); if (pow % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long mul(long a, long b) { return ((a % mod) * (b % mod)) % mod; } static long add(long a, long b) { return ((a % mod) + (b % mod)) % mod; } static long modInv(long x) { return exp(x, mod - 2); } static void ruffleSort(int[] a) { //ruffle int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n), temp = a[i]; a[i] = a[oi]; a[oi] = temp; } //then sort Arrays.sort(a); } static void ruffleSort(long[] a) { //ruffle int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n); long temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static class Pair implements Comparable<Pair> { public int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { if (x == o.x) return Integer.compare(y, o.y); return Integer.compare(x, o.x); } } }
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int[] k = new int[n]; int[] h = new int[n]; for (int i = 0; i < n; ++i) k[i] = sc.nextInt(); for (int i = 0; i < n; ++i) h[i] = sc.nextInt(); Stack<Pair> stk = new Stack<>(); stk.push(new Pair(0, 0)); for (int i = 0; i < n; ++i) { int cur_pos = k[i]; int cur_inc = h[i]; while (!stk.isEmpty() && !(cur_pos - cur_inc + 1 > stk.peek().pos)) { cur_inc = Math.max(cur_inc, stk.peek().inc + cur_pos - stk.peek().pos); stk.pop(); } stk.add(new Pair(cur_pos, cur_inc)); } long answer = 0; while (!stk.isEmpty()) { answer += (1L * stk.peek().inc * (stk.peek().inc + 1) / 2); stk.pop(); } out.println(answer); } out.flush(); } static class Pair { int pos, inc; public Pair (int pos, int inc) { this.pos = pos; this.inc = inc; } } static class Scanner { BufferedReader in; StringTokenizer st; public Scanner() { this.in = new BufferedReader(new InputStreamReader(System.in)); } public Scanner(FileReader f) { this.in = new BufferedReader(f); } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } public void close() throws IOException { in.close(); } } }
0
034030f3
bf992c91
import java.util.*; import java.io.*; public class D_1525 { static int INF = (int)1e9; static int n, m; static int[] full, free; static int[][] memo; public static int dp(int i, int j) { if(i == n) return 0; if(j == m) return INF; if(memo[i][j] != -1) return memo[i][j]; return memo[i][j] = Math.min(dp(i, j + 1), Math.abs(free[j] - full[i]) + dp(i + 1, j + 1)); } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int l = sc.nextInt(); int[] array = sc.nextIntArray(l); n = 0; for(int i = 0; i < l; i++) if(array[i] == 1) n++; m = l - n; full = new int[n]; free = new int[m]; int ind1 = 0, ind2 = 0; for(int i = 0; i < l; i++) if(array[i] == 0) free[ind2++] = i; else full[ind1++] = i; memo = new int[n][m]; for(int[] i : memo) Arrays.fill(i, -1); pw.println(dp(0, 0)); pw.flush(); } public static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public long nextLong() throws IOException { return Long.parseLong(next()); } public int[] nextIntArray(int n) throws IOException { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] array = new Integer[n]; for (int i = 0; i < n; i++) array[i] = new Integer(nextInt()); return array; } public long[] nextLongArray(int n) throws IOException { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public double[] nextDoubleArray(int n) throws IOException { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } public static int[] shuffle(int[] a) { int n = a.length; Random rand = new Random(); for (int i = 0; i < n; i++) { int tmpIdx = rand.nextInt(n); int tmp = a[i]; a[i] = a[tmpIdx]; a[tmpIdx] = tmp; } return a; } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
import java.util.Arrays; import java.util.Scanner; public class P1525D { public static int[] ones, zeros; public static int[][] memo; public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int[] arr = new int[n]; int oneCount = 0; for (int i = 0; i < n; i++) { arr[i] = s.nextInt(); oneCount += arr[i] % 2; } int o = 0, z = 0; ones = new int[oneCount]; zeros = new int[n - oneCount]; for (int i = 0; i < n; i++) { if (arr[i] % 2 == 1) { ones[o++] = i; } else { zeros[z++] = i; } } memo = new int[oneCount][n - oneCount]; for (int[] row : memo) Arrays.fill(row, -1); System.out.println(dp(0, 0)); } public static int dp(int oi, int zi) { if (oi == ones.length) { return 0; } else if (zi == zeros.length) { return 100000000; } else { if (memo[oi][zi] == -1) { memo[oi][zi] = Math.min(Math.abs(ones[oi] - zeros[zi]) + dp(oi + 1, zi + 1), dp(oi, zi + 1)); } return memo[oi][zi]; } } }
1
54d7c21e
a7e7f371
import java.io.*; import java.util.*; public class cp { static int mod=(int)1e9+7; // static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static int[] sp; static int size=(int)1e6; static int[] arInt; static long[] arLong; public static void main(String[] args) throws IOException { long tc=sc.nextLong(); // Scanner sc=new Scanner(System.in); // int tc=1; // primeSet=new HashSet<>(); // sieveOfEratosthenes((int)1e6+5); while(tc-->0) { int n=sc.nextInt(); int k[]=new int[n]; int h[]=new int[n]; for(int i=0;i<n;i++) k[i]=sc.nextInt(); for(int i=0;i<n;i++) h[i]=sc.nextInt(); ArrayList<Pair> interval=new ArrayList<Pair>(); ArrayList<Pair> act=new ArrayList<Pair>(); for(int i=0;i<n;i++) interval.add(new Pair(k[i]-h[i]+1,k[i])); Collections.sort(interval); // out.println(interval); act.add(interval.get(0)); for(int i=1;i<n;i++) { Pair p=act.get(act.size()-1); if(p.y<interval.get(i).x) act.add(interval.get(i)); else p.y=Math.max(p.y, interval.get(i).y); } // out.println(act); long mana=0; for(Pair p: act) { long x=p.y-p.x+1; mana+=(x*(x+1))/2; } out.println(mana); // int n=sc.nextInt(); // long days[]=new long[n]; // long power[]=new long[n]; // for (int i = 0; i < power.length; i++) { // days[i]=sc.nextLong(); // } // for (int i = 0; i < power.length; i++) { // power[i]=sc.nextLong(); // // } // // long ans=0; // for(int i=0;i<n;i++) // { // if(i==0) // { // ans+=power[i]*(power[i]+1L)/2L; // continue; // } // // long temp=power[i]*(power[i]+1)/2L; // long temp2=(power[i-1]+days[i]-days[i-1])*(power[i-1]+days[i]-days[i-1]+1L)/2L; // temp2-=power[i-1]*(power[i-1]+1L)/2L; // ans+=Math.min(temp, temp2); //// if(days[i]-days[i-1]<=power[i]) //// { //// ans+=power[i]*(power[i]+1)/2; //// } //// else { //// ans+=power[i]*(power[i]+1)/2; //// ans-=power[i-1]*(power[i-1]+1)/2; //// } // // // } // // out.println(ans); } out.flush(); out.close(); System.gc(); } /* ...SOLUTION ENDS HERE...........SOLUTION ENDS HERE... */ static int util(char a,char b) { int A=a-'0'; int B=b-'0'; return A+B; } static boolean check(int x,int[] rich,int[] poor) { int cnt=0; for(int i=0;i<rich.length;i++) { if(x-1-rich[i]<=cnt && cnt<=poor[i]) cnt++; } return cnt>=x; } static void arrInt(int n) throws IOException { arInt=new int[n]; for (int i = 0; i < arInt.length; i++) { arInt[i]=sc.nextInt(); } } static void arrLong(int n) throws IOException { arLong=new long[n]; for (int i = 0; i < arLong.length; i++) { arLong[i]=sc.nextLong(); } } static ArrayList<Integer> add(int id,int c) { ArrayList<Integer> newArr=new ArrayList<>(); for(int i=0;i<id;i++) newArr.add(arInt[i]); newArr.add(c); for(int i=id;i<arInt.length;i++) { newArr.add(arInt[i]); } return newArr; } // function to find last index <= y static int upper(ArrayList<Integer> arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) <= y) l = mid + 1; else h = mid - 1; } return h; } static int lower(ArrayList<Integer> arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) >= x) h = mid - 1; else l = mid + 1; } return l; } static int N = 501; // Array to store inverse of 1 to N static long[] factorialNumInverse = new long[N + 1]; // Array to precompute inverse of 1! to N! static long[] naturalNumInverse = new long[N + 1]; // Array to store factorial of first N numbers static long[] fact = new long[N + 1]; // Function to precompute inverse of numbers public static void InverseofNumber(int p) { naturalNumInverse[0] = naturalNumInverse[1] = 1; for(int i = 2; i <= N; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse[0] = factorialNumInverse[1] = 1; // Precompute inverse of natural numbers for(int i = 2; i <= N; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to N public static void factorial(int p) { fact[0] = 1; // Precompute factorials for(int i = 1; i <= N; i++) { fact[i] = (fact[i - 1] * (long)i) % p; } } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R, int p) { // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p; return ans; } static String tr(String s) { int now = 0; while (now + 1 < s.length() && s.charAt(now)== '0') ++now; return s.substring(now); } static ArrayList<Integer> ans; static void dfs(int node,Graph gg,int cnt,int k,ArrayList<Integer> temp) { if(cnt==k) return; for(Integer each:gg.list[node]) { if(each==0) { temp.add(each); ans=new ArrayList<>(temp); temp.remove(temp.size()-1); continue; } temp.add(each); dfs(each,gg,cnt+1,k,temp); temp.remove(temp.size()-1); } return; } static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static ArrayList<Integer> commDiv(int a, int b) { // find gcd of a, b int n = gcd(a, b); // Count divisors of n. ArrayList<Integer> Div=new ArrayList<>(); for (int i = 1; i <= Math.sqrt(n); i++) { // if 'i' is factor of n if (n % i == 0) { // check if divisors are equal if (n / i == i) Div.add(i); else { Div.add(i); Div.add(n/i); } } } return Div; } static HashSet<Integer> factors(int x) { HashSet<Integer> a=new HashSet<Integer>(); for(int i=2;i*i<=x;i++) { if(x%i==0) { a.add(i); a.add(x/i); } } return a; } static class Node { int vertex; HashSet<Node> adj; boolean rem; Node(int ver) { vertex=ver; rem=false; adj=new HashSet<Node>(); } @Override public String toString() { return vertex+" "; } } static class Tuple{ int a; int b; int c; public Tuple(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } } //function to find prime factors of n static HashMap<Long,Long> findFactors(long n2) { HashMap<Long,Long> ans=new HashMap<>(); if(n2%2==0) { ans.put(2L, 0L); // cnt++; while((n2&1)==0) { n2=n2>>1; ans.put(2L, ans.get(2L)+1); // } } for(long i=3;i*i<=n2;i+=2) { if(n2%i==0) { ans.put((long)i, 0L); // cnt++; while(n2%i==0) { n2=n2/i; ans.put((long)i, ans.get((long)i)+1); } } } if(n2!=1) { ans.put(n2, ans.getOrDefault(n2, (long) 0)+1); } return ans; } //fenwick tree implementaion static class fwt { int n; long BITree[]; fwt(int n) { this.n=n; BITree=new long[n+1]; } fwt(int arr[], int n) { this.n=n; BITree=new long[n+1]; for(int i = 0; i < n; i++) updateBIT(n, i, arr[i]); } long getSum(int index) { long sum = 0; index = index + 1; while(index>0) { sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(int n, int index,int val) { index = index + 1; while(index <= n) { BITree[index] += val; index += index & (-index); } } void print() { for(int i=0;i<n;i++) out.print(getSum(i)+" "); out.println(); } } class sparseTable{ int n; long[][]dp; int log2[]; int P; void buildTable(long[] arr) { n=arr.length; P=(int)Math.floor(Math.log(n)/Math.log(2)); log2=new int[n+1]; log2[0]=log2[1]=0; for(int i=2;i<=n;i++) { log2[i]=log2[i/2]+1; } dp=new long[P+1][n]; for(int i=0;i<n;i++) { dp[0][i]=arr[i]; } for(int p=1;p<=P;p++) { for(int i=0;i+(1<<p)<=n;i++) { long left=dp[p-1][i]; long right=dp[p-1][i+(1<<(p-1))]; dp[p][i]=Math.max(left, right); } } } long maxQuery(int l,int r) { int len=r-l+1; int p=(int)Math.floor(log2[len]); long left=dp[p][l]; long right=dp[p][r-(1<<p)+1]; return Math.max(left, right); } } //Function to find number of set bits static int setBitNumber(long n) { if (n == 0) return 0; int msb = 0; n = n / 2; while (n != 0) { n = n / 2; msb++; } return msb; } static int getFirstSetBitPos(long n) { return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1; } static ArrayList<Integer> primes; static HashSet<Integer> primeSet; static boolean prime[]; static void sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. prime= new boolean[n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) primeSet.add(i); } } static long mod(long a, long b) { long c = a % b; return (c < 0) ? c + b : c; } static void swap(long arr[],int i,int j) { long temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } static boolean util(int a,int b,int c) { if(b>a)util(b, a, c); while(c>=a) { c-=a; if(c%b==0) return true; } return (c%b==0); } static void flag(boolean flag) { out.println(flag ? "YES" : "NO"); out.flush(); } static void print(int a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print(long a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print_int(ArrayList<Integer> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void print_long(ArrayList<Long> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void printYesNo(boolean condition) { if (condition) { out.println("YES"); } else { out.println("NO"); } } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int lowerIndex(int arr[], int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] >= x) h = mid - 1; else l = mid + 1; } return l; } // function to find last index <= y static int upperIndex(int arr[], int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int upperIndex(long arr[], int n, long y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static int UpperBound(long a[], long x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static class DisjointUnionSets { int[] rank, parent; int n; // Constructor public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } // Creates n sets with single item in each void makeSet() { for (int i = 0; i < n; i++) parent[i] = i; } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } // Unites the set that includes x and the set // that includes x void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else // if ranks are the same { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } // if(xRoot!=yRoot) // parent[y]=x; } int connectedComponents() { int cnt=0; for(int i=0;i<n;i++) { if(parent[i]==i) cnt++; } return cnt; } } static class Graph { int v; ArrayList<Integer> list[]; Graph(int v) { this.v=v; list=new ArrayList[v+1]; for(int i=1;i<=v;i++) list[i]=new ArrayList<Integer>(); } void addEdge(int a, int b) { this.list[a].add(b); } } // static class GraphMap{ // Map<String,ArrayList<String>> graph; // GraphMap() { // // TODO Auto-generated constructor stub // graph=new HashMap<String,ArrayList<String>>(); // // } // void addEdge(String a,String b) // { // if(graph.containsKey(a)) // this.graph.get(a).add(b); // else { // this.graph.put(a, new ArrayList<>()); // this.graph.get(a).add(b); // } // } // } // static void dfsMap(GraphMap g,HashSet<String> vis,String src,int ok) // { // vis.add(src); // // if(g.graph.get(src)!=null) // { // for(String each:g.graph.get(src)) // { // if(!vis.contains(each)) // { // dfsMap(g, vis, each, ok+1); // } // } // } // // cnt=Math.max(cnt, ok); // } static double sum[]; static long cnt; // static void DFS(Graph g, boolean[] visited, int u) // { // visited[u]=true; // // for(int i=0;i<g.list[u].size();i++) // { // int v=g.list[u].get(i); // // if(!visited[v]) // { // cnt1=cnt1*2; // DFS(g, visited, v); // // } // // } // // // } static class Pair implements Comparable<Pair> { int x; int y; Pair(int x,int y) { this.x=x; this.y=y; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return this.x-o.x; } } static long sum_array(int a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static long sum_array(long a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void reverse_array(int a[]) { int n=a.length; int i,t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static void reverse_array(long a[]) { int n=a.length; int i; long t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } // static long modInverse(long a, long m) // { // long g = gcd(a, m); // // return power(a, m - 2, m); // // } static long power(long x, long y) { long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static int power(int x, int y) { int res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static long gcd(long a, long b) { if (a == 0) return b; //cnt+=a/b; return gcd(b%a,a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static PrintWriter out=new PrintWriter(System.out); static int int_max=Integer.MAX_VALUE; static int int_min=Integer.MIN_VALUE; static long long_max=Long.MAX_VALUE; static long long_min=Long.MIN_VALUE; }
import java.io.*; import java.util.*; public class cp { static int mod=(int)1e9+7; // static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static int[] sp; static int size=(int)1e6; static int[] arInt; static long[] arLong; public static void main(String[] args) throws IOException { long tc=sc.nextLong(); // Scanner sc=new Scanner(System.in); // int tc=1; // primeSet=new HashSet<>(); // sieveOfEratosthenes((int)1e6+5); while(tc-->0) { int n=sc.nextInt(); int k[]=new int[n]; int h[]=new int[n]; for(int i=0;i<n;i++) k[i]=sc.nextInt(); for(int i=0;i<n;i++) h[i]=sc.nextInt(); ArrayList<Pair> interval=new ArrayList<Pair>(); ArrayList<Pair> act=new ArrayList<Pair>(); for(int i=0;i<n;i++) interval.add(new Pair(k[i]-h[i]+1,k[i])); Collections.sort(interval); // out.println(interval); act.add(interval.get(0)); for(int i=1;i<n;i++) { Pair p=act.get(act.size()-1); if(p.y<interval.get(i).x) act.add(interval.get(i)); else p.y=Math.max(p.y, interval.get(i).y); } // out.println(act); long mana=0; for(Pair p: act) { long x=p.y-p.x+1; mana+=(x*(x+1))/2; } out.println(mana); // int n=sc.nextInt(); // long days[]=new long[n]; // long power[]=new long[n]; // for (int i = 0; i < power.length; i++) { // days[i]=sc.nextLong(); // } // for (int i = 0; i < power.length; i++) { // power[i]=sc.nextLong(); // // } // // long ans=0; // for(int i=0;i<n;i++) // { // if(i==0) // { // ans+=power[i]*(power[i]+1L)/2L; // continue; // } // // long temp=power[i]*(power[i]+1)/2L; // long temp2=(power[i-1]+days[i]-days[i-1])*(power[i-1]+days[i]-days[i-1]+1L)/2L; // temp2-=power[i-1]*(power[i-1]+1L)/2L; // ans+=Math.min(temp, temp2); //// if(days[i]-days[i-1]<=power[i]) //// { //// ans+=power[i]*(power[i]+1)/2; //// } //// else { //// ans+=power[i]*(power[i]+1)/2; //// ans-=power[i-1]*(power[i-1]+1)/2; //// } // // // } // // out.println(ans); } out.flush(); out.close(); System.gc(); } /* ...SOLUTION ENDS HERE...........SOLUTION ENDS HERE... */ static int util(char a,char b) { int A=a-'0'; int B=b-'0'; return A+B; } static boolean check(int x,int[] rich,int[] poor) { int cnt=0; for(int i=0;i<rich.length;i++) { if(x-1-rich[i]<=cnt && cnt<=poor[i]) cnt++; } return cnt>=x; } static void arrInt(int n) throws IOException { arInt=new int[n]; for (int i = 0; i < arInt.length; i++) { arInt[i]=sc.nextInt(); } } static void arrLong(int n) throws IOException { arLong=new long[n]; for (int i = 0; i < arLong.length; i++) { arLong[i]=sc.nextLong(); } } static ArrayList<Integer> add(int id,int c) { ArrayList<Integer> newArr=new ArrayList<>(); for(int i=0;i<id;i++) newArr.add(arInt[i]); newArr.add(c); for(int i=id;i<arInt.length;i++) { newArr.add(arInt[i]); } return newArr; } // function to find last index <= y static int upper(ArrayList<Integer> arr, int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) <= y) l = mid + 1; else h = mid - 1; } return h; } static int lower(ArrayList<Integer> arr, int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr.get(mid) >= x) h = mid - 1; else l = mid + 1; } return l; } static int N = 501; // Array to store inverse of 1 to N static long[] factorialNumInverse = new long[N + 1]; // Array to precompute inverse of 1! to N! static long[] naturalNumInverse = new long[N + 1]; // Array to store factorial of first N numbers static long[] fact = new long[N + 1]; // Function to precompute inverse of numbers public static void InverseofNumber(int p) { naturalNumInverse[0] = naturalNumInverse[1] = 1; for(int i = 2; i <= N; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse[0] = factorialNumInverse[1] = 1; // Precompute inverse of natural numbers for(int i = 2; i <= N; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to N public static void factorial(int p) { fact[0] = 1; // Precompute factorials for(int i = 1; i <= N; i++) { fact[i] = (fact[i - 1] * (long)i) % p; } } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R, int p) { // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p; return ans; } static String tr(String s) { int now = 0; while (now + 1 < s.length() && s.charAt(now)== '0') ++now; return s.substring(now); } static ArrayList<Integer> ans; static void dfs(int node,Graph gg,int cnt,int k,ArrayList<Integer> temp) { if(cnt==k) return; for(Integer each:gg.list[node]) { if(each==0) { temp.add(each); ans=new ArrayList<>(temp); temp.remove(temp.size()-1); continue; } temp.add(each); dfs(each,gg,cnt+1,k,temp); temp.remove(temp.size()-1); } return; } static boolean isPrime(long n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static ArrayList<Integer> commDiv(int a, int b) { // find gcd of a, b int n = gcd(a, b); // Count divisors of n. ArrayList<Integer> Div=new ArrayList<>(); for (int i = 1; i <= Math.sqrt(n); i++) { // if 'i' is factor of n if (n % i == 0) { // check if divisors are equal if (n / i == i) Div.add(i); else { Div.add(i); Div.add(n/i); } } } return Div; } static HashSet<Integer> factors(int x) { HashSet<Integer> a=new HashSet<Integer>(); for(int i=2;i*i<=x;i++) { if(x%i==0) { a.add(i); a.add(x/i); } } return a; } static class Node { int vertex; HashSet<Node> adj; boolean rem; Node(int ver) { vertex=ver; rem=false; adj=new HashSet<Node>(); } @Override public String toString() { return vertex+" "; } } static class Tuple{ int a; int b; int c; public Tuple(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } } //function to find prime factors of n static HashMap<Long,Long> findFactors(long n2) { HashMap<Long,Long> ans=new HashMap<>(); if(n2%2==0) { ans.put(2L, 0L); // cnt++; while((n2&1)==0) { n2=n2>>1; ans.put(2L, ans.get(2L)+1); // } } for(long i=3;i*i<=n2;i+=2) { if(n2%i==0) { ans.put((long)i, 0L); // cnt++; while(n2%i==0) { n2=n2/i; ans.put((long)i, ans.get((long)i)+1); } } } if(n2!=1) { ans.put(n2, ans.getOrDefault(n2, (long) 0)+1); } return ans; } //fenwick tree implementaion static class fwt { int n; long BITree[]; fwt(int n) { this.n=n; BITree=new long[n+1]; } fwt(int arr[], int n) { this.n=n; BITree=new long[n+1]; for(int i = 0; i < n; i++) updateBIT(n, i, arr[i]); } long getSum(int index) { long sum = 0; index = index + 1; while(index>0) { sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(int n, int index,int val) { index = index + 1; while(index <= n) { BITree[index] += val; index += index & (-index); } } void print() { for(int i=0;i<n;i++) out.print(getSum(i)+" "); out.println(); } } class sparseTable{ int n; long[][]dp; int log2[]; int P; void buildTable(long[] arr) { n=arr.length; P=(int)Math.floor(Math.log(n)/Math.log(2)); log2=new int[n+1]; log2[0]=log2[1]=0; for(int i=2;i<=n;i++) { log2[i]=log2[i/2]+1; } dp=new long[P+1][n]; for(int i=0;i<n;i++) { dp[0][i]=arr[i]; } for(int p=1;p<=P;p++) { for(int i=0;i+(1<<p)<=n;i++) { long left=dp[p-1][i]; long right=dp[p-1][i+(1<<(p-1))]; dp[p][i]=Math.max(left, right); } } } long maxQuery(int l,int r) { int len=r-l+1; int p=(int)Math.floor(log2[len]); long left=dp[p][l]; long right=dp[p][r-(1<<p)+1]; return Math.max(left, right); } } //Function to find number of set bits static int setBitNumber(long n) { if (n == 0) return 0; int msb = 0; n = n / 2; while (n != 0) { n = n / 2; msb++; } return msb; } static int getFirstSetBitPos(long n) { return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1; } static ArrayList<Integer> primes; static HashSet<Integer> primeSet; static boolean prime[]; static void sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. prime= new boolean[n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) primeSet.add(i); } } static long mod(long a, long b) { long c = a % b; return (c < 0) ? c + b : c; } static void swap(long arr[],int i,int j) { long temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } static boolean util(int a,int b,int c) { if(b>a)util(b, a, c); while(c>=a) { c-=a; if(c%b==0) return true; } return (c%b==0); } static void flag(boolean flag) { out.println(flag ? "YES" : "NO"); out.flush(); } static void print(int a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print(long a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print_int(ArrayList<Integer> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void print_long(ArrayList<Long> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void printYesNo(boolean condition) { if (condition) { out.println("YES"); } else { out.println("NO"); } } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int lowerIndex(int arr[], int n, int x) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] >= x) h = mid - 1; else l = mid + 1; } return l; } // function to find last index <= y static int upperIndex(int arr[], int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int upperIndex(long arr[], int n, long y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static int UpperBound(long a[], long x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static class DisjointUnionSets { int[] rank, parent; int n; // Constructor public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } // Creates n sets with single item in each void makeSet() { for (int i = 0; i < n; i++) parent[i] = i; } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } // Unites the set that includes x and the set // that includes x void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else // if ranks are the same { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } // if(xRoot!=yRoot) // parent[y]=x; } int connectedComponents() { int cnt=0; for(int i=0;i<n;i++) { if(parent[i]==i) cnt++; } return cnt; } } static class Graph { int v; ArrayList<Integer> list[]; Graph(int v) { this.v=v; list=new ArrayList[v+1]; for(int i=1;i<=v;i++) list[i]=new ArrayList<Integer>(); } void addEdge(int a, int b) { this.list[a].add(b); } } // static class GraphMap{ // Map<String,ArrayList<String>> graph; // GraphMap() { // // TODO Auto-generated constructor stub // graph=new HashMap<String,ArrayList<String>>(); // // } // void addEdge(String a,String b) // { // if(graph.containsKey(a)) // this.graph.get(a).add(b); // else { // this.graph.put(a, new ArrayList<>()); // this.graph.get(a).add(b); // } // } // } // static void dfsMap(GraphMap g,HashSet<String> vis,String src,int ok) // { // vis.add(src); // // if(g.graph.get(src)!=null) // { // for(String each:g.graph.get(src)) // { // if(!vis.contains(each)) // { // dfsMap(g, vis, each, ok+1); // } // } // } // // cnt=Math.max(cnt, ok); // } static double sum[]; static long cnt; // static void DFS(Graph g, boolean[] visited, int u) // { // visited[u]=true; // // for(int i=0;i<g.list[u].size();i++) // { // int v=g.list[u].get(i); // // if(!visited[v]) // { // cnt1=cnt1*2; // DFS(g, visited, v); // // } // // } // // // } static class Pair implements Comparable<Pair> { int x; int y; Pair(int x,int y) { this.x=x; this.y=y; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return this.x-o.x; } } static long sum_array(int a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static long sum_array(long a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void reverse_array(int a[]) { int n=a.length; int i,t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static void reverse_array(long a[]) { int n=a.length; int i; long t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } // static long modInverse(long a, long m) // { // long g = gcd(a, m); // // return power(a, m - 2, m); // // } static long power(long x, long y) { long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static int power(int x, int y) { int res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static long gcd(long a, long b) { if (a == 0) return b; //cnt+=a/b; return gcd(b%a,a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static PrintWriter out=new PrintWriter(System.out); static int int_max=Integer.MAX_VALUE; static int int_min=Integer.MIN_VALUE; static long long_max=Long.MAX_VALUE; static long long_min=Long.MIN_VALUE; }
1
317a209c
921b6e4a
import java.io.*; import java.util.*; public class D_Java { public static final int MOD = 998244353; public static int mul(int a, int b) { return (int)((long)a * (long)b % MOD); } int[] f; int[] rf; public int C(int n, int k) { return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n-k], rf[k])); } public static int pow(int a, int n) { int res = 1; while (n != 0) { if ((n & 1) == 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } static void shuffleArray(int[] a) { Random rnd = new Random(); for (int i = a.length-1; i > 0; i--) { int index = rnd.nextInt(i + 1); int tmp = a[index]; a[index] = a[i]; a[i] = tmp; } } public static int inv(int a) { return pow(a, MOD-2); } public void doIt() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tok.nextToken()); int k = Integer.parseInt(tok.nextToken()); f = new int[n+42]; rf = new int[n+42]; f[0] = rf[0] = 1; for (int i = 1; i < f.length; ++i) { f[i] = mul(f[i-1], i); rf[i] = mul(rf[i-1], inv(i)); } int[] events = new int[2*n]; for (int i = 0; i < n; ++i) { tok = new StringTokenizer(in.readLine()); int le = Integer.parseInt(tok.nextToken()); int ri = Integer.parseInt(tok.nextToken()); events[i] = le*2; events[i + n] = ri*2 + 1; } shuffleArray(events); Arrays.sort(events); int ans = 0; int balance = 0; for (int r = 0; r < 2*n;) { int l = r; while (r < 2*n && events[l] == events[r]) { ++r; } int added = r - l; if (events[l] % 2 == 0) { // Open event ans += C(balance + added, k); if (ans >= MOD) ans -= MOD; ans += MOD - C(balance, k); if (ans >= MOD) ans -= MOD; balance += added; } else { // Close event balance -= added; } } in.close(); System.out.println(ans); } public static void main(String[] args) throws IOException { (new D_Java()).doIt(); } }
import java.io.*; import java.util.*; public class D_Java { public static final int MOD = 998244353; public static int mul(int a, int b) { return (int)((long)a * (long)b % MOD); } int[] f; int[] rf; public int C(int n, int k) { return (k < 0 || k > n) ? 0 : mul(f[n], mul(rf[n-k], rf[k])); } public static int pow(int a, int n) { int res = 1; while (n != 0) { if ((n & 1) == 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } static void shuffleArray(int[] a) { Random rnd = new Random(); for (int i = a.length-1; i > 0; i--) { int index = rnd.nextInt(i + 1); int tmp = a[index]; a[index] = a[i]; a[i] = tmp; } } public static int inv(int a) { return pow(a, MOD-2); } public void doIt() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tok.nextToken()); int k = Integer.parseInt(tok.nextToken()); f = new int[n+42]; rf = new int[n+42]; f[0] = rf[0] = 1; for (int i = 1; i < f.length; ++i) { f[i] = mul(f[i-1], i); rf[i] = mul(rf[i-1], inv(i)); } int[] events = new int[2*n]; for (int i = 0; i < n; ++i) { tok = new StringTokenizer(in.readLine()); int le = Integer.parseInt(tok.nextToken()); int ri = Integer.parseInt(tok.nextToken()); events[i] = le*2; events[i + n] = ri*2 + 1; } shuffleArray(events); Arrays.sort(events); int ans = 0; int balance = 0; for (int r = 0; r < 2*n;) { int l = r; while (r < 2*n && events[l] == events[r]) { ++r; } int added = r - l; if (events[l] % 2 == 0) { // Open event ans += C(balance + added, k); if (ans >= MOD) ans -= MOD; ans += MOD - C(balance, k); if (ans >= MOD) ans -= MOD; balance += added; } else { // Close event balance -= added; } } in.close(); System.out.println(ans); } public static void main(String[] args) throws IOException { (new D_Java()).doIt(); } }
1
9be0602e
c4ca2ff3
import java.io.*; import java.util.*; public class Codeforce { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static int mod = (int) (1e9 + 7); public static class pair implements Comparator<pair> { int x; int y; public pair() { } public pair(int x, int y) { this.x = x; this.y = y; } @Override public int compare(pair o1, pair o2) { return o1.y - o2.y; } } public static long modularpow(long a, long b) { long res = 1; if (b == 0) return res; else { while (b > 0) { if (b % 2 == 1) { res *= a; res %= mod; } a = a * a; a %= mod; b /= 2; } return res % mod; } } public static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } public static String binary(int a) { String s1 = ""; while (a > 0) { s1 = a % 2 + s1; a /= 2; } return s1; } public static int Lower_Bound(long a[], int l, int r, long k) { while (r - l > 1) { int mid = l + (r - l) / 2; if (a[mid] <= k) l = mid; else r = mid; } return l; } public static int Upper_Bound(int a[], int l, int r, int k) { while (r - l > 1) { int mid = (l + r) / 2; if (a[mid] <= k) l = mid; else r = mid; } return l + 1; } public static int fun(int ch,int a[][],int tot[],int n){ ArrayList<Integer>ar=new ArrayList<>(); for(int i=0;i<n;i++){ ar.add((a[i][ch]-(tot[i]-a[i][ch]))); } Collections.sort(ar,Collections.reverseOrder()); int cou=0; int ans=0; for(int i:ar){ cou+=i; if(cou>0){ ans++; } else break; } return ans; } public static void main(String arg[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int n=Integer.parseInt(br.readLine()); int a[][]=new int[n][5]; int tot[]=new int[n]; for(int i=0;i<n;i++){ String s=br.readLine(); tot[i]+=s.length(); for(int j=0;j<s.length();j++){ a[i][s.charAt(j)-'a']++; } } int ans=0; for(int i=0;i<5;i++){ ans=Math.max(ans,fun(i,a,tot,n)); } sb.append(ans+"\n"); } System.out.println(sb); } }
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc=new Scanner(System.in); int t=sc.nextInt(); PrintWriter out=new PrintWriter(System.out); while(t-->0) { int n=sc.nextInt(); int freq[][]=new int[n][5]; int rem[][]=new int[n][5]; for(int i=0;i<n;i++) { String str=sc.next(); for(int j=0;j<str.length();j++) { freq[i][str.charAt(j)-'a']++; } for(int k=0;k<5;k++) { rem[i][k]=str.length()-freq[i][k]; } } int ans=0; for(int i=0;i<5;i++) { int arr[]=new int[n]; for(int j=0;j<n;j++) arr[j]=freq[j][i]-rem[j][i]; Arrays.sort(arr); int total=0; int sum=0; for(int k=n-1;k>=0;k--) { if(sum+arr[k]>0) { sum=sum+arr[k]; total++; } else { break; } } ans=Math.max(ans,total); } out.println(ans); } out.flush(); out.close(); } }
0
4138b081
f59d9b6e
import java.io.*; import java.util.*; public class Contest1627C { static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } String next() { // reads in the next string while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { // reads in the next int return Integer.parseInt(next()); } public long nextLong() { // reads in the next long return Long.parseLong(next()); } public double nextDouble() { // reads in the next double return Double.parseDouble(next()); } } static InputReader r = new InputReader(System.in); static PrintWriter pw = new PrintWriter(System.out); static long mod = 1000000007; static ArrayList<Integer>[] adj; static ArrayList<Integer>[] num; static int[] ans; public static void main(String[] args) { int t = r.nextInt(); while (t > 0) { t--; int n = r.nextInt(); adj = new ArrayList[n]; num = new ArrayList[n]; for (int i = 0; i < n; i ++) { adj[i] = new ArrayList<Integer>(); num[i] = new ArrayList<Integer>(); } int[] deg = new int[n]; boolean flag = false; for (int i = 0; i < n - 1; i ++) { int a = r.nextInt()-1; int b = r.nextInt()-1; adj[a].add(b); adj[b].add(a); num[a].add(i); num[b].add(i); deg[a] ++; deg[b] ++; if (deg[a] > 2 || deg[b] > 2) { flag = true; } } if (flag) { pw.println(-1); continue; } ans = new int[n]; for (int i = 0; i < n; i ++) { if (deg[i] == 1) { dfs(i,3,-1); } } for (int i = 0; i < n - 1; i ++) { pw.println(ans[i]); } } pw.close(); } static void dfs(int node, int x, int p) { for (int j = 0; j < adj[node].size(); j ++) { int i = adj[node].get(j); if (i == p) { continue; } ans[num[node].get(j)] = x; dfs(i,5-x,node); } } }
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef{ public static class Edge{ int node; int index; Edge(int node, int index){ this.node = node; this.index = index; } } static Scanner scn = new Scanner(System.in); public static void main (String[] args) throws java.lang.Exception{ int t = scn.nextInt(); while(t-->0){ solve(); } } public static void solve(){ int n = scn.nextInt(); ArrayList<Edge>[]graph = new ArrayList[n]; for(int i = 0; i < n; i++){ graph[i] = new ArrayList<>(); } for(int i = 0; i < n - 1; i++){ int u = scn.nextInt() - 1; int v = scn.nextInt() - 1; graph[u].add(new Edge(v, i)); graph[v].add(new Edge(u, i)); } int start = 0; for(int i = 0; i < n; i++){ if(graph[i].size() > 2){ System.out.println("-1"); return; }else if(graph[i].size() == 1){ start = i; } } int[]weight = new int[n - 1]; int prevNode = -1, curNode = start, curWeight = 2; while(true){ ArrayList<Edge>edges = graph[curNode]; Edge next = edges.get(0); if(next.node == prevNode){ if(edges.size() == 1){ break; }else{ next = edges.get(1); } } weight[next.index] = curWeight; prevNode = curNode; curNode = next.node; curWeight = 5 - curWeight; } for(int i = 0; i < n - 1; i++){ System.out.print(weight[i]); System.out.print(" "); } System.out.println(); } }
0
5e8d1aaa
c4d04ce7
import java.io.*; import java.util.*; /* */ public class E { static FastReader sc=null; static long max=(long)1e15+7; public static void main(String[] args) { sc=new FastReader(); PrintWriter out=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(),k=sc.nextInt(); int a[]=sc.readArray(k),te[]=sc.readArray(k); int curr[]=new int[n]; Arrays.fill(curr, -1); for(int i=0;i<k;i++) { a[i]--; curr[a[i]]=te[i]; } long min=max; long ts[]=new long[n]; Arrays.fill(ts, max); for(int i=n-1;i>=0;i--) { if(curr[i]!=-1) { min=Math.min(min, curr[i]+i); } ts[i]=Math.min(ts[i], min-i); } min=max; for(int i=0;i<n;i++) { if(curr[i]!=-1) { min=Math.min(min, curr[i]-i); } ts[i]=Math.min(ts[i], min+i); } for(long e:ts)out.print(e+" "); out.println(); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int a[]=new int [n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } return a; } } }
import java.io.*; import java.util.*; /* */ public class E { static FastReader sc=null; static long nax=(long)1e15+7; public static void main(String[] args) { sc=new FastReader(); PrintWriter out=new PrintWriter(System.out); int test=sc.nextInt(); while(test-->0) { int n=sc.nextInt(),k=sc.nextInt(); int a[]=sc.readArray(k),t[]=sc.readArray(k); int present[]=new int[n]; Arrays.fill(present, -1); for(int i=0;i<k;i++) { a[i]--; present[a[i]]=t[i]; } long min=nax; long temps[]=new long[n]; Arrays.fill(temps, nax); for(int i=n-1;i>=0;i--) { if(present[i]!=-1) { min=Math.min(min, present[i]+i); } temps[i]=Math.min(temps[i], min-i); } min=nax; for(int i=0;i<n;i++) { if(present[i]!=-1) { min=Math.min(min, present[i]-i); } temps[i]=Math.min(temps[i], min+i); } for(long e:temps)out.print(e+" "); out.println(); } out.close(); } static class Pair implements Comparable<Pair>{ int id,t; Pair(int id,int t){ this.id=id; this.t=t; } @Override public int compareTo(Pair o) { return this.id-o.id; } } static int[] ruffleSort(int a[]) { ArrayList<Integer> al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i<a.length;i++)a[i]=al.get(i); return a; } static void print(int a[]) { for(int e:a) { System.out.print(e+" "); } System.out.println(); } static class FastReader{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while(!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); return a; } } }
1
480de7be
f4757480
import java.util.StringTokenizer; import java.io.*; public class CF_1551c{ public static final void main(String[] args){ Kattio io= new Kattio(); int t= io.getInt(); while(t-->0){ int n= io.getInt(); int[][] ps= new int[5][n]; for(int i=0; i<n; i++){ String w= io.getWord(); int len= w.length(); // count letters for(int j=0; j<len; j++) ps[w.charAt(j)-'a'][i]++; // calculate diffs letter-!letter // e.g. a-!a = a-(a+b+c+d+e-a) = a-(len-a) = a + (a-len) = 2a-len for(int k=0; k<5; k++) ps[k][i]+= ps[k][i]-len; } //sort diffs for(int k=0; k<5; k++) mergeSort(ps[k]); //calculate prefix sums of diffs (until they're non-positive) //start from the end as sort is ascending //pick largest index out of 5 letter at which sum of diffs is positive int max= 0; for(int k=0; k<5; k++){ if(ps[k][n-1]<=0) continue; if(max==0) max= 1; for(int i=2; i<=n; i++){ ps[k][n-i]+= ps[k][n-i+1]; if(ps[k][n-i]<=0) break; if(i>max) max= i; } } io.println(max); } io.close(); } // using mergeSort to avoid Java quicksort TLE hacks static void mergeSort(int arr[]){ int n= arr.length; for (int sz= 1; sz<=n-1; sz=2*sz){ for (int l= 0; l<n-1; l+=2*sz){ int m= Math.min(l + sz-1, n-1); int r= Math.min(l + 2*sz-1, n-1); int n1= m-l+1, n2= r-m; int L[] = new int[n1]; for (int i= 0; i<n1; i++) L[i]= arr[l+i]; int R[] = new int[n2]; for (int j= 0; j<n2; j++) R[j]= arr[m+1+j]; int i= 0, j= 0, k= l; for(;i<n1 && j<n2; k++){ if(L[i]<=R[j]){arr[k]= L[i]; i++;} else{arr[k] = R[j]; j++;} } for(;i<n1; i++, k++) arr[k]= L[i]; for(;j<n2; j++, k++) arr[k]= R[j]; } } } static class Kattio extends PrintWriter { private BufferedReader r; private String line, token; private StringTokenizer st; public Kattio(){this(System.in);} public Kattio(InputStream i){ super(new BufferedOutputStream(System.out)); r= new BufferedReader(new InputStreamReader(i)); } public Kattio(InputStream i, OutputStream o){ super(new BufferedOutputStream(o)); r= new BufferedReader(new InputStreamReader(i)); } public boolean isLineEnd(){ return !st.hasMoreTokens(); } public boolean hasMoreTokens(){ return peekToken()!=null; } public int getInt(){ return Integer.parseInt(nextToken()); } public double getDouble(){ return Double.parseDouble(nextToken()); } public long getLong(){ return Long.parseLong(nextToken()); } public String getWord(){ return nextToken(); } public String getLine(){ try{if(!st.hasMoreTokens()) return r.readLine(); }catch(IOException e){} return null; } private String peekToken(){ if(token==null) try { while(st==null || !st.hasMoreTokens()) { line= r.readLine(); if(line==null) return null; st= new StringTokenizer(line); } token= st.nextToken(); }catch(IOException e){} return token; } private String nextToken() { String ans= peekToken(); token= null; return ans; } } }
import java.util.*; import java.io.*; public class CF_1551c{ public static final void main(String[] args){ Kattio io= new Kattio(); int t= io.getInt(); while(t-->0){ int n= io.getInt(); int[][] ps= new int[5][n]; for(int i=0; i<n; i++){ String w= io.getWord(); int len= w.length(); // count letters for(int j=0; j<len; j++) ps[w.charAt(j)-'a'][i]++; // calculate diffs letter-!letter // e.g. a-!a = a-(a+b+c+d+e-a) = a-(len-a) = a + (a-len) = 2a-len for(int k=0; k<5; k++) ps[k][i]+= ps[k][i]-len; } //sort diffs for(int k=0; k<5; k++) //mergeSort(ps[k]); Arrays.sort(ps[k]); //calculate prefix sums of diffs (until they're non-positive) //start from the end as sort is ascending //pick largest index out of 5 letter at which sum of diffs is positive int max= 0; for(int k=0; k<5; k++){ if(ps[k][n-1]<=0) continue; if(max==0) max= 1; for(int i=2; i<=n; i++){ ps[k][n-i]+= ps[k][n-i+1]; if(ps[k][n-i]<=0) break; if(i>max) max= i; } } io.println(max); } io.close(); } // using mergeSort to avoid Java quicksort TLE hacks static void mergeSort(int arr[]){ int n= arr.length; for (int sz= 1; sz<=n-1; sz=2*sz){ for (int l= 0; l<n-1; l+=2*sz){ int m= Math.min(l + sz-1, n-1); int r= Math.min(l + 2*sz-1, n-1); int n1= m-l+1, n2= r-m; int L[] = new int[n1]; for (int i= 0; i<n1; i++) L[i]= arr[l+i]; int R[] = new int[n2]; for (int j= 0; j<n2; j++) R[j]= arr[m+1+j]; int i= 0, j= 0, k= l; for(;i<n1 && j<n2; k++){ if(L[i]<=R[j]){arr[k]= L[i]; i++;} else{arr[k] = R[j]; j++;} } for(;i<n1; i++, k++) arr[k]= L[i]; for(;j<n2; j++, k++) arr[k]= R[j]; } } } static class Kattio extends PrintWriter { private BufferedReader r; private String line, token; private StringTokenizer st; public Kattio(){this(System.in);} public Kattio(InputStream i){ super(new BufferedOutputStream(System.out)); r= new BufferedReader(new InputStreamReader(i)); } public Kattio(InputStream i, OutputStream o){ super(new BufferedOutputStream(o)); r= new BufferedReader(new InputStreamReader(i)); } public boolean isLineEnd(){ return !st.hasMoreTokens(); } public boolean hasMoreTokens(){ return peekToken()!=null; } public int getInt(){ return Integer.parseInt(nextToken()); } public double getDouble(){ return Double.parseDouble(nextToken()); } public long getLong(){ return Long.parseLong(nextToken()); } public String getWord(){ return nextToken(); } public String getLine(){ try{if(!st.hasMoreTokens()) return r.readLine(); }catch(IOException e){} return null; } private String peekToken(){ if(token==null) try { while(st==null || !st.hasMoreTokens()) { line= r.readLine(); if(line==null) return null; st= new StringTokenizer(line); } token= st.nextToken(); }catch(IOException e){} return token; } private String nextToken() { String ans= peekToken(); token= null; return ans; } } }
1
8a5fe3d9
ea899386
import java.io.*; import java.util.*; import java.text.*; import java.math.*; public class Main { static class pair implements Comparable<pair>{ int sum, index; pair(int i){ sum = 0; index = i; } @Override public int compareTo(pair a){ return sum - a.sum; } } public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int t = Integer.parseInt(br.readLine()); while(t-- > 0){ StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int x = Integer.parseInt(st.nextToken()); int ar[] = new int[n]; st = new StringTokenizer(br.readLine()); int sum = 0, low = x; for(int i = 0; i < n; i++){ ar[i] = Integer.parseInt(st.nextToken()); } PriorityQueue<pair> q = new PriorityQueue<pair>(); for(int i = 0; i < m; i++) q.add(new pair(i + 1)); sb.append("YES\n"); for(int i = 0; i < n; i++){ pair p = q.poll(); p.sum += ar[i]; q.add(p); sb.append(p.index + " "); } sb.append("\n"); } System.out.print(sb); } }
import java.io.*; import java.util.*; public class Codeforces { public static class Tower implements Comparable<Tower>{ int val; int index; public Tower(int ind, int v) { val = v; index = ind; } @Override public int compareTo(Tower o) { return Integer.compare(val, o.val); } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int cases = Integer.parseInt(br.readLine()); while(cases-- > 0) { String[] str = br.readLine().split(" "); int n = Integer.parseInt(str[0]); int m = Integer.parseInt(str[1]); int x = Integer.parseInt(str[2]); int[] h = new int[n]; str = br.readLine().split(" "); for(int i=0; i<n; i++) { h[i] = Integer.parseInt(str[i]); } PriorityQueue<Tower> q = new PriorityQueue<>(m); int[] ans = new int[n]; for(int i=0; i<m; i++) { q.add(new Tower(i, h[i])); ans[i] = i; } for(int i=m; i<n; i++) { Tower lowest = q.poll(); lowest.val += h[i]; ans[i] = lowest.index; q.add(lowest); } System.out.println("YES"); for(int i=0; i<n; i++) { System.out.print((ans[i]+1) + " "); } System.out.println(); } } }
1
fc7dfa16
fe94ee2f
import java.util.*; import java.io.*; public class C { static class Scan { private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in=System.in; } public int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(int arr[],int l1,int r1,int l2,int r2) { int tmp[]=new int[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(long arr[],int l1,int r1,int l2,int r2) { long tmp[]=new long[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } static int n; static long arr[]; public static void main(String args[]) throws IOException { Scan input=new Scan(); StringBuilder ans=new StringBuilder(""); int test=input.scanInt(); for(int tt=1;tt<=test;tt++) { n=input.scanInt(); arr=new long[n]; for(int i=0;i<n;i++) { arr[i]=input.scanInt(); } ans.append(solve()+"\n"); } System.out.println(ans); } public static long solve() { long ans=Long.MAX_VALUE; long r_min=Long.MAX_VALUE,u_min=Long.MAX_VALUE; long r_rem=n,u_rem=n,sum=0; for(int i=0;i<n;i++) { if(i%2==0) { r_min=Math.min(r_min,arr[i]); r_rem--; } else { u_min=Math.min(u_min,arr[i]); u_rem--; } sum+=arr[i]; if(i==0) { continue; } ans=Math.min(ans,sum+(r_rem*r_min)+(u_rem*u_min)); } r_min=Long.MAX_VALUE; u_min=Long.MAX_VALUE; r_rem=n; u_rem=n; sum=0; for(int i=0;i<n;i++) { if(i%2==1) { r_min=Math.min(r_min,arr[i]); r_rem--; } else { u_min=Math.min(u_min,arr[i]); u_rem--; } sum+=arr[i]; if(i==0) { continue; } ans=Math.min(ans,sum+(r_rem*r_min)+(u_rem*u_min)); } return ans; } }
import java.util.*; import java.io.*; public class C { static class Scan { private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in=System.in; } public int scan()throws IOException { if(total<0) throw new InputMismatchException(); if(index>=total) { index=0; total=in.read(buf); if(total<=0) return -1; } return buf[index++]; } public int scanInt()throws IOException { int integer=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { integer*=10; integer+=n-'0'; n=scan(); } else throw new InputMismatchException(); } return neg*integer; } public double scanDouble()throws IOException { double doub=0; int n=scan(); while(isWhiteSpace(n)) n=scan(); int neg=1; if(n=='-') { neg=-1; n=scan(); } while(!isWhiteSpace(n)&&n!='.') { if(n>='0'&&n<='9') { doub*=10; doub+=n-'0'; n=scan(); } else throw new InputMismatchException(); } if(n=='.') { n=scan(); double temp=1; while(!isWhiteSpace(n)) { if(n>='0'&&n<='9') { temp/=10; doub+=(n-'0')*temp; n=scan(); } else throw new InputMismatchException(); } } return doub*neg; } public String scanString()throws IOException { StringBuilder sb=new StringBuilder(); int n=scan(); while(isWhiteSpace(n)) n=scan(); while(!isWhiteSpace(n)) { sb.append((char)n); n=scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1) return true; return false; } } public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(int arr[],int l1,int r1,int l2,int r2) { int tmp[]=new int[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1); if(l==r) { return; } int mid=(l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); merge(arr,l,mid,mid+1,r); } public static void merge(long arr[],int l1,int r1,int l2,int r2) { long tmp[]=new long[r2-l1+1]; int indx1=l1,indx2=l2; //sorting the two halves using a tmp array for(int i=0;i<tmp.length;i++) { if(indx1>r1) { tmp[i]=arr[indx2]; indx2++; continue; } if(indx2>r2) { tmp[i]=arr[indx1]; indx1++; continue; } if(arr[indx1]<arr[indx2]) { tmp[i]=arr[indx1]; indx1++; continue; } tmp[i]=arr[indx2]; indx2++; } //Copying the elements of tmp into the main array for(int i=0,j=l1;i<tmp.length;i++,j++) { arr[j]=tmp[i]; } } static int n; static long arr[]; public static void main(String args[]) throws IOException { Scan input=new Scan(); StringBuilder ans=new StringBuilder(""); int test=input.scanInt(); for(int tt=1;tt<=test;tt++) { n=input.scanInt(); arr=new long[n]; for(int i=0;i<n;i++) { arr[i]=input.scanInt(); } ans.append(solve()+"\n"); } System.out.println(ans); } public static long solve() { long ans=Long.MAX_VALUE; long r_min=Long.MAX_VALUE,u_min=Long.MAX_VALUE; long r_rem=n,u_rem=n,sum=0; for(int i=0;i<n;i++) { if(i%2==0) { r_min=Math.min(r_min,arr[i]); r_rem--; } else { u_min=Math.min(u_min,arr[i]); u_rem--; } sum+=arr[i]; if(i==0) { continue; } ans=Math.min(ans,sum+(r_rem*r_min)+(u_rem*u_min)); } r_min=Long.MAX_VALUE; u_min=Long.MAX_VALUE; r_rem=n; u_rem=n; sum=0; for(int i=0;i<n;i++) { if(i%2==1) { r_min=Math.min(r_min,arr[i]); r_rem--; } else { u_min=Math.min(u_min,arr[i]); u_rem--; } sum+=arr[i]; if(i==0) { continue; } ans=Math.min(ans,sum+(r_rem*r_min)+(u_rem*u_min)); } return ans; } }
1
3a12e509
52cd85f2
import java.io.*; import java.util.*; public class Practice { // static final long mod=7420738134811L; static int mod=1000000007; static final int size=501; static FastReader sc=new FastReader(System.in); // static Reader sc=new Reader(); static PrintWriter out=new PrintWriter(System.out); static long[] factorialNumInverse; static long[] naturalNumInverse; static int[] sp; static long[] fact; static ArrayList<Integer> pr; public static void main(String[] args) throws IOException { // System.setIn(new FileInputStream("input.txt")); // System.setOut(new PrintStream("output.txt")); // factorial(mod); // InverseofNumber(mod); // InverseofFactorial(mod); // make_seive(); int t=1; t=sc.nextInt(); while(t-->0) solve(); out.close(); out.flush(); } static void solve() throws IOException { int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); String s=sc.next(); ArrayList<Integer> blue=new ArrayList<Integer>(); ArrayList<Integer> red=new ArrayList<Integer>(); for(int i=0;i<n;i++) { if(s.charAt(i)=='B') blue.add(arr[i]); else red.add(arr[i]); } Collections.sort(blue); Collections.sort(red); for(int i=0;i<blue.size();i++) { if(blue.get(i)<i+1) { out.println("NO"); return; } } for(int i=0;i<red.size();i++) { if(red.get(i)>i+1+blue.size()) { out.println("NO"); return; } } out.println("YES"); } static class Pair implements Cloneable, Comparable<Pair> { int x,y; Pair(int a,int b) { this.x=a; this.y=b; } @Override public boolean equals(Object obj) { if(obj instanceof Pair) { Pair p=(Pair)obj; return p.x==this.x && p.y==this.y; } return false; } @Override public int hashCode() { return Math.abs(x)+500*Math.abs(y); } @Override public String toString() { return "("+x+" "+y+")"; } @Override protected Pair clone() throws CloneNotSupportedException { return new Pair(this.x,this.y); } @Override public int compareTo(Pair a) { int t= this.x-a.x; if(t!=0) return t; else return this.y-a.y; } public void swap() { this.y=this.y+this.x; this.x=this.y-this.x; this.y=this.y-this.x; } } static class Tuple implements Cloneable, Comparable<Tuple> { int x,y,z; Tuple(int a,int b,int c) { this.x=a; this.y=b; this.z=c; } public boolean equals(Object obj) { if(obj instanceof Tuple) { Tuple p=(Tuple)obj; return p.x==this.x && p.y==this.y && p.z==this.z; } return false; } @Override public int hashCode() { return (this.x+501*this.y); } @Override public String toString() { return "("+x+","+y+","+z+")"; } @Override protected Tuple clone() throws CloneNotSupportedException { return new Tuple(this.x,this.y,this.z); } @Override public int compareTo(Tuple a) { int x=this.z-a.z; if(x!=0) return x; int X= this.x-a.x; if(X!=0) return X; return a.y-this.y; } } static void arraySort(int arr[]) { ArrayList<Integer> a=new ArrayList<Integer>(); for (int i = 0; i < arr.length; i++) { a.add(arr[i]); } Collections.sort(a); for (int i = 0; i < arr.length; i++) { arr[i]=a.get(i); } } static void arraySort(long arr[]) { ArrayList<Long> a=new ArrayList<Long>(); for (int i = 0; i < arr.length; i++) { a.add(arr[i]); } Collections.sort(a); for (int i = 0; i < arr.length; i++) { arr[i]=a.get(i); } } static HashSet<Integer> primeFactors(int n) { HashSet<Integer> ans=new HashSet<Integer>(); if(n%2==0) { ans.add(2); while((n&1)==0) n=n>>1; } for(int i=3;i*i<=n;i+=2) { if(n%i==0) { ans.add(i); while(n%i==0) n=n/i; } } if(n!=1) ans.add(n); return ans; } static void make_seive() { sp=new int[size]; pr=new ArrayList<Integer>(); for (int i=2; i<size; ++i) { if (sp[i] == 0) { sp[i] = i; pr.add(i); } for (int j=0; j<(int)pr.size() && pr.get(j)<=sp[i] && i*pr.get(j)<size; ++j) sp[i * pr.get(j)] = pr.get(j); } } public static void InverseofNumber(int p) { naturalNumInverse=new long[size]; naturalNumInverse[0] = naturalNumInverse[1] = 1; for(int i = 2; i < size; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse=new long[size]; factorialNumInverse[0] = factorialNumInverse[1] = 1; // pre-compute inverse of natural numbers for(int i = 2; i < size; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to 200001 public static void factorial(int p) { fact=new long[size]; fact[0] = 1; for(int i = 1; i < size; i++) fact[i] = (fact[i - 1] * (long)i) % p; } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R) { if(R<0) return 1; // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % mod * factorialNumInverse[N - R]) % mod; return ans; } static int findXOR(int x) //from 0 to x { if(x<0) return 0; if(x%4==0) return x; if(x%4==1) return 1; if(x%4==2) return x+1; return 0; } static boolean isPrime(long x) { if(x==1) return false; if(x<=3) return true; if(x%2==0 || x%3==0) return false; for(int i=5;i<=Math.sqrt(x);i+=2) if(x%i==0) return false; return true; } static long gcd(long a,long b) { return (b==0)?a:gcd(b,a%b); } static int gcd(int a,int b) { return (b==0)?a:gcd(b,a%b); } static class Node { int vertex; HashSet<Edge> adj; int taxC,taxD; Node(int ver) { vertex=ver; taxD=0; taxC=0; adj=new HashSet<Edge>(); } @Override public String toString() { return vertex+" "; } } static class Edge { Node to; int cost; Edge(Node t,int c) { this.to=t; this.cost=c; } @Override public String toString() { return "("+to.vertex+","+cost+") "; } } static long power(long x, long y) { if(x<=0) return 1; long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static long binomialCoeff(long n, long k) { if(n<k) return 0; long res = 1; // if(k>n) // return 0; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (long i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } static class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } public void printarray(int arr[]) { for (int i = 0; i < arr.length; i++) System.out.print(arr[i]+" "); System.out.println(); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.StringTokenizer; public class PC3C { static PrintWriter out = new PrintWriter(System.out); static MyFastReaderPC3C in = new MyFastReaderPC3C(); static long mod = (long) (1e9 + 7); public static void main(String[] args) throws Exception { int test = i(); while (test-- > 0) { int n=i(); int[] arr=arrI(n); String s=string(); ArrayList<Integer> lR=new ArrayList<>(); ArrayList<Integer> lB=new ArrayList<>(); for(int i=0;i<n;i++) { if(s.charAt(i)=='R') lR.add(arr[i]); else lB.add(arr[i]); } Collections.sort(lB); Collections.sort(lR,Collections.reverseOrder()); int k=1; boolean st=true; for(int i=0;i<lB.size();i++) { if(lB.get(i)>=k) { k+=1; } else { st=false; break; } } boolean st2=true; k=n; for(int i=0;i<lR.size();i++) { if(lR.get(i)>k) { st2=false; break; } else { k-=1; } } if(st && st2) out.print("YES"); else out.print("NO"); out.print("\n"); out.flush(); } out.close(); } static class pair { long x, y; pair(long ar, long ar2) { x = ar; y = ar2; } } static void sort(long[] a) // check for long { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static class DescendingComparator implements Comparator<Integer> { public int compare(Integer a, Integer b) { return b - a; } } static class AscendingComparator implements Comparator<Integer> { public int compare(Integer a, Integer b) { return a - b; } } static boolean isPalindrome(char X[]) { int l = 0, r = X.length - 1; while (l <= r) { if (X[l] != X[r]) return false; l++; r--; } return true; } static long fact(long N) { long num = 1L; while (N >= 1) { num = ((num % mod) * (N % mod)) % mod; N--; } return num; } static long pow(long a, long b) { long mod = 1000000007; long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) pow = (pow * x) % mod; x = (x * x) % mod; b /= 2; } return pow; } static long toggleBits(long x)// one's complement || Toggle bits { int n = (int) (Math.floor(Math.log(x) / Math.log(2))) + 1; return ((1 << n) - 1) ^ x; } static int countBits(long a) { return (int) (Math.log(a) / Math.log(2) + 1); } static boolean isPrime(long N) { if (N <= 1) return false; if (N <= 3) return true; if (N % 2 == 0 || N % 3 == 0) return false; for (int i = 5; i * i <= N; i = i + 6) if (N % i == 0 || N % (i + 2) == 0) return false; return true; } static long GCD(long a, long b) { if (b == 0) { return a; } else return GCD(b, a % b); } // Debugging Functions Starts static void print(char A[]) { for (char c : A) System.out.print(c + " "); System.out.println(); } static void print(boolean A[]) { for (boolean c : A) System.out.print(c + " "); System.out.println(); } static void print(int A[]) { for (int a : A) System.out.print(a + " "); System.out.println(); } static void print(long A[]) { for (long i : A) System.out.print(i + " "); System.out.println(); } static void print(ArrayList<Integer> A) { for (int a : A) System.out.print(a + " "); System.out.println(); } // Debugging Functions END // ---------------------- // IO FUNCTIONS STARTS static HashMap<Integer, Integer> getHashMap(int A[]) { HashMap<Integer, Integer> mp = new HashMap<>(); for (int a : A) { int f = mp.getOrDefault(a, 0) + 1; mp.put(a, f); } return mp; } public static Map<Character, Integer> mapSortByValue(Map<Character, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Character, Integer>> list = new LinkedList<Map.Entry<Character, Integer>>(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() { public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) { return o1.getValue() - o2.getValue(); } }); // put data from sorted list to hashmap Map<Character, Integer> temp = new LinkedHashMap<Character, Integer>(); for (Map.Entry<Character, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static String string() { return in.nextLine(); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] arrI(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] arrL(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) A[i] = in.nextLong(); return A; } } class MyFastReaderPC3C { BufferedReader br; StringTokenizer st; public MyFastReaderPC3C() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
0
2915ac5b
9b8c6bd7
import java.util.*; import java.lang.*; import java.io.*; public class Main { // static Scanner sc=new Scanner(System.in); // static ArrayList<Integer> al; static long min; // static long[] dp; static FastReader sc=new FastReader(); public static void main (String[] args) throws java.lang.Exception { PrintWriter w=new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0){ // dp=new long[n+1][n+1][n]; int n=sc.nextInt(); long[] c=new long[n]; for(int i=0;i<n;i++) { c[i]=sc.nextLong(); } min=c[0]*n+c[1]*n; // f(n,c,n,n,0,0); long x=Long.MAX_VALUE-1,y=Long.MAX_VALUE-1,sumx=0,sumy=0,cx=0,cy=0,min1=Long.MAX_VALUE-1,min2=Long.MAX_VALUE-1; for(int i=0;i<n;i++) { if(i%2==0){ x=Math.min(x,c[i]);sumx+=c[i];cx=i/2+1; min1=sumx+x*(n-cx); } else {y=Math.min(y,c[i]);sumy+=c[i];cy=(i+1)/2; min2=sumy+y*(n-cy); } if(i>=1)min=Math.min(min,min1+min2); } System.out.println(min); } w.flush(); w.close(); } // public static long f(int n, long[] c, int x, int y, int i, long cost) { // long l=Long.MAX_VALUE; // if(i>=n || x>n || y>n)return 0; // if(dp[x][y][i]!=-1)return dp[x][y][i]; // if(x==0 && y==0) {min=Math.min(cost, min);return;} // if(x==0) { // if(i%2!=0)min=Math.min(cost+c[i]*y, min); // return; // } // if(y==0) { // if(i%2==0)min=Math.min(cost+c[i]*x, min); // return; // } // if(i%2==0) { // for(int j=1;j<=x;j++) { // l=Math.min(l,f(n,c,x-j,y,i+1,cost+j*c[i])); // } // } // else { // for(int j=1;j<=y;j++) { // l=Math.min(l,f(n,c,x,y-j,i+1,cost+j*c[i])); // } // } // } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
import java.util.*; import java.io.*; import java.math.*; public final class Main{ public static void main(String[] args) throws IOException{ // Scanner sc=new Scanner(System.in); FastReader sc=new FastReader(); PrintWriter writer=new PrintWriter(System.out); int tc=sc.nextInt(); while(tc-->0) { int n=sc.nextInt(); long[] c=new long[n]; for(int i=0;i<n;i++) c[i]=sc.nextInt(); long ans=c[0]*n+c[1]*n; long x=Long.MAX_VALUE-1,y=Long.MAX_VALUE-1,sumx=0,sumy=0,cx=0,cy=0,min1=Long.MAX_VALUE-1,min2=Long.MAX_VALUE-1; for(int i=0;i<n;i++) { if(i%2==0){ x=Math.min(x,c[i]); sumx+=c[i]; cx=i/2+1; min1=sumx+x*(n-cx); } else { y=Math.min(y,c[i]); sumy+=c[i];cy=(i+1)/2; min2=sumy+y*(n-cy); } if(i>=1) ans=Math.min(ans,min1+min2); } System.out.println(ans); } writer.flush(); writer.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1
1ea771ea
3c667d4f
import java.io.*; import java.util.*; public class CODECHEF { static class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } static long MOD=1000000000; static class Pair{ long a; int b; Pair(long i,int j){ a=i; b=j; } } static long[] solve(int[] pos,long[] arr,int n,int k){ long[] ans=new long[n]; long[] left=new long[n]; long[] right=new long[n]; long min=Integer.MAX_VALUE; for(int i=0;i<n;i++){ min=Math.min(min+1,arr[i]); left[i]=min; } min=Integer.MAX_VALUE; for(int i=n-1;i>=0;i--){ min=Math.min(min+1,arr[i]); right[i]=min; } for(int i=0;i<n;i++){ ans[i]=Math.min(left[i],right[i]); } return ans; } public static void main(String[] args) throws java.lang.Exception { FastReader fs=new FastReader(System.in); // StringBuilder sb=new StringBuilder(); // PrintWriter out=new PrintWriter(System.out); int t=fs.nextInt(); while (t-->0){ int n=fs.nextInt(); int k=fs.nextInt(); int[] pos=new int[k]; for(int i=0;i<k;i++) pos[i]=fs.nextInt()-1; long[] temp=new long[n]; int ptr=0; Arrays.fill(temp,Integer.MAX_VALUE); for(int i=0;i<k;i++) temp[pos[ptr++]]=fs.nextLong(); long[] ans=solve(pos,temp,n,k); for(int i=0;i<n;i++) System.out.print(ans[i]+" "); System.out.println(); } //out.close; } }
import java.util.*; public class j { public static void main(String args[]) { Scanner in=new Scanner(System.in); int n=in.nextInt(); while(n-->0) { int len=in.nextInt(); int t=in.nextInt(); int pos[]=new int[t]; int temp[]=new int[t]; for(int i=0;i<t;i++) pos[i]=in.nextInt(); for(int i=0;i<t;i++) temp[i]=in.nextInt(); long range[]=new long[len]; Arrays.fill(range,Long.MAX_VALUE-10000); for(int i=0;i<t;i++) range[pos[i]-1]=temp[i]; for(int i=1;i<len;i++) { range[i]=Math.min(range[i],1+range[i-1]); } for(int i=len-2;i>=0;i--) { range[i]=Math.min(range[i+1]+1,range[i]); } for(int i=0;i<len;i++) { System.out.print(range[i]+" "); }System.out.println(); } } }
0
392218ef
f0ede32a
import java.util.*; import java.lang.*; import java.io.*; public class Main { static void sort(int a[]){ ArrayList<Integer> arr=new ArrayList<>(); for(int i=0;i<a.length;i++)arr.add(a[i]); Collections.sort(arr); for(int i=0;i<a.length;i++)a[i]=arr.get(i); } public static void main (String[] args) throws java.lang.Exception { // Scanner sc=new Scanner(System.in); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); // int t=1; //t=sc.nextInt(); int t=Integer.parseInt(br.readLine()); while(--t>=0){ int n=Integer.parseInt(br.readLine()); int a[]=new int[n]; StringTokenizer st=new StringTokenizer(br.readLine()); for(int i=0;i<n;i++)a[i]=Integer.parseInt(st.nextToken()); String s=br.readLine(); ArrayList<Integer> inc=new ArrayList<>(); ArrayList<Integer> dec=new ArrayList<>(); for(int i=0;i<n;i++){ if(s.charAt(i)=='R')inc.add(a[i]); else dec.add(a[i]); } Collections.sort(dec); Collections.sort(inc,Collections.reverseOrder()); int p=n; boolean flag=false; for(int i=0;i<inc.size();i++){ if(inc.get(i)>p)flag=true; p--; } p=1; for(int i=0;i<dec.size();i++){ if(dec.get(i)<p)flag=true; p++; } if(flag)System.out.println("NO"); else System.out.println("YES"); } } }
import java.util.*; public class Soltion{ public static void main(String []args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); Integer[] arr = new Integer[n]; for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); } String s = sc.next(); List<Integer> blue = new ArrayList<>(); List<Integer> red = new ArrayList<>(); for(int i=0;i<s.length();i++){ if(s.charAt(i)=='B'){ blue.add(arr[i]); } else{ red.add(arr[i]); } } Collections.sort(blue); Collections.sort(red); int p=1,q=n; boolean flag = true; for(int i=red.size()-1;i>=0;i--){ if(red.get(i)>q){ flag = false; break; } q--; } for(int i=0;i<blue.size();i++){ if(blue.get(i)<p){ flag = false; break; } p++; } System.out.println(flag? "Yes" : "No"); } } }
0
29210e56
46e9aed4
import java.util.*; import java.io.*; import java.math.*; /** * * @Har_Har_Mahadev */ /** * Main , Solution , Remove Public */ public class A { private static long[][] dp; private static ArrayList<Integer> lis1,lis0; public static void process() throws IOException { int n = sc.nextInt(); int arr[] = sc.readArray(n); lis1 = new ArrayList<Integer>(); lis0 = new ArrayList<Integer>(); for(int i = 0; i<n; i++) { if(arr[i] == 1)lis1.add(i+1); else lis0.add(i+1); } Collections.sort(lis0); Collections.sort(lis1); int n0 = lis0.size(); int n1 = lis1.size(); dp = new long[n0+1][n1+1]; for(int i = 0; i<=n0; i++) { Arrays.fill(dp[i], -1); } long ans = solve(0,0,n0,n1); println(ans); } private static long solve(int i, int j, int n0, int n1) { if(j == n1)return 0; if(i == n0)return INF; if(dp[i][j] != -1)return dp[i][j]; long ans = solve(i+1, j, n0, n1); ans = min(ans,abs(lis0.get(i) - lis1.get(j)) + solve(i+1, j+1, n0, n1)); return dp[i][j] = ans; } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353; private static int N = 0; private static void google(int tt) { System.out.print("Case #" + (tt) + ": "); } static FastScanner sc; static PrintWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new PrintWriter(System.out); } else { sc = new FastScanner(100); out = new PrintWriter("output.txt"); } int t = 1; // t = sc.nextInt(); int TTT = 1; while (t-- > 0) { // google(TTT++); process(); } out.flush(); out.close(); } static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Integer.compare(this.x, o.x); } // @Override // public boolean equals(Object o) { // if (this == o) return true; // if (!(o instanceof Pair)) return false; // Pair key = (Pair) o; // return x == key.x && y == key.y; // } // // @Override // public int hashCode() { // int result = x; // result = 31 * result + y; // return result; // } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static void println(Object o) { out.println(o); } static void println() { out.println(); } static void print(Object o) { out.print(o); } static void pflush(Object o) { out.println(o); out.flush(); } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int abs(int x) { return Math.abs(x); } static long abs(long x) { return Math.abs(x); } static long sqrt(long z) { long sqz = (long) Math.sqrt(z); while (sqz * 1L * sqz < z) { sqz++; } while (sqz * 1L * sqz > z) { sqz--; } return sqz; } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } public static long lcm(long a, long b) { return (a * b) / gcd(a, b); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static int lower_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound(int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(System.in)); } FastScanner(int a) throws FileNotFoundException { br = new BufferedReader(new FileReader("input.txt")); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) throws IOException { int[] A = new int[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextInt(); } return A; } long[] readArrayLong(int n) throws IOException { long[] A = new long[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextLong(); } return A; } } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } }
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public final class Solution { public static void main(String[] args) throws Exception { Reader sc = new Reader(); BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out)); int n=sc.nextInt(); ArrayList<Integer> fill= new ArrayList<Integer>(); ArrayList<Integer> unfilled= new ArrayList<>(); for(int i=0;i<n;i++){ int x =sc.nextInt(); if(x==1){ fill.add(i); }else{ unfilled.add(i); } } Collections.sort(fill); Collections.sort(unfilled); long[][] dp =new long[fill.size()+1][unfilled.size()+1]; for(int i=0;i<fill.size()+1;i++){ for(int j=0;j<unfilled.size()+1;j++){ dp[i][j]=Integer.MAX_VALUE; } } for(int i=0;i<unfilled.size()+1;i++){ dp[0][i]=0; } // for(int j=0;j<fill.size()+1;j++){ // dp[j][0]=0; // } for(int i=1;i<fill.size()+1;i++){ for(int j=1;j<unfilled.size()+1;j++){ dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j-1]+Math.abs(fill.get(i-1)-unfilled.get(j-1))); } } System.out.println(dp[fill.size()][unfilled.size()]); // for(int i=0;i<fill.size()+1;i++){ // for(int j=0;j<unfilled.size()+1;j++) // { // System.out.print(dp[i][j]+" "); // } // System.out.println(); // } } } class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } }
0
0588b869
11373c16
import java.util.*; import java.io.*; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static final long mod=(long)1e9+7; public static long pow(long a,int p) { long res=1; while(p>0) { if(p%2==1) { p--; res*=a; res%=mod; } else { a*=a; a%=mod; p/=2; } } return res; } static class Pair { int u,v,w; Pair(int u,int v,int w) { this.u=u; this.v=v; this.w=w; } } /*static class Pair implements Comparable<Pair> { int v,l; Pair(int v,int l) { this.v=v; this.l=l; } public int compareTo(Pair p) { return l-p.l; } }*/ static int gcd(int a,int b) { if(b%a==0) return a; return gcd(b%a,a); } public static void dfs(int u,int dist[],int sub[],int mxv[],int par[],ArrayList<Integer> edge[]) { sub[u]=1; for(int v:edge[u]) { if(dist[v]==-1) { par[v]=u; dist[v]=dist[u]+1; dfs(v,dist,sub,mxv,par,edge); if(sub[v]+1>sub[u]) { sub[u]=sub[v]+1; mxv[u]=v; } } } } public static void main(String args[])throws Exception { FastReader fs=new FastReader(); PrintWriter pw=new PrintWriter(System.out); //int tc=fs.nextInt(); int n=fs.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=fs.nextInt(); ArrayList<Integer> o=new ArrayList<>(); ArrayList<Integer> z=new ArrayList<>(); for(int i=0;i<n;i++) { if(a[i]==1)o.add(i); else z.add(i); } int ans[][]=new int[o.size()+1][z.size()+1]; for(int i=1;i<=o.size();i++) { for(int j=i;j<=z.size();j++) { if(i==j)ans[i][j]=ans[i-1][j-1]+(int)Math.abs(o.get(i-1)-z.get(j-1)); else ans[i][j]=Math.min(ans[i][j-1],ans[i-1][j-1]+(int)Math.abs(o.get(i-1)-z.get(j-1))); } } pw.println(ans[o.size()][z.size()]); pw.flush(); pw.close(); } }
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef {static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main (String[] args) throws java.lang.Exception { FastReader scan = new FastReader(); PrintWriter pw = new PrintWriter(System.out); int n = scan.nextInt(); ArrayList<Integer> a = new ArrayList<>(); ArrayList<Integer> b = new ArrayList<>(); for(int i=0;i<n;i++){ int x = scan.nextInt(); if(x==1) a.add(i); else b.add(i); } int x = a.size(); if(x==0){ pw.println(0); pw.flush(); return; } int y = b.size(); int dp[][] = new int[x][y]; int min = Integer.MAX_VALUE; for(int i=0;i<y;i++){ min = Math.min(Math.abs(a.get(0) - b.get(i)),min); dp[0][i] = min; } for(int i=1;i<x;i++){ min = Integer.MAX_VALUE; for(int j=i;j<y;j++){ min = Math.min(Math.abs(a.get(i)-b.get(j))+dp[i-1][j-1],min); dp[i][j] = min; } } pw.println(dp[x-1][y-1]); pw.flush(); } }
0
163d0dde
f87eb1b3
// Working program with FastReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class aa { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static int findIndex(long arr[], long t) { // if array is Null if (arr == null) { return -1; } // find length of array int len = arr.length; int i = 0; // traverse in the array while (i < len) { // if the i-th element is t // then return the index if (arr[i] == t) { return i; } else { i = i + 1; } } return -1; } public static void main(String[] args) { FastReader d=new FastReader(); int t,i,j,c,z,k,l,n; int mod = (int) 1e9 + 7; int Inf=Integer.MAX_VALUE; int negInf=Integer.MIN_VALUE; t=d.nextInt(); //t=1; //String s; //char ch,ch1,ch2,ch3; while(t-->0) { z=c=0; n=d.nextInt(); int a[]=new int[n]; for(i=0;i<n;i++) a[i]=d.nextInt(); //s=d.nextLine();//dont need extra d.nextLine() long p=0; long ans; long x,y; long e,o; ans=Long.MAX_VALUE; x=y=Integer.MAX_VALUE; e=o=n; for(i=0;i<n;i++) { if(i%2==1) { p+=a[i]; e--; x=Long.min(x, a[i]); ans=Long.min(ans,p+(o*y)+(e*x)); } else { p+=a[i]; o--; y=Long.min(y, a[i]); ans=Long.min(ans,p+(o*y)+(e*x)); } } System.out.println(ans); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.PrintStream; import java.io.PrintWriter; public class A { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable<Pair> { int first, second; public Pair(int aa, int bb) { first = aa; second = bb; } public int compareTo(Pair o) { if(this.second < o.second) return -1; if(this.second > o.second) return +1; return this.first - o.first; } } static class Tuple implements Comparable<Tuple>{ long first, second,third; public Tuple(long first, long second, long third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int[] parent; int[] rank; //Size of the trees is used as the rank public DSU(int n) { parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) //finding through path compression { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public boolean union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return false; //if they are already connected we exit by returning false. // if a's parent is less than b's parent if(rank[a] < rank[b]) { //then move a under b parent[a] = b; } //else if rank of j's parent is less than i's parent else if(rank[a] > rank[b]) { //then move b under a parent[b] = a; } //if both have the same rank. else { //move a under b (it doesnt matter if its the other way around. parent[b] = a; rank[a] = 1 + rank[a]; } return true; } } static class Reader { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) throws IOException { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] longReadArray(int n) throws IOException { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static boolean isPrime(long n) { if(n == 1) { return false; } for(int i = 2;i*i<=n;i++) { if(n%i == 0) { return false; } } return true; } public static List<Integer> SieveList(int n) { boolean prime[] = new boolean[n+1]; Arrays.fill(prime, true); ArrayList<Integer> l = new ArrayList<>(); for (int p=2; p*p<=n; p++) { if (prime[p] == true) { for(int i=p*p; i<=n; i += p) { prime[i] = false; } } } for (int p=2; p<=n; p++) { if (prime[p] == true) { l.add(p); } } return l; } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long modPow(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return modPow(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)(n)] * modInverse(fac[(int)(r)], p) % p * modInverse(fac[(int)(n - r)], p) % p) % p; } public static long fact(long n) { long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (long i = 1; i <= n; i++) fac[(int)(i)] = fac[(int)(i - 1)] * i; return fac[(int)(n)]; } public static long nCr(int n, int k) { long ans = 1; for(int i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } public static void Sort(int[] a) { List<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); // Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void ssort(char[] a) { List<Character> l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } //Modular Operations for Addition and Multiplication. public static long perfomMod(long x) { return ((x%M + M)%M); } public static long addMod(long a, long b) { return perfomMod(perfomMod(a)+perfomMod(b)); } public static long subMod(long a, long b) { return perfomMod(perfomMod(a)-perfomMod(b)); } public static long mulMod(long a, long b) { return perfomMod(perfomMod(a)*perfomMod(b)); } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(int a[], int x) { int l=-1, r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { long INF = 1000000000000000007L; int n = sc.nextInt(); long[] c = new long[n+1]; for(int i = 1;i<=n;i++) c[i] = sc.nextLong(); long ans = INF; long mo = INF, so = 0, co = 0; long me = INF, se = 0, ce = 0; for(int i=1;i<=n;i++) { if(i%2 == 1) { mo = min(mo,c[i]); so += c[i]; co++; } else { me = min(me,c[i]); se += c[i]; ce++; } if(i>=2) { long x = so + (n - co) * mo + se + (n - ce) * me; ans=min(ans,x); } } fout.println(ans); } fout.close(); } }
0
7de69858
b76bce8a
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.*; import java.util.*; public class A { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]) { if (System.getProperty("ONLINE_JUDGE") == null) { // Input is a file try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } } else { // Input is System.in } FastReader sc = new FastReader(); // Scanner sc = new Scanner(System.in); //System.out.println(java.time.LocalTime.now()); StringBuilder sb = new StringBuilder(); int t = sc.nextInt(); while(t>0) { int n = sc.nextInt(); long[] arr = new long[n]; long[] pow = new long[n]; for(int i = 0; i<n; i++)arr[i] = sc.nextLong(); for(int i = 0; i<n ;i++)pow[i] = sc.nextLong(); Pair[] p= new Pair[n]; for(int i = 0; i<n; i++){ Pair pp = new Pair(arr[i]-pow[i], arr[i]); p[i] = pp; } long ans = 0; Arrays.sort(p); //System.out.println(Arrays.toString(p)); long prevEnd = -1; long prevN = 0; for(int i = 0; i<n; i++){ if(p[i].x >= prevEnd){ long nn = p[i].y-p[i].x; ans+= ((nn)*(nn+1))/2; prevEnd = p[i].y; prevN = nn; }else{ if(prevEnd < p[i].y){ long a = prevN+1; long nn = p[i].y-prevEnd; long sum = ((nn)*(2*a+(nn-1)))/2; ans +=sum; prevN = a+(nn-1); prevEnd = p[i].y; } } } sb.append(ans+"\n"); t--; } System.out.println(sb); } } class Pair implements Comparable<Pair>{ long x; long y; Pair(long x, long y){ this.x = x; this.y = y; } // @Override // public boolean equals(Object o) { // if(this == o)return true; // if(o == null || this.getClass() != o.getClass())return false; // Pair p = (Pair)o; // return x == p.x && y == p.y; // } // @Override // public int hashCode(){ // return Objects.hash(x , y); // } @Override public String toString() { return "(" + x + ", " + y + ")"; } @Override public int compareTo(Pair o) { return (int)this.x-(int)o.x; } }
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.StringTokenizer; public class MonstersAndSpells { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) throws IOException { FastReader input = new FastReader(); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); int t = input.nextInt(); while (t-- > 0) { int n = input.nextInt(); long k[] = new long[n]; long h[] = new long[n]; long ans = 0; for (int i = 0; i < n; i++) { k[i] = input.nextInt(); } for (int i = 0; i < n; i++) { h[i] = input.nextInt(); } for (int i = n-1; i >0; i--) { long d = k[i]-k[i-1]; if(h[i-1]+d<h[i]){ h[i-1]+=(h[i]-(h[i-1]+d)); } } ans += (h[0]) * (h[0] + 1) / 2; long last = h[0]; for (int i = 1; i < n; i++) { if (k[i] - k[i - 1] >= h[i]) { ans += (h[i]) * (h[i] + 1) / 2; last = h[i]; } else { long x = (k[i] - k[i - 1]); long y = last + x; long sum1 = (last) * (last + 1) / 2; long sum2 = (y) * (y + 1) / 2; ans += (sum2 - sum1); last = y; } } log.write(ans + "\n"); } log.flush(); } }
0
d12f26f0
df594a00
import java.io.*; import java.util.*; public class AirConditioners { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int q = readInt(); while (q-- > 0) { int n = readInt(), k = readInt(); int[] a = new int[k], t = new int[k], c = new int[n]; for (int i = 0; i < k; i ++) a[i] = readInt(); for (int i = 0; i < k; i ++) t[i] = readInt(); Arrays.fill(c, Integer.MAX_VALUE); for (int i = 0; i < k; i ++) c[a[i] - 1] = t[i]; int[] l = new int[n], r = new int[n]; int prev = (int) 2e9; for (int i = 0; i < n; i ++) { l[i] = Math.min(prev + 1, c[i]); prev = l[i]; } prev = (int) 2e9; for (int i = n - 1; i >= 0; i --) { r[i] = Math.min(prev + 1, c[i]); prev = r[i]; } for (int i = 0; i < n; i ++) System.out.print(Math.min(l[i], r[i]) + " "); System.out.println(); } } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readLine() throws IOException { return br.readLine().trim(); } }
import java.io.*; import java.util.*; public class Codeforces { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int cases = Integer.parseInt(br.readLine()); while(cases-- > 0) { br.readLine(); String[] str = br.readLine().split(" "); int n = Integer.parseInt(str[0]); int k = Integer.parseInt(str[1]); int[] a = new int[k]; int[] t = new int[k]; str = br.readLine().split(" "); for(int i=0; i<k; i++) { a[i] = Integer.parseInt(str[i]) - 1; } str = br.readLine().split(" "); for(int i=0; i<k; i++) { t[i] = Integer.parseInt(str[i]); } int[] temp = new int[n]; Arrays.fill(temp, Integer.MAX_VALUE); int[] left = new int[n]; int[] right = new int[n]; Arrays.fill(left, Integer.MAX_VALUE); Arrays.fill(right, Integer.MAX_VALUE); int ind = 0; for(int i=0; i<k; i++) { left[a[i]] = t[i]; right[a[i]] = t[i]; } int minleft = Integer.MAX_VALUE; for(int i=0; i<n; i++) { left[i] = Math.min(left[i], minleft); minleft = left[i] == Integer.MAX_VALUE ? Integer.MAX_VALUE : left[i]+1; } int minright = Integer.MAX_VALUE; for(int i=n-1; i>=0; i--) { right[i] = Math.min(right[i], minright); minright = right[i] == Integer.MAX_VALUE ? Integer.MAX_VALUE : right[i]+1; } for(int i=0; i<n; i++) { temp[i] = Math.min(right[i], left[i]); System.out.print(temp[i]+" "); } System.out.println(); } } }
0
6815a099
f41057db
import java.math.BigInteger; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.StringTokenizer; public class mathcomp { public static int gcd(int a,int b) { BigInteger b1=BigInteger.valueOf(a); BigInteger b2=BigInteger.valueOf(b); BigInteger gcd=b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a,long b) { BigInteger b1=BigInteger.valueOf(a); BigInteger b2=BigInteger.valueOf(b); BigInteger gcd=b1.gcd(b2); return gcd.longValue(); } public static 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(); int aStart=fs.nextInt()-1; int bStart=fs.nextInt()-1; int adist=fs.nextInt();int bdist=fs.nextInt(); Node[] nodes=new Node[n]; for(int i=0;i<n;i++) nodes[i]=new Node(); for(int i=1;i<n;i++) { int a=fs.nextInt()-1,b=fs.nextInt()-1; nodes[a].adj.add(nodes[b]);nodes[b].adj.add(nodes[a]); } bfs(nodes,nodes[bStart]); if(nodes[aStart].dist<=adist) { out.println("Alice"); continue; } Node farthest=farthest(nodes); bfs(nodes,farthest); farthest=farthest(nodes); int diam=farthest.dist; if(2*adist>=diam) { out.println("Alice"); continue; } if(bdist>2*adist) { out.println("Bob"); } else { out.println("Alice"); } } out.close(); } static Node farthest(Node[] nodes) { Node ans=nodes[0]; for(Node nn:nodes) if(nn.dist>ans.dist) ans=nn; return ans; } static void bfs(Node[] nodes,Node from) { for(Node nn:nodes) nn.dist=-1; ArrayDeque<Node> bfs=new ArrayDeque<>(); bfs.add(from); from.dist=0; while(!bfs.isEmpty()) { Node nn=bfs.remove(); for(Node adj:nn.adj) { if(adj.dist==-1) { adj.dist=nn.dist+1; bfs.add(adj); } } } } static class Node{ ArrayList<Node> adj=new ArrayList<>(); int dist=-1; } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while(!st.hasMoreTokens()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for(int i=0;i<n;i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.StringTokenizer; public class Solution{ public static void main(String[] args) throws Exception{ FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int tt = fs.nextInt(); while(tt-->0) { int n = fs.nextInt(), aStart = fs.nextInt()-1, bStart = fs.nextInt()-1, aMove = fs.nextInt(), bMove = fs.nextInt(); Node[] nodes = new Node[n]; for(int i=0;i<n;i++) { nodes[i] = new Node(); } for(int i=0;i<n-1;i++) { int u = fs.nextInt()-1, v = fs.nextInt()-1; nodes[u].adjList.add(nodes[v]); nodes[v].adjList.add(nodes[u]); } if(2*aMove>=bMove) { out.println("Alice"); continue; } bfs(nodes,nodes[aStart]); if(nodes[bStart].dis<=aMove) { out.println("Alice"); continue; } Node fNode = farthest(nodes); bfs(nodes, fNode); fNode = farthest(nodes); if(fNode.dis>=2*aMove+1) { out.println("Bob"); } else { out.println("Alice"); } } out.close(); } static Node farthest(Node[] nodes) { Node fNode = nodes[0]; for(Node n: nodes) { if(n.dis>fNode.dis) { fNode = n; } } return fNode; } static void bfs(Node[] nodes, Node start) { for(Node n: nodes) n.dis = -1; start.dis = 0; ArrayDeque<Node> queue = new ArrayDeque<Node>(); queue.add(start); while(!queue.isEmpty()) { Node n = queue.remove(); for(Node node: n.adjList) { if(node.dis==-1) { node.dis = n.dis + 1; queue.add(node); } } } } static class Node{ LinkedList<Node> adjList = new LinkedList<Node>(); int dis = -1; } static class FastScanner{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next(){ while(!st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt(){ return Integer.parseInt(next()); } public int[] readArray(int n){ int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } public long nextLong() { return Long.parseLong(next()); } } static int lowerBound(int[] arr, int l, int r, int k) { while(r>l) { int mid = (l+r)/2; if(arr[mid]>=k) { r = mid; } else { l = mid+1; } } if(arr[l]<k) return -1; return l; } static int upperBound(int[] arr, int l, int r, int k) { while(r>l) { int mid = (l+r)/2; if(arr[mid]>k) { r = mid; } else { l = mid+1; } } if(arr[l]<=k) return -1; return l; } static long gcd(long a,long b) { return (b==0)?a:gcd(b,a%b); } }
1
5c54f087
c6c7fa0c
import java.util.*; import java.lang.*; import java.io.*; public class Main { PrintWriter out = new PrintWriter(System.out); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok = new StringTokenizer(""); String next() throws IOException { if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int ni() throws IOException { return Integer.parseInt(next()); } long nl() throws IOException { return Long.parseLong(next()); } int n,a,b,da,db,dist,vert; ArrayList<Integer>A[]; void solve() throws IOException { for (int tc=ni();tc>0;tc--) { n=ni(); a=ni(); b=ni(); da=ni(); db=ni(); A=new ArrayList[n+1]; for (int i=1;i<=n;i++) A[i]=new ArrayList(); for (int i=1;i<n;i++) { int u=ni(),v=ni(); A[u].add(v); A[v].add(u); } dist=0; dfs1(a,0,0); if (dist<=da || db<=2*da) { out.println("Alice"); continue; } dist=0; vert=0; dfs2(1,0,0); dist=0; dfs2(vert,0,0); if (dist<=2*da) out.println("Alice"); else out.println("Bob"); } out.flush(); } void dfs2(int u,int p,int d) { if (d>dist) { dist=d; vert=u; } for (Integer v:A[u]) { if (v==p) continue; dfs2(v,u,d+1); } } void dfs1(int u,int p,int d) { if (u==b) dist=d; for (Integer v:A[u]) { if (v==p) continue; dfs1(v,u,d+1); } } public static void main(String[] args) throws IOException { new Main().solve(); } }
import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.PI; import static java.lang.System.in; import static java.lang.System.out; public class B { static ArrayList<ArrayList<Integer>> adj; static int dis[]; static void dfs(int s, int p, int l) { dis[s] = l; for(int i : adj.get(s)) { if(i==p) continue; dfs(i, s, l+1); } } public static void main(String[] args) throws Exception { Foster sc = new Foster(); PrintWriter p = new PrintWriter(out); int t = sc.nextInt(); while(t--!=0) { int n = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt(), da = sc.nextInt(), db = sc.nextInt(); adj = new ArrayList<>(); for(int i = 0; i <= n; i++) { adj.add(new ArrayList<>()); } for(int i = 1; i < n; i++) { int u = sc.nextInt(), v = sc.nextInt(); adj.get(u).add(v); adj.get(v).add(u); } dis = new int[n+1]; dfs(a, 0, 0); if(dis[b] <= da) //Alice reached in first move { p.println("Alice"); continue; } int farthest = 0, maxDis = 0; for(int i = 1; i <= n; i++) { if(maxDis < dis[i]) { maxDis = dis[i]; farthest = i; } } dfs(farthest, 0, 0); int diameter = 0; for(int i = 1; i <= n; i++) { diameter = Math.max(diameter, dis[i]); } if(db > 2*da && diameter > 2*da) { p.println("Bob"); } else { p.println("Alice"); } } p.close(); } /* */ /* 1. Check overflow in pow function or in general 2. Check indices of read array function 3. Think of an easier solution because the problems you solve are always easy 4. Check iterator of loop 5. If still doesn't work, then jump from the 729th floor 'coz "beta tumse na ho paayega" Move to top!! */ static class Foster { BufferedReader br = new BufferedReader(new InputStreamReader(in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] intArray(int n) // Check indices { int arr[] = new int[n]; for(int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } long[] longArray(int n) // Check indices { long arr[] = new long[n]; for(int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } int[] getBits(int n) //in Reverse Order { int a[] = new int[31]; for(int i = 0; i < 31; i++) { if(((1<<i) & n) != 0) a[i] = 1; } return a; } static long pow(long... a) { long mod = Long.MAX_VALUE; if(a.length == 3) mod = a[2]; long res = 1; while(a[1] > 0) { if((a[1] & 1) == 1) res = (res * a[0]) % mod; a[1] /= 2; a[0] = (a[0] * a[0]) % mod; } return res; } void print(Object... o) { for(Object next : o) { System.err.print(next + " "); } } void println(Object... o) { for(Object next : o) { System.err.print(next + " "); } System.err.println(); } void watch(Object...a) throws Exception { int i = 1; for (Object o: a) { boolean found = false; if (o.getClass().isArray()) { String type = o.getClass().getName().toString(); switch (type) { case "[I": { int[] test = (int[]) o; println(i + " : " + Arrays.toString(test)); break; } case "[[I": { int[][] obj = (int[][]) o; println(i + " : " + Arrays.deepToString(obj)); break; } case "[J": { long[] obj = (long[]) o; println(i + " : " + Arrays.toString(obj)); break; } case "[[J": { long[][] obj = (long[][]) o; println(i + " : " + Arrays.deepToString(obj)); break; } case "[D": { double[] obj = (double[]) o; println(i + " : " + Arrays.toString(obj)); break; } case "[[D": { double[][] obj = (double[][]) o; println(i + " : " + Arrays.deepToString(obj)); break; } case "[Ljava.lang.String": { String[] obj = (String[]) o; println(i + " : " + Arrays.toString(obj)); break; } case "[[Ljava.lang.String": { String[][] obj = (String[][]) o; println(i + " : " + Arrays.deepToString(obj)); break; } case "[C": { char[] obj = (char[]) o; println(i + " : " + Arrays.toString(obj)); break; } case "[[C": { char[][] obj = (char[][]) o; println(i + " : " + Arrays.deepToString(obj)); break; } default: { println(i + " : type not identified"); break; } } found = true; } if (o.getClass() == ArrayList.class) { println(i + " : LIST = " + o); found = true; } if (o.getClass() == TreeSet.class) { println(i + " : SET = " + o); found = true; } if (o.getClass() == TreeMap.class) { println(i + " : MAP = " + o); found = true; } if (o.getClass() == HashMap.class) { println(i + " : MAP = " + o); found = true; } if (o.getClass() == LinkedList.class) { println(i + " : LIST = " + o); found = true; } if (o.getClass() == PriorityQueue.class) { println(i + " : PQ = " + o); found = true; } if (!found) { println(i + " = " + o); } i++; } } } }
0
23194f89
bcc5473d
import java.io.*; import java.util.*; public class new1{ static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static void main(String[] args) throws IOException{ BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); FastReader s = new FastReader(); int t = s.nextInt(); for(int z = 0; z < t; z++) { int n = s.nextInt(); int[] ti = new int[n]; for(int i = 0; i < n; i++) ti[i] = s.nextInt(); int[] he = new int[n]; for(int i = 0; i < n; i++) he[i] = s.nextInt(); long ans = 0; int st = ti[n - 1] - he[n - 1] + 1; int en = ti[n - 1]; //System.out.println(st + " " + en); for(int i = n - 2; i >= 0; i--) { int st1 = ti[i] - he[i] + 1; int en1 = ti[i]; if(en1 >= st) { st = Math.min(st, st1); } else { long a1 = ((en - st + 1L) * (en - st + 2L)) / 2; ans = ans + a1; st = st1; en = en1; //System.out.println(a1); } } ans = ans + ((en - st + 1L) * (en - st + 2L)) / 2; System.out.println(ans); } //output.flush(); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; }}
import java.util.*; import java.lang.*; import java.io.*; import java.math.BigInteger; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.math.BigInteger; import java.util.List; import java.util.Map; import java.util.Map.Entry; 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; public class k { //public static int mod=1000000007; public static void printDivisors(int n) { // Note that this loop runs till square root int x=(int) Math.sqrt(n); int p=0; for (int i=1; i<=x; i++) { if (n%i == 0) { // If divisors are equal, print only one if (n/i == i) p++; else // Otherwise print both p=p+2; } } System.out.println(p); } public static ArrayList<Long> Factors(long n) { ArrayList<Long> arr=new ArrayList<Long>(); int k=0; while (n%2==0) { k++; n /=2; arr.add((long)2); } int p=(int) Math.sqrt(n); for (int i = 3; i <=p; i+= 2) { if(n==1)break; while (n%i == 0) { k++; arr.add((long)i); n /= i; } } if (n > 2) { arr.add(n); } return arr; } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static long gcd(long x, long p) { if (x == 0) return p; return gcd(p%x, x); } // method to return LCM of two numbers static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static int sieve = 1000000 ; static boolean[] prime = new boolean[sieve + 1] ; public static void sieveOfEratosthenes() { // FALSE == prime and 1 // TRUE == COMPOSITE // time complexity = 0(NlogLogN)== o(N) // gives prime nos bw 1 to N // size - 1e7(at max) for(int i = 4; i<= sieve ; i++) { prime[i] = true ; i++ ; } for(int p = 3; p*p <= sieve; p++) { if(prime[p] == false) { for(int i = p*p; i <= sieve; i += p) prime[i] = true; } p++ ; } } public static void arrInpInt(int [] arr, int n) throws IOException { Reader reader = new Reader(); for(int i=0;i<n;i++) { arr[i]=reader.nextInt(); } } public static void arrInpLong(long [] arr, int n) throws IOException { Reader reader = new Reader(); for(int i=0;i<n;i++) { arr[i]=reader.nextLong(); } } public static void printArr(int[] arr) { for(int i=0;i<arr.length;i++) { System.out.print(arr[i]+" "); } System.out.println(); } public static int[] decSort(int[] arr) { int[] arr1 = Arrays.stream(arr).boxed().sorted(Collections.reverseOrder()).mapToInt(Integer::intValue).toArray(); return arr1; } //if present - return the first occurrence of the no //not present- return the index of next greater value //if greater than all the values return N(taking high=N-1) //if smaller than all the values return 0(taking low =0) static int lower_bound(int arr[], int low,int high, int X) { if (low > high) { return low; } int mid = low + (high - low) / 2; if (arr[mid] >= X) { return lower_bound(arr, low, mid - 1, X); } return lower_bound(arr, mid + 1, high, X); } //if present - return the index of next greater value //not present- return the index of next greater value //if greater than all the values return N(taking high=N-1) //if smaller than all the values return 0(taking low =0)\ static int upper_bound(int arr[], int low, int high, int X) { if (low > high) return low; int mid = low + (high - low) / 2; if (arr[mid] <= X) { return upper_bound(arr, mid + 1, high, X); } return upper_bound(arr, low, mid - 1, X); } public static class Pair {// comparator with class int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } } public static void sortbyColumn(int arr[][], int col) // send 2d array and col no { Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(final int[] entry1, final int[] entry2) { if (entry1[col] > entry2[col]) return 1; else if (entry1[col] < entry2[col]) return -1; else return 0; } }); } public static void sortbyColumn1(int arr[][], int col) // send 2d array and col no { Arrays.sort(arr, new Comparator<int[]>() { @Override public int compare(final int[] entry1, final int[] entry2) { if (entry1[col] > entry2[col]) return 1; else if (entry1[col] < entry2[col]) return -1; else if(entry1[col] == entry2[col]) { if(entry1[col-1]>entry2[col-1]) return -1; else if(entry1[col-1]<entry2[col-1]) return 1; else return 0; } else return 0; } }); } public static void print2D(int mat[][]) { // Loop through all rows for (int i = 0; i < mat.length; i++) { // Loop through all elements of current row { for (int j = 0; j < mat[i].length; j++) System.out.print(mat[i][j] + " "); } System.out.println(); } } public static int biggestFactor(int num) { int result = 1; for(int i=2; i*i <=num; i++){ if(num%i==0){ result = num/i; break; } } return result; } public static class p { int no; int h; public p(int no, long h) { this.no=no; this.h=(int) h; } } static class com implements Comparator<p>{ public int compare(p s1, p s2) { if (s1.h > s2.h) return -1; else if (s1.h < s2.h) return 1; else if(s1.h==s2.h) { if(s1.no>s2.no)return -1; else return 1; } return 0; } } static long hcf(long a,long b) { while (b > 0) { long temp = b; b = a % b; a = temp; } return a; } static int lower_bound_arr(ArrayList<Integer> arr, int low, int high, int X) { if (low > high) { return low; } int mid = low + (high - low) / 2; if (arr.get(mid) >= X) { return lower_bound_arr(arr, low, mid - 1, X); } return lower_bound_arr(arr, mid + 1, high, X); } public static void main(String args[]) throws NumberFormatException, IOException ,java.lang.Exception { Reader reader = new Reader(); //sieveOfEratosthenes(); //Scanner reader=new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); // BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); // int cases=Integer.parseInt(br.readLine()); // int cases=1; int cases=reader.nextInt(); while (cases-->0){ // long N=reader.nextLong(); //long M=reader.nextLong(); int N=reader.nextInt(); // int P=reader.nextInt(); //// // String[] first=br.readLine().split(" "); // long N=Long.parseLong(first[0]); // long K=Long.parseLong(first[1]); // long X=Long.parseLong(first[2]); // String s2=br.readLine(); // String s3=br.readLine(); // char[] s11=s2.toCharArray(); // char[] s12=new char[s11.length]; //int max=Integer.MIN_VALUE; // int min=Integer.MAX_VALUE; //int mod=1000000007; // HashMap<Integer, TreeSet<Integer>> map=new HashMap<Integer,TreeSet<Integer>>(); HashMap<Integer,Integer> map=new HashMap<Integer,Integer>(); // HashMap<Long,Long> map=new HashMap<Long,Long>(); // HashMap<String,Integer> map1=new HashMap<String,Integer>(); //HashMap<Character,Integer> path=new HashMap<Character,Integer>(); //TreeMap<Integer,Integer> map=new TreeMap<Integer,Integer>(Collections.reverseOrder()); // HashSet<Integer> set =new HashSet<Integer>(); // HashSet<Integer> right =new HashSet<Integer>(); // TreeSet<Integer> a =new TreeSet<Integer>(); //TreeSet<Long> b =new TreeSet<Long>(); // TreeSet<Integer> map=new TreeSet<Integer>(); // long[] arr=new long[N]; // int[] odd=new int[100001]; int[] sec=new int[N]; int[] pow=new int[N]; // int[][] arr=new int[N][P]; ArrayList<Long> list=new ArrayList<Long>(); // ArrayList<Long> list3=new ArrayList<Long>(); // ArrayList<Long> list1=new ArrayList<Long>(); // ArrayList<Long> bees=new ArrayList<Long>(); // boolean[]arr1=new boolean[N]; // // for(int i=0;i<N;i++) // { // for(int i=0;i<N;i++) { sec[i]=reader.nextInt(); } for(int i=0;i<N;i++) { pow[i]=reader.nextInt(); } int last=sec[N-1]; int ind=sec[N-1]-pow[N-1]+1; long ans=0; int i=N-2; while(i>=0) { // System.out.println(last +" "+ind); if(sec[i]>=ind) { if(sec[i]-ind+1<pow[i]) { ind=ind-(pow[i]-sec[i]+ind-1); } i--; } else if(sec[i]<ind) { ans=ans+((long)(last-ind+1))*((long)(last-ind+2))/(long)(2); last=sec[i]; ind=sec[i]-pow[i]+1; i--; } } ans=ans+((long)(last-ind+1))*((long)(last-ind+2))/(long)(2); System.out.println(ans); } // output.flush(); } }
0
b434c275
fdc4f384
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; public final class A { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<Integer> g[]; static long mod=(long)1e9+7,INF=Long.MAX_VALUE; static boolean set[],col[]; static int par[],tot[],partial[]; static int D[],P[][]; static long dp[][],sum=0,max=0,size[]; // static node1 seg[]; //static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)}; public static void main(String args[])throws IOException { int T=i(); outer:while(T-->0) { int N=i(); int size[]=new int[N]; PriorityQueue<node1> q[]=new PriorityQueue[26]; for(int i=0; i<26; i++)q[i]=new PriorityQueue<node1>(); for(int i=0; i<N; i++) { char X[]=in.next().toCharArray(); int s=X.length; size[i]=s; int f[]=new int[26]; for(char x:X)f[x-'a']++; for(int j=0; j<26; j++)q[j].add(new node1(f[j],i,s)); } int max=0; for(int i=0; i<26; i++) { PriorityQueue<node1> q_new=new PriorityQueue<>(); q_new=q[i]; int c=0; long f=0; while(q_new.size()>0) { node1 x=q_new.remove(); // System.out.println(x.f+" "+x.size+" "+x.a); f+=x.a; if(f>0) { c++; max=Math.max(max, c); } else break; } } out.println(max); } out.close(); } static int OR(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("OR "+i+" "+j); return i(); } static int AND(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("AND "+i+" "+j); return i(); } static int XOR(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("XOR "+i+" "+j); return i(); } static boolean f1(int l,char X[]) { int i=0; for(; l<X.length; l++) { if(X[i]!=X[l])return false; i++; } return true; } static int f(int a) { for(int i=a+1; a>0; a++) { if(GCD(i,a)==1)return i; } return 0; } static int min(char X[],char str[],int N) { int s=0; for(int i=0; i<N; i++) { int it=i%3; if(X[i]!=str[it])s++; // ans.append(str[it]); } return s; } static char f(int i,char X[]) { int a=0,b=0,c=0; for(; i<X.length; i+=3) { if(X[i]=='R')a++; if(X[i]=='B')b++; if(X[i]=='G')c++; } if(a>=b && a>=c)return 'R'; if(b>=a && b>=c)return 'B'; return 'G'; } static void f1(int n,int p,long sum,int N) { for(int c:g[n]) { if(c==p)continue; long s=sum+N-2*size[c]; f1(c,n,s,N); max=Math.max(max, s); } } static long f(int i,int j,ArrayList<Integer> A) { if(i+1==A.size()) { return j; } int moves=1+A.get(i); if(j==1)return 1+f(i+1,moves,A); if(j>0 && dp[i][j-1]==0)f(i,j-1,A); return dp[i][j]=dp[i][j-1]+f(i+1,j+moves,A); } // static void build(int v,int tl,int tr,long A[]) // { // if(tl==tr) // { // seg[v]=new node1(A[tl],A[tr],1,true); // return ; // } // int tm=(tl+tr)/2; // build(2*v,tl,tm,A); // build(2*v+1,tm+1,tr,A); // seg[v]=merge(seg[2*v],seg[2*v+1]); // } // static node1 ask(int v,int tl,int tr,int l,int r) // { // if(l>r)return new node1(0,0,0,false);//verify true or false // if(tl==l && tr==r)return seg[v]; // int tm=(tl+tr)/2; // node1 a=ask(v*2,tl,tm,l,Math.min(tm, r)); // node1 b=ask(v*2+1,tm+1,tr,Math.max(tm+1, l),r); // return merge(a,b); // } // static node1 merge(node1 a,node1 b) // { // long s=0; // long l1=a.L,r1=a.R,c1=a.cnt; // long l2=b.L,r2=b.R,c2=b.cnt; // long g=GCD(l2,r1); s=c1+c2; // if(g==1) // { // s--; // g=(l2*r1)/g; // if(c1==1) // { // l1=g; // } // if(c2==1)r2=g; // return new node1(l1,r2,s,true); // } // return new node1(l1,r2,s,a.leaf^b.leaf); // } static long f(long l,long r,long a,long b,long N) { while(r-l>1) { long m=(l+r)/2; long x=m*b; if(N<x) { r=m; continue; } if((N-x)%a==0)r=m; else l=m; } return r; } static long f1(long a,long A[],long bits[],long sum) { long s=A.length; s=mul(s,a); s=(s+(sum%mod))%mod; long p=1L; for(long x:bits) { if((a&p)!=0)s=((s-mul(x,p))+mod)%mod; p<<=1; } return s; } static long f2(long a,long A[],long bits[]) { long s=0; long p=1L; for(long x:bits) { if((a&p)!=0) { s=(s+mul(p,x))%mod; } p<<=1; } return s; } static long f(long x1,long y1,long x2,long y2) { return Math.abs(x1-x2)+Math.abs(y1-y2); } static long f(long x,long max,long s) { long l=-1,r=(x/s)+1; while(r-l>1) { long m=(l+r)/2; if(x-m*s>max)l=m; else r=m; } return l+1; } static int f(long A[],long x) { int l=-1,r=A.length; while(r-l>1) { int m=(l+r)/2; if(A[m]>=x)r=m; else l=m; } return r; } static int bin(int x,ArrayList<Integer> A) { int l=0,r=A.size()-1; while(l<=r) { int m=(l+r)/2; int a=A.get(m); if(a==x)return m; if(a<x)l=m+1; else r=m-1; } return 0; } static int left(int x,ArrayList<Integer> A) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; int a=A.get(m); if(a<=x)l=m; else r=m; } return l; } static int right(int x,ArrayList<Integer> A) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; int a=A.get(m); if(a<x)l=m; else r=m; } return r; } static boolean equal(long A[],long B[]) { for(int i=0; i<A.length; i++)if(A[i]!=B[i])return false; return true; } static int max(int a ,int b,int c,int d) { a=Math.max(a, b); c=Math.max(c,d); return Math.max(a, c); } static int min(int a ,int b,int c,int d) { a=Math.min(a, b); c=Math.min(c,d); return Math.min(a, c); } static HashMap<Integer,Integer> Hash(int A[]) { HashMap<Integer,Integer> mp=new HashMap<>(); for(int a:A) { int f=mp.getOrDefault(a,0)+1; mp.put(a, f); } return mp; } static long mul(long a, long b) { return ( a %mod * 1L * b%mod )%mod; } static void swap(int A[],int a,int b) { int t=A[a]; A[a]=A[b]; A[b]=t; } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { a=find(a); b=find(b); if(a!=b) { par[a]+=par[b]; par[b]=a; } } static boolean isSorted(int A[]) { for(int i=1; i<A.length; i++) { if(A[i]<A[i-1])return false; } return true; } static boolean isDivisible(StringBuilder X,int i,long num) { long r=0; for(; i<X.length(); i++) { r=r*10+(X.charAt(i)-'0'); r=r%num; } return r==0; } static int lower_Bound(int A[],int low,int high, int x) { if (low > high) if (x >= A[high]) return A[high]; int mid = (low + high) / 2; if (A[mid] == x) return A[mid]; if (mid > 0 && A[mid - 1] <= x && x < A[mid]) return A[mid - 1]; if (x < A[mid]) return lower_Bound( A, low, mid - 1, x); return lower_Bound(A, mid + 1, high, x); } static String f(String A) { String X=""; for(int i=A.length()-1; i>=0; i--) { int c=A.charAt(i)-'0'; X+=(c+1)%2; } return X; } static void sort(long[] a) //check for long { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static String swap(String X,int i,int j) { char ch[]=X.toCharArray(); char a=ch[i]; ch[i]=ch[j]; ch[j]=a; return new String(ch); } static int sD(long n) { if (n % 2 == 0 ) return 2; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0 ) return i; } return (int)n; } static void setGraph(int N) { tot=new int[N+1]; partial=new int[N+1]; D=new int[N+1]; P=new int[N+1][(int)(Math.log(N)+10)]; set=new boolean[N+1]; g=new ArrayList[N+1]; for(int i=0; i<=N; i++) { g[i]=new ArrayList<>(); D[i]=Integer.MAX_VALUE; //D2[i]=INF; } } static long pow(long a,long b) { //long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } } class node1 implements Comparable<node1> { int index,f,size; long a; node1(int f,int i,int size) { this.f=f; this.index=i; this.size=size; a=2*f-size; } public int compareTo(node1 x) { if(this.a==x.a)return 0; else if(this.a<x.a)return 1; else return -1; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; public final class A { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<Integer> g[]; static long mod=(long)1e9+7,INF=Long.MAX_VALUE; static boolean set[],col[]; static int par[],tot[],partial[]; static int D[],P[][]; static long dp[][],sum=0,max=0,size[]; // static node1 seg[]; //static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)}; public static void main(String args[])throws IOException { int T=i(); outer:while(T-->0) { int N=i(); int size[]=new int[N]; PriorityQueue<node1> q[]=new PriorityQueue[6]; for(int i=0; i<6; 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[6]; for(char x:X)f[x-'a']++; for(int j=0; j<6; j++)q[j].add(new node1(f[j],i,s)); } int max=0; for(int i=0; i<6; i++) { PriorityQueue<node1> q_new=new PriorityQueue<>(); q_new=q[i]; int c=0; long f=0; while(q_new.size()>0) { node1 x=q_new.remove(); // System.out.println(x.f+" "+x.size+" "+x.a); f+=x.a; if(f>0) { c++; max=Math.max(max, c); } else break; } } out.println(max); } out.close(); } static int OR(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("OR "+i+" "+j); return i(); } static int AND(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("AND "+i+" "+j); return i(); } static int XOR(int i,int j) { if(i>j) { int t=i; i=j; j=t; } System.out.println("XOR "+i+" "+j); return i(); } static boolean f1(int l,char X[]) { int i=0; for(; l<X.length; l++) { if(X[i]!=X[l])return false; i++; } return true; } static int f(int a) { for(int i=a+1; a>0; a++) { if(GCD(i,a)==1)return i; } return 0; } static int min(char X[],char str[],int N) { int s=0; for(int i=0; i<N; i++) { int it=i%3; if(X[i]!=str[it])s++; // ans.append(str[it]); } return s; } static char f(int i,char X[]) { int a=0,b=0,c=0; for(; i<X.length; i+=3) { if(X[i]=='R')a++; if(X[i]=='B')b++; if(X[i]=='G')c++; } if(a>=b && a>=c)return 'R'; if(b>=a && b>=c)return 'B'; return 'G'; } static void f1(int n,int p,long sum,int N) { for(int c:g[n]) { if(c==p)continue; long s=sum+N-2*size[c]; f1(c,n,s,N); max=Math.max(max, s); } } static long f(int i,int j,ArrayList<Integer> A) { if(i+1==A.size()) { return j; } int moves=1+A.get(i); if(j==1)return 1+f(i+1,moves,A); if(j>0 && dp[i][j-1]==0)f(i,j-1,A); return dp[i][j]=dp[i][j-1]+f(i+1,j+moves,A); } // static void build(int v,int tl,int tr,long A[]) // { // if(tl==tr) // { // seg[v]=new node1(A[tl],A[tr],1,true); // return ; // } // int tm=(tl+tr)/2; // build(2*v,tl,tm,A); // build(2*v+1,tm+1,tr,A); // seg[v]=merge(seg[2*v],seg[2*v+1]); // } // static node1 ask(int v,int tl,int tr,int l,int r) // { // if(l>r)return new node1(0,0,0,false);//verify true or false // if(tl==l && tr==r)return seg[v]; // int tm=(tl+tr)/2; // node1 a=ask(v*2,tl,tm,l,Math.min(tm, r)); // node1 b=ask(v*2+1,tm+1,tr,Math.max(tm+1, l),r); // return merge(a,b); // } // static node1 merge(node1 a,node1 b) // { // long s=0; // long l1=a.L,r1=a.R,c1=a.cnt; // long l2=b.L,r2=b.R,c2=b.cnt; // long g=GCD(l2,r1); s=c1+c2; // if(g==1) // { // s--; // g=(l2*r1)/g; // if(c1==1) // { // l1=g; // } // if(c2==1)r2=g; // return new node1(l1,r2,s,true); // } // return new node1(l1,r2,s,a.leaf^b.leaf); // } static long f(long l,long r,long a,long b,long N) { while(r-l>1) { long m=(l+r)/2; long x=m*b; if(N<x) { r=m; continue; } if((N-x)%a==0)r=m; else l=m; } return r; } static long f1(long a,long A[],long bits[],long sum) { long s=A.length; s=mul(s,a); s=(s+(sum%mod))%mod; long p=1L; for(long x:bits) { if((a&p)!=0)s=((s-mul(x,p))+mod)%mod; p<<=1; } return s; } static long f2(long a,long A[],long bits[]) { long s=0; long p=1L; for(long x:bits) { if((a&p)!=0) { s=(s+mul(p,x))%mod; } p<<=1; } return s; } static long f(long x1,long y1,long x2,long y2) { return Math.abs(x1-x2)+Math.abs(y1-y2); } static long f(long x,long max,long s) { long l=-1,r=(x/s)+1; while(r-l>1) { long m=(l+r)/2; if(x-m*s>max)l=m; else r=m; } return l+1; } static int f(long A[],long x) { int l=-1,r=A.length; while(r-l>1) { int m=(l+r)/2; if(A[m]>=x)r=m; else l=m; } return r; } static int bin(int x,ArrayList<Integer> A) { int l=0,r=A.size()-1; while(l<=r) { int m=(l+r)/2; int a=A.get(m); if(a==x)return m; if(a<x)l=m+1; else r=m-1; } return 0; } static int left(int x,ArrayList<Integer> A) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; int a=A.get(m); if(a<=x)l=m; else r=m; } return l; } static int right(int x,ArrayList<Integer> A) { int l=-1,r=A.size(); while(r-l>1) { int m=(l+r)/2; int a=A.get(m); if(a<x)l=m; else r=m; } return r; } static boolean equal(long A[],long B[]) { for(int i=0; i<A.length; i++)if(A[i]!=B[i])return false; return true; } static int max(int a ,int b,int c,int d) { a=Math.max(a, b); c=Math.max(c,d); return Math.max(a, c); } static int min(int a ,int b,int c,int d) { a=Math.min(a, b); c=Math.min(c,d); return Math.min(a, c); } static HashMap<Integer,Integer> Hash(int A[]) { HashMap<Integer,Integer> mp=new HashMap<>(); for(int a:A) { int f=mp.getOrDefault(a,0)+1; mp.put(a, f); } return mp; } static long mul(long a, long b) { return ( a %mod * 1L * b%mod )%mod; } static void swap(int A[],int a,int b) { int t=A[a]; A[a]=A[b]; A[b]=t; } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { a=find(a); b=find(b); if(a!=b) { par[a]+=par[b]; par[b]=a; } } static boolean isSorted(int A[]) { for(int i=1; i<A.length; i++) { if(A[i]<A[i-1])return false; } return true; } static boolean isDivisible(StringBuilder X,int i,long num) { long r=0; for(; i<X.length(); i++) { r=r*10+(X.charAt(i)-'0'); r=r%num; } return r==0; } static int lower_Bound(int A[],int low,int high, int x) { if (low > high) if (x >= A[high]) return A[high]; int mid = (low + high) / 2; if (A[mid] == x) return A[mid]; if (mid > 0 && A[mid - 1] <= x && x < A[mid]) return A[mid - 1]; if (x < A[mid]) return lower_Bound( A, low, mid - 1, x); return lower_Bound(A, mid + 1, high, x); } static String f(String A) { String X=""; for(int i=A.length()-1; i>=0; i--) { int c=A.charAt(i)-'0'; X+=(c+1)%2; } return X; } static void sort(long[] a) //check for long { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static String swap(String X,int i,int j) { char ch[]=X.toCharArray(); char a=ch[i]; ch[i]=ch[j]; ch[j]=a; return new String(ch); } static int sD(long n) { if (n % 2 == 0 ) return 2; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0 ) return i; } return (int)n; } static void setGraph(int N) { tot=new int[N+1]; partial=new int[N+1]; D=new int[N+1]; P=new int[N+1][(int)(Math.log(N)+10)]; set=new boolean[N+1]; g=new ArrayList[N+1]; for(int i=0; i<=N; i++) { g[i]=new ArrayList<>(); D[i]=Integer.MAX_VALUE; //D2[i]=INF; } } static long pow(long a,long b) { //long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static long fact(long N) { long n=2; if(N<=1)return 1; else { for(int i=3; i<=N; i++)n=(n*i)%mod; } return n; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } } class node1 implements Comparable<node1> { int index,f,size; long a; node1(int f,int i,int size) { this.f=f; this.index=i; this.size=size; a=2*f-size; } public int compareTo(node1 x) { if(this.a==x.a)return 0; else if(this.a<x.a)return 1; else return -1; } } //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
1
0d11fb94
8c79c384
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { static int i, j, k, n, m, t, y, x, sum = 0; static long mod = 1000000007; static FastScanner fs = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); static String str; static long ans; static List<Integer> zeros = new ArrayList<>(); static List<Integer> ones = new ArrayList<>(); static int oneCount = 0; static int[][] dp = new int[5005][5005]; public static void main(String[] args) { t = 1; while (t-- > 0) { n = fs.nextInt(); for(int i = 0;i<n;i++){ x = fs.nextInt(); if(x==1){ ones.add(i); oneCount++; } else zeros.add(i); } for(int i=0;i<n;i++){ for(int j = 0; j<n;j++){ dp[i][j]=-1; } } out.println(minCost(0,0)); } out.close(); } static int minCost(int zIndex, int oIndex){ if(oIndex == ones.size()) return 0; if(zIndex == zeros.size()) return 1000000007; if(dp[zIndex][oIndex]==-1) dp[zIndex][oIndex]= Math.min(Math.abs(zeros.get(zIndex) - ones.get(oIndex))+minCost(zIndex+1, oIndex+1) , minCost(zIndex+1, oIndex)); return dp[zIndex][oIndex]; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static void ruffleSort(int[] a) { //ruffle int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n), temp = a[i]; a[i] = a[oi]; a[oi] = temp; } //then sort Arrays.sort(a); } static void ruffleSort(long[] a) { //ruffle int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n); long temp = a[i]; a[i] = a[oi]; a[oi] = temp; } //then sort Arrays.sort(a); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } static class Pair implements Comparable<Pair> { long first, second; public Pair(long first, long second) { this.first = first; this.second = second; } @Override public int compareTo(Pair o) { return Long.compare(first, o.first); } } }
/* JAI MATA DI */ import java.util.*; import javax.print.attribute.HashAttributeSet; import java.io.*; import java.math.BigInteger; import java.sql.Array; public class CP { static class FR{ BufferedReader br; StringTokenizer st; public FR() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int mod = 1000000007; static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static class Pair implements Comparable<Pair>{ int ind; int val; Pair(int key , int value){ this.ind = key; this.val = value; } @Override public int compareTo(Pair o) { return this.val - o.val; } // @Override // public int hashCode(){ // return first + second; // } } /* ***************************************************************************************************************************************************/ static FR sc = new FR(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) { // int tc = sc.nextInt(); // while(tc-- > 0) { TEST_CASE(); // } System.out.println(sb); } static void TEST_CASE() { int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0 ; i< n ;i++) { arr[i] = sc.nextInt(); } ArrayList<Integer> ao = new ArrayList<Integer>(); ArrayList<Integer> az = new ArrayList<Integer>(); for(int i = 0 ; i< n ;i++) { if(arr[i] == 1) ao.add(i); else az.add(i); } long[][] dp = new long[n+1][n+1]; for(int i = 0 ; i<n ; i++) Arrays.fill(dp[i], -1); sb.append(fnc(dp, ao, az, 0, 0)); } static long fnc(long[][] dp ,ArrayList<Integer> ao , ArrayList<Integer> az ,int i , int j) { if(i == ao.size()) return 0; if(j == az.size()) return Long.parseLong("1000000000000"); if(dp[i][j] != -1) return dp[i][j]; long a = Math.abs(ao.get(i) - az.get(j)) + fnc(dp, ao, az, i+1, j+1); long b = fnc(dp, ao, az, i, j+1); dp[i][j] = Math.min(a, b); return dp[i][j]; } }
0