id
stringlengths
22
25
content
stringlengths
327
628k
max_stars_repo_path
stringlengths
49
49
condefects-java_data_2001
import java.util.*; public class Main { public static void main(String[] args) throws Exception { // Your code here! Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); String s = scanner.next(); int count = 0; for(int i=0; i<n; i++){ if(s.charAt(i) == 'T'){ count++; } } int nn = n-1; if(count == 0){ System.out.println("A"); }else if(count == n-count){ for(int i=nn; i>0; i--){ if(s.charAt(i) != s.charAt(i-1)){ System.out.println(s.charAt(i-1)); break; } } }else if(count > n-count){ System.out.println("T"); } } } import java.util.*; public class Main { public static void main(String[] args) throws Exception { // Your code here! Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); String s = scanner.next(); int count = 0; for(int i=0; i<n; i++){ if(s.charAt(i) == 'T'){ count++; } } int nn = n-1; if(count == 0){ System.out.println("A"); }else if(count == n-count){ for(int i=nn; i>0; i--){ if(s.charAt(i) != s.charAt(i-1)){ System.out.println(s.charAt(i-1)); break; } } }else if(count > n-count){ System.out.println("T"); }else{ System.out.println("A"); } } }
ConDefects/ConDefects/Code/abc301_a/Java/42009685
condefects-java_data_2002
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] strs = br.readLine().split(" "); br.close(); int[] s = new int[n]; for (int i = 0; i < n; i++) { s[i] = Integer.parseInt(strs[i]); } int[] a = new int[n]; for (int i = 0; i < n; i++) { if (i == 0) { a[0] = s[0]; continue; } a[i] = s[i] - a[i - 1]; } for (int num : a) { System.out.print(num + " "); } } } import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] strs = br.readLine().split(" "); br.close(); int[] s = new int[n]; for (int i = 0; i < n; i++) { s[i] = Integer.parseInt(strs[i]); } int[] a = new int[n]; for (int i = 0; i < n; i++) { if (i == 0) { a[0] = s[0]; continue; } a[i] = s[i] - s[i - 1]; } for (int num : a) { System.out.print(num + " "); } } }
ConDefects/ConDefects/Code/abc280_b/Java/40808678
condefects-java_data_2003
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] s = new int[n]; for (int i = 0; i < n; i++) { s[i] = sc.nextInt(); } int[] a = new int[n]; a[0] = s[0]; for (int i = 1; i < n; i++) { a[i] = s[i] - (a[i-1]); } for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } sc.close(); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] s = new int[n]; for (int i = 0; i < n; i++) { s[i] = sc.nextInt(); } int[] a = new int[n]; a[0] = s[0]; for (int i = 1; i < n; i++) { a[i] = s[i] - s[i-1]; } for (int i = 0; i < n; i++) { System.out.print(a[i] + " "); } sc.close(); } }
ConDefects/ConDefects/Code/abc280_b/Java/40065779
condefects-java_data_2004
import java.util.Scanner; public class Main { public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { int n = sc.nextInt(); int prev = 0; while (n > 0) { int number = n % 10; if (number <= prev) { System.out.println("No"); return; } prev = number; n /= 10; } System.out.println("Yes"); } } } import java.util.Scanner; public class Main { public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { int n = sc.nextInt(); int prev = -1; while (n > 0) { int number = n % 10; if (number <= prev) { System.out.println("No"); return; } prev = number; n /= 10; } System.out.println("Yes"); } } }
ConDefects/ConDefects/Code/abc321_a/Java/52018575
condefects-java_data_2005
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); String ans = "Yes"; int[] d = new int[s.length()]; for(int i=0;i<s.length();i++){ d[i]=(int)s.charAt(i); } for (int j=0;j<s.length()-1 && s.length()!=1;j++){ if (d[j] < d[j+1]) { ans = "No"; break; } } System.out.println(ans); sc.close(); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); String ans = "Yes"; int[] d = new int[s.length()]; for(int i=0;i<s.length();i++){ d[i]=(int)s.charAt(i); } for (int j=0;j<s.length()-1 && s.length()!=1;j++){ if (d[j] <= d[j+1]) { ans = "No"; break; } } System.out.println(ans); sc.close(); } }
ConDefects/ConDefects/Code/abc321_a/Java/47590238
condefects-java_data_2006
import java.util.*; class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); //-------入力の読み取り----- int n = sc.nextInt(); //-----Scannerを閉じる------ sc.close(); //----------処理---------- boolean f = true; int a = 0; int b = 0; while(n > 0){ b = n % 10; if(b <= a){ f = false; } a = b; n /= 10; } //----------出力---------- System.out.println(yorn(f)); } //----------------------以下メソッド-------------------------- public static String yorn (boolean flag){ //trueならYes、falseならNoを返す String answer; if(flag){ answer = "Yes"; }else{ answer = "No"; } return answer; } } import java.util.*; class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); //-------入力の読み取り----- int n = sc.nextInt(); //-----Scannerを閉じる------ sc.close(); //----------処理---------- boolean f = true; int a = -1; int b = 0; while(n > 0){ b = n % 10; if(b <= a){ f = false; } a = b; n /= 10; } //----------出力---------- System.out.println(yorn(f)); } //----------------------以下メソッド-------------------------- public static String yorn (boolean flag){ //trueならYes、falseならNoを返す String answer; if(flag){ answer = "Yes"; }else{ answer = "No"; } return answer; } }
ConDefects/ConDefects/Code/abc321_a/Java/47861817
condefects-java_data_2007
import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String n = sc.next(); int i=1; for(; i<n.length();i++) if((int)n.charAt(i-1) < n.charAt(i)) break; System.out.print(i == n.length() ? "Yes" : "No"); } } import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String n = sc.next(); int i=1; for(; i<n.length();i++) if((int)n.charAt(i-1) <= n.charAt(i)) break; System.out.print(i == n.length() ? "Yes" : "No"); } }
ConDefects/ConDefects/Code/abc321_a/Java/54929639
condefects-java_data_2008
import java.io.*; import java.math.*; import java.time.*; import java.util.*; import java.util.Map.Entry; class Main implements Runnable { public static void solve () { int n = nextInt(), m = nextInt(), k = nextInt(); int[] a = nextIntArray(n); //長さnからrange m で順に連続部分を抜き出す //連続部分のうち、小さい方からk個の和を求めて出力する long nowSum = 0; CountTreeMap ink = new CountTreeMap(new TreeMap<>()), outk = new CountTreeMap(new TreeMap<>()); List<Integer> list = new ArrayList<>(); for (int i=0; i<m; i++) list.add(a[i]); Collections.sort(list); for (int i=0; i<k; i++) { ink.plus(list.get(i), 1); nowSum += list.get(i); } for (int i=k; i<list.size(); i++) { outk.plus(list.get(i), 1); } print(nowSum + " "); for (int i=m; i<n; i++) { int remove = a[i-m]; int add = a[i]; int inkCount = 0, outkCount = 0; if (ink.containsKey(remove) == true) { ink.minus(remove, 1); nowSum -= remove; inkCount -= 1; } else { outk.minus(remove, 1); outkCount -= 1; } if (ink.size() > 0 && a[i] < ink.map.lastKey()) { ink.plus(a[i], 1); nowSum += a[i]; inkCount += 1; } else { outk.plus(a[i], 1); outkCount += 1; } if (inkCount < outkCount) { int temp = outk.map.firstKey(); outk.minus(temp, 1); ink.plus(temp, 1); nowSum += temp; } else if (inkCount > outkCount) { int temp = ink.map.lastKey(); ink.minus(temp, 1); outk.plus(temp, 1); nowSum -= temp; } print(nowSum + " "); } } public static class CountTreeMap { TreeMap<Integer, Integer> map; CountTreeMap (TreeMap<Integer, Integer> map) { this.map = map; } void plus (int a, int num) { if (map.get(a) == null) { map.put(a, num); } else { map.put(a, map.get(a) + num); } } void minus (int a, int num) { if (map.get(a) == null) { System.out.println("CountTreeMap minus error : null"); System.exit(0); } else if (map.get(a) > num) { map.put(a, map.get(a) - num); } else if (map.get(a) == 0) { map.remove(a); } else if (map.get(a) < num) { System.out.println("CountTreeMap minus error : can not minus"); } } boolean containsKey (int a) { return map.containsKey(a); } int size () { return map.size(); } } /* * ############################################################################################ * # useful fields, useful methods, useful class * ############################################################################################## */ // fields public static final int infi = (int)1e9; public static final long infl = (long)1e18; public static final int modi = (int)1e9 + 7; public static final long modl = (long)1e18 + 7; public static int[] dy = {-1, 0, 1, 0}; public static int[] dx = {0, 1, 0, -1}; // public static int[] dy = {-1, 0, -1, 1, 0, 1}; // public static int[] dx = {-1, -1, 0, 0, 1, 1}; // public static int[] dy = {-1, -1, -1, 0, 1, 1, 1, 0}; // public static int[] dx = {-1, 0, 1, 1, 1, 0, -1, -1}; // methods public static int min (int... a) {Arrays.sort(a); return a[0];} public static int max (int... a) {Arrays.sort(a); return a[a.length-1];} public static long min (long... a) {Arrays.sort(a); return a[0];} public static long max (long... a) {Arrays.sort(a); return a[a.length-1];} public static long pow (long c, long b) { long res = 1; for (int i=0; i<b; i++) { res *= c; } return res; } // class public static class Edge implements Comparable<Edge> { int id, from, to, cost; Edge(int to, int cost) { //基本コレ this.to = to; this.cost = cost; } Edge(int from, int to, int cost) { this.from = from; this.to = to; this.cost = cost; } Edge(int id, int from, int to, int cost) { this.id = id; this.from = from; this.to = to; this.cost = cost; } @Override public int compareTo (Edge e) { return this.cost - e.cost; } } public static class Point implements Comparable<Point> { int x, y; Point (int x, int y) { this.x = x; this.y = y; } @Override public int compareTo (Point p) { return this.y - p.y; } } /* * ############################################################################################## * # input * ############################################################################################## */ // input - fields public static final InputStream in = System.in; public static final byte[] buffer = new byte[1024]; public static int ptr = 0; public static int bufferLength = 0; // input - basic methods public static boolean hasNextByte() { if (ptr < bufferLength) { return true; } else { ptr = 0; try { bufferLength = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (bufferLength <= 0) { return false; } } return true; } public static int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } public static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public static void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } public static boolean hasNext() { skipUnprintable(); return hasNextByte(); } // input - single public static String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public static int nextInt() { return (int) nextLong(); } public static long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public static double nextDouble() { return Double.parseDouble(next()); } // input - array public static String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } public static int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public static long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public static double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } // input - table public static char[][] nextCharTable(int h, int w) { char[][] array = new char[h][w]; for (int i = 0; i < h; i++) array[i] = next().toCharArray(); return array; } public static int[][] nextIntTable(int h, int w) { int[][] a = new int[h][]; for (int i=0; i<h; i++) { for (int j=0; j<w; j++) a[i][j] = nextInt(); } return a; } /* * ############################################################################################## * # output * ############################################################################################## */ // output - fields static PrintWriter out = new PrintWriter(System.out); //output - single public static void print(Object o) {out.print(o);} public static void println(Object o) {out.println(o);} //output - array public static void printStringArray(String[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printIntArray(int[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printLongArray(long[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printBooleanArray (boolean[] a) { for (int i=0; i<a.length; i++) { char c = a[i]==true? 'o' : 'x'; print(c); } println(""); } public static void printCharTable(char[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { print(a[i][j]); } println(""); } } public static void printIntTable(int[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } public static void printBooleanTable(boolean[][] b) { for (int i=0; i<b.length; i++) { for (int j=0; j<b[0].length; j++) { print(b[i][j]? "o" : "x"); } println(""); } } public static void printLongTable(long[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } /* * ############################################################################################## * # main * ############################################################################################## */ public static void main(String[] args) { new Thread(null, new Main(), "", 64 * 1024 * 1024).start(); } public void run() { solve(); out.close(); } } import java.io.*; import java.math.*; import java.time.*; import java.util.*; import java.util.Map.Entry; class Main implements Runnable { public static void solve () { int n = nextInt(), m = nextInt(), k = nextInt(); int[] a = nextIntArray(n); //長さnからrange m で順に連続部分を抜き出す //連続部分のうち、小さい方からk個の和を求めて出力する long nowSum = 0; CountTreeMap ink = new CountTreeMap(new TreeMap<>()), outk = new CountTreeMap(new TreeMap<>()); List<Integer> list = new ArrayList<>(); for (int i=0; i<m; i++) list.add(a[i]); Collections.sort(list); for (int i=0; i<k; i++) { ink.plus(list.get(i), 1); nowSum += list.get(i); } for (int i=k; i<list.size(); i++) { outk.plus(list.get(i), 1); } print(nowSum + " "); for (int i=m; i<n; i++) { int remove = a[i-m]; int add = a[i]; int inkCount = 0, outkCount = 0; if (ink.containsKey(remove) == true) { ink.minus(remove, 1); nowSum -= remove; inkCount -= 1; } else { outk.minus(remove, 1); outkCount -= 1; } if (ink.size() > 0 && a[i] < ink.map.lastKey()) { ink.plus(a[i], 1); nowSum += a[i]; inkCount += 1; } else { outk.plus(a[i], 1); outkCount += 1; } if (inkCount < outkCount) { int temp = outk.map.firstKey(); outk.minus(temp, 1); ink.plus(temp, 1); nowSum += temp; } else if (inkCount > outkCount) { int temp = ink.map.lastKey(); ink.minus(temp, 1); outk.plus(temp, 1); nowSum -= temp; } print(nowSum + " "); } } public static class CountTreeMap { TreeMap<Integer, Integer> map; CountTreeMap (TreeMap<Integer, Integer> map) { this.map = map; } void plus (int a, int num) { if (map.get(a) == null) { map.put(a, num); } else { map.put(a, map.get(a) + num); } } void minus (int a, int num) { if (map.get(a) == null) { System.out.println("CountTreeMap minus error : null"); System.exit(0); } else if (map.get(a) > num) { map.put(a, map.get(a) - num); } else if (map.get(a) == num) { map.remove(a); } else if (map.get(a) < num) { System.out.println("CountTreeMap minus error : can not minus"); } } boolean containsKey (int a) { return map.containsKey(a); } int size () { return map.size(); } } /* * ############################################################################################ * # useful fields, useful methods, useful class * ############################################################################################## */ // fields public static final int infi = (int)1e9; public static final long infl = (long)1e18; public static final int modi = (int)1e9 + 7; public static final long modl = (long)1e18 + 7; public static int[] dy = {-1, 0, 1, 0}; public static int[] dx = {0, 1, 0, -1}; // public static int[] dy = {-1, 0, -1, 1, 0, 1}; // public static int[] dx = {-1, -1, 0, 0, 1, 1}; // public static int[] dy = {-1, -1, -1, 0, 1, 1, 1, 0}; // public static int[] dx = {-1, 0, 1, 1, 1, 0, -1, -1}; // methods public static int min (int... a) {Arrays.sort(a); return a[0];} public static int max (int... a) {Arrays.sort(a); return a[a.length-1];} public static long min (long... a) {Arrays.sort(a); return a[0];} public static long max (long... a) {Arrays.sort(a); return a[a.length-1];} public static long pow (long c, long b) { long res = 1; for (int i=0; i<b; i++) { res *= c; } return res; } // class public static class Edge implements Comparable<Edge> { int id, from, to, cost; Edge(int to, int cost) { //基本コレ this.to = to; this.cost = cost; } Edge(int from, int to, int cost) { this.from = from; this.to = to; this.cost = cost; } Edge(int id, int from, int to, int cost) { this.id = id; this.from = from; this.to = to; this.cost = cost; } @Override public int compareTo (Edge e) { return this.cost - e.cost; } } public static class Point implements Comparable<Point> { int x, y; Point (int x, int y) { this.x = x; this.y = y; } @Override public int compareTo (Point p) { return this.y - p.y; } } /* * ############################################################################################## * # input * ############################################################################################## */ // input - fields public static final InputStream in = System.in; public static final byte[] buffer = new byte[1024]; public static int ptr = 0; public static int bufferLength = 0; // input - basic methods public static boolean hasNextByte() { if (ptr < bufferLength) { return true; } else { ptr = 0; try { bufferLength = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (bufferLength <= 0) { return false; } } return true; } public static int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } public static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public static void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } public static boolean hasNext() { skipUnprintable(); return hasNextByte(); } // input - single public static String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public static int nextInt() { return (int) nextLong(); } public static long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public static double nextDouble() { return Double.parseDouble(next()); } // input - array public static String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } public static int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public static long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public static double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } // input - table public static char[][] nextCharTable(int h, int w) { char[][] array = new char[h][w]; for (int i = 0; i < h; i++) array[i] = next().toCharArray(); return array; } public static int[][] nextIntTable(int h, int w) { int[][] a = new int[h][]; for (int i=0; i<h; i++) { for (int j=0; j<w; j++) a[i][j] = nextInt(); } return a; } /* * ############################################################################################## * # output * ############################################################################################## */ // output - fields static PrintWriter out = new PrintWriter(System.out); //output - single public static void print(Object o) {out.print(o);} public static void println(Object o) {out.println(o);} //output - array public static void printStringArray(String[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printIntArray(int[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printLongArray(long[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printBooleanArray (boolean[] a) { for (int i=0; i<a.length; i++) { char c = a[i]==true? 'o' : 'x'; print(c); } println(""); } public static void printCharTable(char[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { print(a[i][j]); } println(""); } } public static void printIntTable(int[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } public static void printBooleanTable(boolean[][] b) { for (int i=0; i<b.length; i++) { for (int j=0; j<b[0].length; j++) { print(b[i][j]? "o" : "x"); } println(""); } } public static void printLongTable(long[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } /* * ############################################################################################## * # main * ############################################################################################## */ public static void main(String[] args) { new Thread(null, new Main(), "", 64 * 1024 * 1024).start(); } public void run() { solve(); out.close(); } }
ConDefects/ConDefects/Code/abc281_e/Java/40164805
condefects-java_data_2009
import java.io.*; import java.math.*; import java.time.*; import java.util.*; import java.util.Map.Entry; class Main implements Runnable { public static void solve () { int n = nextInt(), m = nextInt(), k = nextInt(); int[] a = nextIntArray(n); //長さnからrange m で順に連続部分を抜き出す //連続部分のうち、小さい方からk個の和を求めて出力する long nowSum = 0; TreeMap<Integer, Integer> ink = new TreeMap<>(), outk = new TreeMap<>(); List<Integer> list = new ArrayList<>(); for (int i=0; i<m; i++) list.add(a[i]); Collections.sort(list); for (int i=0; i<k; i++) { if (ink.get(list.get(i)) == null) ink.put(list.get(i), 1); else ink.put(list.get(i), ink.get(list.get(i)) + 1); nowSum += list.get(i); } for (int i=k; i<list.size(); i++) { if (outk.get(list.get(i)) == null) outk.put(list.get(i), 1); else outk.put(list.get(i), outk.get(list.get(i)) + 1); } print(nowSum + " "); for (int i=m; i<n; i++) { int remove = a[i-m]; int add = a[i]; int inkCount = 0, outkCount = 0; if (ink.containsKey(remove) == true) { ink.put(remove, ink.get(remove) - 1); if (ink.get(remove) == 0) ink.remove(remove); nowSum -= remove; inkCount -= 1; } else { outk.put(remove, outk.get(remove) - 1); if (outk.get(remove) == 0) outk.remove(remove); outkCount -= 1; } if (a[i] < ink.lastKey()) { if (ink.get(a[i]) == null) ink.put(a[i], 1); else ink.put(a[i], ink.get(a[i]) + 1); nowSum += a[i]; inkCount += 1; } else { if (outk.get(a[i]) == null) outk.put(a[i], 1); else outk.put(a[i], outk.get(a[i]) + 1); outkCount += 1; } if (inkCount < outkCount) { int temp = outk.firstKey(); outk.put(temp, outk.get(temp) - 1); if (outk.get(temp) == 0) outk.remove(temp); if (ink.get(temp) == null) ink.put(temp, 1); else ink.put(temp, ink.get(temp) + 1); nowSum += temp; } else if (inkCount > outkCount) { int temp = ink.lastKey(); ink.put(temp, ink.get(temp) - 1); if (ink.get(temp) == 0) ink.remove(temp); if (outk.get(temp) == null) outk.put(temp, 1); else outk.put(temp, outk.get(temp) + 1); nowSum -= temp; } print(nowSum + " "); } } /* * ############################################################################################ * # useful fields, useful methods, useful class * ############################################################################################## */ // fields public static final int infi = (int)1e9; public static final long infl = (long)1e18; public static final int modi = (int)1e9 + 7; public static final long modl = (long)1e18 + 7; public static int[] dy = {-1, 0, 1, 0}; public static int[] dx = {0, 1, 0, -1}; // public static int[] dy = {-1, 0, -1, 1, 0, 1}; // public static int[] dx = {-1, -1, 0, 0, 1, 1}; // public static int[] dy = {-1, -1, -1, 0, 1, 1, 1, 0}; // public static int[] dx = {-1, 0, 1, 1, 1, 0, -1, -1}; // methods public static int min (int... a) {Arrays.sort(a); return a[0];} public static int max (int... a) {Arrays.sort(a); return a[a.length-1];} public static long min (long... a) {Arrays.sort(a); return a[0];} public static long max (long... a) {Arrays.sort(a); return a[a.length-1];} public static long pow (long c, long b) { long res = 1; for (int i=0; i<b; i++) { res *= c; } return res; } // class public static class Edge implements Comparable<Edge> { int id, from, to, cost; Edge(int to, int cost) { //基本コレ this.to = to; this.cost = cost; } Edge(int from, int to, int cost) { this.from = from; this.to = to; this.cost = cost; } Edge(int id, int from, int to, int cost) { this.id = id; this.from = from; this.to = to; this.cost = cost; } @Override public int compareTo (Edge e) { return this.cost - e.cost; } } public static class Point implements Comparable<Point> { int x, y; Point (int x, int y) { this.x = x; this.y = y; } @Override public int compareTo (Point p) { return this.y - p.y; } } /* * ############################################################################################## * # input * ############################################################################################## */ // input - fields public static final InputStream in = System.in; public static final byte[] buffer = new byte[1024]; public static int ptr = 0; public static int bufferLength = 0; // input - basic methods public static boolean hasNextByte() { if (ptr < bufferLength) { return true; } else { ptr = 0; try { bufferLength = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (bufferLength <= 0) { return false; } } return true; } public static int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } public static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public static void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } public static boolean hasNext() { skipUnprintable(); return hasNextByte(); } // input - single public static String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public static int nextInt() { return (int) nextLong(); } public static long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public static double nextDouble() { return Double.parseDouble(next()); } // input - array public static String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } public static int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public static long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public static double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } // input - table public static char[][] nextCharTable(int h, int w) { char[][] array = new char[h][w]; for (int i = 0; i < h; i++) array[i] = next().toCharArray(); return array; } public static int[][] nextIntTable(int h, int w) { int[][] a = new int[h][]; for (int i=0; i<h; i++) { for (int j=0; j<w; j++) a[i][j] = nextInt(); } return a; } /* * ############################################################################################## * # output * ############################################################################################## */ // output - fields static PrintWriter out = new PrintWriter(System.out); //output - single public static void print(Object o) {out.print(o);} public static void println(Object o) {out.println(o);} //output - array public static void printStringArray(String[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printIntArray(int[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printLongArray(long[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printBooleanArray (boolean[] a) { for (int i=0; i<a.length; i++) { char c = a[i]==true? 'o' : 'x'; print(c); } println(""); } public static void printCharTable(char[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { print(a[i][j]); } println(""); } } public static void printIntTable(int[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } public static void printBooleanTable(boolean[][] b) { for (int i=0; i<b.length; i++) { for (int j=0; j<b[0].length; j++) { print(b[i][j]? "o" : "x"); } println(""); } } public static void printLongTable(long[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } /* * ############################################################################################## * # main * ############################################################################################## */ public static void main(String[] args) { new Thread(null, new Main(), "", 64 * 1024 * 1024).start(); } public void run() { solve(); out.close(); } } import java.io.*; import java.math.*; import java.time.*; import java.util.*; import java.util.Map.Entry; class Main implements Runnable { public static void solve () { int n = nextInt(), m = nextInt(), k = nextInt(); int[] a = nextIntArray(n); //長さnからrange m で順に連続部分を抜き出す //連続部分のうち、小さい方からk個の和を求めて出力する long nowSum = 0; TreeMap<Integer, Integer> ink = new TreeMap<>(), outk = new TreeMap<>(); List<Integer> list = new ArrayList<>(); for (int i=0; i<m; i++) list.add(a[i]); Collections.sort(list); for (int i=0; i<k; i++) { if (ink.get(list.get(i)) == null) ink.put(list.get(i), 1); else ink.put(list.get(i), ink.get(list.get(i)) + 1); nowSum += list.get(i); } for (int i=k; i<list.size(); i++) { if (outk.get(list.get(i)) == null) outk.put(list.get(i), 1); else outk.put(list.get(i), outk.get(list.get(i)) + 1); } print(nowSum + " "); for (int i=m; i<n; i++) { int remove = a[i-m]; int add = a[i]; int inkCount = 0, outkCount = 0; if (ink.containsKey(remove) == true) { ink.put(remove, ink.get(remove) - 1); if (ink.get(remove) == 0) ink.remove(remove); nowSum -= remove; inkCount -= 1; } else { outk.put(remove, outk.get(remove) - 1); if (outk.get(remove) == 0) outk.remove(remove); outkCount -= 1; } if (ink.size() > 0 && a[i] < ink.lastKey()) { if (ink.get(a[i]) == null) ink.put(a[i], 1); else ink.put(a[i], ink.get(a[i]) + 1); nowSum += a[i]; inkCount += 1; } else { if (outk.get(a[i]) == null) outk.put(a[i], 1); else outk.put(a[i], outk.get(a[i]) + 1); outkCount += 1; } if (inkCount < outkCount) { int temp = outk.firstKey(); outk.put(temp, outk.get(temp) - 1); if (outk.get(temp) == 0) outk.remove(temp); if (ink.get(temp) == null) ink.put(temp, 1); else ink.put(temp, ink.get(temp) + 1); nowSum += temp; } else if (inkCount > outkCount) { int temp = ink.lastKey(); ink.put(temp, ink.get(temp) - 1); if (ink.get(temp) == 0) ink.remove(temp); if (outk.get(temp) == null) outk.put(temp, 1); else outk.put(temp, outk.get(temp) + 1); nowSum -= temp; } print(nowSum + " "); } } /* * ############################################################################################ * # useful fields, useful methods, useful class * ############################################################################################## */ // fields public static final int infi = (int)1e9; public static final long infl = (long)1e18; public static final int modi = (int)1e9 + 7; public static final long modl = (long)1e18 + 7; public static int[] dy = {-1, 0, 1, 0}; public static int[] dx = {0, 1, 0, -1}; // public static int[] dy = {-1, 0, -1, 1, 0, 1}; // public static int[] dx = {-1, -1, 0, 0, 1, 1}; // public static int[] dy = {-1, -1, -1, 0, 1, 1, 1, 0}; // public static int[] dx = {-1, 0, 1, 1, 1, 0, -1, -1}; // methods public static int min (int... a) {Arrays.sort(a); return a[0];} public static int max (int... a) {Arrays.sort(a); return a[a.length-1];} public static long min (long... a) {Arrays.sort(a); return a[0];} public static long max (long... a) {Arrays.sort(a); return a[a.length-1];} public static long pow (long c, long b) { long res = 1; for (int i=0; i<b; i++) { res *= c; } return res; } // class public static class Edge implements Comparable<Edge> { int id, from, to, cost; Edge(int to, int cost) { //基本コレ this.to = to; this.cost = cost; } Edge(int from, int to, int cost) { this.from = from; this.to = to; this.cost = cost; } Edge(int id, int from, int to, int cost) { this.id = id; this.from = from; this.to = to; this.cost = cost; } @Override public int compareTo (Edge e) { return this.cost - e.cost; } } public static class Point implements Comparable<Point> { int x, y; Point (int x, int y) { this.x = x; this.y = y; } @Override public int compareTo (Point p) { return this.y - p.y; } } /* * ############################################################################################## * # input * ############################################################################################## */ // input - fields public static final InputStream in = System.in; public static final byte[] buffer = new byte[1024]; public static int ptr = 0; public static int bufferLength = 0; // input - basic methods public static boolean hasNextByte() { if (ptr < bufferLength) { return true; } else { ptr = 0; try { bufferLength = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (bufferLength <= 0) { return false; } } return true; } public static int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } public static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public static void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } public static boolean hasNext() { skipUnprintable(); return hasNextByte(); } // input - single public static String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public static int nextInt() { return (int) nextLong(); } public static long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public static double nextDouble() { return Double.parseDouble(next()); } // input - array public static String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } public static int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public static long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public static double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } // input - table public static char[][] nextCharTable(int h, int w) { char[][] array = new char[h][w]; for (int i = 0; i < h; i++) array[i] = next().toCharArray(); return array; } public static int[][] nextIntTable(int h, int w) { int[][] a = new int[h][]; for (int i=0; i<h; i++) { for (int j=0; j<w; j++) a[i][j] = nextInt(); } return a; } /* * ############################################################################################## * # output * ############################################################################################## */ // output - fields static PrintWriter out = new PrintWriter(System.out); //output - single public static void print(Object o) {out.print(o);} public static void println(Object o) {out.println(o);} //output - array public static void printStringArray(String[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printIntArray(int[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printLongArray(long[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printBooleanArray (boolean[] a) { for (int i=0; i<a.length; i++) { char c = a[i]==true? 'o' : 'x'; print(c); } println(""); } public static void printCharTable(char[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { print(a[i][j]); } println(""); } } public static void printIntTable(int[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } public static void printBooleanTable(boolean[][] b) { for (int i=0; i<b.length; i++) { for (int j=0; j<b[0].length; j++) { print(b[i][j]? "o" : "x"); } println(""); } } public static void printLongTable(long[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } /* * ############################################################################################## * # main * ############################################################################################## */ public static void main(String[] args) { new Thread(null, new Main(), "", 64 * 1024 * 1024).start(); } public void run() { solve(); out.close(); } }
ConDefects/ConDefects/Code/abc281_e/Java/40163753
condefects-java_data_2010
import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.ArrayList; class Main { public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); List<Integer> A= new ArrayList<>(), B = new ArrayList<>(); //List<Float> T = new ArrayList<>(); float sum = 0, x = 0; for(int i = 0; i < N; i++){ A.add(scan.nextInt()); B.add(scan.nextInt()); sum += (float)A.get(i)/B.get(i); } sum = sum/2; System.out.println(sum); for(int i = 0; sum > 0; i++){ if(sum < (float)A.get(i)/B.get(i)){ System.out.println(sum); x += B.get(i)*sum; sum = 0; }else{ x += A.get(i); sum-= (float)A.get(i)/B.get(i); } } System.out.println(x); } } import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.ArrayList; class Main { public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); List<Integer> A= new ArrayList<>(), B = new ArrayList<>(); //List<Float> T = new ArrayList<>(); float sum = 0, x = 0; for(int i = 0; i < N; i++){ A.add(scan.nextInt()); B.add(scan.nextInt()); sum += (float)A.get(i)/B.get(i); } sum = sum/2; for(int i = 0; sum > 0; i++){ if(sum < (float)A.get(i)/B.get(i)){ x += B.get(i)*sum; sum = 0; }else{ x += A.get(i); sum-= (float)A.get(i)/B.get(i); } } System.out.println(x); } }
ConDefects/ConDefects/Code/abc223_c/Java/36786321
condefects-java_data_2011
import java.util.*; import java.io.*; import java.math.*; import java.util.stream.*; class Main implements Runnable{ // introduced in Java 16 // record pair(int first , int second) { } void solve() { // jを固定して上にlower下にupperなんか見たことある問題 int n = in.nextInt(); int [] A = in.IntArray(n); String mex = in.next(); List<Integer> [][] G = new ArrayList[3][3]; Counter<Integer> cnt = new Counter(); long ans = 0 ; for(int i = 0 ; i < 3 ; i ++ ) { for(int j = 0 ; j < 3 ; j ++ ) { G[i][j] = new ArrayList<>(); } } for(int i = 0 ; i < n ; i ++ ) { char now = mex.charAt(i); if(now == 'M') G[0][A[i]].add(i); if(now == 'E') G[1][A[i]].add(i); if(now == 'X') G[2][A[i]].add(i); } for(int E = 0 ; E < 3 ; E ++ ) { int A_j = E ; for(int j_index : G[1][A_j]) { for(int M = 0 ; M < 3 ; M ++ ) { for(int X = 0 ; X < 3 ; X ++ ) { int A_i = M ; int A_k = X ; int i_cnt = cnt.under(G[0][A_i], j_index); int k_cnt = cnt.greater(G[2][A_k], j_index); long use_able = -1 ; for(long u = 0 ; u <= 3 ; u ++ ) { if(u != M && u != E && u != X) { use_able = u ; break; } } long c = i_cnt * k_cnt ; ans += (long)use_able * c; } } } } out.print(ans); } public static void main(String[] args) { new Thread(null, new Main(), "", Runtime.getRuntime().maxMemory()).start(); } public void run() { solve(); out.flush(); } PrintWriter out = new PrintWriter(System.out); In in = new In(); } class Counter <T extends Comparable<T>>{ public List<Integer> toList(int [] a) { var List = Arrays.stream(a).boxed().collect(Collectors.toList()); Collections.sort(List); return List; } public List<Long> toList(long [] a) { var List = Arrays.stream(a).boxed().collect(Collectors.toList()); Collections.sort(List); return List; } int or_greater(List<T> A , T key) { return A.size() - lower_bound(A, key); } int or_under(List<T> A , T key) { return upper_bound(A, key); } int greater(List<T> A , T key) { return A.size() - upper_bound(A, key); } int under(List<T> A , T key) { return lower_bound(A, key); } private int lower_bound(List<T> A, T key) { int left = 0; int right = A.size(); while(left < right) { int mid = (left + right) / 2; if(compare(A.get(mid), key , 0)) left = mid + 1; else right = mid; } return right; } private int upper_bound(List<T> A , T key) { int left = 0; int right = A.size(); while(left < right) { int mid = (left + right) / 2; if(compare(A.get(mid) , key , 1)) left = mid + 1; else right = mid; } return right; } boolean compare(T o1, T o2 , int c) { int res = o1.compareTo(o2); return c == 0 ? res == -1 : (res == 0 || res == -1); } } class In{ private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) { ptr++; } return hasNextByte(); } String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return next().charAt(0); } int [] IntArray(int n) { final int [] Array = new int [n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = nextInt(); } return Array; } int [][] IntArray(int n , int m) { final int [][] Array = new int [n][m]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = IntArray(m); } return Array; } long [] LongArray(int n) { final long [] Array = new long [n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = nextLong(); } return Array; } long [][] LongArray(int n , int m) { final long [][] Array = new long [n][m]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = LongArray(m); } return Array; } String [] StringArray(int n) { final String [] Array = new String [n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = next(); } return Array; } char [] CharArray(int n) { final char [] Array = new char[n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = next().charAt(0); } return Array; } char [][] CharArray(int n , int m) { final char [][] Array = new char [n][m]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = next().toCharArray(); } return Array; } char [][] CharArray2(int n , int m) { final char [][] Array = new char [n][m]; for(int i = 0 ; i < n ; i ++ ) { for(int j = 0 ; j < n ; j ++ ) { Array[i][j] = next().charAt(0); } } return Array; } List<Integer> [] Graph(int n) { @SuppressWarnings("unchecked") List<Integer> [] G = new ArrayList[n]; for(int i = 0 ; i < n ; i ++ ) { G[i] = new ArrayList<>(); } return G ; } } import java.util.*; import java.io.*; import java.math.*; import java.util.stream.*; class Main implements Runnable{ // introduced in Java 16 // record pair(int first , int second) { } void solve() { // jを固定して上にlower下にupperなんか見たことある問題 int n = in.nextInt(); int [] A = in.IntArray(n); String mex = in.next(); List<Integer> [][] G = new ArrayList[3][3]; Counter<Integer> cnt = new Counter(); long ans = 0 ; for(int i = 0 ; i < 3 ; i ++ ) { for(int j = 0 ; j < 3 ; j ++ ) { G[i][j] = new ArrayList<>(); } } for(int i = 0 ; i < n ; i ++ ) { char now = mex.charAt(i); if(now == 'M') G[0][A[i]].add(i); if(now == 'E') G[1][A[i]].add(i); if(now == 'X') G[2][A[i]].add(i); } for(int E = 0 ; E < 3 ; E ++ ) { int A_j = E ; for(int j_index : G[1][A_j]) { for(int M = 0 ; M < 3 ; M ++ ) { for(int X = 0 ; X < 3 ; X ++ ) { int A_i = M ; int A_k = X ; int i_cnt = cnt.under(G[0][A_i], j_index); int k_cnt = cnt.greater(G[2][A_k], j_index); long use_able = -1 ; for(long u = 0 ; u <= 3 ; u ++ ) { if(u != M && u != E && u != X) { use_able = u ; break; } } long c = (long) i_cnt * k_cnt ; ans += (long)use_able * c; } } } } out.print(ans); } public static void main(String[] args) { new Thread(null, new Main(), "", Runtime.getRuntime().maxMemory()).start(); } public void run() { solve(); out.flush(); } PrintWriter out = new PrintWriter(System.out); In in = new In(); } class Counter <T extends Comparable<T>>{ public List<Integer> toList(int [] a) { var List = Arrays.stream(a).boxed().collect(Collectors.toList()); Collections.sort(List); return List; } public List<Long> toList(long [] a) { var List = Arrays.stream(a).boxed().collect(Collectors.toList()); Collections.sort(List); return List; } int or_greater(List<T> A , T key) { return A.size() - lower_bound(A, key); } int or_under(List<T> A , T key) { return upper_bound(A, key); } int greater(List<T> A , T key) { return A.size() - upper_bound(A, key); } int under(List<T> A , T key) { return lower_bound(A, key); } private int lower_bound(List<T> A, T key) { int left = 0; int right = A.size(); while(left < right) { int mid = (left + right) / 2; if(compare(A.get(mid), key , 0)) left = mid + 1; else right = mid; } return right; } private int upper_bound(List<T> A , T key) { int left = 0; int right = A.size(); while(left < right) { int mid = (left + right) / 2; if(compare(A.get(mid) , key , 1)) left = mid + 1; else right = mid; } return right; } boolean compare(T o1, T o2 , int c) { int res = o1.compareTo(o2); return c == 0 ? res == -1 : (res == 0 || res == -1); } } class In{ private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) { ptr++; } return hasNextByte(); } String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return next().charAt(0); } int [] IntArray(int n) { final int [] Array = new int [n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = nextInt(); } return Array; } int [][] IntArray(int n , int m) { final int [][] Array = new int [n][m]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = IntArray(m); } return Array; } long [] LongArray(int n) { final long [] Array = new long [n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = nextLong(); } return Array; } long [][] LongArray(int n , int m) { final long [][] Array = new long [n][m]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = LongArray(m); } return Array; } String [] StringArray(int n) { final String [] Array = new String [n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = next(); } return Array; } char [] CharArray(int n) { final char [] Array = new char[n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = next().charAt(0); } return Array; } char [][] CharArray(int n , int m) { final char [][] Array = new char [n][m]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = next().toCharArray(); } return Array; } char [][] CharArray2(int n , int m) { final char [][] Array = new char [n][m]; for(int i = 0 ; i < n ; i ++ ) { for(int j = 0 ; j < n ; j ++ ) { Array[i][j] = next().charAt(0); } } return Array; } List<Integer> [] Graph(int n) { @SuppressWarnings("unchecked") List<Integer> [] G = new ArrayList[n]; for(int i = 0 ; i < n ; i ++ ) { G[i] = new ArrayList<>(); } return G ; } }
ConDefects/ConDefects/Code/abc308_e/Java/45101314
condefects-java_data_2012
import java.io.*; import java.util.*; public class Main { static Scanner sc = new Scanner(System.in); static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer stn = new StreamTokenizer(bf); static PrintWriter printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String[] args) throws IOException { solve(); closeAndFlush(); } public static void solve() throws IOException { int n = sc.nextInt(); int[] nums = new int[n + 1]; char[] chars = new char[n + 1]; for (int i = 1; i <= n; i++) { nums[i] = sc.nextInt(); } chars = (" " + sc.next()).toCharArray(); int m0 = 0, m1 = 0, m2 = 0, x0 = 0, x1 = 0, x2 = 0; int[][] ms = new int[n + 1][3], xs = new int[n + 1][3]; //统计当前 e的前面有多少个 m和它后面有多少个 x for (int i = 1, j = n; i <= n; i++, j--) { if (chars[i] == 'E') { ms[i][0] = m0; ms[i][1] = m1; ms[i][2] = m2; } else if (chars[i] == 'M') { if (nums[i] == 0) m0++; else if (nums[i] == 1) m1++; else if (nums[i] == 2) m2++; } if (chars[j] == 'E') { xs[j][0] = x0; xs[j][1] = x1; xs[j][2] = x2; } else if (chars[j] == 'X') { if (nums[j] == 0) x0++; else if (nums[j] == 1) x1++; else if (nums[j] == 2) x2++; } } long sum = 0; for (int i = 2; i <= n - 1; i++) { if (chars[i] == 'E') { for (int j = 0; j < 3; j++) { if (ms[i][j] == 0) continue;//没有m for (int k = 0; k < 3; k++) { if (xs[i][k] == 0) continue;//没有x sum += mex(nums[i], j, k) * ms[i][j] * xs[i][k]; } } } } printWriter.println(sum); } //求出三个数中最小的且没有出现过的数 public static int mex(int x, int y, int z) { for (int i = 0; i <= 2; i++) { if (x != i && y != i && z != i) return i; } return 3; } public static void closeAndFlush() throws IOException { printWriter.flush(); printWriter.close(); bf.close(); sc.close(); } public static int readInt() throws IOException { stn.nextToken(); return (int) stn.nval; // return Integer.parseInt(readString()); } public static String readString() throws IOException { return bf.readLine(); } } import java.io.*; import java.util.*; public class Main { static Scanner sc = new Scanner(System.in); static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer stn = new StreamTokenizer(bf); static PrintWriter printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String[] args) throws IOException { solve(); closeAndFlush(); } public static void solve() throws IOException { int n = sc.nextInt(); int[] nums = new int[n + 1]; char[] chars = new char[n + 1]; for (int i = 1; i <= n; i++) { nums[i] = sc.nextInt(); } chars = (" " + sc.next()).toCharArray(); int m0 = 0, m1 = 0, m2 = 0, x0 = 0, x1 = 0, x2 = 0; int[][] ms = new int[n + 1][3], xs = new int[n + 1][3]; //统计当前 e的前面有多少个 m和它后面有多少个 x for (int i = 1, j = n; i <= n; i++, j--) { if (chars[i] == 'E') { ms[i][0] = m0; ms[i][1] = m1; ms[i][2] = m2; } else if (chars[i] == 'M') { if (nums[i] == 0) m0++; else if (nums[i] == 1) m1++; else if (nums[i] == 2) m2++; } if (chars[j] == 'E') { xs[j][0] = x0; xs[j][1] = x1; xs[j][2] = x2; } else if (chars[j] == 'X') { if (nums[j] == 0) x0++; else if (nums[j] == 1) x1++; else if (nums[j] == 2) x2++; } } long sum = 0; for (int i = 2; i <= n - 1; i++) { if (chars[i] == 'E') { for (int j = 0; j < 3; j++) { if (ms[i][j] == 0) continue;//没有m for (int k = 0; k < 3; k++) { if (xs[i][k] == 0) continue;//没有x sum += (long) mex(nums[i], j, k) * ms[i][j] * xs[i][k]; } } } } printWriter.println(sum); } //求出三个数中最小的且没有出现过的数 public static int mex(int x, int y, int z) { for (int i = 0; i <= 2; i++) { if (x != i && y != i && z != i) return i; } return 3; } public static void closeAndFlush() throws IOException { printWriter.flush(); printWriter.close(); bf.close(); sc.close(); } public static int readInt() throws IOException { stn.nextToken(); return (int) stn.nval; // return Integer.parseInt(readString()); } public static String readString() throws IOException { return bf.readLine(); } }
ConDefects/ConDefects/Code/abc308_e/Java/43712411
condefects-java_data_2013
import java.util.*; public class Main { static int N = 200010; static int [][] left = new int [N][3]; static int [][] right = new int [N][3]; static int [] a = new int [N]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n ; ++i) { a[i] = sc.nextInt(); } String s = sc.next(); for (int i = 0; i < n ; ++i) { if (i > 0) { for (int j = 0; j <= 2 ; ++j) { left[i][j] = left[i - 1][j]; } } if (s.charAt(i) == 'M') { left[i][a[i]]++; } } for (int i = n - 1; i >= 0; --i) { if (i < n - 1) { for (int j = 0; j <= 2 ; ++j) { right[i][j] = right[i + 1][j]; } } if (s.charAt(i) == 'X') { right[i][a[i]]++; } } long ret = 0; for (int i = 0; i < n ; ++i) { if (s.charAt(i) == 'E') { for (int j = 0; j <= 2 ; ++j) { for (int k = 0; k <= 2 ; ++k) { int mex = get(j, k, a[i]); ret += mex * left[i][j] * right[i][k]; } } } } System.out.println(ret); } static int get (int a, int b, int c){ boolean [] s = new boolean[4]; s[a] = s[b] = s[c] = true; for (int i = 0; i <= 3 ; ++i) { if (!s[i]) return i; } return 0; } } import java.util.*; public class Main { static int N = 200010; static int [][] left = new int [N][3]; static int [][] right = new int [N][3]; static int [] a = new int [N]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n ; ++i) { a[i] = sc.nextInt(); } String s = sc.next(); for (int i = 0; i < n ; ++i) { if (i > 0) { for (int j = 0; j <= 2 ; ++j) { left[i][j] = left[i - 1][j]; } } if (s.charAt(i) == 'M') { left[i][a[i]]++; } } for (int i = n - 1; i >= 0; --i) { if (i < n - 1) { for (int j = 0; j <= 2 ; ++j) { right[i][j] = right[i + 1][j]; } } if (s.charAt(i) == 'X') { right[i][a[i]]++; } } long ret = 0; for (int i = 0; i < n ; ++i) { if (s.charAt(i) == 'E') { for (int j = 0; j <= 2 ; ++j) { for (int k = 0; k <= 2 ; ++k) { int mex = get(j, k, a[i]); ret += 1L * mex * left[i][j] * right[i][k]; } } } } System.out.println(ret); } static int get (int a, int b, int c){ boolean [] s = new boolean[4]; s[a] = s[b] = s[c] = true; for (int i = 0; i <= 3 ; ++i) { if (!s[i]) return i; } return 0; } }
ConDefects/ConDefects/Code/abc308_e/Java/45259269
condefects-java_data_2014
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = Arrays.stream(new int[n]).map(e -> sc.nextInt()).toArray(); char[] str = sc.next().toCharArray(); //右側のXに対応する数字について int[][] dp1 = new int[n][3]; for(int i = n - 2; i >= 0; i--) { for(int j = 0; j < 3; j++) { dp1[i][j] = dp1[i + 1][j]; } if(str[i + 1] == 'X'){ dp1[i][a[i + 1]]++; } } int[][][] dp2 = new int[n][3][3]; for(int i = n - 2; i >= 0; i--) { for(int j = 0; j < 3; j++) { for(int k = 0; k < 3; k++) { dp2[i][j][k] = dp2[i + 1][j][k]; } } if(str[i + 1] == 'E') { for(int j = 0; j < 3; j++) { dp2[i][a[i + 1]][j] += dp1[i + 1][j]; } } } long[][][][] dp3 = new long[n][3][3][3]; for(int i = n - 3; i >= 0; i--) { for(int j = 0; j < 3; j++) { for(int k = 0; k < 3; k++) { for(int l = 0; l < 3; l++) { dp3[i][j][k][l] = dp3[i + 1][j][k][l]; } } } if(str[i] == 'M') { for(int j = 0; j < 3; j++) { for(int k = 0; k < 3; k++) { dp3[i][a[i]][j][k] += dp2[i][j][k]; } } } } long result = 0; result = dp3[0][0][0][0] + dp3[0][0][0][1]*2 + dp3[0][0][0][2] + dp3[0][0][1][0]*2 + dp3[0][0][1][1]*2 + dp3[0][0][1][2]*3 + dp3[0][0][2][0] + dp3[0][0][2][1]*3 + dp3[0][0][2][2] + dp3[0][1][0][0]*2 + dp3[0][1][0][1]*2 + dp3[0][1][0][2]*3 + dp3[0][1][1][0]*2 + dp3[0][1][1][1]*0 + dp3[0][1][1][2]*0 + dp3[0][1][2][0]*3 + dp3[0][1][2][1]*0 + dp3[0][1][2][2]*0 + dp3[0][2][0][0]*1 + dp3[0][2][0][1]*3 + dp3[0][2][0][2]*1 + dp3[0][2][1][0]*3 + dp3[0][2][1][1]*0 + dp3[0][2][1][2]*0 + dp3[0][2][2][0]*1 + dp3[0][2][2][1]*0 + dp3[0][2][2][2]*0; System.out.println(result); } } import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = Arrays.stream(new int[n]).map(e -> sc.nextInt()).toArray(); char[] str = sc.next().toCharArray(); //右側のXに対応する数字について int[][] dp1 = new int[n][3]; for(int i = n - 2; i >= 0; i--) { for(int j = 0; j < 3; j++) { dp1[i][j] = dp1[i + 1][j]; } if(str[i + 1] == 'X'){ dp1[i][a[i + 1]]++; } } long[][][] dp2 = new long[n][3][3]; for(int i = n - 2; i >= 0; i--) { for(int j = 0; j < 3; j++) { for(int k = 0; k < 3; k++) { dp2[i][j][k] = dp2[i + 1][j][k]; } } if(str[i + 1] == 'E') { for(int j = 0; j < 3; j++) { dp2[i][a[i + 1]][j] += dp1[i + 1][j]; } } } long[][][][] dp3 = new long[n][3][3][3]; for(int i = n - 3; i >= 0; i--) { for(int j = 0; j < 3; j++) { for(int k = 0; k < 3; k++) { for(int l = 0; l < 3; l++) { dp3[i][j][k][l] = dp3[i + 1][j][k][l]; } } } if(str[i] == 'M') { for(int j = 0; j < 3; j++) { for(int k = 0; k < 3; k++) { dp3[i][a[i]][j][k] += dp2[i][j][k]; } } } } long result = 0; result = dp3[0][0][0][0] + dp3[0][0][0][1]*2 + dp3[0][0][0][2] + dp3[0][0][1][0]*2 + dp3[0][0][1][1]*2 + dp3[0][0][1][2]*3 + dp3[0][0][2][0] + dp3[0][0][2][1]*3 + dp3[0][0][2][2] + dp3[0][1][0][0]*2 + dp3[0][1][0][1]*2 + dp3[0][1][0][2]*3 + dp3[0][1][1][0]*2 + dp3[0][1][1][1]*0 + dp3[0][1][1][2]*0 + dp3[0][1][2][0]*3 + dp3[0][1][2][1]*0 + dp3[0][1][2][2]*0 + dp3[0][2][0][0]*1 + dp3[0][2][0][1]*3 + dp3[0][2][0][2]*1 + dp3[0][2][1][0]*3 + dp3[0][2][1][1]*0 + dp3[0][2][1][2]*0 + dp3[0][2][2][0]*1 + dp3[0][2][2][1]*0 + dp3[0][2][2][2]*0; System.out.println(result); } }
ConDefects/ConDefects/Code/abc308_e/Java/46022061
condefects-java_data_2015
// package date0905; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0 ; i < n ;i++){ arr[i] = sc.nextInt(); } String s = sc.next(); char[] cs = s.toCharArray(); long ans = 0; int[] pre = new int[3] , suf = new int[3]; int[] mex = {0,1,0,2,0,1,0,3}; for(int i = 0 ; i < n ; i++){ if(cs[i] == 'X') suf[arr[i]]++; } for(int i = 0 ; i < n ; i++){ if(cs[i] == 'M') pre[arr[i]]++; else if(cs[i] == 'E'){ for(int j = 0 ; j < 3 ; j++){ for(int k = 0 ; k < 3 ; k++){ ans += mex[(1 << arr[i]) | (1 << j)| (1 << k)] * pre[j] * suf[k]; } } }else{ suf[arr[i]]--; } } System.out.println(ans); } } // package date0905; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0 ; i < n ;i++){ arr[i] = sc.nextInt(); } String s = sc.next(); char[] cs = s.toCharArray(); long ans = 0; int[] pre = new int[3] , suf = new int[3]; int[] mex = {0,1,0,2,0,1,0,3}; for(int i = 0 ; i < n ; i++){ if(cs[i] == 'X') suf[arr[i]]++; } for(int i = 0 ; i < n ; i++){ if(cs[i] == 'M') pre[arr[i]]++; else if(cs[i] == 'E'){ for(int j = 0 ; j < 3 ; j++){ for(int k = 0 ; k < 3 ; k++){ ans += 1L * mex[(1 << arr[i]) | (1 << j)| (1 << k)] * pre[j] * suf[k]; } } }else{ suf[arr[i]]--; } } System.out.println(ans); } }
ConDefects/ConDefects/Code/abc308_e/Java/45256197
condefects-java_data_2016
import java.io.*; import java.util.*; /** * https://atcoder.jp/contests/abc308/tasks/abc308_e */ public class Main { private static final Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); private static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); String s = sc.next(); out.println(solve(n, a, s)); out.flush(); } private static long solve(int n, int[] a, String s) { char[] chars = s.toCharArray(); int[][] icnt = new int[n][3]; int[][] kcnt = new int[n][3]; int[] temp = new int[3]; for (int i = 0; i < n; i++) { if (chars[i] == 'M') temp[a[i]]++; System.arraycopy(temp, 0, icnt[i], 0, 3); } temp = new int[3]; for (int k = n - 1; k >= 0; k--) { if (chars[k] == 'X') temp[a[k]]++; System.arraycopy(temp, 0, kcnt[k], 0, 3); } long res = 0; int[] mex = new int[]{0, 1, 0, 2, 0, 1, 0, 3}; for (int j = 0; j < n; j++) { if (chars[j] != 'E') continue; for (int t = 0; t < 9; t++) { int i = t / 3, k = t % 3; if (icnt[j][i] == 0 || kcnt[j][k] == 0) continue; res += mex[1 << a[j] | 1 << i | 1 << k] * icnt[j][i] * kcnt[j][k]; } } return res; } } import java.io.*; import java.util.*; /** * https://atcoder.jp/contests/abc308/tasks/abc308_e */ public class Main { private static final Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); private static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); String s = sc.next(); out.println(solve(n, a, s)); out.flush(); } private static long solve(int n, int[] a, String s) { char[] chars = s.toCharArray(); int[][] icnt = new int[n][3]; int[][] kcnt = new int[n][3]; int[] temp = new int[3]; for (int i = 0; i < n; i++) { if (chars[i] == 'M') temp[a[i]]++; System.arraycopy(temp, 0, icnt[i], 0, 3); } temp = new int[3]; for (int k = n - 1; k >= 0; k--) { if (chars[k] == 'X') temp[a[k]]++; System.arraycopy(temp, 0, kcnt[k], 0, 3); } long res = 0; long[] mex = new long[]{0, 1, 0, 2, 0, 1, 0, 3}; for (int j = 0; j < n; j++) { if (chars[j] != 'E') continue; for (int t = 0; t < 9; t++) { int i = t / 3, k = t % 3; if (icnt[j][i] == 0 || kcnt[j][k] == 0) continue; res += mex[1 << a[j] | 1 << i | 1 << k] * icnt[j][i] * kcnt[j][k]; } } return res; } }
ConDefects/ConDefects/Code/abc308_e/Java/45257180
condefects-java_data_2017
import java.io.*; import java.util.*; /** * https://atcoder.jp/contests/abc308/tasks/abc308_e */ public class Main { private static final Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); private static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); String s = sc.next(); out.println(solve(n, a, s)); out.flush(); } private static long solve(int n, int[] a, String s) { char[] chars = s.toCharArray(); int[][] icnt = new int[n][3]; int[][] kcnt = new int[n][3]; int[] temp = new int[3]; for (int i = 0; i < n; i++) { if (chars[i] == 'M') temp[a[i]]++; System.arraycopy(temp, 0, icnt[i], 0, 3); } temp = new int[3]; for (int k = n - 1; k >= 0; k--) { if (chars[k] == 'X') temp[a[k]]++; System.arraycopy(temp, 0, kcnt[k], 0, 3); } long res = 0; for (int j = 0; j < n; j++) { if (chars[j] != 'E') continue; for (int t = 0; t < 9; t++) { int i = t / 3, k = t % 3; if (icnt[j][i] == 0 || kcnt[j][k] == 0) continue; for (int v = 0; v <= 3; v++) { if (v != a[j] && v != i && v != k) { res += v * icnt[j][i] * kcnt[j][k]; break; } } } } return res; } } import java.io.*; import java.util.*; /** * https://atcoder.jp/contests/abc308/tasks/abc308_e */ public class Main { private static final Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); private static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); String s = sc.next(); out.println(solve(n, a, s)); out.flush(); } private static long solve(int n, int[] a, String s) { char[] chars = s.toCharArray(); int[][] icnt = new int[n][3]; int[][] kcnt = new int[n][3]; int[] temp = new int[3]; for (int i = 0; i < n; i++) { if (chars[i] == 'M') temp[a[i]]++; System.arraycopy(temp, 0, icnt[i], 0, 3); } temp = new int[3]; for (int k = n - 1; k >= 0; k--) { if (chars[k] == 'X') temp[a[k]]++; System.arraycopy(temp, 0, kcnt[k], 0, 3); } long res = 0; for (int j = 0; j < n; j++) { if (chars[j] != 'E') continue; for (int t = 0; t < 9; t++) { int i = t / 3, k = t % 3; if (icnt[j][i] == 0 || kcnt[j][k] == 0) continue; for (long v = 0; v <= 3; v++) { if (v != a[j] && v != i && v != k) { res += v * icnt[j][i] * kcnt[j][k]; break; } } } } return res; } }
ConDefects/ConDefects/Code/abc308_e/Java/45257066
condefects-java_data_2018
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } String s = sc.next(); //贡献法,计算每个 E 的贡献 int[][] left = new int[2][3]; int[][] right = new int[2][3]; int[][][] calc = new int[3][3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { int t = 0; while (t == i || t == j || t == k) t++; calc[i][j][k] = t; } } } char[] chars = s.toCharArray(); for (int i = 0; i < n; i++) { if (chars[i] == 'M') { right[0][a[i]]++; } else if (chars[i] == 'X') { right[1][a[i]]++; } } long ans = 0; for (int i = 0; i < n; i++) { if (chars[i] == 'M') { right[0][a[i]]--; left[0][a[i]]++; } else if (chars[i] == 'X') { right[1][a[i]]--; left[1][a[i]]--; } else { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { ans += left[0][j] * right[1][k] * calc[a[i]][j][k]; } } } } System.out.println(ans); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } String s = sc.next(); //贡献法,计算每个 E 的贡献 int[][] left = new int[2][3]; int[][] right = new int[2][3]; int[][][] calc = new int[3][3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { int t = 0; while (t == i || t == j || t == k) t++; calc[i][j][k] = t; } } } char[] chars = s.toCharArray(); for (int i = 0; i < n; i++) { if (chars[i] == 'M') { right[0][a[i]]++; } else if (chars[i] == 'X') { right[1][a[i]]++; } } long ans = 0; for (int i = 0; i < n; i++) { if (chars[i] == 'M') { right[0][a[i]]--; left[0][a[i]]++; } else if (chars[i] == 'X') { right[1][a[i]]--; left[1][a[i]]--; } else { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { ans +=(long) left[0][j] * right[1][k] * calc[a[i]][j][k]; } } } } System.out.println(ans); } }
ConDefects/ConDefects/Code/abc308_e/Java/45265732
condefects-java_data_2019
//package com.example.practice.codeforces.below2000; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.StringTokenizer; //atcoder: F - Rook Score public class Main { public static void main(String [] args) throws IOException { // Use BufferedReader rather than RandomAccessFile; it's much faster final BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); final PrintWriter out = new PrintWriter(System.out); // input file name goes above int Q = 1;//Integer.parseInt(input.readLine()); while (Q > 0) { StringTokenizer st = new StringTokenizer(input.readLine()); final int n = Integer.parseInt(st.nextToken()); final int[][] ns = readArray2DInt(n, n, input); out.println(calc(n, ns)); Q--; } out.close(); // close the output file } private static long calc(final int n, final int[][] ns) { HashMap<Integer, Long> mapx = new HashMap<>(), mapy = new HashMap<>(); HashMap<Integer, HashSet<Integer>> mpx = new HashMap<>(); ArrayDeque<Integer> ll = new ArrayDeque<>(); for (int[] po : ns){ if (!mpx.containsKey(po[0])){ HashSet<Integer> set = new HashSet<>(); set.add(po[1]); mapx.put(po[0], (long)po[2]); mpx.put(po[0], set); }else { mapx.put(po[0], mapx.get(po[0])+po[2]); mpx.get(po[0]).add(po[1]); } if (!mapy.containsKey(po[1])){ mapy.put(po[1], (long)po[2]); ll.add(po[1]); }else { mapy.put(po[1], mapy.get(po[1])+po[2]); } } long res = 0; for (int[] po : ns){ res = Math.max(res, mapx.get(po[0]) + mapy.get(po[1]) - po[2]); } long[][] rd = new long[mapx.size()][]; int p = 0; for (int k : mapx.keySet()){ rd[p++] = new long[]{k, mapx.get(k)}; } Arrays.sort(rd, (a,b) -> (Long.compare(b[0], a[0]))); for (long[] po : rd){ int k = (int) po[0], len = ll.size(); HashSet<Integer> set = mpx.get(k); for (int i=1;i<=len;++i){ int y = ll.poll(); if (set.contains(y)){ ll.add(y); }else { res = Math.max(res, po[1] + mapy.get(y)); } } } return res; } private static void printArray(long[] ns, final PrintWriter out){ for (int i=0;i<ns.length;++i){ out.print(ns[i]); if (i+1<ns.length)out.print(" "); else out.println(); } } private static void printArrayInt(int[] ns, final PrintWriter out){ for (int i=0;i<ns.length;++i){ out.print(ns[i]); if (i+1<ns.length)out.print(" "); else out.println(); } } private static void printArrayVertical(long[] ns, final PrintWriter out){ for (long a : ns){ out.println(a); } } private static void printArrayVerticalInt(int[] ns, final PrintWriter out){ for (int a : ns){ out.println(a); } } private static void printArray2D(long[][] ns, final int len, final PrintWriter out){ int cnt = 0; for (long[] kk : ns){ cnt++; if (cnt > len)break; for (int i=0;i<kk.length;++i){ out.print(kk[i]); if (i+1<kk.length)out.print(" "); else out.println(); } } } private static void printArray2DInt(int[][] ns, final int len, final PrintWriter out){ int cnt = 0; for (int[] kk : ns){ cnt++; if (cnt > len)break; for (int i=0;i<kk.length;++i){ out.print(kk[i]); if (i+1<kk.length)out.print(" "); else out.println(); } } } private static long[] readArray(final int n, final BufferedReader input) throws IOException{ long[] ns = new long[n]; StringTokenizer st = new StringTokenizer(input.readLine()); for (int i=0;i<n;++i){ ns[i] = Long.parseLong(st.nextToken()); } return ns; } private static int[] readArrayInt(final int n, final BufferedReader input) throws IOException{ int[] ns = new int[n]; StringTokenizer st = new StringTokenizer(input.readLine()); for (int i=0;i<n;++i){ ns[i] = Integer.parseInt(st.nextToken()); } return ns; } private static long[] readArrayVertical(final int n, final BufferedReader input) throws IOException{ long[] ns = new long[n]; for (int i=0;i<n;++i){ ns[i] = Long.parseLong(input.readLine()); } return ns; } private static int[] readArrayVerticalInt(final int n, final BufferedReader input) throws IOException{ int[] ns = new int[n]; for (int i=0;i<n;++i){ ns[i] = Integer.parseInt(input.readLine()); } return ns; } private static long[][] readArray2D(final int n, final int len, final BufferedReader input) throws IOException{ long[][] ns = new long[len][]; for (int i=0;i<n;++i){ StringTokenizer st = new StringTokenizer(input.readLine()); ArrayList<Long> al = new ArrayList<>(); while (st.hasMoreTokens()){ al.add(Long.parseLong(st.nextToken())); } long[] kk = new long[al.size()]; for (int j=0;j<kk.length;++j){ kk[j] = al.get(j); } ns[i] = kk; } return ns; } private static int[][] readArray2DInt(final int n, final int len, final BufferedReader input) throws IOException{ int[][] ns = new int[len][]; for (int i=0;i<n;++i){ StringTokenizer st = new StringTokenizer(input.readLine()); ArrayList<Integer> al = new ArrayList<>(); while (st.hasMoreTokens()){ al.add(Integer.parseInt(st.nextToken())); } int[] kk = new int[al.size()]; for (int j=0;j<kk.length;++j){ kk[j] = al.get(j); } ns[i] = kk; } return ns; } private static int GCD(int x, int y){ if (x > y)return GCD(y, x); if (x==0)return y; return GCD(y%x, x); } private static long GCD(long x, long y){ if (x > y)return GCD(y, x); if (x==0)return y; return GCD(y%x, x); } private static ArrayList<int[]>[] convertToGraphUnDirectWithWeight(final int n, final int[][] es){ ArrayList<int[]>[] als = new ArrayList[n+1]; for (int i=0;i<=n;++i){ als[i] = new ArrayList<>(); } for (int[] e : es){ als[e[0]].add(new int[]{e[1], e[2]}); als[e[1]].add(new int[]{e[0], e[2]}); } return als; } private static ArrayList<int[]>[] convertToGraphDirectWithWeight(final int n, final int[][] es){ ArrayList<int[]>[] als = new ArrayList[n+1]; for (int i=0;i<=n;++i){ als[i] = new ArrayList<>(); } for (int[] e : es){ als[e[0]].add(new int[]{e[1], e[2]}); } return als; } private static ArrayList<Integer>[] convertToGraphUnDirect(final int n, final int[][] es){ ArrayList<Integer>[] als = new ArrayList[n+1]; for (int i=0;i<=n;++i){ als[i] = new ArrayList<>(); } for (int[] e : es){ als[e[0]].add(e[1]); als[e[1]].add(e[0]); } return als; } private static ArrayList<Integer>[] convertToGraphDirect(final int n, final int[][] es){ ArrayList<Integer>[] als = new ArrayList[n+1]; for (int i=0;i<=n;++i){ als[i] = new ArrayList<>(); } for (int[] e : es){ als[e[0]].add(e[1]); } return als; } private static int find(final int[] rd, int idx){ while (idx != rd[idx]){ rd[idx] = rd[rd[idx]]; idx = rd[idx]; } return idx; } private static long pow(final long a, final long x, final int MODE){ if (x==0)return 1L; long res = pow(a, x>>1, MODE); res = res * res % MODE; if ((x&1) == 1){ res = res * a % MODE; } return res; } } //package com.example.practice.codeforces.below2000; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.StringTokenizer; //atcoder: F - Rook Score public class Main { public static void main(String [] args) throws IOException { // Use BufferedReader rather than RandomAccessFile; it's much faster final BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); final PrintWriter out = new PrintWriter(System.out); // input file name goes above int Q = 1;//Integer.parseInt(input.readLine()); while (Q > 0) { StringTokenizer st = new StringTokenizer(input.readLine()); final int n = Integer.parseInt(st.nextToken()); final int[][] ns = readArray2DInt(n, n, input); out.println(calc(n, ns)); Q--; } out.close(); // close the output file } private static long calc(final int n, final int[][] ns) { HashMap<Integer, Long> mapx = new HashMap<>(), mapy = new HashMap<>(); HashMap<Integer, HashSet<Integer>> mpx = new HashMap<>(); ArrayDeque<Integer> ll = new ArrayDeque<>(); for (int[] po : ns){ if (!mpx.containsKey(po[0])){ HashSet<Integer> set = new HashSet<>(); set.add(po[1]); mapx.put(po[0], (long)po[2]); mpx.put(po[0], set); }else { mapx.put(po[0], mapx.get(po[0])+po[2]); mpx.get(po[0]).add(po[1]); } if (!mapy.containsKey(po[1])){ mapy.put(po[1], (long)po[2]); ll.add(po[1]); }else { mapy.put(po[1], mapy.get(po[1])+po[2]); } } long res = 0; for (int[] po : ns){ res = Math.max(res, mapx.get(po[0]) + mapy.get(po[1]) - po[2]); } long[][] rd = new long[mapx.size()][]; int p = 0; for (int k : mapx.keySet()){ rd[p++] = new long[]{k, mapx.get(k)}; } Arrays.sort(rd, (a,b) -> (Long.compare(b[1], a[1]))); for (long[] po : rd){ int k = (int) po[0], len = ll.size(); HashSet<Integer> set = mpx.get(k); for (int i=1;i<=len;++i){ int y = ll.poll(); if (set.contains(y)){ ll.add(y); }else { res = Math.max(res, po[1] + mapy.get(y)); } } } return res; } private static void printArray(long[] ns, final PrintWriter out){ for (int i=0;i<ns.length;++i){ out.print(ns[i]); if (i+1<ns.length)out.print(" "); else out.println(); } } private static void printArrayInt(int[] ns, final PrintWriter out){ for (int i=0;i<ns.length;++i){ out.print(ns[i]); if (i+1<ns.length)out.print(" "); else out.println(); } } private static void printArrayVertical(long[] ns, final PrintWriter out){ for (long a : ns){ out.println(a); } } private static void printArrayVerticalInt(int[] ns, final PrintWriter out){ for (int a : ns){ out.println(a); } } private static void printArray2D(long[][] ns, final int len, final PrintWriter out){ int cnt = 0; for (long[] kk : ns){ cnt++; if (cnt > len)break; for (int i=0;i<kk.length;++i){ out.print(kk[i]); if (i+1<kk.length)out.print(" "); else out.println(); } } } private static void printArray2DInt(int[][] ns, final int len, final PrintWriter out){ int cnt = 0; for (int[] kk : ns){ cnt++; if (cnt > len)break; for (int i=0;i<kk.length;++i){ out.print(kk[i]); if (i+1<kk.length)out.print(" "); else out.println(); } } } private static long[] readArray(final int n, final BufferedReader input) throws IOException{ long[] ns = new long[n]; StringTokenizer st = new StringTokenizer(input.readLine()); for (int i=0;i<n;++i){ ns[i] = Long.parseLong(st.nextToken()); } return ns; } private static int[] readArrayInt(final int n, final BufferedReader input) throws IOException{ int[] ns = new int[n]; StringTokenizer st = new StringTokenizer(input.readLine()); for (int i=0;i<n;++i){ ns[i] = Integer.parseInt(st.nextToken()); } return ns; } private static long[] readArrayVertical(final int n, final BufferedReader input) throws IOException{ long[] ns = new long[n]; for (int i=0;i<n;++i){ ns[i] = Long.parseLong(input.readLine()); } return ns; } private static int[] readArrayVerticalInt(final int n, final BufferedReader input) throws IOException{ int[] ns = new int[n]; for (int i=0;i<n;++i){ ns[i] = Integer.parseInt(input.readLine()); } return ns; } private static long[][] readArray2D(final int n, final int len, final BufferedReader input) throws IOException{ long[][] ns = new long[len][]; for (int i=0;i<n;++i){ StringTokenizer st = new StringTokenizer(input.readLine()); ArrayList<Long> al = new ArrayList<>(); while (st.hasMoreTokens()){ al.add(Long.parseLong(st.nextToken())); } long[] kk = new long[al.size()]; for (int j=0;j<kk.length;++j){ kk[j] = al.get(j); } ns[i] = kk; } return ns; } private static int[][] readArray2DInt(final int n, final int len, final BufferedReader input) throws IOException{ int[][] ns = new int[len][]; for (int i=0;i<n;++i){ StringTokenizer st = new StringTokenizer(input.readLine()); ArrayList<Integer> al = new ArrayList<>(); while (st.hasMoreTokens()){ al.add(Integer.parseInt(st.nextToken())); } int[] kk = new int[al.size()]; for (int j=0;j<kk.length;++j){ kk[j] = al.get(j); } ns[i] = kk; } return ns; } private static int GCD(int x, int y){ if (x > y)return GCD(y, x); if (x==0)return y; return GCD(y%x, x); } private static long GCD(long x, long y){ if (x > y)return GCD(y, x); if (x==0)return y; return GCD(y%x, x); } private static ArrayList<int[]>[] convertToGraphUnDirectWithWeight(final int n, final int[][] es){ ArrayList<int[]>[] als = new ArrayList[n+1]; for (int i=0;i<=n;++i){ als[i] = new ArrayList<>(); } for (int[] e : es){ als[e[0]].add(new int[]{e[1], e[2]}); als[e[1]].add(new int[]{e[0], e[2]}); } return als; } private static ArrayList<int[]>[] convertToGraphDirectWithWeight(final int n, final int[][] es){ ArrayList<int[]>[] als = new ArrayList[n+1]; for (int i=0;i<=n;++i){ als[i] = new ArrayList<>(); } for (int[] e : es){ als[e[0]].add(new int[]{e[1], e[2]}); } return als; } private static ArrayList<Integer>[] convertToGraphUnDirect(final int n, final int[][] es){ ArrayList<Integer>[] als = new ArrayList[n+1]; for (int i=0;i<=n;++i){ als[i] = new ArrayList<>(); } for (int[] e : es){ als[e[0]].add(e[1]); als[e[1]].add(e[0]); } return als; } private static ArrayList<Integer>[] convertToGraphDirect(final int n, final int[][] es){ ArrayList<Integer>[] als = new ArrayList[n+1]; for (int i=0;i<=n;++i){ als[i] = new ArrayList<>(); } for (int[] e : es){ als[e[0]].add(e[1]); } return als; } private static int find(final int[] rd, int idx){ while (idx != rd[idx]){ rd[idx] = rd[rd[idx]]; idx = rd[idx]; } return idx; } private static long pow(final long a, final long x, final int MODE){ if (x==0)return 1L; long res = pow(a, x>>1, MODE); res = res * res % MODE; if ((x&1) == 1){ res = res * a % MODE; } return res; } }
ConDefects/ConDefects/Code/abc298_f/Java/44886467
condefects-java_data_2020
import java.io.*; import java.util.*; public class Main { public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353; public static void main(String args[]){ try { PrintWriter o = new PrintWriter(System.out); boolean multiTest = false; // init if(multiTest) { int t = fReader.nextInt(), loop = 0; while (loop < t) {loop++;solve(o);} } else solve(o); o.close(); } catch (Exception e) {e.printStackTrace();} } static void solve(PrintWriter o) { try { int n = fReader.nextInt(); int[] D = new int[n]; for(int i=0;i<n;i++) D[i] = fReader.nextInt(); int[] L = new int[2]; int[] C = new int[2]; int[] K = new int[2]; for(int i=0;i<2;i++) { L[i] = fReader.nextInt(); C[i] = fReader.nextInt(); K[i] = fReader.nextInt(); } int[] dp = new int[K[0]+1]; Arrays.fill(dp, Integer.MAX_VALUE); dp[0] = 0; for(int i=0;i<n;i++) { int[] ndp = new int[K[0]+1]; Arrays.fill(ndp, Integer.MAX_VALUE); for(int j=0;j<=K[0];j++) { if(dp[j] == Integer.MAX_VALUE) continue; for (int u = 0; u <= (D[i] + L[0] - 1) / L[0]; u++) { int v = (Math.max(0, D[i] - u * L[0]) + L[1] - 1) / L[1]; if(j+u <= K[0]) ndp[j+u] = Math.min(ndp[j+u], dp[j]+v); } } dp = ndp; } long res = 1l<<60; for(int i=0;i<=K[0];i++) { if(dp[i] >= K[1]) continue; res = Math.min(res, 1l*i*C[0]+1l*dp[i]*C[1]); } o.println(res >= 1l<<60 ? -1 : res); } catch (Exception e) { e.printStackTrace(); } } public static int upper_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) <= val) l = mid + 1; else r = mid; } return l; } public static int lower_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) < val) l = mid + 1; else r = mid; } return l; } public static long gcd(long a, long b){ return b == 0 ? a : gcd(b, a%b); } public static long lcm(long a, long b){ return a / gcd(a,b)*b; } public static long qpow(long a, long n, int md){ a %= md; long ret = 1l; while(n > 0){ if((n & 1) == 1){ ret = ret * a % md; } n >>= 1; a = a * a % md; } return ret; } public static class DSU { int[] parent; int[] size; int n; public DSU(int n){ this.n = n; parent = new int[n]; size = new int[n]; for(int i=0;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int p){ while(parent[p] != p){ parent[p] = parent[parent[p]]; p = parent[p]; } return p; } public void union(int p, int q){ int root_p = find(p); int root_q = find(q); if(root_p == root_q) return; if(size[root_p] >= size[root_q]){ parent[root_q] = root_p; size[root_p] += size[root_q]; size[root_q] = 0; } else{ parent[root_p] = root_q; size[root_q] += size[root_p]; size[root_p] = 0; } n--; } public int getTotalComNum(){ return n; } public int getSize(int i){ return size[find(i)]; } } public static class FenWick { int n; long[] a; long[] tree; public FenWick(int n){ this.n = n; a = new long[n+1]; tree = new long[n+1]; } private void add(int x, long val){ while(x <= n){ tree[x] += val; x += x&-x; } } private void addMx(int x, long val) { a[x] += val; tree[x] = a[x]; while(x <= n) { for(int i=1;i<(x&-x);i<<=1) { tree[x] = Math.max(tree[x], tree[x-i]); } x += x&-x; } } private long query(int x){ long ret = 0l; while(x > 0){ ret += tree[x]; x -= x&-x; } return ret; } private long queryMx(int l, int r) { long res = 0l; while(l <= r) { if(r-(r&-r) >= l) { res = Math.max(res, tree[r]); r -= r&-r; } else { res = Math.max(res, a[r]); r--; } } return res; } } public static class Pair{ Integer c1; Integer c2; Integer c3; Integer c4; public Pair(Integer c1, Integer c2, Integer c3, Integer c4) { this.c1 = c1; this.c2 = c2; this.c3 = c3; this.c4 = c4; } @Override public int hashCode() { int prime = 31, ret = 1; ret = ret*prime + c1.hashCode(); ret = ret*prime + c2.hashCode(); ret = ret*prime + c3.hashCode(); ret = ret*prime + c4.hashCode(); return ret; } @Override public boolean equals(Object obj) { if(obj instanceof Pair) { return c1.equals(((Pair) obj).c1) && c2.equals(((Pair) obj).c2) && c3.equals(((Pair) obj).c3) && c4.equals(((Pair) obj).c4); } return false; } } public static class fReader { private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tokenizer = new StringTokenizer(""); private static String next() throws IOException{ while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());} return tokenizer.nextToken(); } public static int nextInt() throws IOException {return Integer.parseInt(next());} public static Long nextLong() throws IOException {return Long.parseLong(next());} public static double nextDouble() throws IOException {return Double.parseDouble(next());} public static char nextChar() throws IOException {return next().toCharArray()[0];} public static String nextString() throws IOException {return next();} public static String nextLine() throws IOException {return reader.readLine();} } } import java.io.*; import java.util.*; public class Main { public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353; public static void main(String args[]){ try { PrintWriter o = new PrintWriter(System.out); boolean multiTest = false; // init if(multiTest) { int t = fReader.nextInt(), loop = 0; while (loop < t) {loop++;solve(o);} } else solve(o); o.close(); } catch (Exception e) {e.printStackTrace();} } static void solve(PrintWriter o) { try { int n = fReader.nextInt(); int[] D = new int[n]; for(int i=0;i<n;i++) D[i] = fReader.nextInt(); int[] L = new int[2]; int[] C = new int[2]; int[] K = new int[2]; for(int i=0;i<2;i++) { L[i] = fReader.nextInt(); C[i] = fReader.nextInt(); K[i] = fReader.nextInt(); } int[] dp = new int[K[0]+1]; Arrays.fill(dp, Integer.MAX_VALUE); dp[0] = 0; for(int i=0;i<n;i++) { int[] ndp = new int[K[0]+1]; Arrays.fill(ndp, Integer.MAX_VALUE); for(int j=0;j<=K[0];j++) { if(dp[j] == Integer.MAX_VALUE) continue; for (int u = 0; u <= (D[i] + L[0] - 1) / L[0]; u++) { int v = (Math.max(0, D[i] - u * L[0]) + L[1] - 1) / L[1]; if(j+u <= K[0]) ndp[j+u] = Math.min(ndp[j+u], dp[j]+v); } } dp = ndp; } long res = 1l<<60; for(int i=0;i<=K[0];i++) { if(dp[i] > K[1]) continue; res = Math.min(res, 1l*i*C[0]+1l*dp[i]*C[1]); } o.println(res >= 1l<<60 ? -1 : res); } catch (Exception e) { e.printStackTrace(); } } public static int upper_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) <= val) l = mid + 1; else r = mid; } return l; } public static int lower_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) < val) l = mid + 1; else r = mid; } return l; } public static long gcd(long a, long b){ return b == 0 ? a : gcd(b, a%b); } public static long lcm(long a, long b){ return a / gcd(a,b)*b; } public static long qpow(long a, long n, int md){ a %= md; long ret = 1l; while(n > 0){ if((n & 1) == 1){ ret = ret * a % md; } n >>= 1; a = a * a % md; } return ret; } public static class DSU { int[] parent; int[] size; int n; public DSU(int n){ this.n = n; parent = new int[n]; size = new int[n]; for(int i=0;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int p){ while(parent[p] != p){ parent[p] = parent[parent[p]]; p = parent[p]; } return p; } public void union(int p, int q){ int root_p = find(p); int root_q = find(q); if(root_p == root_q) return; if(size[root_p] >= size[root_q]){ parent[root_q] = root_p; size[root_p] += size[root_q]; size[root_q] = 0; } else{ parent[root_p] = root_q; size[root_q] += size[root_p]; size[root_p] = 0; } n--; } public int getTotalComNum(){ return n; } public int getSize(int i){ return size[find(i)]; } } public static class FenWick { int n; long[] a; long[] tree; public FenWick(int n){ this.n = n; a = new long[n+1]; tree = new long[n+1]; } private void add(int x, long val){ while(x <= n){ tree[x] += val; x += x&-x; } } private void addMx(int x, long val) { a[x] += val; tree[x] = a[x]; while(x <= n) { for(int i=1;i<(x&-x);i<<=1) { tree[x] = Math.max(tree[x], tree[x-i]); } x += x&-x; } } private long query(int x){ long ret = 0l; while(x > 0){ ret += tree[x]; x -= x&-x; } return ret; } private long queryMx(int l, int r) { long res = 0l; while(l <= r) { if(r-(r&-r) >= l) { res = Math.max(res, tree[r]); r -= r&-r; } else { res = Math.max(res, a[r]); r--; } } return res; } } public static class Pair{ Integer c1; Integer c2; Integer c3; Integer c4; public Pair(Integer c1, Integer c2, Integer c3, Integer c4) { this.c1 = c1; this.c2 = c2; this.c3 = c3; this.c4 = c4; } @Override public int hashCode() { int prime = 31, ret = 1; ret = ret*prime + c1.hashCode(); ret = ret*prime + c2.hashCode(); ret = ret*prime + c3.hashCode(); ret = ret*prime + c4.hashCode(); return ret; } @Override public boolean equals(Object obj) { if(obj instanceof Pair) { return c1.equals(((Pair) obj).c1) && c2.equals(((Pair) obj).c2) && c3.equals(((Pair) obj).c3) && c4.equals(((Pair) obj).c4); } return false; } } public static class fReader { private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tokenizer = new StringTokenizer(""); private static String next() throws IOException{ while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());} return tokenizer.nextToken(); } public static int nextInt() throws IOException {return Integer.parseInt(next());} public static Long nextLong() throws IOException {return Long.parseLong(next());} public static double nextDouble() throws IOException {return Double.parseDouble(next());} public static char nextChar() throws IOException {return next().toCharArray()[0];} public static String nextString() throws IOException {return next();} public static String nextLine() throws IOException {return reader.readLine();} } }
ConDefects/ConDefects/Code/abc325_f/Java/46962459
condefects-java_data_2021
import java.util.*; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] dist = new int[n + 1]; for(int i = 1;i <= n;i++) { dist[i] = sc.nextInt(); }int[] range = new int[2]; int[] cost = new int[2]; int[] max = new int[2]; for(int i = 0;i< 2;i++) { range[i] =sc.nextInt(); cost[i] = sc.nextInt(); max[i] = sc.nextInt(); }int[][] dp = new int[n + 1][max[0] + 1]; for(int i = 0;i <= n;i++) { for(int j = 0;j <= max[0];j++) { dp[i][j] = max[1] + 1; } }dp[0][0] = 0; for(int i = 1;i <= n;i++) { int maxPlus = Math.min(max[0],(dist[i] + range[0] - 1)/range[0]); for(int j = 0;j <= max[0];j++) { if(dp[i - 1][j] == -1) { continue; } for(int k = 0;k <= maxPlus;k++) { if(j + k > max[0]) { continue; }int addCencer = (dist[i] - range[0] * k + range[1] - 1)/range[1]; dp[i][j + k] = Math.min(dp[i][j + k],dp[i - 1][j] + addCencer); } } }long ans = -1; for(int i = 0;i <= max[0];i++) { if(dp[n][i] <= max[1]) { long tmp = (long)i * (long)cost[0] + dp[n][i] * cost[1]; if(ans == -1 || ans > tmp) { ans = tmp; } } }System.out.print(ans); } } import java.util.*; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] dist = new int[n + 1]; for(int i = 1;i <= n;i++) { dist[i] = sc.nextInt(); }int[] range = new int[2]; int[] cost = new int[2]; int[] max = new int[2]; for(int i = 0;i< 2;i++) { range[i] =sc.nextInt(); cost[i] = sc.nextInt(); max[i] = sc.nextInt(); }int[][] dp = new int[n + 1][max[0] + 1]; for(int i = 0;i <= n;i++) { for(int j = 0;j <= max[0];j++) { dp[i][j] = max[1] + 1; } }dp[0][0] = 0; for(int i = 1;i <= n;i++) { int maxPlus = Math.min(max[0],(dist[i] + range[0] - 1)/range[0]); for(int j = 0;j <= max[0];j++) { if(dp[i - 1][j] == -1) { continue; } for(int k = 0;k <= maxPlus;k++) { if(j + k > max[0]) { continue; }int addCencer = (dist[i] - range[0] * k + range[1] - 1)/range[1]; dp[i][j + k] = Math.min(dp[i][j + k],dp[i - 1][j] + addCencer); } } }long ans = -1; for(int i = 0;i <= max[0];i++) { if(dp[n][i] <= max[1]) { long tmp = (long)i * (long)cost[0] + (long)dp[n][i] * (long)cost[1]; if(ans == -1 || ans > tmp) { ans = tmp; } } }System.out.print(ans); } }
ConDefects/ConDefects/Code/abc325_f/Java/46978700
condefects-java_data_2022
import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int p = sc.nextInt()-1; int q = sc.nextInt()-1; int r = sc.nextInt()-1; int s = sc.nextInt()-1; int[] li = new int[n]; int[] li2 = new int[n]; for (int i=0;i<n;i++){ int a = sc.nextInt(); li[i] = a; li2[i] = a; } for (int i = 0;i< q-p; i++){ li2[p+i] = li[r+i]; li2[r+i] = li[p+i]; } for (int i = 0;i< n; i++){ System.out.print(li2[i]+" "); } } } import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int p = sc.nextInt()-1; int q = sc.nextInt()-1; int r = sc.nextInt()-1; int s = sc.nextInt()-1; int[] li = new int[n]; int[] li2 = new int[n]; for (int i=0;i<n;i++){ int a = sc.nextInt(); li[i] = a; li2[i] = a; } for (int i = 0;i<= q-p; i++){ li2[p+i] = li[r+i]; li2[r+i] = li[p+i]; } for (int i = 0;i< n; i++){ System.out.print(li2[i]+" "); } } }
ConDefects/ConDefects/Code/abc286_a/Java/42265974
condefects-java_data_2023
import java.util.*; public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String S = scanner.nextLine(); boolean[] flag = new boolean[10]; for(int i = 0;i<10;i++){ flag[i] = true; } for(int i = 0;i<9;i++){ flag[Character.getNumericValue(S.charAt(i))] = false; } for(int i = 0;i<10;i++){ if(flag[i]==false){ System.out.println(i); } } scanner.close(); } } import java.util.*; public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String S = scanner.nextLine(); boolean[] flag = new boolean[10]; for(int i = 0;i<10;i++){ flag[i] = true; } for(int i = 0;i<9;i++){ flag[Character.getNumericValue(S.charAt(i))] = false; } for(int i = 0;i<10;i++){ if(flag[i]){ System.out.println(i); } } scanner.close(); } }
ConDefects/ConDefects/Code/abc248_a/Java/45712643
condefects-java_data_2024
import java.util.*; import java.io.*; import java.math.*; class Main{ public static final int [] x8 = {0 , 1,1,1,0,-1,-1,-1}; public static final int [] y8 = {-1,-1,0,1,1, 1, 0,-1}; public static final int [] y4 = {0,1,0,-1}; public static final int [] x4 = {1,0,-1,0}; public static final int MOD = 1000000007; public static final int INF = Integer.MAX_VALUE; public static void main(String[] args) throws IOException { PrintWriter output = new PrintWriter(System.out); Scanner sc = new Scanner(System.in); BufferedReader buff = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; //st = new StringTokenizer(buff.readLine()); String s = sc.next(); for(int i=1;i<=9;i++){ boolean ok = false; for(int j=0;j<s.length();j++){ if(Integer.parseInt(s.substring(j,j+1)) == i) ok = true; } if(!ok) output.println(i); } output.flush(); } } import java.util.*; import java.io.*; import java.math.*; class Main{ public static final int [] x8 = {0 , 1,1,1,0,-1,-1,-1}; public static final int [] y8 = {-1,-1,0,1,1, 1, 0,-1}; public static final int [] y4 = {0,1,0,-1}; public static final int [] x4 = {1,0,-1,0}; public static final int MOD = 1000000007; public static final int INF = Integer.MAX_VALUE; public static void main(String[] args) throws IOException { PrintWriter output = new PrintWriter(System.out); Scanner sc = new Scanner(System.in); BufferedReader buff = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; //st = new StringTokenizer(buff.readLine()); String s = sc.next(); for(int i=0;i<=9;i++){ boolean ok = false; for(int j=0;j<s.length();j++){ if(Integer.parseInt(s.substring(j,j+1)) == i) ok = true; } if(!ok) output.println(i); } output.flush(); } }
ConDefects/ConDefects/Code/abc248_a/Java/36933799
condefects-java_data_2025
// 문제 설명 // 숫자로 구성된 길이가 정확히 9인 문자열 S가 주어진다. 0부터 9까지의 모든 숫자가 S에 정확히 한 번 나타난다. // S에서 누락된 유일한 숫자를 인쇄하라. // 제약 // S는 숫자로 구성된 길이 9의 문자열이다. // S의 모든 문자는 서로 다르다. // 입력 // 입력은 다음 형식의 표준입력으로 제공된다. // 출력 // S에서 누락된 유일한 숫자를 인쇄한다. import java.util.*; public class Main { public static void main (String[]args){ Scanner scn = new Scanner(System.in); String S = scn.nextLine(); // 길이 9 String checkS = "0123456789"; // 길이 10 int Array [] = new int [S.length()]; boolean checkArray [] = new boolean[checkS.length()]; for ( int x = 0 ; x < S.length() ; x++){ Array[x] = S.charAt(x); // Array[]에 char 0 ~9 까지 넣음(하나없음) } for ( int x = 0 ; x < 9 ; x++){ checkArray[Array[x]-'0'] = true; } for ( int x = 0 ; x < 9 ; x++){ if ( checkArray[x] == false){ System.out.println(x); break; } } // for ( int x = 0 ; x < checkS.length(); x++){ // if ( checkArray[x] = false){ // System.out.println(x); // } // } // for ( int k : Array){ // System.out.println(k); // } // for ( boolean k : checkArray){ // System.out.println(k); // } } } // 문제 설명 // 숫자로 구성된 길이가 정확히 9인 문자열 S가 주어진다. 0부터 9까지의 모든 숫자가 S에 정확히 한 번 나타난다. // S에서 누락된 유일한 숫자를 인쇄하라. // 제약 // S는 숫자로 구성된 길이 9의 문자열이다. // S의 모든 문자는 서로 다르다. // 입력 // 입력은 다음 형식의 표준입력으로 제공된다. // 출력 // S에서 누락된 유일한 숫자를 인쇄한다. import java.util.*; public class Main { public static void main (String[]args){ Scanner scn = new Scanner(System.in); String S = scn.nextLine(); // 길이 9 String checkS = "0123456789"; // 길이 10 int Array [] = new int [S.length()]; boolean checkArray [] = new boolean[checkS.length()]; for ( int x = 0 ; x < S.length() ; x++){ Array[x] = S.charAt(x); // Array[]에 char 0 ~9 까지 넣음(하나없음) } for ( int x = 0 ; x < 9 ; x++){ checkArray[Array[x]-'0'] = true; } for ( int x = 0 ; x <= 9 ; x++){ if ( checkArray[x] == false){ System.out.println(x); break; } } // for ( int x = 0 ; x < checkS.length(); x++){ // if ( checkArray[x] = false){ // System.out.println(x); // } // } // for ( int k : Array){ // System.out.println(k); // } // for ( boolean k : checkArray){ // System.out.println(k); // } } }
ConDefects/ConDefects/Code/abc248_a/Java/45465589
condefects-java_data_2026
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { JS sc = new JS(); PrintWriter out = new PrintWriter(System.out); //int T = sc.nextInt(); int T = 1; for(int tt = 0; tt < T; tt++) { int n = sc.nextInt(); HashMap<Integer, Integer> hm = new HashMap<>(); int[][] arr = new int[n][2]; for(int i = 0; i < n; i++) { arr[i][0] = sc.nextInt(); arr[i][1] = sc.nextInt(); } String dir = sc.nextLine(); boolean collides = false; for(int i = 0; i < n; i++) { if(!hm.containsKey(arr[i][1]) && dir.charAt(i) == 'R') { hm.put(arr[i][1], arr[i][0]); } else if(hm.containsKey(arr[i][1])) { if(arr[i][0] < hm.get(arr[i][1])) hm.put(arr[i][1], arr[i][0]); } } for(int i = 0; i < n; i++) { if(hm.containsKey(arr[i][1])) { if(dir.charAt(i) == 'L' && arr[i][0] > hm.get(arr[i][1])) collides = true; } } if(collides) out.println("Yes"); else out.println("No"); } out.close(); } static class JS { public int BS = 1<<16; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/num; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c!='\n') { res.append(c); c=nextChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=nextChar(); if(c==NC)return false; else if(c>32)return true; } } } } import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { JS sc = new JS(); PrintWriter out = new PrintWriter(System.out); //int T = sc.nextInt(); int T = 1; for(int tt = 0; tt < T; tt++) { int n = sc.nextInt(); HashMap<Integer, Integer> hm = new HashMap<>(); int[][] arr = new int[n][2]; for(int i = 0; i < n; i++) { arr[i][0] = sc.nextInt(); arr[i][1] = sc.nextInt(); } String dir = sc.nextLine(); boolean collides = false; for(int i = 0; i < n; i++) { if(!hm.containsKey(arr[i][1]) && dir.charAt(i) == 'R') { hm.put(arr[i][1], arr[i][0]); } else if(hm.containsKey(arr[i][1]) && dir.charAt(i) == 'R') { if(arr[i][0] < hm.get(arr[i][1])) hm.put(arr[i][1], arr[i][0]); } } for(int i = 0; i < n; i++) { if(hm.containsKey(arr[i][1])) { if(dir.charAt(i) == 'L' && arr[i][0] > hm.get(arr[i][1])) collides = true; } } if(collides) out.println("Yes"); else out.println("No"); } out.close(); } static class JS { public int BS = 1<<16; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/num; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c!='\n') { res.append(c); c=nextChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=nextChar(); if(c==NC)return false; else if(c>32)return true; } } } }
ConDefects/ConDefects/Code/abc243_c/Java/39141878
condefects-java_data_2027
import java.util.Scanner; import java.util.TreeMap; // javac C.java && java C public class Main { static int N; static int[] X,Y; static char[] S; static TreeMap<Integer, Pair> map = new TreeMap<>(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); N = sc.nextInt(); X = new int[N]; Y = new int[N]; for(int i=0; i<N; i++) { X[i] = sc.nextInt(); Y[i] = sc.nextInt(); } S = sc.next().toCharArray(); sc.close(); for(int i=0; i<N; i++){ int y = Y[i]; if(!map.containsKey(y)) map.put(y, new Pair(-1,Integer.MAX_VALUE)); Pair p = map.get(y); if(S[i] == 'L'){ p.xl = Math.max(p.xl, X[i]); }else{ p.xr = Math.min(p.xl, X[i]); } if(p.xl > p.xr){ System.out.println("Yes"); return; } } System.out.println("No"); } static class Pair { int xl; int xr; public Pair(int a, int b){ this.xl = a; this.xr = b; } } } import java.util.Scanner; import java.util.TreeMap; // javac C.java && java C public class Main { static int N; static int[] X,Y; static char[] S; static TreeMap<Integer, Pair> map = new TreeMap<>(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); N = sc.nextInt(); X = new int[N]; Y = new int[N]; for(int i=0; i<N; i++) { X[i] = sc.nextInt(); Y[i] = sc.nextInt(); } S = sc.next().toCharArray(); sc.close(); for(int i=0; i<N; i++){ int y = Y[i]; if(!map.containsKey(y)) map.put(y, new Pair(-1,Integer.MAX_VALUE)); Pair p = map.get(y); if(S[i] == 'L'){ p.xl = Math.max(p.xl, X[i]); }else{ p.xr = Math.min(p.xr, X[i]); } if(p.xl > p.xr){ System.out.println("Yes"); return; } } System.out.println("No"); } static class Pair { int xl; int xr; public Pair(int a, int b){ this.xl = a; this.xr = b; } } }
ConDefects/ConDefects/Code/abc243_c/Java/36157274
condefects-java_data_2028
import java.util.Scanner; public class Main implements Runnable { public static boolean ans = false; public static int count = 0; public static void main(String[] args) { new Thread(null,new Main(),"",64 * 1024 * 1024).start(); }public void run() { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int h = sc.nextInt(); int w = sc.nextInt(); int startH = 0; int startW= 0; String s; char[][] field = new char[h][w]; boolean[][] visited = new boolean[h][w]; for(int i = 0;i < h;i++) { s = sc.next(); for(int j = 0;j < w;j++) { field[i][j] = s.charAt(j); if(s.charAt(j) == 'S') { startH = i; startW = j; } } } if(startH + 1 < h) { if(startW + 1 < w) { dfs(h,w,startH + 1,startW,field,visited,startH, startW + 1); }if(startW > 0) { visited = new boolean[h][w]; dfs(h,w,startH + 1,startW,field,visited,startH, startW - 1); }if(startH > 0) { visited = new boolean[h][w]; dfs(h,w,startH + 1,startW,field,visited,startH - 1, startW); } }if(startH > 0) { if(startW + 1 < w) { visited = new boolean[h][w]; dfs(h,w,startH - 1 ,startW,field,visited,startH, startW + 1); }if(startW > 0) { visited = new boolean[h][w]; dfs(h,w,startH - 1,startW,field,visited,startH, startW - 1); } }if(startW + 1 < w && startW > 0) { visited = new boolean[h][w]; dfs(h,w,startH ,startW + 1,field,visited,startH, startW - 1); } System.out.print(ans ? "Yes": "No"); } public static boolean dfs(int h,int w,int sth,int stw,char[][] f,boolean[][] visited, int goh,int gow) { if(f[sth][stw] != '.') { return false; } visited[sth][stw] = true; if(sth == goh && stw == gow) { ans = true; } if(!ans) { //System.out.println("h " + sth + " w " + stw); if(sth + 1 < h && (f[sth + 1][stw] == '.') && visited[sth + 1][stw] == false){ count++; dfs(h,w,sth + 1,stw,f,visited,goh,gow); } if(sth - 1 >= 0 && (f[sth - 1][stw] == '.')&& visited[sth - 1][stw] == false) { count++; dfs(h,w,sth - 1,stw,f,visited,goh,gow); }if(stw + 1 < w && (f[sth][stw + 1] == '.')&& visited[sth][stw + 1] == false) { count++; dfs(h,w,sth,stw + 1,f,visited,goh,gow); }if(stw - 1 >= 0 && (f[sth][stw - 1] == '.')&& visited[sth][stw - 1] == false) { count++; dfs(h,w,sth,stw - 1,f,visited,goh,gow); } count--; } return ans; } } import java.util.Scanner; public class Main implements Runnable { public static boolean ans = false; public static int count = 0; public static void main(String[] args) { new Thread(null,new Main(),"",80 * 1024 * 1024).start(); }public void run() { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int h = sc.nextInt(); int w = sc.nextInt(); int startH = 0; int startW= 0; String s; char[][] field = new char[h][w]; boolean[][] visited = new boolean[h][w]; for(int i = 0;i < h;i++) { s = sc.next(); for(int j = 0;j < w;j++) { field[i][j] = s.charAt(j); if(s.charAt(j) == 'S') { startH = i; startW = j; } } } if(startH + 1 < h) { if(startW + 1 < w) { dfs(h,w,startH + 1,startW,field,visited,startH, startW + 1); }if(startW > 0) { visited = new boolean[h][w]; dfs(h,w,startH + 1,startW,field,visited,startH, startW - 1); }if(startH > 0) { visited = new boolean[h][w]; dfs(h,w,startH + 1,startW,field,visited,startH - 1, startW); } }if(startH > 0) { if(startW + 1 < w) { visited = new boolean[h][w]; dfs(h,w,startH - 1 ,startW,field,visited,startH, startW + 1); }if(startW > 0) { visited = new boolean[h][w]; dfs(h,w,startH - 1,startW,field,visited,startH, startW - 1); } }if(startW + 1 < w && startW > 0) { visited = new boolean[h][w]; dfs(h,w,startH ,startW + 1,field,visited,startH, startW - 1); } System.out.print(ans ? "Yes": "No"); } public static boolean dfs(int h,int w,int sth,int stw,char[][] f,boolean[][] visited, int goh,int gow) { if(f[sth][stw] != '.') { return false; } visited[sth][stw] = true; if(sth == goh && stw == gow) { ans = true; } if(!ans) { //System.out.println("h " + sth + " w " + stw); if(sth + 1 < h && (f[sth + 1][stw] == '.') && visited[sth + 1][stw] == false){ count++; dfs(h,w,sth + 1,stw,f,visited,goh,gow); } if(sth - 1 >= 0 && (f[sth - 1][stw] == '.')&& visited[sth - 1][stw] == false) { count++; dfs(h,w,sth - 1,stw,f,visited,goh,gow); }if(stw + 1 < w && (f[sth][stw + 1] == '.')&& visited[sth][stw + 1] == false) { count++; dfs(h,w,sth,stw + 1,f,visited,goh,gow); }if(stw - 1 >= 0 && (f[sth][stw - 1] == '.')&& visited[sth][stw - 1] == false) { count++; dfs(h,w,sth,stw - 1,f,visited,goh,gow); } count--; } return ans; } }
ConDefects/ConDefects/Code/abc276_e/Java/39749728
condefects-java_data_2029
import java.io.*; import java.nio.charset.StandardCharsets; import java.util.*; public class Main { // private static int[][] dirs = {{-1,-1}, {1, 1}, {-1, 1}, {1, -1}}; private static int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; private static long inf = (long) 1e13; private static long div = 998_244_353L; // private static long div = ((long)1e9) + 7; private static long pow(long a, long b) { if (b == 0) { return 1; } else if (b == 1) { return a; } long mid = pow(a, b/2); long res = mid * mid; if (b%2 == 1) { res *= a; } return res; } public static void main(String[] commands) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] parts = in.readLine().split(" "); int N = Integer.parseInt(parts[0]); double[][] dp = new double[N][N]; parts = in.readLine().split(" "); int[] vals = new int[N]; for(int i = 0;i < N; ++i) { vals[i] = Integer.parseInt(parts[i]); } for(int col = 0;col < N; ++col) { if (col == 0) { dp[0][0] = vals[0]; } else { dp[0][col] = Math.max(dp[0][col - 1], vals[col]); } } for(int row = 1;row < N; ++row) { for(int col = row; col < N; ++col) { if (row == col) { dp[row][col] = 0.9 * dp[row - 1][col - 1] + vals[col]; } else { dp[row][col] = Math.max(dp[row][col - 1], vals[col] + 0.9 * dp[row - 1][col - 1]); } } } double nom = 1.0; double ans = -1.0; for(int i = 0;i < N; ++i) { double rating = dp[i][N - 1]/nom - 1200/Math.sqrt(i + 1); ans = Math.max(ans, rating); nom = 0.9 * nom + 1; } System.out.println(ans); } } import java.io.*; import java.nio.charset.StandardCharsets; import java.util.*; public class Main { // private static int[][] dirs = {{-1,-1}, {1, 1}, {-1, 1}, {1, -1}}; private static int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; private static long inf = (long) 1e13; private static long div = 998_244_353L; // private static long div = ((long)1e9) + 7; private static long pow(long a, long b) { if (b == 0) { return 1; } else if (b == 1) { return a; } long mid = pow(a, b/2); long res = mid * mid; if (b%2 == 1) { res *= a; } return res; } public static void main(String[] commands) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] parts = in.readLine().split(" "); int N = Integer.parseInt(parts[0]); double[][] dp = new double[N][N]; parts = in.readLine().split(" "); int[] vals = new int[N]; for(int i = 0;i < N; ++i) { vals[i] = Integer.parseInt(parts[i]); } for(int col = 0;col < N; ++col) { if (col == 0) { dp[0][0] = vals[0]; } else { dp[0][col] = Math.max(dp[0][col - 1], vals[col]); } } for(int row = 1;row < N; ++row) { for(int col = row; col < N; ++col) { if (row == col) { dp[row][col] = 0.9 * dp[row - 1][col - 1] + vals[col]; } else { dp[row][col] = Math.max(dp[row][col - 1], vals[col] + 0.9 * dp[row - 1][col - 1]); } } } double nom = 1.0; double ans = -10000.0; for(int i = 0;i < N; ++i) { double rating = dp[i][N - 1]/nom - 1200/Math.sqrt(i + 1); ans = Math.max(ans, rating); nom = 0.9 * nom + 1; } System.out.println(ans); } }
ConDefects/ConDefects/Code/abc327_e/Java/47285935
condefects-java_data_2030
import java.io.*; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class Main { public static void solve() throws IOException{ int n = in.nextInt(); int[] p = new int[n+1]; for (int i = 1; i <= n; i++) { p[i] = in.nextInt(); } //dp[i][j] = max(dp[i][j], dp[i-1][j-1] * 0.9 / double[][] dp = new double[n+1][n+1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j-1] * 0.9 + p[i]); } } double mul = 1; double down = 0; double ans = Long.MIN_VALUE; for (int i = 1; i <= n; i++) { down += mul; ans = Math.max(ans, dp[n][i] / down - ((double) 1200 / Math.sqrt(i))); mul *= 0.9; } out.println(ans); } static boolean MULTI_CASE = false; public static void main(String[] args) throws IOException { if (MULTI_CASE) { int T = in.nextInt(); for (int i = 0; i < T; ++i) { solve(); } } else { solve(); } out.close(); } static InputReader in = new InputReader(); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static class InputReader { private StringTokenizer st; private BufferedReader bf; public InputReader() { bf = new BufferedReader(new InputStreamReader(System.in)); st = null; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(bf.readLine()); } return st.nextToken(); } public String nextLine() throws IOException { return bf.readLine(); } 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()); } } } /* 0.81 0.9 1 */ import java.io.*; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class Main { public static void solve() throws IOException{ int n = in.nextInt(); int[] p = new int[n+1]; for (int i = 1; i <= n; i++) { p[i] = in.nextInt(); } //dp[i][j] = max(dp[i][j], dp[i-1][j-1] * 0.9 / double[][] dp = new double[n+1][n+1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j-1] * 0.9 + p[i]); } } double mul = 1; double down = 0; double ans = Long.MIN_VALUE; for (int i = 1; i <= n; i++) { down += mul; ans = Math.max(ans, dp[n][i] / down - ((double) 1200 / Math.sqrt(i))); mul *= 0.9; } out.println(ans); } static boolean MULTI_CASE = false; public static void main(String[] args) throws IOException { if (MULTI_CASE) { int T = in.nextInt(); for (int i = 0; i < T; ++i) { solve(); } } else { solve(); } out.close(); } static InputReader in = new InputReader(); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static class InputReader { private StringTokenizer st; private BufferedReader bf; public InputReader() { bf = new BufferedReader(new InputStreamReader(System.in)); st = null; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(bf.readLine()); } return st.nextToken(); } public String nextLine() throws IOException { return bf.readLine(); } 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()); } } } /* 0.81 0.9 1 */
ConDefects/ConDefects/Code/abc327_e/Java/47298578
condefects-java_data_2031
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigDecimal; import java.util.Arrays; import java.util.StringJoiner; public class Main { static int N; static int[] P; public static void main(String[] args) { var sc = new FastScanner(System.in); N = sc.nextInt(); P = new int[N]; for (int i = 0; i < N; i++) { P[i] = sc.nextInt(); } System.out.println(BigDecimal.valueOf(solve()).toPlainString()); } static double solve() { // 最初コンテストの出る順序が変更できると考えてこんなん一意では…?となってた // dp[i][k] = コンテストをi個見て、k回出場したとした時の最大レート // が求まるのでそれを使って各回数毎に求める var dp = new double[N+1][N+1]; for (double[] each : dp) { Arrays.fill(each, -1); } dp[0][0] = 0.0; var mul = new double[N+1]; mul[0] = 1; for (int i = 0; i < N; i++) { mul[i+1] = mul[i]*0.9; } for (int i = 0; i < N; i++) { int idx = N-i-1; var p = P[idx]; for (int k = 0; k <= i; k++) { dp[i+1][k+1] = Math.max(dp[i+1][k+1], dp[i][k] + mul[k]*p); dp[i+1][k] = Math.max(dp[i+1][k], dp[i][k]); } } var ans = -1.0; double b = 0; // 1.0 + 0.9 + ... for (int k = 1; k <= N; k++) { var q = dp[N][k]; b += mul[k-1]; // debug(q, b, q/b, 1200.0/Math.sqrt(k)); ans = Math.max(ans, q/b - 1200.0/Math.sqrt(k) ); } return ans; } static void writeLines(int[] as) { var pw = new PrintWriter(System.out); for (var a : as) pw.println(a); pw.flush(); } static void writeLines(long[] as) { var pw = new PrintWriter(System.out); for (var a : as) pw.println(a); pw.flush(); } static void writeSingleLine(int[] as) { var pw = new PrintWriter(System.out); for (var i = 0; i < as.length; i++) { if (i != 0) pw.print(" "); pw.print(as[i]); } pw.println(); pw.flush(); } static void debug(Object... args) { var j = new StringJoiner(" "); for (var arg : args) { if (arg == null) j.add("null"); else if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg)); else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg)); else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg)); else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg)); else j.add(arg.toString()); } System.err.println(j); } @SuppressWarnings("unused") private static class FastScanner { private final InputStream in; private final byte[] buffer = new byte[1024]; private int curbuf; private int lenbuf; public FastScanner(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 RuntimeException(); } 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 RuntimeException(); 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 RuntimeException(); 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 RuntimeException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public long nextLong() { if (!hasNext()) throw new RuntimeException(); 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 RuntimeException(); 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; } } } import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigDecimal; import java.util.Arrays; import java.util.StringJoiner; public class Main { static int N; static int[] P; public static void main(String[] args) { var sc = new FastScanner(System.in); N = sc.nextInt(); P = new int[N]; for (int i = 0; i < N; i++) { P[i] = sc.nextInt(); } System.out.println(BigDecimal.valueOf(solve()).toPlainString()); } static double solve() { // 最初コンテストの出る順序が変更できると考えてこんなん一意では…?となってた // dp[i][k] = コンテストをi個見て、k回出場したとした時の最大レート // が求まるのでそれを使って各回数毎に求める var dp = new double[N+1][N+1]; for (double[] each : dp) { Arrays.fill(each, -1); } dp[0][0] = 0.0; var mul = new double[N+1]; mul[0] = 1; for (int i = 0; i < N; i++) { mul[i+1] = mul[i]*0.9; } for (int i = 0; i < N; i++) { int idx = N-i-1; var p = P[idx]; for (int k = 0; k <= i; k++) { dp[i+1][k+1] = Math.max(dp[i+1][k+1], dp[i][k] + mul[k]*p); dp[i+1][k] = Math.max(dp[i+1][k], dp[i][k]); } } var ans = Double.NEGATIVE_INFINITY; double b = 0; // 1.0 + 0.9 + ... for (int k = 1; k <= N; k++) { var q = dp[N][k]; b += mul[k-1]; // debug(q, b, q/b, 1200.0/Math.sqrt(k)); ans = Math.max(ans, q/b - 1200.0/Math.sqrt(k) ); } return ans; } static void writeLines(int[] as) { var pw = new PrintWriter(System.out); for (var a : as) pw.println(a); pw.flush(); } static void writeLines(long[] as) { var pw = new PrintWriter(System.out); for (var a : as) pw.println(a); pw.flush(); } static void writeSingleLine(int[] as) { var pw = new PrintWriter(System.out); for (var i = 0; i < as.length; i++) { if (i != 0) pw.print(" "); pw.print(as[i]); } pw.println(); pw.flush(); } static void debug(Object... args) { var j = new StringJoiner(" "); for (var arg : args) { if (arg == null) j.add("null"); else if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg)); else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg)); else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg)); else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg)); else j.add(arg.toString()); } System.err.println(j); } @SuppressWarnings("unused") private static class FastScanner { private final InputStream in; private final byte[] buffer = new byte[1024]; private int curbuf; private int lenbuf; public FastScanner(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 RuntimeException(); } 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 RuntimeException(); 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 RuntimeException(); 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 RuntimeException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public long nextLong() { if (!hasNext()) throw new RuntimeException(); 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 RuntimeException(); 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; } } }
ConDefects/ConDefects/Code/abc327_e/Java/47349643
condefects-java_data_2032
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; class Rate implements Comparable<Rate>{ double top; double under; double k; public Rate(double top, double under, double k) { this.top = top; this.under = under; this.k = k; } public double value() { if (this.k == 0) return Integer.MIN_VALUE; return this.top / this.under - 1200 / Math.sqrt(k); } @Override public int compareTo(Rate o) { return Double.compare(this.value(), o.value()); } public Rate add(double p) { double top = this.top * 0.9 + p; double under = this.under * 0.9 + 1; double k = this.k + 1; return new Rate(top, under, k); } @Override public String toString() { return this.value() + ""; } } public class Main { public static int[] dijkstraDistance; static ContestPrinter printer = new ContestPrinter(System.out); static int[][] directions4 = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}}; static int[][] directions8 = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; public static void solve() { ContestScanner scan = new ContestScanner(); int N = scan.nextInt(); Rate[][] dp = new Rate[N + 1][N + 1]; for (int i = 0; i <= N; i++) { Arrays.fill(dp[i], new Rate(0, 0, 0)); } double[] P = scan.nextDoubleArray(N); for (int i = 1; i <= N; i++) { for (int k = 1; k <= N; k++) { dp[i][k] = max(dp[i - 1][k], dp[i - 1][k - 1].add(P[i - 1])); } } double max = Integer.MIN_VALUE; for (int i = 1; i <= N; i++) { max = max(max, dp[N][i].value()); } print(max); } public static void main(String[] args) { solve(); printer.flush(); printer.close(); } public static void write(Object... objs) { try (PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("", true)))) { for (Object o : objs) { pw.println(o); } } catch (IOException e) { e.printStackTrace(); } } public static long gcd(long l, long r) { if (r == 0) return l; return gcd(r, l % r); } public static long lcm(long l, long r) { return lcm(new BigInteger(String.valueOf(l)), new BigInteger(String.valueOf(r))).longValue(); } public static BigInteger gcd(BigInteger l, BigInteger r) { return l.gcd(r); } public static BigInteger lcm(BigInteger l, BigInteger r) { return l.multiply(r).divide(gcd(l, r)); } @SafeVarargs public static <T extends Comparable<T>> T max(T... values) { return Collections.max(Arrays.asList(values)); } public static <T extends Comparable<T>> T max(Collection<T> values) { return Collections.max(values); } @SafeVarargs public static <T extends Comparable<T>> T min(T... values) { return Collections.min(Arrays.asList(values)); } public static <T extends Comparable<T>> T min(Collection<T> values) { return Collections.min(values); } public static <T extends Comparable<T>> int lowerBound(List<T> list, T key) { return ~Collections.binarySearch(list, key, (x, y) -> x.compareTo(y) >= 0 ? 1 : -1); } public static <T extends Comparable<T>> int upperBound(List<T> list, T key) { return ~Collections.binarySearch(list, key, (x, y) -> x.compareTo(y) > 0 ? -1 : 1); } public static <T1 extends Comparable<T1>, T2> LinkedHashMap<T1, T2> sortMapByKey(Map<T1, T2> map) { return sortMapByKey(map, false); } public static <T1 extends Comparable<T1>, T2> LinkedHashMap<T1, T2> sortMapByKey(Map<T1, T2> map, boolean isReverse) { List<Entry<T1, T2>> entries = new LinkedList<>(map.entrySet()); if (isReverse) entries.sort(Entry.comparingByKey(Collections.reverseOrder())); else entries.sort(Entry.comparingByKey()); LinkedHashMap<T1, T2> result = new LinkedHashMap<>(); for (Entry<T1, T2> entry : entries) { result.put(entry.getKey(), entry.getValue()); } return result; } public static <T1, T2 extends Comparable<T2>> LinkedHashMap<T1, T2> sortMapByValue(Map<T1, T2> map) { return sortMapByValue(map, false); } public static <T1, T2 extends Comparable<T2>> LinkedHashMap<T1, T2> sortMapByValue(Map<T1, T2> map, boolean isReverse) { List<Entry<T1, T2>> entries = new LinkedList<>(map.entrySet()); if (isReverse) entries.sort(Entry.comparingByValue(Collections.reverseOrder())); else entries.sort(Entry.comparingByValue()); LinkedHashMap<T1, T2> result = new LinkedHashMap<>(); for (Entry<T1, T2> entry : entries) { result.put(entry.getKey(), entry.getValue()); } return result; } public static long nCr(long n, long r) { long result = 1; for (int i = 1; i <= r; i++) { result = result * (n - i + 1) / i; } return result; } public static <T extends Comparable<T>> int[] lis(List<T> array) { int N = array.size(); int[] result = new int[N]; List<T> B = new ArrayList<>(); for (int i = 0; i < N; i++) { int k = lowerBound(B, array.get(i)); if (k == B.size()) B.add(array.get(i)); else B.set(k, array.get(i)); result[i] = k + 1; } return result; } public static long lsqrt(long x) { long b = (long) Math.sqrt(x); if (b * b > x) b--; if (b * b < x) b++; return b; } public static void print() { print(""); } public static void print(Object o) { printer.println(o); } public static void print(Object... objs) { printer.printArray(objs); } } class DijkstraComparator<T> implements Comparator<T> { @Override public int compare(T o1, T o2) { return Integer.compare(Main.dijkstraDistance[(int) o1], Main.dijkstraDistance[(int) o2]); } } class IndexedObject<T extends Comparable<T>> implements Comparable<IndexedObject> { int i; T value; public IndexedObject(int i, T value) { this.i = i; this.value = value; } @Override public boolean equals(Object o) { if (!(o instanceof IndexedObject)) return false; return this.i == ((IndexedObject<?>)o).i && this.value.equals(((IndexedObject<?>)o).value); } @Override public int compareTo(IndexedObject o) { if (o.value.getClass() != this.value.getClass()) throw new IllegalArgumentException(); return value.compareTo((T) o.value); } @Override public int hashCode() { return this.i + this.value.hashCode(); } @Override public String toString() { return "IndexedObject{" + "i=" + i + ", value=" + value + '}'; } } class Point { long x; long y; public Point(long x, long y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (!(o instanceof Point)) return false; return this.x == ((Point)o).x && this.y == ((Point)o).y; } @Override public int hashCode() { return Long.hashCode(x) * 524287 + Long.hashCode(y); } public Point add(Point q) { return new Point(x + q.x, y + q.y); } public int getIntX() { return (int) x; } public int getIntY() { return (int) y; } } class GraphBuilder { private Map<Integer, List<Integer>> edges = new HashMap<>(); private final int N; private final boolean isDirected; public GraphBuilder(int N, boolean isDirected) { this.isDirected = isDirected; this.N = N; for (int i = 0; i < N; i++) { edges.put(i, new ArrayList<>()); } } public GraphBuilder(int N) { this(N, false); } public void addEdge(int u, int v) { edges.get(u).add(v); if (!isDirected) edges.get(v).add(u); } public Map<Integer, List<Integer>> getEdges() { return edges; } public int getN() { return N; } } class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public String[] nextStringArray(int length){ String[] array = new String[length]; for(int i=0; i<length; i++) array[i] = this.next(); return array; } public String[] nextStringArray(int length, java.util.function.UnaryOperator<String> map){ String[] array = new String[length]; for(int i=0; i<length; i++) array[i] = map.apply(this.next()); return array; } public double[] nextDoubleArray(int length){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width){ long[][] mat = new long[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width){ int[][] mat = new int[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width){ double[][] mat = new double[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width){ char[][] mat = new char[height][width]; for(int h=0; h<height; h++){ String s = this.next(); for(int w=0; w<width; w++){ mat[h][w] = s.charAt(w); } } return mat; } } class ContestPrinter extends java.io.PrintWriter{ public ContestPrinter(java.io.PrintStream stream){ super(stream); } public ContestPrinter(java.io.File file) throws java.io.FileNotFoundException{ super(new java.io.PrintStream(file)); } public ContestPrinter(){ super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if(x < 0){ sb.append('-'); x = -x; } x += Math.pow(10, -n)/2; sb.append((long)x); sb.append("."); x -= (long)x; for(int i = 0;i < n;i++){ x *= 10; sb.append((int)x); x -= (int)x; } return sb.toString(); } @Override public void print(float f){ super.print(dtos(f, 20)); } @Override public void println(float f){ super.println(dtos(f, 20)); } @Override public void print(double d){ super.print(dtos(d, 20)); } @Override public void println(double d){ super.println(dtos(d, 20)); } public void printArray(int[] array, String separator){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(array[i]); super.print(separator); } super.println(array[n-1]); } public void printArray(int[] array){ this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n-1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map){ this.printArray(array, " ", map); } public void printArray(long[] array, String separator){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(array[i]); super.print(separator); } super.println(array[n-1]); } public void printArray(long[] array){ this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n-1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map){ this.printArray(array, " ", map); } public <T> void printArray(T[] array, String separator){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(array[i]); super.print(separator); } super.println(array[n-1]); } public <T> void printArray(T[] array){ this.printArray(array, " "); } public <T> void printArray(T[] array, String separator, java.util.function.UnaryOperator<T> map){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(map.apply(array[i])); super.print(separator); } super.println(map.apply(array[n-1])); } public <T> void printArray(T[] array, java.util.function.UnaryOperator<T> map){ this.printArray(array, " ", map); } } class DSU { private int n; private int[] parentOrSize; public DSU(int n) { this.n = n; this.parentOrSize = new int[n]; java.util.Arrays.fill(parentOrSize, -1); } int merge(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); int x = leader(a); int y = leader(b); if (x == y) return x; if (-parentOrSize[x] < -parentOrSize[y]) { int tmp = x; x = y; y = tmp; } parentOrSize[x] += parentOrSize[y]; parentOrSize[y] = x; return x; } boolean same(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); return leader(a) == leader(b); } int leader(int a) { if (parentOrSize[a] < 0) { return a; } else { parentOrSize[a] = leader(parentOrSize[a]); return parentOrSize[a]; } } int size(int a) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("" + a); return -parentOrSize[leader(a)]; } java.util.ArrayList<java.util.ArrayList<Integer>> groups() { int[] leaderBuf = new int[n]; int[] groupSize = new int[n]; for (int i = 0; i < n; i++) { leaderBuf[i] = leader(i); groupSize[leaderBuf[i]]++; } java.util.ArrayList<java.util.ArrayList<Integer>> result = new java.util.ArrayList<>(n); for (int i = 0; i < n; i++) { result.add(new java.util.ArrayList<>(groupSize[i])); } for (int i = 0; i < n; i++) { result.get(leaderBuf[i]).add(i); } result.removeIf(java.util.ArrayList::isEmpty); return result; } } class ModIntFactory { private final ModArithmetic ma; private final int mod; private final boolean usesMontgomery; private final ModArithmetic.ModArithmeticMontgomery maMontgomery; private ArrayList<Integer> factorial; public ModIntFactory(int mod) { this.ma = ModArithmetic.of(mod); this.usesMontgomery = ma instanceof ModArithmetic.ModArithmeticMontgomery; this.maMontgomery = usesMontgomery ? (ModArithmetic.ModArithmeticMontgomery) ma : null; this.mod = mod; this.factorial = new ArrayList<>(); } public ModInt create(long value) { if ((value %= mod) < 0) value += mod; if (usesMontgomery) { return new ModInt(maMontgomery.generate(value)); } else { return new ModInt((int) value); } } private void prepareFactorial(int max){ factorial.ensureCapacity(max+1); if(factorial.size()==0) factorial.add(1); if (usesMontgomery) { for(int i=factorial.size(); i<=max; i++){ factorial.add(ma.mul(factorial.get(i-1), maMontgomery.generate(i))); } } else { for(int i=factorial.size(); i<=max; i++){ factorial.add(ma.mul(factorial.get(i-1), i)); } } } public ModInt factorial(int i){ prepareFactorial(i); return create(factorial.get(i)); } public ModInt permutation(int n, int r){ if(n < 0 || r < 0 || n < r) return create(0); prepareFactorial(n); return create(ma.div(factorial.get(n), factorial.get(r))); } public ModInt combination(int n, int r){ if(n < 0 || r < 0 || n < r) return create(0); prepareFactorial(n); return create(ma.div(factorial.get(n), ma.mul(factorial.get(r),factorial.get(n-r)))); } public int getMod() { return mod; } public class ModInt { private int value; private ModInt(int value) { this.value = value; } public int mod() { return mod; } public int value() { if (usesMontgomery) { return maMontgomery.reduce(value); } return value; } public ModInt add(ModInt mi) { return new ModInt(ma.add(value, mi.value)); } public ModInt add(ModInt mi1, ModInt mi2) { return new ModInt(ma.add(value, mi1.value)).addAsg(mi2); } public ModInt add(ModInt mi1, ModInt mi2, ModInt mi3) { return new ModInt(ma.add(value, mi1.value)).addAsg(mi2).addAsg(mi3); } public ModInt add(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return new ModInt(ma.add(value, mi1.value)).addAsg(mi2).addAsg(mi3).addAsg(mi4); } public ModInt add(ModInt mi1, ModInt... mis) { ModInt mi = add(mi1); for (ModInt m : mis) mi.addAsg(m); return mi; } public ModInt add(long mi) { return new ModInt(ma.add(value, ma.remainder(mi))); } public ModInt sub(ModInt mi) { return new ModInt(ma.sub(value, mi.value)); } public ModInt sub(long mi) { return new ModInt(ma.sub(value, ma.remainder(mi))); } public ModInt mul(ModInt mi) { return new ModInt(ma.mul(value, mi.value)); } public ModInt mul(ModInt mi1, ModInt mi2) { return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2); } public ModInt mul(ModInt mi1, ModInt mi2, ModInt mi3) { return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2).mulAsg(mi3); } public ModInt mul(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2).mulAsg(mi3).mulAsg(mi4); } public ModInt mul(ModInt mi1, ModInt... mis) { ModInt mi = mul(mi1); for (ModInt m : mis) mi.mulAsg(m); return mi; } public ModInt mul(long mi) { return new ModInt(ma.mul(value, ma.remainder(mi))); } public ModInt div(ModInt mi) { return new ModInt(ma.div(value, mi.value)); } public ModInt div(long mi) { return new ModInt(ma.div(value, ma.remainder(mi))); } public ModInt inv() { return new ModInt(ma.inv(value)); } public ModInt pow(long b) { return new ModInt(ma.pow(value, b)); } public ModInt addAsg(ModInt mi) { this.value = ma.add(value, mi.value); return this; } public ModInt addAsg(ModInt mi1, ModInt mi2) { return addAsg(mi1).addAsg(mi2); } public ModInt addAsg(ModInt mi1, ModInt mi2, ModInt mi3) { return addAsg(mi1).addAsg(mi2).addAsg(mi3); } public ModInt addAsg(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return addAsg(mi1).addAsg(mi2).addAsg(mi3).addAsg(mi4); } public ModInt addAsg(ModInt... mis) { for (ModInt m : mis) addAsg(m); return this; } public ModInt addAsg(long mi) { this.value = ma.add(value, ma.remainder(mi)); return this; } public ModInt subAsg(ModInt mi) { this.value = ma.sub(value, mi.value); return this; } public ModInt subAsg(long mi) { this.value = ma.sub(value, ma.remainder(mi)); return this; } public ModInt mulAsg(ModInt mi) { this.value = ma.mul(value, mi.value); return this; } public ModInt mulAsg(ModInt mi1, ModInt mi2) { return mulAsg(mi1).mulAsg(mi2); } public ModInt mulAsg(ModInt mi1, ModInt mi2, ModInt mi3) { return mulAsg(mi1).mulAsg(mi2).mulAsg(mi3); } public ModInt mulAsg(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return mulAsg(mi1).mulAsg(mi2).mulAsg(mi3).mulAsg(mi4); } public ModInt mulAsg(ModInt... mis) { for (ModInt m : mis) mulAsg(m); return this; } public ModInt mulAsg(long mi) { this.value = ma.mul(value, ma.remainder(mi)); return this; } public ModInt divAsg(ModInt mi) { this.value = ma.div(value, mi.value); return this; } public ModInt divAsg(long mi) { this.value = ma.div(value, ma.remainder(mi)); return this; } @Override public String toString() { return String.valueOf(value()); } @Override public boolean equals(Object o) { if (o instanceof ModInt) { ModInt mi = (ModInt) o; return mod() == mi.mod() && value() == mi.value(); } return false; } @Override public int hashCode() { return (1 * 37 + mod()) * 37 + value(); } } private static abstract class ModArithmetic { abstract int mod(); abstract int remainder(long value); abstract int add(int a, int b); abstract int sub(int a, int b); abstract int mul(int a, int b); int div(int a, int b) { return mul(a, inv(b)); } int inv(int a) { int b = mod(); if (b == 1) return 0; long u = 1, v = 0; while (b >= 1) { int t = a / b; a -= t * b; int tmp1 = a; a = b; b = tmp1; u -= t * v; long tmp2 = u; u = v; v = tmp2; } if (a != 1) { throw new ArithmeticException("divide by zero"); } return remainder(u); } int pow(int a, long b) { if (b < 0) throw new ArithmeticException("negative power"); int r = 1; int x = a; while (b > 0) { if ((b & 1) == 1) r = mul(r, x); x = mul(x, x); b >>= 1; } return r; } static ModArithmetic of(int mod) { if (mod <= 0) { throw new IllegalArgumentException(); } else if (mod == 1) { return new ModArithmetic1(); } else if (mod == 2) { return new ModArithmetic2(); } else if (mod == 998244353) { return new ModArithmetic998244353(); } else if (mod == 1000000007) { return new ModArithmetic1000000007(); } else if ((mod & 1) == 1) { return new ModArithmeticMontgomery(mod); } else { return new ModArithmeticBarrett(mod); } } private static final class ModArithmetic1 extends ModArithmetic { int mod() {return 1;} int remainder(long value) {return 0;} int add(int a, int b) {return 0;} int sub(int a, int b) {return 0;} int mul(int a, int b) {return 0;} int pow(int a, long b) {return 0;} } private static final class ModArithmetic2 extends ModArithmetic { int mod() {return 2;} int remainder(long value) {return (int) (value & 1);} int add(int a, int b) {return a ^ b;} int sub(int a, int b) {return a ^ b;} int mul(int a, int b) {return a & b;} } private static final class ModArithmetic998244353 extends ModArithmetic { private final int mod = 998244353; int mod() { return mod; } int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } int add(int a, int b) { int res = a + b; return res >= mod ? res - mod : res; } int sub(int a, int b) { int res = a - b; return res < 0 ? res + mod : res; } int mul(int a, int b) { return (int) (((long) a * b) % mod); } } private static final class ModArithmetic1000000007 extends ModArithmetic { private final int mod = 1000000007; int mod() { return mod; } int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } int add(int a, int b) { int res = a + b; return res >= mod ? res - mod : res; } int sub(int a, int b) { int res = a - b; return res < 0 ? res + mod : res; } int mul(int a, int b) { return (int) (((long) a * b) % mod); } } private static final class ModArithmeticMontgomery extends ModArithmeticDynamic { private final long negInv; private final long r2; private ModArithmeticMontgomery(int mod) { super(mod); long inv = 0; long s = 1, t = 0; for (int i = 0; i < 32; i++) { if ((t & 1) == 0) { t += mod; inv += s; } t >>= 1; s <<= 1; } long r = (1l << 32) % mod; this.negInv = inv; this.r2 = (r * r) % mod; } private int generate(long x) { return reduce(x * r2); } private int reduce(long x) { x = (x + ((x * negInv) & 0xffff_ffffl) * mod) >>> 32; return (int) (x < mod ? x : x - mod); } @Override int remainder(long value) { return generate((value %= mod) < 0 ? value + mod : value); } @Override int mul(int a, int b) { return reduce((long) a * b); } @Override int inv(int a) { return super.inv(reduce(a)); } @Override int pow(int a, long b) { return generate(super.pow(a, b)); } } private static final class ModArithmeticBarrett extends ModArithmeticDynamic { private static final long mask = 0xffff_ffffl; private final long mh; private final long ml; private ModArithmeticBarrett(int mod) { super(mod); /** * m = floor(2^64/mod) * 2^64 = p*mod + q, 2^32 = a*mod + b * => (a*mod + b)^2 = p*mod + q * => p = mod*a^2 + 2ab + floor(b^2/mod) */ long a = (1l << 32) / mod; long b = (1l << 32) % mod; long m = a * a * mod + 2 * a * b + (b * b) / mod; mh = m >>> 32; ml = m & mask; } private int reduce(long x) { long z = (x & mask) * ml; z = (x & mask) * mh + (x >>> 32) * ml + (z >>> 32); z = (x >>> 32) * mh + (z >>> 32); x -= z * mod; return (int) (x < mod ? x : x - mod); } @Override int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } @Override int mul(int a, int b) { return reduce((long) a * b); } } private static class ModArithmeticDynamic extends ModArithmetic { final int mod; ModArithmeticDynamic(int mod) { this.mod = mod; } int mod() { return mod; } int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } int add(int a, int b) { int sum = a + b; return sum >= mod ? sum - mod : sum; } int sub(int a, int b) { int sum = a - b; return sum < 0 ? sum + mod : sum; } int mul(int a, int b) { return (int) (((long) a * b) % mod); } } } } class SCC { static class Edge { int from, to; public Edge(int from, int to) { this.from = from; this.to = to; } } final int n; int m; final java.util.ArrayList<Edge> unorderedEdges; final int[] start; final int[] ids; boolean hasBuilt = false; public SCC(int n) { this.n = n; this.unorderedEdges = new java.util.ArrayList<>(); this.start = new int[n + 1]; this.ids = new int[n]; } public void addEdge(int from, int to) { rangeCheck(from); rangeCheck(to); unorderedEdges.add(new Edge(from, to)); start[from + 1]++; this.m++; } public int id(int i) { if (!hasBuilt) { throw new UnsupportedOperationException( "Graph hasn't been built." ); } rangeCheck(i); return ids[i]; } public int[][] build() { for (int i = 1; i <= n; i++) { start[i] += start[i - 1]; } Edge[] orderedEdges = new Edge[m]; int[] count = new int[n + 1]; System.arraycopy(start, 0, count, 0, n + 1); for (Edge e : unorderedEdges) { orderedEdges[count[e.from]++] = e; } int nowOrd = 0; int groupNum = 0; int k = 0; // parent int[] par = new int[n]; int[] vis = new int[n]; int[] low = new int[n]; int[] ord = new int[n]; java.util.Arrays.fill(ord, -1); // u = lower32(stack[i]) : visiting vertex // j = upper32(stack[i]) : jth child long[] stack = new long[n]; // size of stack int ptr = 0; // non-recursional DFS for (int i = 0; i < n; i++) { if (ord[i] >= 0) continue; par[i] = -1; // vertex i, 0th child. stack[ptr++] = 0l << 32 | i; // stack is not empty while (ptr > 0) { // last element long p = stack[--ptr]; // vertex int u = (int) (p & 0xffff_ffffl); // jth child int j = (int) (p >>> 32); if (j == 0) { // first visit low[u] = ord[u] = nowOrd++; vis[k++] = u; } if (start[u] + j < count[u]) { // there are more children // jth child int to = orderedEdges[start[u] + j].to; // incr children counter stack[ptr++] += 1l << 32; if (ord[to] == -1) { // new vertex stack[ptr++] = 0l << 32 | to; par[to] = u; } else { // backward edge low[u] = Math.min(low[u], ord[to]); } } else { // no more children (leaving) while (j --> 0) { int to = orderedEdges[start[u] + j].to; // update lowlink if (par[to] == u) low[u] = Math.min(low[u], low[to]); } if (low[u] == ord[u]) { // root of a component while (true) { // gathering verticies int v = vis[--k]; ord[v] = n; ids[v] = groupNum; if (v == u) break; } groupNum++; // incr the number of components } } } } for (int i = 0; i < n; i++) { ids[i] = groupNum - 1 - ids[i]; } int[] counts = new int[groupNum]; for (int x : ids) counts[x]++; int[][] groups = new int[groupNum][]; for (int i = 0; i < groupNum; i++) { groups[i] = new int[counts[i]]; } for (int i = 0; i < n; i++) { int cmp = ids[i]; groups[cmp][--counts[cmp]] = i; } hasBuilt = true; return groups; } private void rangeCheck(int i) { if (i < 0 || i >= n) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for length %d", i, n) ); } } } class Permutation implements java.util.Iterator<int[]>, Iterable<int[]> { private int[] next; public Permutation(int n) { next = java.util.stream.IntStream.range(0, n).toArray(); } @Override public boolean hasNext() { return next != null; } @Override public int[] next() { int[] r = next.clone(); next = nextPermutation(next); return r; } @Override public java.util.Iterator<int[]> iterator() { return this; } public static int[] nextPermutation(int[] a) { if (a == null || a.length < 2) return null; int p = 0; for (int i = a.length - 2; i >= 0; i--) { if (a[i] >= a[i + 1]) continue; p = i; break; } int q = 0; for (int i = a.length - 1; i > p; i--) { if (a[i] <= a[p]) continue; q = i; break; } if (p == 0 && q == 0) return null; int temp = a[p]; a[p] = a[q]; a[q] = temp; int l = p, r = a.length; while (++l < --r) { temp = a[l]; a[l] = a[r]; a[r] = temp; } return a; } } class SegTree<S> { final int MAX; final int N; final java.util.function.BinaryOperator<S> op; final S E; final S[] data; @SuppressWarnings("unchecked") public SegTree(int n, java.util.function.BinaryOperator<S> op, S e) { this.MAX = n; int k = 1; while (k < n) k <<= 1; this.N = k; this.E = e; this.op = op; this.data = (S[]) new Object[N << 1]; java.util.Arrays.fill(data, E); } public SegTree(S[] dat, java.util.function.BinaryOperator<S> op, S e) { this(dat.length, op, e); build(dat); } private void build(S[] dat) { int l = dat.length; System.arraycopy(dat, 0, data, N, l); for (int i = N - 1; i > 0; i--) { data[i] = op.apply(data[i << 1 | 0], data[i << 1 | 1]); } } public void set(int p, S x) { exclusiveRangeCheck(p); data[p += N] = x; p >>= 1; while (p > 0) { data[p] = op.apply(data[p << 1 | 0], data[p << 1 | 1]); p >>= 1; } } public S get(int p) { exclusiveRangeCheck(p); return data[p + N]; } public S prod(int l, int r) { if (l > r) { throw new IllegalArgumentException( String.format("Invalid range: [%d, %d)", l, r) ); } inclusiveRangeCheck(l); inclusiveRangeCheck(r); S sumLeft = E; S sumRight = E; l += N; r += N; while (l < r) { if ((l & 1) == 1) sumLeft = op.apply(sumLeft, data[l++]); if ((r & 1) == 1) sumRight = op.apply(data[--r], sumRight); l >>= 1; r >>= 1; } return op.apply(sumLeft, sumRight); } public S allProd() { return data[1]; } public int maxRight(int l, java.util.function.Predicate<S> f) { inclusiveRangeCheck(l); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (l == MAX) return MAX; l += N; S sum = E; do { l >>= Integer.numberOfTrailingZeros(l); if (!f.test(op.apply(sum, data[l]))) { while (l < N) { l = l << 1; if (f.test(op.apply(sum, data[l]))) { sum = op.apply(sum, data[l]); l++; } } return l - N; } sum = op.apply(sum, data[l]); l++; } while ((l & -l) != l); return MAX; } public int minLeft(int r, java.util.function.Predicate<S> f) { inclusiveRangeCheck(r); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (r == 0) return 0; r += N; S sum = E; do { r--; while (r > 1 && (r & 1) == 1) r >>= 1; if (!f.test(op.apply(data[r], sum))) { while (r < N) { r = r << 1 | 1; if (f.test(op.apply(data[r], sum))) { sum = op.apply(data[r], sum); r--; } } return r + 1 - N; } sum = op.apply(data[r], sum); } while ((r & -r) != r); return 0; } private void exclusiveRangeCheck(int p) { if (p < 0 || p >= MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d).", p, 0, MAX) ); } } private void inclusiveRangeCheck(int p) { if (p < 0 || p > MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d].", p, 0, MAX) ); } } // **************** DEBUG **************** // private int indent = 6; public void setIndent(int newIndent) { this.indent = newIndent; } @Override public String toString() { return toSimpleString(); } public String toDetailedString() { return toDetailedString(1, 0); } private String toDetailedString(int k, int sp) { if (k >= N) return indent(sp) + data[k]; String s = ""; s += toDetailedString(k << 1 | 1, sp + indent); s += "\n"; s += indent(sp) + data[k]; s += "\n"; s += toDetailedString(k << 1 | 0, sp + indent); return s; } private static String indent(int n) { StringBuilder sb = new StringBuilder(); while (n --> 0) sb.append(' '); return sb.toString(); } public String toSimpleString() { StringBuilder sb = new StringBuilder(); sb.append('['); for (int i = 0; i < N; i++) { sb.append(data[i + N]); if (i < N - 1) sb.append(',').append(' '); } sb.append(']'); return sb.toString(); } } class Pair<S extends Comparable<S>, T extends Comparable<T>> implements Comparable<Pair<S,T>>{ S first; T second; public Pair(S s, T t){ first = s; second = t; } public S getFirst(){return first;} public T getSecond(){return second;} public boolean equals(Object another){ if(this==another) return true; if(!(another instanceof Pair)) return false; Pair otherPair = (Pair)another; return this.first.equals(otherPair.first) && this.second.equals(otherPair.second); } public int compareTo(Pair<S,T> another){ java.util.Comparator<Pair<S,T>> comp1 = java.util.Comparator.comparing(Pair::getFirst); java.util.Comparator<Pair<S,T>> comp2 = comp1.thenComparing(Pair::getSecond); return comp2.compare(this, another); } public int hashCode(){ return first.hashCode() * 10007 + second.hashCode(); } public String toString(){ return String.format("(%s, %s)", first, second); } } import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; class Rate implements Comparable<Rate>{ double top; double under; double k; public Rate(double top, double under, double k) { this.top = top; this.under = under; this.k = k; } public double value() { if (this.k == 0) return Integer.MIN_VALUE; return this.top / this.under - 1200 / Math.sqrt(k); } @Override public int compareTo(Rate o) { return Double.compare(this.value(), o.value()); } public Rate add(double p) { double top = this.top * 0.9 + p; double under = this.under * 0.9 + 1; double k = this.k + 1; return new Rate(top, under, k); } @Override public String toString() { return this.value() + ""; } } public class Main { public static int[] dijkstraDistance; static ContestPrinter printer = new ContestPrinter(System.out); static int[][] directions4 = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}}; static int[][] directions8 = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; public static void solve() { ContestScanner scan = new ContestScanner(); int N = scan.nextInt(); Rate[][] dp = new Rate[N + 1][N + 1]; for (int i = 0; i <= N; i++) { Arrays.fill(dp[i], new Rate(0, 0, 0)); } double[] P = scan.nextDoubleArray(N); for (int i = 1; i <= N; i++) { for (int k = 1; k <= i; k++) { dp[i][k] = max(dp[i - 1][k], dp[i - 1][k - 1].add(P[i - 1])); } } double max = Integer.MIN_VALUE; for (int i = 1; i <= N; i++) { max = max(max, dp[N][i].value()); } print(max); } public static void main(String[] args) { solve(); printer.flush(); printer.close(); } public static void write(Object... objs) { try (PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("", true)))) { for (Object o : objs) { pw.println(o); } } catch (IOException e) { e.printStackTrace(); } } public static long gcd(long l, long r) { if (r == 0) return l; return gcd(r, l % r); } public static long lcm(long l, long r) { return lcm(new BigInteger(String.valueOf(l)), new BigInteger(String.valueOf(r))).longValue(); } public static BigInteger gcd(BigInteger l, BigInteger r) { return l.gcd(r); } public static BigInteger lcm(BigInteger l, BigInteger r) { return l.multiply(r).divide(gcd(l, r)); } @SafeVarargs public static <T extends Comparable<T>> T max(T... values) { return Collections.max(Arrays.asList(values)); } public static <T extends Comparable<T>> T max(Collection<T> values) { return Collections.max(values); } @SafeVarargs public static <T extends Comparable<T>> T min(T... values) { return Collections.min(Arrays.asList(values)); } public static <T extends Comparable<T>> T min(Collection<T> values) { return Collections.min(values); } public static <T extends Comparable<T>> int lowerBound(List<T> list, T key) { return ~Collections.binarySearch(list, key, (x, y) -> x.compareTo(y) >= 0 ? 1 : -1); } public static <T extends Comparable<T>> int upperBound(List<T> list, T key) { return ~Collections.binarySearch(list, key, (x, y) -> x.compareTo(y) > 0 ? -1 : 1); } public static <T1 extends Comparable<T1>, T2> LinkedHashMap<T1, T2> sortMapByKey(Map<T1, T2> map) { return sortMapByKey(map, false); } public static <T1 extends Comparable<T1>, T2> LinkedHashMap<T1, T2> sortMapByKey(Map<T1, T2> map, boolean isReverse) { List<Entry<T1, T2>> entries = new LinkedList<>(map.entrySet()); if (isReverse) entries.sort(Entry.comparingByKey(Collections.reverseOrder())); else entries.sort(Entry.comparingByKey()); LinkedHashMap<T1, T2> result = new LinkedHashMap<>(); for (Entry<T1, T2> entry : entries) { result.put(entry.getKey(), entry.getValue()); } return result; } public static <T1, T2 extends Comparable<T2>> LinkedHashMap<T1, T2> sortMapByValue(Map<T1, T2> map) { return sortMapByValue(map, false); } public static <T1, T2 extends Comparable<T2>> LinkedHashMap<T1, T2> sortMapByValue(Map<T1, T2> map, boolean isReverse) { List<Entry<T1, T2>> entries = new LinkedList<>(map.entrySet()); if (isReverse) entries.sort(Entry.comparingByValue(Collections.reverseOrder())); else entries.sort(Entry.comparingByValue()); LinkedHashMap<T1, T2> result = new LinkedHashMap<>(); for (Entry<T1, T2> entry : entries) { result.put(entry.getKey(), entry.getValue()); } return result; } public static long nCr(long n, long r) { long result = 1; for (int i = 1; i <= r; i++) { result = result * (n - i + 1) / i; } return result; } public static <T extends Comparable<T>> int[] lis(List<T> array) { int N = array.size(); int[] result = new int[N]; List<T> B = new ArrayList<>(); for (int i = 0; i < N; i++) { int k = lowerBound(B, array.get(i)); if (k == B.size()) B.add(array.get(i)); else B.set(k, array.get(i)); result[i] = k + 1; } return result; } public static long lsqrt(long x) { long b = (long) Math.sqrt(x); if (b * b > x) b--; if (b * b < x) b++; return b; } public static void print() { print(""); } public static void print(Object o) { printer.println(o); } public static void print(Object... objs) { printer.printArray(objs); } } class DijkstraComparator<T> implements Comparator<T> { @Override public int compare(T o1, T o2) { return Integer.compare(Main.dijkstraDistance[(int) o1], Main.dijkstraDistance[(int) o2]); } } class IndexedObject<T extends Comparable<T>> implements Comparable<IndexedObject> { int i; T value; public IndexedObject(int i, T value) { this.i = i; this.value = value; } @Override public boolean equals(Object o) { if (!(o instanceof IndexedObject)) return false; return this.i == ((IndexedObject<?>)o).i && this.value.equals(((IndexedObject<?>)o).value); } @Override public int compareTo(IndexedObject o) { if (o.value.getClass() != this.value.getClass()) throw new IllegalArgumentException(); return value.compareTo((T) o.value); } @Override public int hashCode() { return this.i + this.value.hashCode(); } @Override public String toString() { return "IndexedObject{" + "i=" + i + ", value=" + value + '}'; } } class Point { long x; long y; public Point(long x, long y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (!(o instanceof Point)) return false; return this.x == ((Point)o).x && this.y == ((Point)o).y; } @Override public int hashCode() { return Long.hashCode(x) * 524287 + Long.hashCode(y); } public Point add(Point q) { return new Point(x + q.x, y + q.y); } public int getIntX() { return (int) x; } public int getIntY() { return (int) y; } } class GraphBuilder { private Map<Integer, List<Integer>> edges = new HashMap<>(); private final int N; private final boolean isDirected; public GraphBuilder(int N, boolean isDirected) { this.isDirected = isDirected; this.N = N; for (int i = 0; i < N; i++) { edges.put(i, new ArrayList<>()); } } public GraphBuilder(int N) { this(N, false); } public void addEdge(int u, int v) { edges.get(u).add(v); if (!isDirected) edges.get(v).add(u); } public Map<Integer, List<Integer>> getEdges() { return edges; } public int getN() { return N; } } class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public String[] nextStringArray(int length){ String[] array = new String[length]; for(int i=0; i<length; i++) array[i] = this.next(); return array; } public String[] nextStringArray(int length, java.util.function.UnaryOperator<String> map){ String[] array = new String[length]; for(int i=0; i<length; i++) array[i] = map.apply(this.next()); return array; } public double[] nextDoubleArray(int length){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width){ long[][] mat = new long[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width){ int[][] mat = new int[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width){ double[][] mat = new double[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width){ char[][] mat = new char[height][width]; for(int h=0; h<height; h++){ String s = this.next(); for(int w=0; w<width; w++){ mat[h][w] = s.charAt(w); } } return mat; } } class ContestPrinter extends java.io.PrintWriter{ public ContestPrinter(java.io.PrintStream stream){ super(stream); } public ContestPrinter(java.io.File file) throws java.io.FileNotFoundException{ super(new java.io.PrintStream(file)); } public ContestPrinter(){ super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if(x < 0){ sb.append('-'); x = -x; } x += Math.pow(10, -n)/2; sb.append((long)x); sb.append("."); x -= (long)x; for(int i = 0;i < n;i++){ x *= 10; sb.append((int)x); x -= (int)x; } return sb.toString(); } @Override public void print(float f){ super.print(dtos(f, 20)); } @Override public void println(float f){ super.println(dtos(f, 20)); } @Override public void print(double d){ super.print(dtos(d, 20)); } @Override public void println(double d){ super.println(dtos(d, 20)); } public void printArray(int[] array, String separator){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(array[i]); super.print(separator); } super.println(array[n-1]); } public void printArray(int[] array){ this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n-1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map){ this.printArray(array, " ", map); } public void printArray(long[] array, String separator){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(array[i]); super.print(separator); } super.println(array[n-1]); } public void printArray(long[] array){ this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n-1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map){ this.printArray(array, " ", map); } public <T> void printArray(T[] array, String separator){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(array[i]); super.print(separator); } super.println(array[n-1]); } public <T> void printArray(T[] array){ this.printArray(array, " "); } public <T> void printArray(T[] array, String separator, java.util.function.UnaryOperator<T> map){ int n = array.length; if(n==0){ super.println(); return; } for(int i=0; i<n-1; i++){ super.print(map.apply(array[i])); super.print(separator); } super.println(map.apply(array[n-1])); } public <T> void printArray(T[] array, java.util.function.UnaryOperator<T> map){ this.printArray(array, " ", map); } } class DSU { private int n; private int[] parentOrSize; public DSU(int n) { this.n = n; this.parentOrSize = new int[n]; java.util.Arrays.fill(parentOrSize, -1); } int merge(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); int x = leader(a); int y = leader(b); if (x == y) return x; if (-parentOrSize[x] < -parentOrSize[y]) { int tmp = x; x = y; y = tmp; } parentOrSize[x] += parentOrSize[y]; parentOrSize[y] = x; return x; } boolean same(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); return leader(a) == leader(b); } int leader(int a) { if (parentOrSize[a] < 0) { return a; } else { parentOrSize[a] = leader(parentOrSize[a]); return parentOrSize[a]; } } int size(int a) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("" + a); return -parentOrSize[leader(a)]; } java.util.ArrayList<java.util.ArrayList<Integer>> groups() { int[] leaderBuf = new int[n]; int[] groupSize = new int[n]; for (int i = 0; i < n; i++) { leaderBuf[i] = leader(i); groupSize[leaderBuf[i]]++; } java.util.ArrayList<java.util.ArrayList<Integer>> result = new java.util.ArrayList<>(n); for (int i = 0; i < n; i++) { result.add(new java.util.ArrayList<>(groupSize[i])); } for (int i = 0; i < n; i++) { result.get(leaderBuf[i]).add(i); } result.removeIf(java.util.ArrayList::isEmpty); return result; } } class ModIntFactory { private final ModArithmetic ma; private final int mod; private final boolean usesMontgomery; private final ModArithmetic.ModArithmeticMontgomery maMontgomery; private ArrayList<Integer> factorial; public ModIntFactory(int mod) { this.ma = ModArithmetic.of(mod); this.usesMontgomery = ma instanceof ModArithmetic.ModArithmeticMontgomery; this.maMontgomery = usesMontgomery ? (ModArithmetic.ModArithmeticMontgomery) ma : null; this.mod = mod; this.factorial = new ArrayList<>(); } public ModInt create(long value) { if ((value %= mod) < 0) value += mod; if (usesMontgomery) { return new ModInt(maMontgomery.generate(value)); } else { return new ModInt((int) value); } } private void prepareFactorial(int max){ factorial.ensureCapacity(max+1); if(factorial.size()==0) factorial.add(1); if (usesMontgomery) { for(int i=factorial.size(); i<=max; i++){ factorial.add(ma.mul(factorial.get(i-1), maMontgomery.generate(i))); } } else { for(int i=factorial.size(); i<=max; i++){ factorial.add(ma.mul(factorial.get(i-1), i)); } } } public ModInt factorial(int i){ prepareFactorial(i); return create(factorial.get(i)); } public ModInt permutation(int n, int r){ if(n < 0 || r < 0 || n < r) return create(0); prepareFactorial(n); return create(ma.div(factorial.get(n), factorial.get(r))); } public ModInt combination(int n, int r){ if(n < 0 || r < 0 || n < r) return create(0); prepareFactorial(n); return create(ma.div(factorial.get(n), ma.mul(factorial.get(r),factorial.get(n-r)))); } public int getMod() { return mod; } public class ModInt { private int value; private ModInt(int value) { this.value = value; } public int mod() { return mod; } public int value() { if (usesMontgomery) { return maMontgomery.reduce(value); } return value; } public ModInt add(ModInt mi) { return new ModInt(ma.add(value, mi.value)); } public ModInt add(ModInt mi1, ModInt mi2) { return new ModInt(ma.add(value, mi1.value)).addAsg(mi2); } public ModInt add(ModInt mi1, ModInt mi2, ModInt mi3) { return new ModInt(ma.add(value, mi1.value)).addAsg(mi2).addAsg(mi3); } public ModInt add(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return new ModInt(ma.add(value, mi1.value)).addAsg(mi2).addAsg(mi3).addAsg(mi4); } public ModInt add(ModInt mi1, ModInt... mis) { ModInt mi = add(mi1); for (ModInt m : mis) mi.addAsg(m); return mi; } public ModInt add(long mi) { return new ModInt(ma.add(value, ma.remainder(mi))); } public ModInt sub(ModInt mi) { return new ModInt(ma.sub(value, mi.value)); } public ModInt sub(long mi) { return new ModInt(ma.sub(value, ma.remainder(mi))); } public ModInt mul(ModInt mi) { return new ModInt(ma.mul(value, mi.value)); } public ModInt mul(ModInt mi1, ModInt mi2) { return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2); } public ModInt mul(ModInt mi1, ModInt mi2, ModInt mi3) { return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2).mulAsg(mi3); } public ModInt mul(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2).mulAsg(mi3).mulAsg(mi4); } public ModInt mul(ModInt mi1, ModInt... mis) { ModInt mi = mul(mi1); for (ModInt m : mis) mi.mulAsg(m); return mi; } public ModInt mul(long mi) { return new ModInt(ma.mul(value, ma.remainder(mi))); } public ModInt div(ModInt mi) { return new ModInt(ma.div(value, mi.value)); } public ModInt div(long mi) { return new ModInt(ma.div(value, ma.remainder(mi))); } public ModInt inv() { return new ModInt(ma.inv(value)); } public ModInt pow(long b) { return new ModInt(ma.pow(value, b)); } public ModInt addAsg(ModInt mi) { this.value = ma.add(value, mi.value); return this; } public ModInt addAsg(ModInt mi1, ModInt mi2) { return addAsg(mi1).addAsg(mi2); } public ModInt addAsg(ModInt mi1, ModInt mi2, ModInt mi3) { return addAsg(mi1).addAsg(mi2).addAsg(mi3); } public ModInt addAsg(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return addAsg(mi1).addAsg(mi2).addAsg(mi3).addAsg(mi4); } public ModInt addAsg(ModInt... mis) { for (ModInt m : mis) addAsg(m); return this; } public ModInt addAsg(long mi) { this.value = ma.add(value, ma.remainder(mi)); return this; } public ModInt subAsg(ModInt mi) { this.value = ma.sub(value, mi.value); return this; } public ModInt subAsg(long mi) { this.value = ma.sub(value, ma.remainder(mi)); return this; } public ModInt mulAsg(ModInt mi) { this.value = ma.mul(value, mi.value); return this; } public ModInt mulAsg(ModInt mi1, ModInt mi2) { return mulAsg(mi1).mulAsg(mi2); } public ModInt mulAsg(ModInt mi1, ModInt mi2, ModInt mi3) { return mulAsg(mi1).mulAsg(mi2).mulAsg(mi3); } public ModInt mulAsg(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return mulAsg(mi1).mulAsg(mi2).mulAsg(mi3).mulAsg(mi4); } public ModInt mulAsg(ModInt... mis) { for (ModInt m : mis) mulAsg(m); return this; } public ModInt mulAsg(long mi) { this.value = ma.mul(value, ma.remainder(mi)); return this; } public ModInt divAsg(ModInt mi) { this.value = ma.div(value, mi.value); return this; } public ModInt divAsg(long mi) { this.value = ma.div(value, ma.remainder(mi)); return this; } @Override public String toString() { return String.valueOf(value()); } @Override public boolean equals(Object o) { if (o instanceof ModInt) { ModInt mi = (ModInt) o; return mod() == mi.mod() && value() == mi.value(); } return false; } @Override public int hashCode() { return (1 * 37 + mod()) * 37 + value(); } } private static abstract class ModArithmetic { abstract int mod(); abstract int remainder(long value); abstract int add(int a, int b); abstract int sub(int a, int b); abstract int mul(int a, int b); int div(int a, int b) { return mul(a, inv(b)); } int inv(int a) { int b = mod(); if (b == 1) return 0; long u = 1, v = 0; while (b >= 1) { int t = a / b; a -= t * b; int tmp1 = a; a = b; b = tmp1; u -= t * v; long tmp2 = u; u = v; v = tmp2; } if (a != 1) { throw new ArithmeticException("divide by zero"); } return remainder(u); } int pow(int a, long b) { if (b < 0) throw new ArithmeticException("negative power"); int r = 1; int x = a; while (b > 0) { if ((b & 1) == 1) r = mul(r, x); x = mul(x, x); b >>= 1; } return r; } static ModArithmetic of(int mod) { if (mod <= 0) { throw new IllegalArgumentException(); } else if (mod == 1) { return new ModArithmetic1(); } else if (mod == 2) { return new ModArithmetic2(); } else if (mod == 998244353) { return new ModArithmetic998244353(); } else if (mod == 1000000007) { return new ModArithmetic1000000007(); } else if ((mod & 1) == 1) { return new ModArithmeticMontgomery(mod); } else { return new ModArithmeticBarrett(mod); } } private static final class ModArithmetic1 extends ModArithmetic { int mod() {return 1;} int remainder(long value) {return 0;} int add(int a, int b) {return 0;} int sub(int a, int b) {return 0;} int mul(int a, int b) {return 0;} int pow(int a, long b) {return 0;} } private static final class ModArithmetic2 extends ModArithmetic { int mod() {return 2;} int remainder(long value) {return (int) (value & 1);} int add(int a, int b) {return a ^ b;} int sub(int a, int b) {return a ^ b;} int mul(int a, int b) {return a & b;} } private static final class ModArithmetic998244353 extends ModArithmetic { private final int mod = 998244353; int mod() { return mod; } int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } int add(int a, int b) { int res = a + b; return res >= mod ? res - mod : res; } int sub(int a, int b) { int res = a - b; return res < 0 ? res + mod : res; } int mul(int a, int b) { return (int) (((long) a * b) % mod); } } private static final class ModArithmetic1000000007 extends ModArithmetic { private final int mod = 1000000007; int mod() { return mod; } int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } int add(int a, int b) { int res = a + b; return res >= mod ? res - mod : res; } int sub(int a, int b) { int res = a - b; return res < 0 ? res + mod : res; } int mul(int a, int b) { return (int) (((long) a * b) % mod); } } private static final class ModArithmeticMontgomery extends ModArithmeticDynamic { private final long negInv; private final long r2; private ModArithmeticMontgomery(int mod) { super(mod); long inv = 0; long s = 1, t = 0; for (int i = 0; i < 32; i++) { if ((t & 1) == 0) { t += mod; inv += s; } t >>= 1; s <<= 1; } long r = (1l << 32) % mod; this.negInv = inv; this.r2 = (r * r) % mod; } private int generate(long x) { return reduce(x * r2); } private int reduce(long x) { x = (x + ((x * negInv) & 0xffff_ffffl) * mod) >>> 32; return (int) (x < mod ? x : x - mod); } @Override int remainder(long value) { return generate((value %= mod) < 0 ? value + mod : value); } @Override int mul(int a, int b) { return reduce((long) a * b); } @Override int inv(int a) { return super.inv(reduce(a)); } @Override int pow(int a, long b) { return generate(super.pow(a, b)); } } private static final class ModArithmeticBarrett extends ModArithmeticDynamic { private static final long mask = 0xffff_ffffl; private final long mh; private final long ml; private ModArithmeticBarrett(int mod) { super(mod); /** * m = floor(2^64/mod) * 2^64 = p*mod + q, 2^32 = a*mod + b * => (a*mod + b)^2 = p*mod + q * => p = mod*a^2 + 2ab + floor(b^2/mod) */ long a = (1l << 32) / mod; long b = (1l << 32) % mod; long m = a * a * mod + 2 * a * b + (b * b) / mod; mh = m >>> 32; ml = m & mask; } private int reduce(long x) { long z = (x & mask) * ml; z = (x & mask) * mh + (x >>> 32) * ml + (z >>> 32); z = (x >>> 32) * mh + (z >>> 32); x -= z * mod; return (int) (x < mod ? x : x - mod); } @Override int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } @Override int mul(int a, int b) { return reduce((long) a * b); } } private static class ModArithmeticDynamic extends ModArithmetic { final int mod; ModArithmeticDynamic(int mod) { this.mod = mod; } int mod() { return mod; } int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } int add(int a, int b) { int sum = a + b; return sum >= mod ? sum - mod : sum; } int sub(int a, int b) { int sum = a - b; return sum < 0 ? sum + mod : sum; } int mul(int a, int b) { return (int) (((long) a * b) % mod); } } } } class SCC { static class Edge { int from, to; public Edge(int from, int to) { this.from = from; this.to = to; } } final int n; int m; final java.util.ArrayList<Edge> unorderedEdges; final int[] start; final int[] ids; boolean hasBuilt = false; public SCC(int n) { this.n = n; this.unorderedEdges = new java.util.ArrayList<>(); this.start = new int[n + 1]; this.ids = new int[n]; } public void addEdge(int from, int to) { rangeCheck(from); rangeCheck(to); unorderedEdges.add(new Edge(from, to)); start[from + 1]++; this.m++; } public int id(int i) { if (!hasBuilt) { throw new UnsupportedOperationException( "Graph hasn't been built." ); } rangeCheck(i); return ids[i]; } public int[][] build() { for (int i = 1; i <= n; i++) { start[i] += start[i - 1]; } Edge[] orderedEdges = new Edge[m]; int[] count = new int[n + 1]; System.arraycopy(start, 0, count, 0, n + 1); for (Edge e : unorderedEdges) { orderedEdges[count[e.from]++] = e; } int nowOrd = 0; int groupNum = 0; int k = 0; // parent int[] par = new int[n]; int[] vis = new int[n]; int[] low = new int[n]; int[] ord = new int[n]; java.util.Arrays.fill(ord, -1); // u = lower32(stack[i]) : visiting vertex // j = upper32(stack[i]) : jth child long[] stack = new long[n]; // size of stack int ptr = 0; // non-recursional DFS for (int i = 0; i < n; i++) { if (ord[i] >= 0) continue; par[i] = -1; // vertex i, 0th child. stack[ptr++] = 0l << 32 | i; // stack is not empty while (ptr > 0) { // last element long p = stack[--ptr]; // vertex int u = (int) (p & 0xffff_ffffl); // jth child int j = (int) (p >>> 32); if (j == 0) { // first visit low[u] = ord[u] = nowOrd++; vis[k++] = u; } if (start[u] + j < count[u]) { // there are more children // jth child int to = orderedEdges[start[u] + j].to; // incr children counter stack[ptr++] += 1l << 32; if (ord[to] == -1) { // new vertex stack[ptr++] = 0l << 32 | to; par[to] = u; } else { // backward edge low[u] = Math.min(low[u], ord[to]); } } else { // no more children (leaving) while (j --> 0) { int to = orderedEdges[start[u] + j].to; // update lowlink if (par[to] == u) low[u] = Math.min(low[u], low[to]); } if (low[u] == ord[u]) { // root of a component while (true) { // gathering verticies int v = vis[--k]; ord[v] = n; ids[v] = groupNum; if (v == u) break; } groupNum++; // incr the number of components } } } } for (int i = 0; i < n; i++) { ids[i] = groupNum - 1 - ids[i]; } int[] counts = new int[groupNum]; for (int x : ids) counts[x]++; int[][] groups = new int[groupNum][]; for (int i = 0; i < groupNum; i++) { groups[i] = new int[counts[i]]; } for (int i = 0; i < n; i++) { int cmp = ids[i]; groups[cmp][--counts[cmp]] = i; } hasBuilt = true; return groups; } private void rangeCheck(int i) { if (i < 0 || i >= n) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for length %d", i, n) ); } } } class Permutation implements java.util.Iterator<int[]>, Iterable<int[]> { private int[] next; public Permutation(int n) { next = java.util.stream.IntStream.range(0, n).toArray(); } @Override public boolean hasNext() { return next != null; } @Override public int[] next() { int[] r = next.clone(); next = nextPermutation(next); return r; } @Override public java.util.Iterator<int[]> iterator() { return this; } public static int[] nextPermutation(int[] a) { if (a == null || a.length < 2) return null; int p = 0; for (int i = a.length - 2; i >= 0; i--) { if (a[i] >= a[i + 1]) continue; p = i; break; } int q = 0; for (int i = a.length - 1; i > p; i--) { if (a[i] <= a[p]) continue; q = i; break; } if (p == 0 && q == 0) return null; int temp = a[p]; a[p] = a[q]; a[q] = temp; int l = p, r = a.length; while (++l < --r) { temp = a[l]; a[l] = a[r]; a[r] = temp; } return a; } } class SegTree<S> { final int MAX; final int N; final java.util.function.BinaryOperator<S> op; final S E; final S[] data; @SuppressWarnings("unchecked") public SegTree(int n, java.util.function.BinaryOperator<S> op, S e) { this.MAX = n; int k = 1; while (k < n) k <<= 1; this.N = k; this.E = e; this.op = op; this.data = (S[]) new Object[N << 1]; java.util.Arrays.fill(data, E); } public SegTree(S[] dat, java.util.function.BinaryOperator<S> op, S e) { this(dat.length, op, e); build(dat); } private void build(S[] dat) { int l = dat.length; System.arraycopy(dat, 0, data, N, l); for (int i = N - 1; i > 0; i--) { data[i] = op.apply(data[i << 1 | 0], data[i << 1 | 1]); } } public void set(int p, S x) { exclusiveRangeCheck(p); data[p += N] = x; p >>= 1; while (p > 0) { data[p] = op.apply(data[p << 1 | 0], data[p << 1 | 1]); p >>= 1; } } public S get(int p) { exclusiveRangeCheck(p); return data[p + N]; } public S prod(int l, int r) { if (l > r) { throw new IllegalArgumentException( String.format("Invalid range: [%d, %d)", l, r) ); } inclusiveRangeCheck(l); inclusiveRangeCheck(r); S sumLeft = E; S sumRight = E; l += N; r += N; while (l < r) { if ((l & 1) == 1) sumLeft = op.apply(sumLeft, data[l++]); if ((r & 1) == 1) sumRight = op.apply(data[--r], sumRight); l >>= 1; r >>= 1; } return op.apply(sumLeft, sumRight); } public S allProd() { return data[1]; } public int maxRight(int l, java.util.function.Predicate<S> f) { inclusiveRangeCheck(l); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (l == MAX) return MAX; l += N; S sum = E; do { l >>= Integer.numberOfTrailingZeros(l); if (!f.test(op.apply(sum, data[l]))) { while (l < N) { l = l << 1; if (f.test(op.apply(sum, data[l]))) { sum = op.apply(sum, data[l]); l++; } } return l - N; } sum = op.apply(sum, data[l]); l++; } while ((l & -l) != l); return MAX; } public int minLeft(int r, java.util.function.Predicate<S> f) { inclusiveRangeCheck(r); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (r == 0) return 0; r += N; S sum = E; do { r--; while (r > 1 && (r & 1) == 1) r >>= 1; if (!f.test(op.apply(data[r], sum))) { while (r < N) { r = r << 1 | 1; if (f.test(op.apply(data[r], sum))) { sum = op.apply(data[r], sum); r--; } } return r + 1 - N; } sum = op.apply(data[r], sum); } while ((r & -r) != r); return 0; } private void exclusiveRangeCheck(int p) { if (p < 0 || p >= MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d).", p, 0, MAX) ); } } private void inclusiveRangeCheck(int p) { if (p < 0 || p > MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d].", p, 0, MAX) ); } } // **************** DEBUG **************** // private int indent = 6; public void setIndent(int newIndent) { this.indent = newIndent; } @Override public String toString() { return toSimpleString(); } public String toDetailedString() { return toDetailedString(1, 0); } private String toDetailedString(int k, int sp) { if (k >= N) return indent(sp) + data[k]; String s = ""; s += toDetailedString(k << 1 | 1, sp + indent); s += "\n"; s += indent(sp) + data[k]; s += "\n"; s += toDetailedString(k << 1 | 0, sp + indent); return s; } private static String indent(int n) { StringBuilder sb = new StringBuilder(); while (n --> 0) sb.append(' '); return sb.toString(); } public String toSimpleString() { StringBuilder sb = new StringBuilder(); sb.append('['); for (int i = 0; i < N; i++) { sb.append(data[i + N]); if (i < N - 1) sb.append(',').append(' '); } sb.append(']'); return sb.toString(); } } class Pair<S extends Comparable<S>, T extends Comparable<T>> implements Comparable<Pair<S,T>>{ S first; T second; public Pair(S s, T t){ first = s; second = t; } public S getFirst(){return first;} public T getSecond(){return second;} public boolean equals(Object another){ if(this==another) return true; if(!(another instanceof Pair)) return false; Pair otherPair = (Pair)another; return this.first.equals(otherPair.first) && this.second.equals(otherPair.second); } public int compareTo(Pair<S,T> another){ java.util.Comparator<Pair<S,T>> comp1 = java.util.Comparator.comparing(Pair::getFirst); java.util.Comparator<Pair<S,T>> comp2 = comp1.thenComparing(Pair::getSecond); return comp2.compare(this, another); } public int hashCode(){ return first.hashCode() * 10007 + second.hashCode(); } public String toString(){ return String.format("(%s, %s)", first, second); } }
ConDefects/ConDefects/Code/abc327_e/Java/47261550
condefects-java_data_2033
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc=new Scanner(System.in); long X=sc.nextLong(); long A=sc.nextLong(); long D=sc.nextLong(); long N=sc.nextLong(); long fina=A+N*D; if(D==0){ System.out.println(Math.abs(X-A)); }else if(fina>=A){ if(X<=fina && X>=A){ long n=(X-A)/D; long n1=n+1; long min=Math.min(Math.abs(X-A-(n)*D), Math.abs(X-A-(n1)*D)); System.out.println(min); }else if(X>fina){ System.out.println(Math.abs(X-fina)); }else if(X<A){ System.out.println(Math.abs(A-X)); } }else if(fina<A){ if(X>=fina && X<=A){ long n=(X-A)/D; long n1=n+1; long min=Math.min(Math.abs(X-A-(n)*D), Math.abs(X-A-(n1)*D)); System.out.println(min); }else if(X<fina){ System.out.println(Math.abs(fina-X)); }else if(X>A){ System.out.println(Math.abs(X-A)); } } /** for(long i=A;i<=A+N*D;i=i+D){ a++; if(min>Math.abs(X-i)){ min1=min; min=Math.abs(X-i); } if(a==N){ break; } System.out.println(min); if(min<Math.abs(X-i)){ break; } } System.out.println(min);**/ } } import java.util.*; public class Main { public static void main(String[] args){ Scanner sc=new Scanner(System.in); long X=sc.nextLong(); long A=sc.nextLong(); long D=sc.nextLong(); long N=sc.nextLong(); long fina=A+(N-1)*D; if(D==0){ System.out.println(Math.abs(X-A)); }else if(fina>=A){ if(X<=fina && X>=A){ long n=(X-A)/D; long n1=n+1; long min=Math.min(Math.abs(X-A-(n)*D), Math.abs(X-A-(n1)*D)); System.out.println(min); }else if(X>fina){ System.out.println(Math.abs(X-fina)); }else if(X<A){ System.out.println(Math.abs(A-X)); } }else if(fina<A){ if(X>=fina && X<=A){ long n=(X-A)/D; long n1=n+1; long min=Math.min(Math.abs(X-A-(n)*D), Math.abs(X-A-(n1)*D)); System.out.println(min); }else if(X<fina){ System.out.println(Math.abs(fina-X)); }else if(X>A){ System.out.println(Math.abs(X-A)); } } /** for(long i=A;i<=A+N*D;i=i+D){ a++; if(min>Math.abs(X-i)){ min1=min; min=Math.abs(X-i); } if(a==N){ break; } System.out.println(min); if(min<Math.abs(X-i)){ break; } } System.out.println(min);**/ } }
ConDefects/ConDefects/Code/abc255_c/Java/43531547
condefects-java_data_2034
import java.io.*; import java.math.*; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.Map.Entry; import java.util.regex.Pattern; import java.util.stream.Collectors; class Main { public static void solve () { x = nextLong(); a = nextLong(); d = nextLong(); n = nextLong(); if (d < 0) { long temp = a + d * (n-1); a = temp; d *= -1; } if (x <= a) { println(a - x); return; } if (x >= a+(n-1)*d) { println((a+(n-1)*d) - x); return; } println(binarySearch(0, n)); } public static long x, a, d, n; public static long binarySearch(long ok, long ng) { while (Math.abs(ok - ng) > 1) { long mid = (ok+ng)/2; if (isOK(mid) == true) ok = mid; else ng = mid; } long opNum1 = Math.abs(x - (a + ok*d)); long opNum2 = Math.abs(x - (a + ng*d)); return Math.min(opNum1, opNum2); } public static boolean isOK(long k) { //a+k*d のkの値で二分探索する。 if (a + k*d < x) return true; else return false; } ///////////////////////////////////////////////////////////////////////////////////////////////// // useful methods, useful fields, useful static inner class ///////////////////////////////////////////////////////////////////////////////////////////////// public static final int infi = (int)1e9; public static final long infl = (long)1e18; public static final int modi = (int)1e9 + 7; public static final long modl = (long)1e18 + 7; public static int[] dy = {-1, 0, 1, 0}; public static int[] dx = {0, 1, 0, -1}; public static class Edge { int id, from, to, cost; Edge(int to, int cost) { this.to = to; this.cost = cost; } Edge(int from, int to, int cost) { this.from = from; this.to = to; this.cost = cost; } Edge(int id, int from, int to, int cost) { this.id = id; this.from = from; this.to = to; this.cost = cost; } int getCost() {return this.cost;} } public static String yesno(boolean b) {return b ? "Yes" : "No";} ///////////////////////////////////////////////////////////////////////////////////////////////// // input ///////////////////////////////////////////////////////////////////////////////////////////////// public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 32768); public static StringTokenizer tokenizer = null; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public static String[] nextArray(int n) { String[] a = new String[n]; for (int i=0; i<n; i++) a[i] = next(); return a; } public static int nextInt() {return Integer.parseInt(next());}; public static int[] nextIntArray(int n) { int[] a = new int[n]; for (int i=0; i<n; i++) a[i] = nextInt(); return a; } public static int[][] nextIntTable(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; } public static long nextLong() {return Long.parseLong(next());} public static long[] nextLongArray(int n) { long[] a = new long[n]; for (int i=0; i<n; i++) a[i] = nextLong(); return a; } public static double nextDouble() {return Double.parseDouble(next());} public static char nextChar() {return next().charAt(0);} public static char[] nextCharArray() {return next().toCharArray();} public static char[][] nextCharTable(int n, int m) { char[][] a = new char[n][m]; for (int i=0; i<n; i++) { a[i] = next().toCharArray(); } return a; } public static List<List<Integer>> nextDirectedGraph(int n, int m) { List<List<Integer>> g = new ArrayList<>(); for (int i=0; i<n; i++) { g.add(new ArrayList<>()); } for (int i=0; i<m; i++) { int a = nextInt()-1, b = nextInt()-1; g.get(a).add(b); g.get(b).add(a); } return g; } public static List<List<Integer>> nextUndirectedGraph(int n, int m) { List<List<Integer>> g = new ArrayList<>(); for (int i=0; i<n; i++) { g.add(new ArrayList<>()); } for (int i=0; i<m; i++) { int a = nextInt()-1, b = nextInt()-1; g.get(a).add(b); } return g; } ///////////////////////////////////////////////////////////////////////////////////////////////// // output ///////////////////////////////////////////////////////////////////////////////////////////////// static PrintWriter out = new PrintWriter(System.out); public static void print(Object o) {out.print(o);} public static void println(Object o) {out.println(o);} public static void printStringArray(String[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printIntArray(int[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printLongArray(long[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printBooleanArray (boolean[] a) { for (int i=0; i<a.length; i++) { char c = a[i]==true? 'o' : 'x'; print(c); } println(""); } public static void printCharTable(char[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { print(a[i][j]); } println(""); } } public static void printIntTable(int[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } public static void printBooleanTable(boolean[][] b) { for (int i=0; i<b.length; i++) { for (int j=0; j<b[0].length; j++) { print(b[i][j]? "o" : "x"); } println(""); } } public static void printLongTable(long[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { print(a[i][j]==-infl? "# " : a[i][j]+" "); } println(""); } } ///////////////////////////////////////////////////////////////////////////////////////////////// // main method ///////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { solve(); out.close(); } } import java.io.*; import java.math.*; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.Map.Entry; import java.util.regex.Pattern; import java.util.stream.Collectors; class Main { public static void solve () { x = nextLong(); a = nextLong(); d = nextLong(); n = nextLong(); if (d < 0) { long temp = a + d * (n-1); a = temp; d *= -1; } if (x <= a) { println(a - x); return; } if (x >= a+(n-1)*d) { println(x - (a+(n-1)*d)); return; } println(binarySearch(0, n)); } public static long x, a, d, n; public static long binarySearch(long ok, long ng) { while (Math.abs(ok - ng) > 1) { long mid = (ok+ng)/2; if (isOK(mid) == true) ok = mid; else ng = mid; } long opNum1 = Math.abs(x - (a + ok*d)); long opNum2 = Math.abs(x - (a + ng*d)); return Math.min(opNum1, opNum2); } public static boolean isOK(long k) { //a+k*d のkの値で二分探索する。 if (a + k*d < x) return true; else return false; } ///////////////////////////////////////////////////////////////////////////////////////////////// // useful methods, useful fields, useful static inner class ///////////////////////////////////////////////////////////////////////////////////////////////// public static final int infi = (int)1e9; public static final long infl = (long)1e18; public static final int modi = (int)1e9 + 7; public static final long modl = (long)1e18 + 7; public static int[] dy = {-1, 0, 1, 0}; public static int[] dx = {0, 1, 0, -1}; public static class Edge { int id, from, to, cost; Edge(int to, int cost) { this.to = to; this.cost = cost; } Edge(int from, int to, int cost) { this.from = from; this.to = to; this.cost = cost; } Edge(int id, int from, int to, int cost) { this.id = id; this.from = from; this.to = to; this.cost = cost; } int getCost() {return this.cost;} } public static String yesno(boolean b) {return b ? "Yes" : "No";} ///////////////////////////////////////////////////////////////////////////////////////////////// // input ///////////////////////////////////////////////////////////////////////////////////////////////// public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 32768); public static StringTokenizer tokenizer = null; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public static String[] nextArray(int n) { String[] a = new String[n]; for (int i=0; i<n; i++) a[i] = next(); return a; } public static int nextInt() {return Integer.parseInt(next());}; public static int[] nextIntArray(int n) { int[] a = new int[n]; for (int i=0; i<n; i++) a[i] = nextInt(); return a; } public static int[][] nextIntTable(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; } public static long nextLong() {return Long.parseLong(next());} public static long[] nextLongArray(int n) { long[] a = new long[n]; for (int i=0; i<n; i++) a[i] = nextLong(); return a; } public static double nextDouble() {return Double.parseDouble(next());} public static char nextChar() {return next().charAt(0);} public static char[] nextCharArray() {return next().toCharArray();} public static char[][] nextCharTable(int n, int m) { char[][] a = new char[n][m]; for (int i=0; i<n; i++) { a[i] = next().toCharArray(); } return a; } public static List<List<Integer>> nextDirectedGraph(int n, int m) { List<List<Integer>> g = new ArrayList<>(); for (int i=0; i<n; i++) { g.add(new ArrayList<>()); } for (int i=0; i<m; i++) { int a = nextInt()-1, b = nextInt()-1; g.get(a).add(b); g.get(b).add(a); } return g; } public static List<List<Integer>> nextUndirectedGraph(int n, int m) { List<List<Integer>> g = new ArrayList<>(); for (int i=0; i<n; i++) { g.add(new ArrayList<>()); } for (int i=0; i<m; i++) { int a = nextInt()-1, b = nextInt()-1; g.get(a).add(b); } return g; } ///////////////////////////////////////////////////////////////////////////////////////////////// // output ///////////////////////////////////////////////////////////////////////////////////////////////// static PrintWriter out = new PrintWriter(System.out); public static void print(Object o) {out.print(o);} public static void println(Object o) {out.println(o);} public static void printStringArray(String[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printIntArray(int[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printLongArray(long[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printBooleanArray (boolean[] a) { for (int i=0; i<a.length; i++) { char c = a[i]==true? 'o' : 'x'; print(c); } println(""); } public static void printCharTable(char[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { print(a[i][j]); } println(""); } } public static void printIntTable(int[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } public static void printBooleanTable(boolean[][] b) { for (int i=0; i<b.length; i++) { for (int j=0; j<b[0].length; j++) { print(b[i][j]? "o" : "x"); } println(""); } } public static void printLongTable(long[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { print(a[i][j]==-infl? "# " : a[i][j]+" "); } println(""); } } ///////////////////////////////////////////////////////////////////////////////////////////////// // main method ///////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { solve(); out.close(); } }
ConDefects/ConDefects/Code/abc255_c/Java/39161799
condefects-java_data_2035
import java.io.*; import java.util.*; class Main{ final static long INF = Long.MAX_VALUE / 2; final static int MOD = 1_000_000_007; final static int SIZE = 1_000_000; long[] fac = new long[SIZE]; long[] inv = new long[SIZE]; long[] finv = new long[SIZE]; FastScanner sc = new FastScanner(); public static void main(String[] args) { new Main().solve(); } void solve(){ long X = sc.nextLong(); long A = sc.nextLong(); long D = sc.nextLong(); long N = sc.nextLong(); long l = A + D * (N - 1); long max = Math.max(A, l); long min = Math.min(A, l); if(X <= min) System.out.println(Math.abs(min - X)); else if(X >= max) System.out.println(Math.abs(max - X)); else System.out.println(Math.min((X - A) % D, D - (X - A) % D)); } long gcd(long a, long b){ // return aとbの最大公約数 if(b == 0){ return a; } return gcd(b, a % b); } long lcm(long a, long b){ // return aとbの最小公倍数 return a * b / gcd(a, b); } long inv(long a){ // return aの逆元 (mod MOD) return pow(a, MOD - 2); } long pow(long a, long r){ // return a^r (mod MOD) long sum = 1; while(r > 0){ if((r & 1) == 1){ // 2進数表記で末尾1の時 sum *= a; sum %= MOD; } a *= a; a %= MOD; r >>= 1; } return sum; } long modFact(long n){ // retur n! (mod MOD) if(n == 0){ return 1; } return n * modFact(n - 1) % MOD; } long fact(long n){ // return n! if(n == 0){ return 1; } return n * fact(n - 1); } void initCOMB(){ fac[0] = fac[1] = 1; inv[1] = 1; finv[0] = finv[1] = 1; for(int i = 2; i < SIZE; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } long modComb(int n, int r){ // return nCr (先にinitCOMB()必要) if(n < r || n < 0 || r < 0) return 0; return fac[n] * finv[r] % MOD * finv[n - r] % MOD; } long comb(long n, long r){ // return nCr long num = 1; for(long i = 1; i <= r; i++){ num = num * (n - i + 1) / i; } return num; } boolean isPrime(long a){ // aの素数判定 if(a <= 1) return false; for(int i = 2; i * i <= a; i++){ if(a % i == 0) return false; } return true; } int lowerBound(long[] a, long v){ // return 配列a内のv以上の要素の内最低の要素のイテレータ int r = a.length; int l = -1; while(r - l > 1){ int mid = (r + l) / 2; if(a[mid] >= v){ r = mid; }else{ l = mid; } } return r; } int lowerBound(List<Long> a, long v){ // return 配列a内のv以上の要素の内最低の要素のイテレータ int r = a.size(); int l = -1; while(r - l > 1){ int mid = (r + l) / 2; if(a.get(mid) >= v){ r = mid; }else{ l = mid; } } return r; } int upperBound(long[] a, long v){ // return 配列a内のvより大きい要素の内最低の要素のイテレータ int r = a.length; int l = -1; while(r - l > 1){ int mid = (r + l) / 2; if(a[mid] > v){ r = mid; }else{ l = mid; } } return r; } String nextPermutation(String s){ // return sの次の順列 ArrayList<Character> list = new ArrayList<>(); for(int i = 0; i < s.length(); i++) list.add(s.charAt(i)); int pivotPos = -1; char pivot = 0; for(int i = list.size() - 2; i >= 0; i--){ if(list.get(i) < list.get(i+1)){ pivotPos = i; pivot = list.get(i); break; } } if(pivotPos == -1 && pivot == 0) return null; int L = pivotPos + 1; int R = list.size() - 1; int minPos = -1; char min = Character.MAX_VALUE; for(int i = R; i >= L; i--){ if(pivot < list.get(i)){ if(list.get(i) < min){ min = list.get(i); minPos = i; } } } Collections.swap(list, pivotPos, minPos); Collections.sort(list.subList(L, R + 1)); StringBuilder sb = new StringBuilder(); for(int i=0; i<list.size(); i++) sb.append(list.get(i)); return sb.toString(); } boolean nextPermutation(long[] a){ for(int i = a.length - 1; i > 0; i--){ if(a[i - 1] < a[i]){ int swapIndex = find(a[i - 1], a, i, a.length - 1); long temp = a[swapIndex]; a[swapIndex] = a[i - 1]; a[i - 1] = temp; Arrays.sort(a, i, a.length); return true; } } return false; } int find(long dest, long[] a, int s, int e){ if(s == e){ return s; } int m = (s + e + 1) / 2; return a[m] <= dest ? find(dest, a, s, m - 1) : find(dest, a, m, e); } void elimination(int[][] a, int[] b) { int n = a.length; double f; for(int k = 0; k < n - 1; k++){ for(int i = k + 1; i < n; i++){ f = - a[i][k] / a[k][k]; for(int j = k + 1; j < n; j++){ a[i][j] += f * a[k][j]; } b[i] += f * b[k]; } for(int i = n - 1; i >= 0; i--){ for(int j = i + 1; j < n; j++){ b[i] -= a[i][j] * b[j]; } b[i] = b[i] / a[i][i]; } } } } class SegmentTree{ //------------------------------------------------------------ // 2 * n - 1 : 木全体のノード数 // i + n - 1 : 配列のi番目が対応するノードの番号 // 2 * i + 1, 2 * i + 2 : i番目のノードの子ノードの番号 // (i - 1) / 2 : i番目のノードの親ノードの番号 // // int n = sc.nextInt(); // long[] a = new long[n]; // for(int i = 0; i < n; i++) a[i] = sc.nextLong(); // SegmentTree st = new SegmentTree(a); // int l = sc.nextInt() - 1; // int r = sc.nextInt() - 1; // System.out.println(st.query(l, r)); //------------------------------------------------------------ final static long INF = Long.MAX_VALUE / 2; // long e = INF; // 単位元 long e = 0; long func(long a, long b){ // 処理 // return Math.min(a, b); return a + b; } int n; // 配列の要素数を超える最小の2のべき乗 long[] node; SegmentTree(long[] a){ init(a); } void init(long[] a){ // 配列aで初期化 n = 1; while(n < a.length){ n *= 2; } node = new long[2 * n - 1]; Arrays.fill(node, e); for(int i = 0; i < a.length; i++){ node[i + n - 1] = a[i]; } for(int i = n - 2; i >= 0; i--){ node[i] = func(node[2 * i + 1], node[2 * i + 2]); } } void update(int p, long v){ // 配列のp番目をvに変更し、木全体を更新 p = p + n - 1; node[p] = v; while(p > 0){ p = (p - 1) / 2; node[p] = func(node[2 * p + 1], node[2 * p + 2]); } } long query(int a, int b){ // 区間[a, b)についてクエリを処理 return query(a, b, 0, 0, n); } long query(int a, int b, int k, int l, int r){ if(r <= a || b <= l) return e; if(a <= l && r <= b) return node[k]; return func(query(a, b, 2 * k + 1, l, (l + r) / 2), query(a, b, 2 * k + 2, (l + r) / 2, r)); } } class UnionFindTree{ //------------------------------------------------------------ // int n = sc.nextInt(); // int q = sc.nextInt(); // UnionFindTree uft = new UnionFindTree(n); // List<String> ans = new ArrayList<>(); // for(int i = 0; i < q; i++){ // int p = sc.nextInt(); // 0 : union, 1 : same // if(p == 0){ // int a = sc.nextInt() - 1; // int b = sc.nextInt() - 1; // uft.union(a, b); // }else if(p == 1){ // int a = sc.nextInt() - 1; // int b = sc.nextInt() - 1; // if(uft.same(a, b)) ans.add("Yes"); // else ans.add("No"); // } // } // for(String s : ans){ // System.out.println(s); // } //------------------------------------------------------------ int[] parent; // インデックスにとノードを対応させ、そのルートノードのインデックスを格納 int[] rank; // parentと同様に、木の高さを格納 UnionFindTree(int size){ parent = new int[size]; rank = new int[size]; for(int i = 0; i < size; i++){ makeSet(i); } } void makeSet(int i){ parent[i] = i; rank[i] = 0; } void union(int x, int y){ int xRoot = find(x); int yRoot = find(y); if(rank[xRoot] > rank[yRoot]){ // xが属する木の方が大きい場合 parent[yRoot] = xRoot; }else if(rank[xRoot] < rank[yRoot]){ parent[xRoot] = yRoot; // yの親をxに更新 }else{ parent[yRoot] = xRoot; rank[xRoot]++; // 同じ高さの木がルートの子として着くから大きさ++; } } int find(int i){ // iの属するルートを返す if(i != parent[i]){ parent[i] = find(parent[i]); } return parent[i]; } boolean same(int x, int y){ // xとyが同じ木に属しているかを返す return find(x) == find(y); } } class FordFulkerson { //------------------------------------------------------------ // int n = sc.nextInt(); // int m = sc.nextInt(); // FordFulkerson ff = new FordFulkerson(n); // // for(int i = 0; i < m; i++){ // int a = sc.nextInt() - 1; // int b = sc.nextInt() - 1; // long c = sc.nextLong(); // ff.addEdge(a, b, c); // } // // System.out.println(ff.solve(0, n - 1)); //------------------------------------------------------------ final static long INF = Long.MAX_VALUE / 2; List<HashMap<Integer, Long>> l; boolean[] seen; FordFulkerson(int n) { l = new ArrayList<>(); for (int i = 0; i < n; i++) l.add(new HashMap<>()); seen = new boolean[n]; } void addEdge(int from, int to, long cap) { l.get(from).put(to, cap); l.get(to).put(from, 0L); } void runFlow(int from, int to, long flow) { l.get(from).put(to, l.get(from).get(to) - flow); l.get(to).put(from, l.get(to).get(from) + flow); } long ffdfs(int v, int t, long f) { if (v == t) return f; seen[v] = true; for (int next: l.get(v).keySet()) { if (seen[next]) continue; if (l.get(v).get(next) == 0) continue; long flow = ffdfs(next, t, Math.min(f, l.get(v).get(next))); if (flow == 0) continue; runFlow(v, next, flow); return flow; } return 0; } int solve(int s, int t) { int res = 0; while (true) { Arrays.fill(seen, false); long flow = ffdfs(s, t, INF); if(flow == 0) return res; res += flow; } } } class Pair implements Comparable<Pair>{ long a, b; public Pair(long a, long b){ this.a = a; this.b = b; } @Override public int compareTo(Pair p){ if(this.b < p.b) return -1; else if(this.b > p.b) return 1; else return 0; } @Override public String toString(){ return a + " " + b; } } class Triple implements Comparable<Triple>{ long a, b, c; public Triple(long a, long b, long c){ this.a = a; this.b = b; this.c = c; } @Override public int compareTo(Triple q){ if(this.c < q.c) return -1; else if(this.c > q.c) return 1; else return 0; } @Override public String toString(){ return a + " " + b + " " + c; } } class Quadruple implements Comparable<Quadruple>{ long a, b, c, d; public Quadruple(long a, long b, long c, long d){ this.a = a; this.b = b; this.c = c; this.d = d; } @Override public int compareTo(Quadruple q){ if(this.d < q.d) return -1; else if(this.d > q.d) return 1; else return 0; } @Override public String toString(){ return a + " " + b + " " + c + " " + d; } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte(){ if(ptr < buflen){ return true; }else{ ptr = 0; try{ buflen = in.read(buffer); }catch(IOException e){ e.printStackTrace(); } if(buflen <= 0){ return false; } } return true; } private int readByte(){ if(hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c){ return 33 <= c && c <= 126; } public boolean hasNext(){ while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next(){ if(!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong(){ if(!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if(b == '-'){ minus = true; b = readByte(); } if(b < '0' || '9' < b){ throw new NumberFormatException(); } while(true){ if('0' <= b && b <= '9'){ n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt(){ long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble(){ return Double.parseDouble(next()); } } import java.io.*; import java.util.*; class Main{ final static long INF = Long.MAX_VALUE / 2; final static int MOD = 1_000_000_007; final static int SIZE = 1_000_000; long[] fac = new long[SIZE]; long[] inv = new long[SIZE]; long[] finv = new long[SIZE]; FastScanner sc = new FastScanner(); public static void main(String[] args) { new Main().solve(); } void solve(){ long X = sc.nextLong(); long A = sc.nextLong(); long D = sc.nextLong(); long N = sc.nextLong(); long l = A + D * (N - 1); long max = Math.max(A, l); long min = Math.min(A, l); if(X <= min) System.out.println(Math.abs(min - X)); else if(X >= max) System.out.println(Math.abs(max - X)); else System.out.println(Math.min(Math.abs((X - A) % D), Math.abs(D - (X - A) % D))); } long gcd(long a, long b){ // return aとbの最大公約数 if(b == 0){ return a; } return gcd(b, a % b); } long lcm(long a, long b){ // return aとbの最小公倍数 return a * b / gcd(a, b); } long inv(long a){ // return aの逆元 (mod MOD) return pow(a, MOD - 2); } long pow(long a, long r){ // return a^r (mod MOD) long sum = 1; while(r > 0){ if((r & 1) == 1){ // 2進数表記で末尾1の時 sum *= a; sum %= MOD; } a *= a; a %= MOD; r >>= 1; } return sum; } long modFact(long n){ // retur n! (mod MOD) if(n == 0){ return 1; } return n * modFact(n - 1) % MOD; } long fact(long n){ // return n! if(n == 0){ return 1; } return n * fact(n - 1); } void initCOMB(){ fac[0] = fac[1] = 1; inv[1] = 1; finv[0] = finv[1] = 1; for(int i = 2; i < SIZE; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } long modComb(int n, int r){ // return nCr (先にinitCOMB()必要) if(n < r || n < 0 || r < 0) return 0; return fac[n] * finv[r] % MOD * finv[n - r] % MOD; } long comb(long n, long r){ // return nCr long num = 1; for(long i = 1; i <= r; i++){ num = num * (n - i + 1) / i; } return num; } boolean isPrime(long a){ // aの素数判定 if(a <= 1) return false; for(int i = 2; i * i <= a; i++){ if(a % i == 0) return false; } return true; } int lowerBound(long[] a, long v){ // return 配列a内のv以上の要素の内最低の要素のイテレータ int r = a.length; int l = -1; while(r - l > 1){ int mid = (r + l) / 2; if(a[mid] >= v){ r = mid; }else{ l = mid; } } return r; } int lowerBound(List<Long> a, long v){ // return 配列a内のv以上の要素の内最低の要素のイテレータ int r = a.size(); int l = -1; while(r - l > 1){ int mid = (r + l) / 2; if(a.get(mid) >= v){ r = mid; }else{ l = mid; } } return r; } int upperBound(long[] a, long v){ // return 配列a内のvより大きい要素の内最低の要素のイテレータ int r = a.length; int l = -1; while(r - l > 1){ int mid = (r + l) / 2; if(a[mid] > v){ r = mid; }else{ l = mid; } } return r; } String nextPermutation(String s){ // return sの次の順列 ArrayList<Character> list = new ArrayList<>(); for(int i = 0; i < s.length(); i++) list.add(s.charAt(i)); int pivotPos = -1; char pivot = 0; for(int i = list.size() - 2; i >= 0; i--){ if(list.get(i) < list.get(i+1)){ pivotPos = i; pivot = list.get(i); break; } } if(pivotPos == -1 && pivot == 0) return null; int L = pivotPos + 1; int R = list.size() - 1; int minPos = -1; char min = Character.MAX_VALUE; for(int i = R; i >= L; i--){ if(pivot < list.get(i)){ if(list.get(i) < min){ min = list.get(i); minPos = i; } } } Collections.swap(list, pivotPos, minPos); Collections.sort(list.subList(L, R + 1)); StringBuilder sb = new StringBuilder(); for(int i=0; i<list.size(); i++) sb.append(list.get(i)); return sb.toString(); } boolean nextPermutation(long[] a){ for(int i = a.length - 1; i > 0; i--){ if(a[i - 1] < a[i]){ int swapIndex = find(a[i - 1], a, i, a.length - 1); long temp = a[swapIndex]; a[swapIndex] = a[i - 1]; a[i - 1] = temp; Arrays.sort(a, i, a.length); return true; } } return false; } int find(long dest, long[] a, int s, int e){ if(s == e){ return s; } int m = (s + e + 1) / 2; return a[m] <= dest ? find(dest, a, s, m - 1) : find(dest, a, m, e); } void elimination(int[][] a, int[] b) { int n = a.length; double f; for(int k = 0; k < n - 1; k++){ for(int i = k + 1; i < n; i++){ f = - a[i][k] / a[k][k]; for(int j = k + 1; j < n; j++){ a[i][j] += f * a[k][j]; } b[i] += f * b[k]; } for(int i = n - 1; i >= 0; i--){ for(int j = i + 1; j < n; j++){ b[i] -= a[i][j] * b[j]; } b[i] = b[i] / a[i][i]; } } } } class SegmentTree{ //------------------------------------------------------------ // 2 * n - 1 : 木全体のノード数 // i + n - 1 : 配列のi番目が対応するノードの番号 // 2 * i + 1, 2 * i + 2 : i番目のノードの子ノードの番号 // (i - 1) / 2 : i番目のノードの親ノードの番号 // // int n = sc.nextInt(); // long[] a = new long[n]; // for(int i = 0; i < n; i++) a[i] = sc.nextLong(); // SegmentTree st = new SegmentTree(a); // int l = sc.nextInt() - 1; // int r = sc.nextInt() - 1; // System.out.println(st.query(l, r)); //------------------------------------------------------------ final static long INF = Long.MAX_VALUE / 2; // long e = INF; // 単位元 long e = 0; long func(long a, long b){ // 処理 // return Math.min(a, b); return a + b; } int n; // 配列の要素数を超える最小の2のべき乗 long[] node; SegmentTree(long[] a){ init(a); } void init(long[] a){ // 配列aで初期化 n = 1; while(n < a.length){ n *= 2; } node = new long[2 * n - 1]; Arrays.fill(node, e); for(int i = 0; i < a.length; i++){ node[i + n - 1] = a[i]; } for(int i = n - 2; i >= 0; i--){ node[i] = func(node[2 * i + 1], node[2 * i + 2]); } } void update(int p, long v){ // 配列のp番目をvに変更し、木全体を更新 p = p + n - 1; node[p] = v; while(p > 0){ p = (p - 1) / 2; node[p] = func(node[2 * p + 1], node[2 * p + 2]); } } long query(int a, int b){ // 区間[a, b)についてクエリを処理 return query(a, b, 0, 0, n); } long query(int a, int b, int k, int l, int r){ if(r <= a || b <= l) return e; if(a <= l && r <= b) return node[k]; return func(query(a, b, 2 * k + 1, l, (l + r) / 2), query(a, b, 2 * k + 2, (l + r) / 2, r)); } } class UnionFindTree{ //------------------------------------------------------------ // int n = sc.nextInt(); // int q = sc.nextInt(); // UnionFindTree uft = new UnionFindTree(n); // List<String> ans = new ArrayList<>(); // for(int i = 0; i < q; i++){ // int p = sc.nextInt(); // 0 : union, 1 : same // if(p == 0){ // int a = sc.nextInt() - 1; // int b = sc.nextInt() - 1; // uft.union(a, b); // }else if(p == 1){ // int a = sc.nextInt() - 1; // int b = sc.nextInt() - 1; // if(uft.same(a, b)) ans.add("Yes"); // else ans.add("No"); // } // } // for(String s : ans){ // System.out.println(s); // } //------------------------------------------------------------ int[] parent; // インデックスにとノードを対応させ、そのルートノードのインデックスを格納 int[] rank; // parentと同様に、木の高さを格納 UnionFindTree(int size){ parent = new int[size]; rank = new int[size]; for(int i = 0; i < size; i++){ makeSet(i); } } void makeSet(int i){ parent[i] = i; rank[i] = 0; } void union(int x, int y){ int xRoot = find(x); int yRoot = find(y); if(rank[xRoot] > rank[yRoot]){ // xが属する木の方が大きい場合 parent[yRoot] = xRoot; }else if(rank[xRoot] < rank[yRoot]){ parent[xRoot] = yRoot; // yの親をxに更新 }else{ parent[yRoot] = xRoot; rank[xRoot]++; // 同じ高さの木がルートの子として着くから大きさ++; } } int find(int i){ // iの属するルートを返す if(i != parent[i]){ parent[i] = find(parent[i]); } return parent[i]; } boolean same(int x, int y){ // xとyが同じ木に属しているかを返す return find(x) == find(y); } } class FordFulkerson { //------------------------------------------------------------ // int n = sc.nextInt(); // int m = sc.nextInt(); // FordFulkerson ff = new FordFulkerson(n); // // for(int i = 0; i < m; i++){ // int a = sc.nextInt() - 1; // int b = sc.nextInt() - 1; // long c = sc.nextLong(); // ff.addEdge(a, b, c); // } // // System.out.println(ff.solve(0, n - 1)); //------------------------------------------------------------ final static long INF = Long.MAX_VALUE / 2; List<HashMap<Integer, Long>> l; boolean[] seen; FordFulkerson(int n) { l = new ArrayList<>(); for (int i = 0; i < n; i++) l.add(new HashMap<>()); seen = new boolean[n]; } void addEdge(int from, int to, long cap) { l.get(from).put(to, cap); l.get(to).put(from, 0L); } void runFlow(int from, int to, long flow) { l.get(from).put(to, l.get(from).get(to) - flow); l.get(to).put(from, l.get(to).get(from) + flow); } long ffdfs(int v, int t, long f) { if (v == t) return f; seen[v] = true; for (int next: l.get(v).keySet()) { if (seen[next]) continue; if (l.get(v).get(next) == 0) continue; long flow = ffdfs(next, t, Math.min(f, l.get(v).get(next))); if (flow == 0) continue; runFlow(v, next, flow); return flow; } return 0; } int solve(int s, int t) { int res = 0; while (true) { Arrays.fill(seen, false); long flow = ffdfs(s, t, INF); if(flow == 0) return res; res += flow; } } } class Pair implements Comparable<Pair>{ long a, b; public Pair(long a, long b){ this.a = a; this.b = b; } @Override public int compareTo(Pair p){ if(this.b < p.b) return -1; else if(this.b > p.b) return 1; else return 0; } @Override public String toString(){ return a + " " + b; } } class Triple implements Comparable<Triple>{ long a, b, c; public Triple(long a, long b, long c){ this.a = a; this.b = b; this.c = c; } @Override public int compareTo(Triple q){ if(this.c < q.c) return -1; else if(this.c > q.c) return 1; else return 0; } @Override public String toString(){ return a + " " + b + " " + c; } } class Quadruple implements Comparable<Quadruple>{ long a, b, c, d; public Quadruple(long a, long b, long c, long d){ this.a = a; this.b = b; this.c = c; this.d = d; } @Override public int compareTo(Quadruple q){ if(this.d < q.d) return -1; else if(this.d > q.d) return 1; else return 0; } @Override public String toString(){ return a + " " + b + " " + c + " " + d; } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte(){ if(ptr < buflen){ return true; }else{ ptr = 0; try{ buflen = in.read(buffer); }catch(IOException e){ e.printStackTrace(); } if(buflen <= 0){ return false; } } return true; } private int readByte(){ if(hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c){ return 33 <= c && c <= 126; } public boolean hasNext(){ while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next(){ if(!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong(){ if(!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if(b == '-'){ minus = true; b = readByte(); } if(b < '0' || '9' < b){ throw new NumberFormatException(); } while(true){ if('0' <= b && b <= '9'){ n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt(){ long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble(){ return Double.parseDouble(next()); } }
ConDefects/ConDefects/Code/abc255_c/Java/34873361
condefects-java_data_2036
import java.util.*; import java.io.*; import java.math.*; class Main{ void solve(PrintWriter out, In in) { long x = in.nextLong() , a = in.nextLong() , d = in.nextLong() , n = in.nextLong(); if(d < 0) { long left = a + (n - 1) * d ; a = left ; d *= -1; } long l = 0 , r = n - 1 ; while(l <= r) { long mid = (r + l) / 2; if(a + d * mid < x) l = mid + 1; else r = mid - 1; } long ans = lnf; for(long i = Math.max(0L,l - 100); i <= Math.min(n - 1 , l + 100) ; i ++ ) { ans = Math.min(Math.abs((a + d * i) - x), ans); } out.print(ans); } void swap(int [] array , int l , int r) { int tmp = array[l]; array[l] = array[r]; array[r] = tmp; } void swap(long [] array , int l , int r) { long tmp = array[l]; array[l] = array[r]; array[r] = tmp; } int [] fill(int [] array , int max) { Arrays.fill(array,max); return array; } long [] fill(long [] array , long max) { Arrays.fill(array,max); return array; } int [][] fill(int [][] array , int max) { for(int [] tmp : array) Arrays.fill(tmp,max); return array; } long [][] fill(long [][] array , long max) { for(long [] tmp : array) Arrays.fill(tmp,max); return array; } public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); In in = new In(); new Main().solve(out,in); out.flush(); } final int inf = Integer.MAX_VALUE / 10 ; final long lnf = Long.MAX_VALUE / 10 ; } class Pair implements Comparable<Pair>{ private int first ; private int second; Pair(int first,int second) { this.first = first; this.second = second; } int first() { return this.first ; } int second() { return this.second; } @Override public boolean equals(Object o) { if (!(o instanceof Pair)) { return false; } Pair that = (Pair)o; return first == that.first && second == that.second; } @Override public int hashCode() { return Objects.hash(first, second); } @Override public int compareTo(Pair o) { return first == o.first ? Integer.compare(second, o.second) : Integer.compare(first, o.first); } @Override public String toString(){ return first()+" "+second(); } } class PairII { private int first; private int second; private int third; PairII(int first, int second, int third) { this.first = first; this.second = second; this.third = third; } int first() { return this.first; } int second() { return this.second; } int third() { return this.third; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } PairII other = (PairII) obj; return this.first == other.first && this.second == other.second && this.third == other.third; } @Override public int hashCode() { int result = 17; result = 31 * result + first; result = 31 * result + second; result = 31 * result + third; return result; } @Override public String toString() { return this.first+" "+this.second+" "+this.third; } } class In{ private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) { ptr++; } return hasNextByte(); } String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } double nextDouble() { return Double.parseDouble(next()); } int [] IntArray(int n) { final int [] Array = new int [n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = nextInt(); } return Array; } int [][] IntArray(int n , int m) { final int [][] Array = new int [n][m]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = IntArray(m); } return Array; } long [] LongArray(int n) { final long [] Array = new long [n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = nextLong(); } return Array; } long [][] LongArray(int n , int m) { final long [][] Array = new long [n][m]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = LongArray(m); } return Array; } String [] StringArray(int n) { final String [] Array = new String [n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = next(); } return Array; } char [] CharArray(int n) { final char [] Array = new char[n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = next().charAt(0); } return Array; } char [][] CharArray(int n , int m) { final char [][] Array = new char [n][m]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = next().toCharArray(); } return Array; } } import java.util.*; import java.io.*; import java.math.*; class Main{ void solve(PrintWriter out, In in) { long x = in.nextLong() , a = in.nextLong() , d = in.nextLong() , n = in.nextLong(); if(d < 0) { long left = a + (n - 1) * d ; a = left ; d *= -1; } long l = 0 , r = n - 1 ; while(l <= r) { long mid = (r + l) / 2; if(a + d * mid < x) l = mid + 1; else r = mid - 1; } long ans = (long)8e18; for(long i = Math.max(0L,l - 100); i <= Math.min(n - 1 , l + 100) ; i ++ ) { ans = Math.min(Math.abs((a + d * i) - x), ans); } out.print(ans); } void swap(int [] array , int l , int r) { int tmp = array[l]; array[l] = array[r]; array[r] = tmp; } void swap(long [] array , int l , int r) { long tmp = array[l]; array[l] = array[r]; array[r] = tmp; } int [] fill(int [] array , int max) { Arrays.fill(array,max); return array; } long [] fill(long [] array , long max) { Arrays.fill(array,max); return array; } int [][] fill(int [][] array , int max) { for(int [] tmp : array) Arrays.fill(tmp,max); return array; } long [][] fill(long [][] array , long max) { for(long [] tmp : array) Arrays.fill(tmp,max); return array; } public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); In in = new In(); new Main().solve(out,in); out.flush(); } final int inf = Integer.MAX_VALUE / 10 ; final long lnf = Long.MAX_VALUE / 10 ; } class Pair implements Comparable<Pair>{ private int first ; private int second; Pair(int first,int second) { this.first = first; this.second = second; } int first() { return this.first ; } int second() { return this.second; } @Override public boolean equals(Object o) { if (!(o instanceof Pair)) { return false; } Pair that = (Pair)o; return first == that.first && second == that.second; } @Override public int hashCode() { return Objects.hash(first, second); } @Override public int compareTo(Pair o) { return first == o.first ? Integer.compare(second, o.second) : Integer.compare(first, o.first); } @Override public String toString(){ return first()+" "+second(); } } class PairII { private int first; private int second; private int third; PairII(int first, int second, int third) { this.first = first; this.second = second; this.third = third; } int first() { return this.first; } int second() { return this.second; } int third() { return this.third; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } PairII other = (PairII) obj; return this.first == other.first && this.second == other.second && this.third == other.third; } @Override public int hashCode() { int result = 17; result = 31 * result + first; result = 31 * result + second; result = 31 * result + third; return result; } @Override public String toString() { return this.first+" "+this.second+" "+this.third; } } class In{ private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) { ptr++; } return hasNextByte(); } String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } double nextDouble() { return Double.parseDouble(next()); } int [] IntArray(int n) { final int [] Array = new int [n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = nextInt(); } return Array; } int [][] IntArray(int n , int m) { final int [][] Array = new int [n][m]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = IntArray(m); } return Array; } long [] LongArray(int n) { final long [] Array = new long [n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = nextLong(); } return Array; } long [][] LongArray(int n , int m) { final long [][] Array = new long [n][m]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = LongArray(m); } return Array; } String [] StringArray(int n) { final String [] Array = new String [n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = next(); } return Array; } char [] CharArray(int n) { final char [] Array = new char[n]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = next().charAt(0); } return Array; } char [][] CharArray(int n , int m) { final char [][] Array = new char [n][m]; for(int i = 0 ; i < n ; i ++ ) { Array[i] = next().toCharArray(); } return Array; } }
ConDefects/ConDefects/Code/abc255_c/Java/42775842
condefects-java_data_2037
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); int cnt = 0; String result = "Out"; for(int i = 0; i < n; i++) { if(s.charAt(i) == '|') { cnt++; } if(cnt == 1 && s.charAt(i) == '*') { result = "in"; } } System.out.println(result); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); int cnt = 0; String result = "out"; for(int i = 0; i < n; i++) { if(s.charAt(i) == '|') { cnt++; } if(cnt == 1 && s.charAt(i) == '*') { result = "in"; } } System.out.println(result); } }
ConDefects/ConDefects/Code/abc299_a/Java/42974908
condefects-java_data_2038
import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) throws IOException { new Thread(new Task()).start(); } static class Task implements Runnable { public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); InputReader in = new InputReader(inputStream); // for(int i=4;i<=4;i++) { // InputStream uinputStream = new FileInputStream("timeline.in"); // String f = i+".in"; // InputStream uinputStream = new FileInputStream(f); // InputReader in = new InputReader(uinputStream); // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("timeline.out"))); // } // PrintWriter out = new PrintWriter(new BufferedWriter(new // FileWriter("timeline.out"))); try { solve(in, out); } catch (IOException e) { e.printStackTrace(); } out.close(); } public void solve1(InputReader in, PrintWriter out) throws IOException { int n = in.nextInt(); int m = in.nextInt(); int arr[] = new int[n]; int brr[] = new int[n]; for(int i=0;i<n;i++) arr[i] = in.nextInt(); for(int i=0;i<n;i++) brr[i] = in.nextInt(); int crr[][] = new int[n][2]; for(int i=0;i<n;i++) { crr[i][0] = Math.max(arr[i]-m, brr[i]-m); crr[i][1] = Math.min(arr[i]+m, brr[i]+m); } ArrayList<pair>[] g = new ArrayList[n]; for(int i=0;i<n;i++) { g[i] = new ArrayList<pair>(); for(int j=crr[i][0];j<=crr[i][1];j++) { int d1 = Math.abs(j-arr[i]); int d2 = Math.abs(j-brr[i]); g[i].add(new pair(d1,d2)); } } int dp[][][] = new int[n+1][m+1][m+1]; int f = 998244353; dp[0][0][0] = 1; for(int i=1;i<=n;i++) { for(pair t:g[i-1]) { for(int j=t.a;j<=m;j++) { for(int k=t.b;k<=m;k++) { dp[i][j][k] += dp[i-1][j-t.a][k-t.b]; dp[i][j][k]%=f; } } } } int s = 0; for(int i=0;i<=m;i++) { for(int j=0;j<=m;j++) { s+=dp[n][i][j]; s%=f; } } out.println(s); } class pair { int a; int b; public pair(int x,int y) { a=x;b=y; } } class edge1 implements Comparable<edge1>{ int f,t,len; public edge1(int a, int b, int c) { f=a;t=b;len=c; } @Override public int compareTo(edge1 o) { // TODO Auto-generated method stub return 0; } } public void solve(InputReader in, PrintWriter out) throws IOException { int n = in.nextInt(); int crr[] = in.readIntArray(n); int xrr[] = in.readIntArray(n); ArrayList<Integer>[] g = new ArrayList[n+1]; for(int i=0;i<=n;i++) g[i] = new ArrayList<Integer>(); for(int i=0;i<n;i++) { g[crr[i]].add(xrr[i]); } int ret = 0; BIT b = new BIT(n+1); for(int i=n-1;i>=0;i--) { ret+=b.sum(xrr[i]-1);//inversion - same color b.add(xrr[i], 1); } b = new BIT(n+1); for(int i=1;i<=n;i++) { for(int j=g[i].size()-1;j>=0;j--) { ret-=b.sum(g[i].get(j)-1); b.add(g[i].get(j), 1); } for(int j=g[i].size()-1;j>=0;j--) { b.add(g[i].get(j), -1); } } out.println(ret); } public class edge implements Comparable<edge> { int f, t; int len; int id; public edge(int a, int b, int c, int d) { f = a; t = b; len = c; id = d; } @Override public int compareTo(Main.Task.edge o) { if (this.len - o.len < 0) return -1; else if (this.len == o.len) return 0; else return 1; } } public Set<Integer> get_factor(int number) { int n = number; Set<Integer> primeFactors = new HashSet<Integer>(); for (int i = 2; i <= n / i; i++) { while (n % i == 0) { primeFactors.add(i); n /= i; } } if (n > 1) primeFactors.add(n); return primeFactors; } private static long cnr(int n, int m, long mod, long fac[], long inv[]) { if (n < m) return 0; return fac[n] * inv[n - m] % mod * inv[m] % mod; } private static int combx(int n, int k, int mod) { if (n < k) return 0; int comb[][] = new int[n + 1][n + 1]; for (int i = 0; i <= n; i++) { comb[i][0] = comb[i][i] = 1; for (int j = 1; j < i; j++) { comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1]; comb[i][j] %= mod; } } return comb[n][k]; } private static long qpow(long a, long p, long MOD) { long m = Long.highestOneBit(p); long ans = 1; for (; m > 0; m >>>= 1) { ans = ans * ans % MOD; if ((p & m) > 0) ans = ans * a % MOD; } return (int) ans; } static class lca_naive { int n; ArrayList<edge>[] g; int lvl[]; int pare[]; int dist[]; public lca_naive(int t, ArrayList<edge>[] x) { n = t; g = x; lvl = new int[n]; pare = new int[n]; dist = new int[n]; } void pre_process() { dfs(0, -1, g, lvl, pare, dist); } void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[]) { for (edge nxt_edge : g[cur]) { if (nxt_edge.t != pre) { lvl[nxt_edge.t] = lvl[cur] + 1; dist[nxt_edge.t] = (int) (dist[cur] + nxt_edge.len); pare[nxt_edge.t] = cur; dfs(nxt_edge.t, cur, g, lvl, pare, dist); } } } public int work(int p, int q) { int a = p; int b = q; while (lvl[p] < lvl[q]) q = pare[q]; while (lvl[p] > lvl[q]) p = pare[p]; while (p != q) { p = pare[p]; q = pare[q]; } int c = p; return dist[a] + dist[b] - dist[c] * 2; } } static class lca_binary_lifting { int n; ArrayList<edge>[] g; int lvl[]; int pare[]; int dist[]; int table[][]; public lca_binary_lifting(int a, ArrayList<edge>[] t) { n = a; g = t; lvl = new int[n]; pare = new int[n]; dist = new int[n]; table = new int[20][n]; } void pre_process() { dfs(0, -1, g, lvl, pare, dist); for (int i = 0; i < 20; i++) { for (int j = 0; j < n; j++) { if (i == 0) table[0][j] = pare[j]; else table[i][j] = table[i - 1][table[i - 1][j]]; } } } void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[]) { for (edge nxt_edge : g[cur]) { if (nxt_edge.t != pre) { lvl[nxt_edge.t] = lvl[cur] + 1; dist[nxt_edge.t] = (int) (dist[cur] + nxt_edge.len); pare[nxt_edge.t] = cur; dfs(nxt_edge.t, cur, g, lvl, pare, dist); } } } public int work(int p, int q) { int a = p; int b = q; if (lvl[p] > lvl[q]) { int tmp = p; p = q; q = tmp; } for (int i = 19; i >= 0; i--) { if (lvl[table[i][q]] >= lvl[p]) q = table[i][q]; } if (p == q) return p;// return dist[a]+dist[b]-dist[p]*2; for (int i = 19; i >= 0; i--) { if (table[i][p] != table[i][q]) { p = table[i][p]; q = table[i][q]; } } return table[0][p]; // return dist[a]+dist[b]-dist[table[0][p]]*2; } } static class lca_sqrt_root { int n; ArrayList<edge>[] g; int lvl[]; int pare[]; int dist[]; int jump[]; int sz; public lca_sqrt_root(int a, ArrayList<edge>[] b) { n = a; g = b; lvl = new int[n]; pare = new int[n]; dist = new int[n]; jump = new int[n]; sz = (int) Math.sqrt(n); } void pre_process() { dfs(0, -1, g, lvl, pare, dist, jump); } void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[], int[] jump) { for (edge nxt_edge : g[cur]) { if (nxt_edge.t != pre) { lvl[nxt_edge.t] = lvl[cur] + 1; dist[nxt_edge.t] = (int) (dist[cur] + nxt_edge.len); pare[nxt_edge.t] = cur; if (lvl[nxt_edge.t] % sz == 0) { jump[nxt_edge.t] = cur; } else { jump[nxt_edge.t] = jump[cur]; } dfs(nxt_edge.t, cur, g, lvl, pare, dist, jump); } } } int work(int p, int q) { int a = p; int b = q; if (lvl[p] > lvl[q]) { int tmp = p; p = q; q = tmp; } while (jump[p] != jump[q]) { if (lvl[p] > lvl[q]) p = jump[p]; else q = jump[q]; } while (p != q) { if (lvl[p] > lvl[q]) p = pare[p]; else q = pare[q]; } return dist[a] + dist[b] - dist[p] * 2; } } static class Combination { private static final int MEMO_THRESHOLD = 1000000; static long mod = 1000000007; private static final List<Long> inv = new ArrayList<>(); private static final List<Long> fact = new ArrayList<>(); private static final List<Long> invFact = new ArrayList<>(); private static final Map<Long, List<Long>> pow = new HashMap<>(); private static void buildInvTable(int n) { if (inv.isEmpty()) { inv.add(null); inv.add(1L); } for (int i = inv.size(); i <= n; i++) { inv.add(mod - inv.get((int)(mod % i)) * (mod / i) % mod); } } private static void buildFactTable(int n) { if (fact.isEmpty()) { fact.add(1L); invFact.add(1L); } for (int i = fact.size(); i <= n; i++) { fact.add(fact.get(i - 1) * i % mod); invFact.add(inv(fact.get(i))); } } public static void setupPowTable(long a) { pow.put(a, new ArrayList<>(Collections.singleton(1L))); } private static void rangeCheck(long n, long r) { if (n < r) { throw new IllegalArgumentException("n < r"); } if (n < 0) { throw new IllegalArgumentException("n < 0"); } if (r < 0) { throw new IllegalArgumentException("r < 0"); } } static long fact(int n) { buildFactTable(n); return fact.get(n); } static long invFact(int n) { buildFactTable(n); return invFact.get(n); } private static long comb0(int n, int r) { rangeCheck(n, r); return fact(n) * invFact(r) % mod * invFact(n - r) % mod; } static long comb(long n, long r) { rangeCheck(n, r); if (n < MEMO_THRESHOLD) { return comb0((int)n, (int)r); } r = Math.min(r, n - r); long x = 1, y = 1; for (long i = 1; i <= r; i++) { x = x * (n - r + i) % mod; y = y * i % mod; } return x * inv(y) % mod; } private static long perm0(int n, int r) { rangeCheck(n, r); return fact(n) * invFact(n - r) % mod; } static long perm(long n, long r) { rangeCheck(n, r); if (n < MEMO_THRESHOLD) { return perm0((int)n, (int)r); } long x = 1; for (long i = 1; i <= r; i++) { x = x * (n - r + i) % mod; } return x; } static long homo(long n, long r) { return r == 0 ? 1 : comb(n + r - 1, r); } private static long inv0(int a) { buildInvTable(a); return inv.get(a); } static long inv(long a) { if (a < MEMO_THRESHOLD) { return inv0((int)a); } long b = mod; long u = 1, v = 0; while (b >= 1) { long t = a / b; a -= t * b; u -= t * v; if (a < 1) { return (v %= mod) < 0 ? v + mod : v; } t = b / a; b -= t * a; v -= t * u; } return (u %= mod) < 0 ? u + mod : u; } static long pow(long a, long b) { if (pow.containsKey(a) && b < MEMO_THRESHOLD) { return powMemo(a, (int)b); } long x = 1; while (b > 0) { if (b % 2 == 1) { x = x * a % mod; } a = a * a % mod; b >>= 1; } return x; } static long powMemo(long a, int b) { List<Long> powMemo = pow.get(a); while (powMemo.size() <= b) { powMemo.add(powMemo.get(powMemo.size() - 1) * a % Combination.mod); } return powMemo.get(b); } } static class lca_RMQ { int n; ArrayList<edge>[] g; int lvl[]; int dist[]; int tour[]; int tour_rank[]; int first_occ[]; int c; sgt s; public lca_RMQ(int a, ArrayList<edge>[] b) { n = a; g = b; c = 0; lvl = new int[n]; dist = new int[n]; tour = new int[2 * n]; tour_rank = new int[2 * n]; first_occ = new int[n]; Arrays.fill(first_occ, -1); } void pre_process() { tour[c++] = 0; dfs(0, -1); for (int i = 0; i < 2 * n; i++) { tour_rank[i] = lvl[tour[i]]; if (first_occ[tour[i]] == -1) first_occ[tour[i]] = i; } s = new sgt(0, 2 * n, tour_rank); } void dfs(int cur, int pre) { for (edge nxt_edge : g[cur]) { if (nxt_edge.t != pre) { lvl[nxt_edge.t] = lvl[cur] + 1; dist[nxt_edge.t] = (int) (dist[cur] + nxt_edge.len); tour[c++] = nxt_edge.t; dfs(nxt_edge.t, cur); tour[c++] = cur; } } } int work(int p, int q) { int a = Math.max(first_occ[p], first_occ[q]); int b = Math.min(first_occ[p], first_occ[q]); int idx = s.query_min_idx(b, a + 1); // Dumper.print(a+" "+b+" "+idx); int c = tour[idx]; return dist[p] + dist[q] - dist[c] * 2; } } static class sgt { sgt lt; sgt rt; int l, r; int sum, max, min, lazy; int min_idx; public sgt(int L, int R, int arr[]) { l = L; r = R; if (l == r - 1) { sum = max = min = arr[l]; lazy = 0; min_idx = l; return; } lt = new sgt(l, l + r >> 1, arr); rt = new sgt(l + r >> 1, r, arr); pop_up(); } void pop_up() { this.sum = lt.sum + rt.sum; this.max = Math.max(lt.max, rt.max); this.min = Math.min(lt.min, rt.min); if (lt.min < rt.min) this.min_idx = lt.min_idx; else if (lt.min > rt.min) this.min_idx = rt.min_idx; else this.min = Math.min(lt.min_idx, rt.min_idx); } void push_down() { if (this.lazy != 0) { lt.sum += lazy; rt.sum += lazy; lt.max += lazy; lt.min += lazy; rt.max += lazy; rt.min += lazy; lt.lazy += this.lazy; rt.lazy += this.lazy; this.lazy = 0; } } void change(int L, int R, int v) { if (R <= l || r <= L) return; if (L <= l && r <= R) { this.max += v; this.min += v; this.sum += v * (r - l); this.lazy += v; return; } push_down(); lt.change(L, R, v); rt.change(L, R, v); pop_up(); } int query_max(int L, int R) { if (L <= l && r <= R) return this.max; if (r <= L || R <= l) return Integer.MIN_VALUE; push_down(); return Math.max(lt.query_max(L, R), rt.query_max(L, R)); } int query_min(int L, int R) { if (L <= l && r <= R) return this.min; if (r <= L || R <= l) return Integer.MAX_VALUE; push_down(); return Math.min(lt.query_min(L, R), rt.query_min(L, R)); } int query_sum(int L, int R) { if (L <= l && r <= R) return this.sum; if (r <= L || R <= l) return 0; push_down(); return lt.query_sum(L, R) + rt.query_sum(L, R); } int query_min_idx(int L, int R) { if (L <= l && r <= R) return this.min_idx; if (r <= L || R <= l) return Integer.MAX_VALUE; int a = lt.query_min_idx(L, R); int b = rt.query_min_idx(L, R); int aa = lt.query_min(L, R); int bb = rt.query_min(L, R); if (aa < bb) return a; else if (aa > bb) return b; return Math.min(a, b); } } List<List<Integer>> convert(int arr[][]) { int n = arr.length; List<List<Integer>> ret = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> tmp = new ArrayList<Integer>(); for (int j = 0; j < arr[i].length; j++) tmp.add(arr[i][j]); ret.add(tmp); } return ret; } public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } public long GCD(long a, long b) { if (b == 0) return a; return GCD(b, a % b); } } static class ArrayUtils { static final long seed = System.nanoTime(); static final Random rand = new Random(seed); public static void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(int[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); int t = a[i]; a[i] = a[j]; a[j] = t; } } public static void sort(long[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(long[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); long t = a[i]; a[i] = a[j]; a[j] = t; } } } static class BIT { long arr[]; int n; public BIT(int a) { n = a; arr = new long[n]; } long sum(int p) { long s = 0; while (p > 0) { s += arr[p]; p -= p & (-p); } return s; } void add(int p, long v) { while (p < n) { arr[p] += v; p += p & (-p); } } } static class DSU { int[] arr; int[] sz; public DSU(int n) { arr = new int[n]; sz = new int[n]; for (int i = 0; i < n; i++) arr[i] = i; Arrays.fill(sz, 1); } public int find(int a) { if (arr[a] != a) arr[a] = find(arr[a]); return arr[a]; } public void union(int a, int b) { int x = find(a); int y = find(b); if (x == y) return; arr[y] = x; sz[x] += sz[y]; } public int size(int x) { return sz[find(x)]; } } private static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int zcurChar; private int znumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (znumChars == -1) throw new InputMismatchException(); if (zcurChar >= znumChars) { zcurChar = 0; try { znumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (znumChars <= 0) return -1; } return buf[zcurChar++]; } 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 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 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 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 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 nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public int[] readIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } static class Dumper { static void print_int_arr(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_char_arr(char[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_double_arr(double[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_2d_arr(int[][] arr, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("---------------------"); } static void print_2d_arr(boolean[][] arr, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("---------------------"); } static void print(Object o) { System.out.println(o.toString()); } static void getc() { System.out.println("here"); } } } import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) throws IOException { new Thread(new Task()).start(); } static class Task implements Runnable { public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); InputReader in = new InputReader(inputStream); // for(int i=4;i<=4;i++) { // InputStream uinputStream = new FileInputStream("timeline.in"); // String f = i+".in"; // InputStream uinputStream = new FileInputStream(f); // InputReader in = new InputReader(uinputStream); // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("timeline.out"))); // } // PrintWriter out = new PrintWriter(new BufferedWriter(new // FileWriter("timeline.out"))); try { solve(in, out); } catch (IOException e) { e.printStackTrace(); } out.close(); } public void solve1(InputReader in, PrintWriter out) throws IOException { int n = in.nextInt(); int m = in.nextInt(); int arr[] = new int[n]; int brr[] = new int[n]; for(int i=0;i<n;i++) arr[i] = in.nextInt(); for(int i=0;i<n;i++) brr[i] = in.nextInt(); int crr[][] = new int[n][2]; for(int i=0;i<n;i++) { crr[i][0] = Math.max(arr[i]-m, brr[i]-m); crr[i][1] = Math.min(arr[i]+m, brr[i]+m); } ArrayList<pair>[] g = new ArrayList[n]; for(int i=0;i<n;i++) { g[i] = new ArrayList<pair>(); for(int j=crr[i][0];j<=crr[i][1];j++) { int d1 = Math.abs(j-arr[i]); int d2 = Math.abs(j-brr[i]); g[i].add(new pair(d1,d2)); } } int dp[][][] = new int[n+1][m+1][m+1]; int f = 998244353; dp[0][0][0] = 1; for(int i=1;i<=n;i++) { for(pair t:g[i-1]) { for(int j=t.a;j<=m;j++) { for(int k=t.b;k<=m;k++) { dp[i][j][k] += dp[i-1][j-t.a][k-t.b]; dp[i][j][k]%=f; } } } } int s = 0; for(int i=0;i<=m;i++) { for(int j=0;j<=m;j++) { s+=dp[n][i][j]; s%=f; } } out.println(s); } class pair { int a; int b; public pair(int x,int y) { a=x;b=y; } } class edge1 implements Comparable<edge1>{ int f,t,len; public edge1(int a, int b, int c) { f=a;t=b;len=c; } @Override public int compareTo(edge1 o) { // TODO Auto-generated method stub return 0; } } public void solve(InputReader in, PrintWriter out) throws IOException { int n = in.nextInt(); int crr[] = in.readIntArray(n); int xrr[] = in.readIntArray(n); ArrayList<Integer>[] g = new ArrayList[n+1]; for(int i=0;i<=n;i++) g[i] = new ArrayList<Integer>(); for(int i=0;i<n;i++) { g[crr[i]].add(xrr[i]); } long ret = 0; BIT b = new BIT(n+1); for(int i=n-1;i>=0;i--) { ret+=b.sum(xrr[i]-1);//inversion - same color b.add(xrr[i], 1); } b = new BIT(n+1); for(int i=1;i<=n;i++) { for(int j=g[i].size()-1;j>=0;j--) { ret-=b.sum(g[i].get(j)-1); b.add(g[i].get(j), 1); } for(int j=g[i].size()-1;j>=0;j--) { b.add(g[i].get(j), -1); } } out.println(ret); } public class edge implements Comparable<edge> { int f, t; int len; int id; public edge(int a, int b, int c, int d) { f = a; t = b; len = c; id = d; } @Override public int compareTo(Main.Task.edge o) { if (this.len - o.len < 0) return -1; else if (this.len == o.len) return 0; else return 1; } } public Set<Integer> get_factor(int number) { int n = number; Set<Integer> primeFactors = new HashSet<Integer>(); for (int i = 2; i <= n / i; i++) { while (n % i == 0) { primeFactors.add(i); n /= i; } } if (n > 1) primeFactors.add(n); return primeFactors; } private static long cnr(int n, int m, long mod, long fac[], long inv[]) { if (n < m) return 0; return fac[n] * inv[n - m] % mod * inv[m] % mod; } private static int combx(int n, int k, int mod) { if (n < k) return 0; int comb[][] = new int[n + 1][n + 1]; for (int i = 0; i <= n; i++) { comb[i][0] = comb[i][i] = 1; for (int j = 1; j < i; j++) { comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1]; comb[i][j] %= mod; } } return comb[n][k]; } private static long qpow(long a, long p, long MOD) { long m = Long.highestOneBit(p); long ans = 1; for (; m > 0; m >>>= 1) { ans = ans * ans % MOD; if ((p & m) > 0) ans = ans * a % MOD; } return (int) ans; } static class lca_naive { int n; ArrayList<edge>[] g; int lvl[]; int pare[]; int dist[]; public lca_naive(int t, ArrayList<edge>[] x) { n = t; g = x; lvl = new int[n]; pare = new int[n]; dist = new int[n]; } void pre_process() { dfs(0, -1, g, lvl, pare, dist); } void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[]) { for (edge nxt_edge : g[cur]) { if (nxt_edge.t != pre) { lvl[nxt_edge.t] = lvl[cur] + 1; dist[nxt_edge.t] = (int) (dist[cur] + nxt_edge.len); pare[nxt_edge.t] = cur; dfs(nxt_edge.t, cur, g, lvl, pare, dist); } } } public int work(int p, int q) { int a = p; int b = q; while (lvl[p] < lvl[q]) q = pare[q]; while (lvl[p] > lvl[q]) p = pare[p]; while (p != q) { p = pare[p]; q = pare[q]; } int c = p; return dist[a] + dist[b] - dist[c] * 2; } } static class lca_binary_lifting { int n; ArrayList<edge>[] g; int lvl[]; int pare[]; int dist[]; int table[][]; public lca_binary_lifting(int a, ArrayList<edge>[] t) { n = a; g = t; lvl = new int[n]; pare = new int[n]; dist = new int[n]; table = new int[20][n]; } void pre_process() { dfs(0, -1, g, lvl, pare, dist); for (int i = 0; i < 20; i++) { for (int j = 0; j < n; j++) { if (i == 0) table[0][j] = pare[j]; else table[i][j] = table[i - 1][table[i - 1][j]]; } } } void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[]) { for (edge nxt_edge : g[cur]) { if (nxt_edge.t != pre) { lvl[nxt_edge.t] = lvl[cur] + 1; dist[nxt_edge.t] = (int) (dist[cur] + nxt_edge.len); pare[nxt_edge.t] = cur; dfs(nxt_edge.t, cur, g, lvl, pare, dist); } } } public int work(int p, int q) { int a = p; int b = q; if (lvl[p] > lvl[q]) { int tmp = p; p = q; q = tmp; } for (int i = 19; i >= 0; i--) { if (lvl[table[i][q]] >= lvl[p]) q = table[i][q]; } if (p == q) return p;// return dist[a]+dist[b]-dist[p]*2; for (int i = 19; i >= 0; i--) { if (table[i][p] != table[i][q]) { p = table[i][p]; q = table[i][q]; } } return table[0][p]; // return dist[a]+dist[b]-dist[table[0][p]]*2; } } static class lca_sqrt_root { int n; ArrayList<edge>[] g; int lvl[]; int pare[]; int dist[]; int jump[]; int sz; public lca_sqrt_root(int a, ArrayList<edge>[] b) { n = a; g = b; lvl = new int[n]; pare = new int[n]; dist = new int[n]; jump = new int[n]; sz = (int) Math.sqrt(n); } void pre_process() { dfs(0, -1, g, lvl, pare, dist, jump); } void dfs(int cur, int pre, ArrayList<edge>[] g, int lvl[], int pare[], int dist[], int[] jump) { for (edge nxt_edge : g[cur]) { if (nxt_edge.t != pre) { lvl[nxt_edge.t] = lvl[cur] + 1; dist[nxt_edge.t] = (int) (dist[cur] + nxt_edge.len); pare[nxt_edge.t] = cur; if (lvl[nxt_edge.t] % sz == 0) { jump[nxt_edge.t] = cur; } else { jump[nxt_edge.t] = jump[cur]; } dfs(nxt_edge.t, cur, g, lvl, pare, dist, jump); } } } int work(int p, int q) { int a = p; int b = q; if (lvl[p] > lvl[q]) { int tmp = p; p = q; q = tmp; } while (jump[p] != jump[q]) { if (lvl[p] > lvl[q]) p = jump[p]; else q = jump[q]; } while (p != q) { if (lvl[p] > lvl[q]) p = pare[p]; else q = pare[q]; } return dist[a] + dist[b] - dist[p] * 2; } } static class Combination { private static final int MEMO_THRESHOLD = 1000000; static long mod = 1000000007; private static final List<Long> inv = new ArrayList<>(); private static final List<Long> fact = new ArrayList<>(); private static final List<Long> invFact = new ArrayList<>(); private static final Map<Long, List<Long>> pow = new HashMap<>(); private static void buildInvTable(int n) { if (inv.isEmpty()) { inv.add(null); inv.add(1L); } for (int i = inv.size(); i <= n; i++) { inv.add(mod - inv.get((int)(mod % i)) * (mod / i) % mod); } } private static void buildFactTable(int n) { if (fact.isEmpty()) { fact.add(1L); invFact.add(1L); } for (int i = fact.size(); i <= n; i++) { fact.add(fact.get(i - 1) * i % mod); invFact.add(inv(fact.get(i))); } } public static void setupPowTable(long a) { pow.put(a, new ArrayList<>(Collections.singleton(1L))); } private static void rangeCheck(long n, long r) { if (n < r) { throw new IllegalArgumentException("n < r"); } if (n < 0) { throw new IllegalArgumentException("n < 0"); } if (r < 0) { throw new IllegalArgumentException("r < 0"); } } static long fact(int n) { buildFactTable(n); return fact.get(n); } static long invFact(int n) { buildFactTable(n); return invFact.get(n); } private static long comb0(int n, int r) { rangeCheck(n, r); return fact(n) * invFact(r) % mod * invFact(n - r) % mod; } static long comb(long n, long r) { rangeCheck(n, r); if (n < MEMO_THRESHOLD) { return comb0((int)n, (int)r); } r = Math.min(r, n - r); long x = 1, y = 1; for (long i = 1; i <= r; i++) { x = x * (n - r + i) % mod; y = y * i % mod; } return x * inv(y) % mod; } private static long perm0(int n, int r) { rangeCheck(n, r); return fact(n) * invFact(n - r) % mod; } static long perm(long n, long r) { rangeCheck(n, r); if (n < MEMO_THRESHOLD) { return perm0((int)n, (int)r); } long x = 1; for (long i = 1; i <= r; i++) { x = x * (n - r + i) % mod; } return x; } static long homo(long n, long r) { return r == 0 ? 1 : comb(n + r - 1, r); } private static long inv0(int a) { buildInvTable(a); return inv.get(a); } static long inv(long a) { if (a < MEMO_THRESHOLD) { return inv0((int)a); } long b = mod; long u = 1, v = 0; while (b >= 1) { long t = a / b; a -= t * b; u -= t * v; if (a < 1) { return (v %= mod) < 0 ? v + mod : v; } t = b / a; b -= t * a; v -= t * u; } return (u %= mod) < 0 ? u + mod : u; } static long pow(long a, long b) { if (pow.containsKey(a) && b < MEMO_THRESHOLD) { return powMemo(a, (int)b); } long x = 1; while (b > 0) { if (b % 2 == 1) { x = x * a % mod; } a = a * a % mod; b >>= 1; } return x; } static long powMemo(long a, int b) { List<Long> powMemo = pow.get(a); while (powMemo.size() <= b) { powMemo.add(powMemo.get(powMemo.size() - 1) * a % Combination.mod); } return powMemo.get(b); } } static class lca_RMQ { int n; ArrayList<edge>[] g; int lvl[]; int dist[]; int tour[]; int tour_rank[]; int first_occ[]; int c; sgt s; public lca_RMQ(int a, ArrayList<edge>[] b) { n = a; g = b; c = 0; lvl = new int[n]; dist = new int[n]; tour = new int[2 * n]; tour_rank = new int[2 * n]; first_occ = new int[n]; Arrays.fill(first_occ, -1); } void pre_process() { tour[c++] = 0; dfs(0, -1); for (int i = 0; i < 2 * n; i++) { tour_rank[i] = lvl[tour[i]]; if (first_occ[tour[i]] == -1) first_occ[tour[i]] = i; } s = new sgt(0, 2 * n, tour_rank); } void dfs(int cur, int pre) { for (edge nxt_edge : g[cur]) { if (nxt_edge.t != pre) { lvl[nxt_edge.t] = lvl[cur] + 1; dist[nxt_edge.t] = (int) (dist[cur] + nxt_edge.len); tour[c++] = nxt_edge.t; dfs(nxt_edge.t, cur); tour[c++] = cur; } } } int work(int p, int q) { int a = Math.max(first_occ[p], first_occ[q]); int b = Math.min(first_occ[p], first_occ[q]); int idx = s.query_min_idx(b, a + 1); // Dumper.print(a+" "+b+" "+idx); int c = tour[idx]; return dist[p] + dist[q] - dist[c] * 2; } } static class sgt { sgt lt; sgt rt; int l, r; int sum, max, min, lazy; int min_idx; public sgt(int L, int R, int arr[]) { l = L; r = R; if (l == r - 1) { sum = max = min = arr[l]; lazy = 0; min_idx = l; return; } lt = new sgt(l, l + r >> 1, arr); rt = new sgt(l + r >> 1, r, arr); pop_up(); } void pop_up() { this.sum = lt.sum + rt.sum; this.max = Math.max(lt.max, rt.max); this.min = Math.min(lt.min, rt.min); if (lt.min < rt.min) this.min_idx = lt.min_idx; else if (lt.min > rt.min) this.min_idx = rt.min_idx; else this.min = Math.min(lt.min_idx, rt.min_idx); } void push_down() { if (this.lazy != 0) { lt.sum += lazy; rt.sum += lazy; lt.max += lazy; lt.min += lazy; rt.max += lazy; rt.min += lazy; lt.lazy += this.lazy; rt.lazy += this.lazy; this.lazy = 0; } } void change(int L, int R, int v) { if (R <= l || r <= L) return; if (L <= l && r <= R) { this.max += v; this.min += v; this.sum += v * (r - l); this.lazy += v; return; } push_down(); lt.change(L, R, v); rt.change(L, R, v); pop_up(); } int query_max(int L, int R) { if (L <= l && r <= R) return this.max; if (r <= L || R <= l) return Integer.MIN_VALUE; push_down(); return Math.max(lt.query_max(L, R), rt.query_max(L, R)); } int query_min(int L, int R) { if (L <= l && r <= R) return this.min; if (r <= L || R <= l) return Integer.MAX_VALUE; push_down(); return Math.min(lt.query_min(L, R), rt.query_min(L, R)); } int query_sum(int L, int R) { if (L <= l && r <= R) return this.sum; if (r <= L || R <= l) return 0; push_down(); return lt.query_sum(L, R) + rt.query_sum(L, R); } int query_min_idx(int L, int R) { if (L <= l && r <= R) return this.min_idx; if (r <= L || R <= l) return Integer.MAX_VALUE; int a = lt.query_min_idx(L, R); int b = rt.query_min_idx(L, R); int aa = lt.query_min(L, R); int bb = rt.query_min(L, R); if (aa < bb) return a; else if (aa > bb) return b; return Math.min(a, b); } } List<List<Integer>> convert(int arr[][]) { int n = arr.length; List<List<Integer>> ret = new ArrayList<>(); for (int i = 0; i < n; i++) { ArrayList<Integer> tmp = new ArrayList<Integer>(); for (int j = 0; j < arr[i].length; j++) tmp.add(arr[i][j]); ret.add(tmp); } return ret; } public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } public long GCD(long a, long b) { if (b == 0) return a; return GCD(b, a % b); } } static class ArrayUtils { static final long seed = System.nanoTime(); static final Random rand = new Random(seed); public static void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(int[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); int t = a[i]; a[i] = a[j]; a[j] = t; } } public static void sort(long[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(long[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); long t = a[i]; a[i] = a[j]; a[j] = t; } } } static class BIT { long arr[]; int n; public BIT(int a) { n = a; arr = new long[n]; } long sum(int p) { long s = 0; while (p > 0) { s += arr[p]; p -= p & (-p); } return s; } void add(int p, long v) { while (p < n) { arr[p] += v; p += p & (-p); } } } static class DSU { int[] arr; int[] sz; public DSU(int n) { arr = new int[n]; sz = new int[n]; for (int i = 0; i < n; i++) arr[i] = i; Arrays.fill(sz, 1); } public int find(int a) { if (arr[a] != a) arr[a] = find(arr[a]); return arr[a]; } public void union(int a, int b) { int x = find(a); int y = find(b); if (x == y) return; arr[y] = x; sz[x] += sz[y]; } public int size(int x) { return sz[find(x)]; } } private static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int zcurChar; private int znumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (znumChars == -1) throw new InputMismatchException(); if (zcurChar >= znumChars) { zcurChar = 0; try { znumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (znumChars <= 0) return -1; } return buf[zcurChar++]; } 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 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 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 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 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 nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public int[] readIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } static class Dumper { static void print_int_arr(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_char_arr(char[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_double_arr(double[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_2d_arr(int[][] arr, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("---------------------"); } static void print_2d_arr(boolean[][] arr, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("---------------------"); } static void print(Object o) { System.out.println(o.toString()); } static void getc() { System.out.println("here"); } } }
ConDefects/ConDefects/Code/abc261_f/Java/37618812
condefects-java_data_2039
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String[] str = {"a", "b", "c", "d", "e", "f", "g", "h"}; int[] num = {1, 2, 3, 4, 5, 6, 7, 8}; for(int i = 0; i < 8; i++){ String[] m = sc.nextLine().split(""); for(int j = 0; j < 8; j++){ if(m[j].equals("*")){ System.out.println(str[j] + "" + num[j]); return; } } } } } import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String[] str = {"a", "b", "c", "d", "e", "f", "g", "h"}; int[] num = {1, 2, 3, 4, 5, 6, 7, 8}; for(int i = 0; i < 8; i++){ String[] m = sc.nextLine().split(""); for(int j = 0; j < 8; j++){ if(m[j].equals("*")){ System.out.println(str[j] + "" + num[7 - i]); return; } } } } }
ConDefects/ConDefects/Code/abc296_b/Java/43230575
condefects-java_data_2040
import java.io.PrintWriter; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); String t = sc.next(); char[] s = t.toCharArray(); int x = 0, y = 0, d = 0; for (int i=0; i<n;i++) { if (s[i]=='S') { if (d==0) x++; if (d==1) y--; if (d==2) x--; if (d==3) y++; } else { d=(d+1)%4; } } out.println("x y"); out.flush(); } } import java.io.PrintWriter; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); String t = sc.next(); char[] s = t.toCharArray(); int x = 0, y = 0, d = 0; for (int i=0; i<n;i++) { if (s[i]=='S') { if (d==0) x++; if (d==1) y--; if (d==2) x--; if (d==3) y++; } else { d=(d+1)%4; } } out.println(x+" "+y); out.flush(); } }
ConDefects/ConDefects/Code/abc244_b/Java/42762772
condefects-java_data_2041
import java.util.Scanner; public class Main { public static void main(String []args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); int count = 0; int x = 0; int y = 0; for (int i = 0;i < s.length();i++) { if (s.charAt(i) == 'r') { count++; count = count % 4; } if (s.charAt(i) == 'S'&& count % 4 == 0) { x++; } else if (s.charAt(i) == 'S'&& count % 4 == 1) { y--; } else if (s.charAt(i) == 'S' && count % 4 == 2) { x--; } else if (s.charAt(i) == 'S' && count % 4 == 3) { y++; } } System.out.println(x+" "+y); } } import java.util.Scanner; public class Main { public static void main(String []args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); int count = 0; int x = 0; int y = 0; for (int i = 0;i < s.length();i++) { if (s.charAt(i) == 'R') { count++; count = count % 4; } if (s.charAt(i) == 'S'&& count % 4 == 0) { x++; } else if (s.charAt(i) == 'S'&& count % 4 == 1) { y--; } else if (s.charAt(i) == 'S' && count % 4 == 2) { x--; } else if (s.charAt(i) == 'S' && count % 4 == 3) { y++; } } System.out.println(x+" "+y); } }
ConDefects/ConDefects/Code/abc244_b/Java/41262899
condefects-java_data_2042
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int N = sc.nextInt(); String str = sc.next(); char[] c = str.toCharArray(); int X = 0; int Y = 0; //方角 char muki = 'E'; for(int i =0; i<c.length; i++) { if(c[i] == 'S') { if(muki == 'N') { Y++; }else if(muki == 'E') { X++; }else if(muki == 'S') { Y--; }else if(muki == 'W') { X--; } }else if(c[i] == 'R') { if(muki == 'N') { muki = 'E'; }else if(muki == 'E') { muki = 'S'; }else if(muki == 'S') { muki = 'W'; }else if(muki == 'W') { muki = 'N'; } } System.out.println(X+" "+Y); } } } import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int N = sc.nextInt(); String str = sc.next(); char[] c = str.toCharArray(); int X = 0; int Y = 0; //方角 char muki = 'E'; for(int i =0; i<c.length; i++) { if(c[i] == 'S') { if(muki == 'N') { Y++; }else if(muki == 'E') { X++; }else if(muki == 'S') { Y--; }else if(muki == 'W') { X--; } }else if(c[i] == 'R') { if(muki == 'N') { muki = 'E'; }else if(muki == 'E') { muki = 'S'; }else if(muki == 'S') { muki = 'W'; }else if(muki == 'W') { muki = 'N'; } } } System.out.println(X+" "+Y); } }
ConDefects/ConDefects/Code/abc244_b/Java/32431442
condefects-java_data_2043
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); String T = sc.next(); int d = 0; int x = 0; int y = 0; char[] t = T.toCharArray(); for(int i = 0; i < t.length; i++){ if(t[i] == 'S'){ if(d==0) x++; if(d==1) y--; if(d==2) x--; if(d==3) y++; } else{ d = d%4+1; } } System.out.println(x); System.out.println(y); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); String T = sc.next(); int d = 0; int x = 0; int y = 0; char[] t = T.toCharArray(); for(int i = 0; i < t.length; i++){ if(t[i] == 'S'){ if(d==0) x++; if(d==1) y--; if(d==2) x--; if(d==3) y++; } else{ d = (d+1)%4; } } System.out.println(x); System.out.println(y); } }
ConDefects/ConDefects/Code/abc244_b/Java/31985308
condefects-java_data_2044
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = Integer.parseInt(sc.next()); int[][] grid = new int[N][N]; for (int i = 0; i < N; i++) { String s = sc.next(); for (int j = 0; j < N; j++) { if (s.charAt(j) == '0') { grid[i][j] = 0; } else { grid[i][j] = 1; } } } sc.close(); int[][] newGrid = new int[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (i == 0) { if (j != 0) { newGrid[i][j] = grid[i][j - 1]; } else { newGrid[i][j] = grid[i + 1][j]; } } else if (i == N - 1) { if (j != N - 1) { newGrid[i][j] = grid[i][j + 1]; } else { newGrid[i][j] = grid[i - 1][j]; } } else { if (j == 0) { newGrid[i][j] = grid[i + 1][j]; } else if (j == N - 1) { newGrid[i][j] = grid[i - 1][j]; } } } } // Print the result for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { System.out.print(newGrid[i][j]); } System.out.println(); } } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = Integer.parseInt(sc.next()); int[][] grid = new int[N][N]; for (int i = 0; i < N; i++) { String s = sc.next(); for (int j = 0; j < N; j++) { if (s.charAt(j) == '0') { grid[i][j] = 0; } else { grid[i][j] = 1; } } } sc.close(); int[][] newGrid = new int[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (i == 0) { if (j != 0) { newGrid[i][j] = grid[i][j - 1]; } else { newGrid[i][j] = grid[i + 1][j]; } } else if (i == N - 1) { if (j != N - 1) { newGrid[i][j] = grid[i][j + 1]; } else { newGrid[i][j] = grid[i - 1][j]; } } else { if (j == 0) { newGrid[i][j] = grid[i + 1][j]; } else if (j == N - 1) { newGrid[i][j] = grid[i - 1][j]; } else { newGrid[i][j] = grid[i][j]; } } } } // Print the result for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { System.out.print(newGrid[i][j]); } System.out.println(); } } }
ConDefects/ConDefects/Code/abc309_b/Java/43580958
condefects-java_data_2045
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.NoSuchElementException; import java.util.TreeMap; public class Main { public static void main(String[] args) { final Scanner scanner = new Scanner(); int N = scanner.nextInt(); int M = scanner.nextInt(); int[][] Choco = new int[N][2]; int[][] Box = new int[M][2]; for (int j = 0; j < 2; j++) { for (int i = 0; i < N; i++) Choco[i][j] = scanner.nextInt(); } for (int j = 0; j < 2; j++) { for (int i = 0; i < M; i++) Box[i][j] = scanner.nextInt(); } Arrays.sort(Choco, Comparator.comparingInt(o -> o[0])); Arrays.sort(Box, Comparator.comparingInt(o -> o[0])); TreeMap<Integer,Integer> longerHeightBox = new TreeMap<Integer,Integer>(); int i = N-1, j = M-1; boolean flag = true; while (i>=0) { while (j>=0 && Box[j][0] >= Choco[i][0]) { final int finalJ = j; longerHeightBox.merge(Box[j][1], 1, (old, it) -> Box[finalJ][1] + 1); j--; } Integer lower_bound = longerHeightBox.ceilingKey(Choco[i][1]); if (lower_bound == null) { flag = false; break; } longerHeightBox.put(lower_bound, longerHeightBox.get(lower_bound) - 1); if (longerHeightBox.get(lower_bound) == 0) { longerHeightBox.remove(lower_bound); } i--; } System.out.println(flag ? "Yes" : "No"); } } @SuppressWarnings("unused") class Scanner { private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private String[] buffer; private int pointer = 0; public Scanner() { readLine(); } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); return Integer.parseInt(buffer[pointer++]); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); return Long.parseLong(buffer[pointer++]); } public double nextDouble() { if (!hasNext()) throw new NoSuchElementException(); return Double.parseDouble(buffer[pointer++]); } public String nextString() { if (!hasNext()) throw new NoSuchElementException(); return buffer[pointer++]; } public int[] nextIntArray(int size) { int[] result = new int[size]; for (int i = 0; i < size; i++) { result[i] = nextInt(); } return result; } public long[] nextLongArray(int size) { long[] result = new long[size]; for (int i = 0; i < size; i++) { result[i] = nextInt(); } return result; } public double[] nextDoubleArray(int size) { double[] result = new double[size]; for (int i = 0; i < size; i++) { result[i] = nextInt(); } return result; } private boolean hasNext() { String[] buffer = this.buffer; if (buffer == null) return false; if (pointer == buffer.length) return readLine(); return true; } private boolean readLine() { try { String line = reader.readLine(); if (line == null) { this.buffer = null; return false; } this.buffer = line.split(" "); pointer = 0; } catch (IOException e) { throw new RuntimeException(e); } return true; } } import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.NoSuchElementException; import java.util.TreeMap; public class Main { public static void main(String[] args) { final Scanner scanner = new Scanner(); int N = scanner.nextInt(); int M = scanner.nextInt(); int[][] Choco = new int[N][2]; int[][] Box = new int[M][2]; for (int j = 0; j < 2; j++) { for (int i = 0; i < N; i++) Choco[i][j] = scanner.nextInt(); } for (int j = 0; j < 2; j++) { for (int i = 0; i < M; i++) Box[i][j] = scanner.nextInt(); } Arrays.sort(Choco, Comparator.comparingInt(o -> o[0])); Arrays.sort(Box, Comparator.comparingInt(o -> o[0])); TreeMap<Integer,Integer> longerHeightBox = new TreeMap<Integer,Integer>(); int i = N-1, j = M-1; boolean flag = true; while (i>=0) { while (j>=0 && Box[j][0] >= Choco[i][0]) { final int finalJ = j; longerHeightBox.put(Box[j][1], longerHeightBox.getOrDefault(Box[j][1], 0) + 1); j--; } Integer lower_bound = longerHeightBox.ceilingKey(Choco[i][1]); if (lower_bound == null) { flag = false; break; } longerHeightBox.put(lower_bound, longerHeightBox.get(lower_bound) - 1); if (longerHeightBox.get(lower_bound) == 0) { longerHeightBox.remove(lower_bound); } i--; } System.out.println(flag ? "Yes" : "No"); } } @SuppressWarnings("unused") class Scanner { private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private String[] buffer; private int pointer = 0; public Scanner() { readLine(); } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); return Integer.parseInt(buffer[pointer++]); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); return Long.parseLong(buffer[pointer++]); } public double nextDouble() { if (!hasNext()) throw new NoSuchElementException(); return Double.parseDouble(buffer[pointer++]); } public String nextString() { if (!hasNext()) throw new NoSuchElementException(); return buffer[pointer++]; } public int[] nextIntArray(int size) { int[] result = new int[size]; for (int i = 0; i < size; i++) { result[i] = nextInt(); } return result; } public long[] nextLongArray(int size) { long[] result = new long[size]; for (int i = 0; i < size; i++) { result[i] = nextInt(); } return result; } public double[] nextDoubleArray(int size) { double[] result = new double[size]; for (int i = 0; i < size; i++) { result[i] = nextInt(); } return result; } private boolean hasNext() { String[] buffer = this.buffer; if (buffer == null) return false; if (pointer == buffer.length) return readLine(); return true; } private boolean readLine() { try { String line = reader.readLine(); if (line == null) { this.buffer = null; return false; } this.buffer = line.split(" "); pointer = 0; } catch (IOException e) { throw new RuntimeException(e); } return true; } }
ConDefects/ConDefects/Code/abc245_e/Java/37473738