├── Facebook Hacker Cup └── 2017 │ ├── Qualification Round │ ├── A.java │ ├── B.java │ └── C.java │ ├── Round1 │ ├── A.java │ ├── B.java │ └── C.java │ ├── Round2 │ ├── A.java │ ├── B.java │ └── D.java │ └── Round3 │ ├── A.java │ └── B.java ├── IO for Women ├── 2015 │ ├── A.java │ ├── B.java │ ├── C.java │ ├── D.java │ └── README.md ├── 2016 │ ├── A.java │ ├── B.java │ ├── C.java │ ├── D.java │ └── README.md └── 2017 │ ├── A.java │ ├── B.java │ ├── C.java │ ├── D.java │ └── README.md ├── README.md ├── apac ├── 2015 │ ├── RoundA │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ ├── D.java │ │ └── README.md │ ├── RoundB │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ ├── D.java │ │ └── README.md │ ├── RoundC │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ ├── D.java │ │ └── README.md │ └── RoundD │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ ├── C.js │ │ ├── D.java │ │ └── README.md ├── 2016 │ ├── Practice Round │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── README.md │ ├── RoundA │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ ├── D.java │ │ └── README.md │ ├── RoundB │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ ├── D.java │ │ └── README.md │ ├── RoundC │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ ├── D.java │ │ └── README.md │ ├── RoundD │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ ├── D.java │ │ └── README.md │ └── RoundE │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ ├── D.java │ │ └── README.md └── 2017 │ ├── Practice Round │ ├── A.java │ ├── B.java │ ├── C.java │ ├── D.java │ └── README.md │ ├── RoundA │ ├── A.java │ ├── B.java │ ├── C.java │ ├── D.java │ └── README.md │ ├── RoundB │ ├── A.java │ ├── B.java │ ├── C.java │ ├── D.java │ └── README.md │ ├── RoundC │ ├── A.java │ ├── B.java │ ├── C.java │ ├── D.java │ └── README.md │ ├── RoundD │ ├── A.java │ ├── B.java │ ├── C.java │ ├── D.java │ └── README.md │ └── RoundE │ ├── A.java │ ├── B.java │ ├── C.java │ ├── D.java │ └── README.md ├── codejam ├── 2008 │ ├── Qualification Round │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1A │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1B │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1C │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round2 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ ├── Round3 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ ├── Semifinal AMER │ │ ├── A.java │ │ ├── C.java │ │ └── D.java │ ├── Semifinal APAC │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ └── Semifinal EMEA │ │ ├── A.java │ │ ├── B.java │ │ └── C.java ├── 2009 │ ├── Qualification Round │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1A │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1B │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1C │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round2 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ └── Round3 │ │ ├── A.java │ │ ├── B.java │ │ └── C.java ├── 2010 │ ├── Qualification Round │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1A │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1B │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1C │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round2 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ └── Round3 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java ├── 2011 │ ├── Qualification Round │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ ├── Round1A │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1B │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1C │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round2 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ └── Round3 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java ├── 2012 │ ├── Qualification Round │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ ├── Round1A │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1B │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1C │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round2 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ └── Round3 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java ├── 2013 │ ├── Qualification Round │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ ├── Round1A │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1B │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1C │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round2 │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ └── Round3 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java ├── 2014 │ ├── Qualification Round │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ ├── Round1A │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1B │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1C │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round2 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ └── Round3 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java ├── 2015 │ ├── Qualification Round │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ ├── Round1A │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1B │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1C │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round2 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ └── Round3 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ ├── D.java │ │ └── E.java ├── 2016 │ ├── Qualification Round │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ ├── Round1A │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1B │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1C │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round2 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ └── Round3 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java ├── 2017 │ ├── Qualification Round │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java │ ├── Round1A │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1B │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round1C │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ ├── Round2 │ │ ├── A.java │ │ ├── B.java │ │ └── C.java │ └── Round3 │ │ ├── A.java │ │ ├── B.java │ │ ├── C.java │ │ └── D.java └── 2019 │ ├── Round1C │ ├── A--robot_programming_strategy.js │ ├── A.java │ ├── B.java │ └── C.java │ └── Round2 │ ├── A.java │ └── C.java └── kickstart ├── 2017 ├── Practice Round │ ├── A.java │ ├── A.js │ ├── B.java │ ├── C.java │ └── README.md ├── RoundA │ ├── A.java │ ├── B.java │ ├── C.java │ └── README.md ├── RoundB │ ├── A.java │ ├── B.java │ ├── C.java │ └── README.md ├── RoundC │ ├── A.java │ ├── B.java │ ├── C.java │ └── D.java ├── RoundD │ ├── A.java │ ├── B.java │ ├── C.java │ └── README.md ├── RoundE │ ├── A.java │ ├── B.java │ └── C.java ├── RoundF │ ├── A.java │ ├── B.java │ ├── C.java │ └── D.java └── RoundG │ ├── A.java │ ├── B.java │ ├── C.java │ └── README.md ├── 2018 ├── RoundA │ ├── A.java │ ├── A.js │ ├── B.java │ └── C.java ├── RoundC │ ├── A.java │ ├── B.java │ └── C.java └── RoundH │ └── B.js └── 2019 ├── Practice_Round └── B.js ├── RoundA └── A.js └── RoundB └── A--building_palindromes.js /Facebook Hacker Cup/2017/Qualification Round/A.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /** 5 | * Facebook Hacker Cup 2017 Qualification Round 6 | * Problem A. Progress Pie 7 | */ 8 | public class Main { 9 | 10 | public static String solve(Scanner scanner) { 11 | int p=scanner.nextInt(), x=scanner.nextInt(), y=scanner.nextInt(); 12 | if ((x-50)*(x-50)+(y-50)*(y-50)>=50*50) return "white"; 13 | if (p==0) return "white"; 14 | double angle=Math.atan2(x-50., y-50.); 15 | if (angle<0) angle+=2*Math.PI; 16 | return angle/2/Math.PI

list=new LinkedList<>(); 13 | for (int i=0;i=50) { 17 | int val=list.pollLast(), x=1; 18 | while (val*x<50) { 19 | x++; list.pollFirst(); 20 | } 21 | cnt++; 22 | } 23 | else break; 24 | } 25 | return String.valueOf(cnt); 26 | 27 | } 28 | 29 | public static void main(String[] args) throws Exception { 30 | System.setOut(new PrintStream("output.txt")); 31 | Scanner scanner=new Scanner(System.in); 32 | int times=Integer.parseInt(scanner.nextLine()); 33 | long start=System.currentTimeMillis(); 34 | for (int t=1;t<=times;t++) { 35 | try { 36 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 37 | } 38 | catch (Throwable e) { 39 | System.err.println("ERROR in case #"+t); 40 | e.printStackTrace(); 41 | } 42 | } 43 | long end=System.currentTimeMillis(); 44 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Facebook Hacker Cup/2017/Round1/A.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /** 5 | * Facebook Hacker Cup 2017 Round 1 6 | * Problem A. Pie Progress 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | int n=scanner.nextInt(), m=scanner.nextInt(); 12 | int[][] dp=new int[n+1][n+1]; 13 | for (int i=1;i<=n;i++) dp[0][i]=999999999; 14 | for (int i=1;i<=n;i++) { 15 | int[] data=new int[m+1]; 16 | for (int j=1;j<=m;j++) data[j]=scanner.nextInt(); 17 | Arrays.sort(data); 18 | for (int j=1;j<=m;j++) data[j]+=data[j-1]; 19 | for (int j=i;j<=n;j++) { 20 | dp[i][j]=999999999; 21 | for (int w=0;w<=m;w++) { 22 | if (j-w>=i-1) { 23 | dp[i][j]=Math.min(dp[i][j], dp[i-1][j-w]+data[w]+w*w); 24 | } 25 | } 26 | } 27 | } 28 | return String.valueOf(dp[n][n]); 29 | } 30 | 31 | public static void main(String[] args) throws Exception { 32 | System.setIn(new FileInputStream("input.txt")); 33 | System.setOut(new PrintStream("output.txt")); 34 | Scanner scanner=new Scanner(System.in); 35 | int times=Integer.parseInt(scanner.nextLine()); 36 | long start=System.currentTimeMillis(); 37 | for (int t=1;t<=times;t++) { 38 | try { 39 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 40 | } 41 | catch (Throwable e) { 42 | System.err.println("ERROR in case #"+t); 43 | e.printStackTrace(); 44 | } 45 | } 46 | long end=System.currentTimeMillis(); 47 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Facebook Hacker Cup/2017/Round2/A.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /** 5 | * Facebook Hacker Cup 2017 Round 2 6 | * Problem A. Subtle Sabotage 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | int m=scanner.nextInt(), n=scanner.nextInt(), k=scanner.nextInt(); 12 | if (m>=n) {int x=m;m=n;n=x;} 13 | int ans=Math.min(cal1(m,n,k),cal2(m,n,k)); 14 | return ans==Integer.MAX_VALUE?"-1":String.valueOf(ans); 15 | } 16 | 17 | private int cal1(int m, int n, int k) { 18 | return m>=k+1&&n>=2*k+3?(m-1)/k+1:Integer.MAX_VALUE; 19 | } 20 | 21 | private int cal2(int m, int n, int k) { 22 | if (k==1) 23 | return m>=3&&n>=5?5:Integer.MAX_VALUE; 24 | return m>=2*k+1&&n>=3*k+1?4:Integer.MAX_VALUE; 25 | } 26 | 27 | public static void main(String[] args) throws Exception { 28 | // System.setIn(new FileInputStream("input.txt")); 29 | System.setOut(new PrintStream("output.txt")); 30 | Scanner scanner=new Scanner(System.in); 31 | int times=Integer.parseInt(scanner.nextLine()); 32 | long start=System.currentTimeMillis(); 33 | for (int t=1;t<=times;t++) { 34 | try { 35 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 36 | } 37 | catch (Throwable e) { 38 | System.err.println("ERROR in case #"+t); 39 | e.printStackTrace(); 40 | } 41 | } 42 | long end=System.currentTimeMillis(); 43 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Facebook Hacker Cup/2017/Round3/A.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /** 5 | * Facebook Hacker Cup 2017 Round 3 6 | * Problem A. Salient Strings 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) throws Exception { 11 | int n=scanner.nextInt(); 12 | char[] s=new char[n]; 13 | int[] a=new int[n+1], p=new int[n]; 14 | for (int i=0;ia[p[i-1]+1]) 22 | s[p[i]]=s[p[i-1]]; 23 | else { 24 | if (s[p[i-1]]=='Z') return "-1"; 25 | s[p[i]]=(char)(s[p[i-1]]+1); 26 | } 27 | } 28 | return new String(s); 29 | } 30 | 31 | public static void main(String[] args) throws Exception { 32 | System.setIn(new FileInputStream("input.txt")); 33 | System.setOut(new PrintStream("output.txt")); 34 | Scanner scanner=new Scanner(System.in); 35 | int times=Integer.parseInt(scanner.nextLine()); 36 | long start=System.currentTimeMillis(); 37 | for (int t=1;t<=times;t++) { 38 | try { 39 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 40 | } 41 | catch (Throwable e) { 42 | System.err.println("ERROR in case #"+t); 43 | e.printStackTrace(); 44 | } 45 | } 46 | long end=System.currentTimeMillis(); 47 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /IO for Women/2015/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam to I/O for Women 2015 Problem A: I/O Error 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | int n=scanner.nextInt(); 12 | String s=scanner.next().replace('I','1').replace('O','0'); 13 | StringBuilder builder=new StringBuilder(); 14 | for (int i=0;i1) { 19 | v+=Math.log10(x); 20 | x-=mid; 21 | } 22 | if ((int)Math.ceil(v)s` then the number should be `3*t`. 15 | - If `a=b>s, c=s` then the number shoule be `3*t`. 16 | 17 | If `a!=b, b!=c, c!=a`: the number shoule be `6*t*(t-1)`. 18 | Sum them, and get `(3v^2+3v+1)*(k-v)+(v+1)^3`. 19 | 20 | ## Problem C: Power Levels 21 | 22 | How to calculates the digits number of `9000!!!!!...!`? 23 | Just log10 it! 24 | Binary search to find `log10(9000!!!....!) priorityQueue=new PriorityQueue<>(); 13 | for (int i=0;i<2*n;i++) priorityQueue.offer(scanner.nextInt()); 14 | ArrayList ans=new ArrayList<>(); 15 | while (!priorityQueue.isEmpty()) { 16 | int v=priorityQueue.poll(); 17 | priorityQueue.remove(v/3*4); 18 | ans.add(String.valueOf(v)); 19 | } 20 | return String.join(" ", ans); 21 | } 22 | 23 | public static void main(String[] args) throws Exception { 24 | System.setOut(new PrintStream("output.txt")); 25 | Scanner scanner=new Scanner(System.in); 26 | int times=scanner.nextInt(); 27 | long start=System.currentTimeMillis(); 28 | for (int t=1;t<=times;t++) { 29 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 30 | } 31 | long end=System.currentTimeMillis(); 32 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 33 | 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /IO for Women/2016/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam to I/O for Women 2016 Problem B: Dance Around The Clock 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | int d=scanner.nextInt(), k=scanner.nextInt(), n=scanner.nextInt(), x, y; 12 | if (k%2==0) { 13 | x=k+1-2*n; 14 | y=k-1-2*n; 15 | while (x<=0) x+=d; 16 | while (y<=0) y+=d; 17 | } 18 | else { 19 | x=k+1+2*n; 20 | y=k-1+2*n; 21 | while (x>d) x-=d; 22 | while (y>d) y-=d; 23 | } 24 | return x+" "+y; 25 | } 26 | 27 | public static void main(String[] args) throws Exception { 28 | System.setOut(new PrintStream("output.txt")); 29 | Scanner scanner=new Scanner(System.in); 30 | int times=scanner.nextInt(); 31 | long start=System.currentTimeMillis(); 32 | for (int t=1;t<=times;t++) { 33 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 34 | } 35 | long end=System.currentTimeMillis(); 36 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 37 | 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /IO for Women/2016/C.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam to I/O for Women 2016 Problem C: Polynesiaglot 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | int c=scanner.nextInt(), v=scanner.nextInt(), l=scanner.nextInt(); 12 | long[] dp=new long[l+1]; 13 | dp[0]=1;dp[1]=v; 14 | for (int i=2;i<=l;i++) { 15 | dp[i]=v*(dp[i-1]+c*dp[i-2])%1000000007; 16 | } 17 | return String.valueOf(dp[l]); 18 | } 19 | 20 | public static void main(String[] args) throws Exception { 21 | System.setOut(new PrintStream("output.txt")); 22 | Scanner scanner=new Scanner(System.in); 23 | int times=scanner.nextInt(); 24 | long start=System.currentTimeMillis(); 25 | for (int t=1;t<=times;t++) { 26 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 27 | } 28 | long end=System.currentTimeMillis(); 29 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 30 | 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /IO for Women/2016/D.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam to I/O for Women 2016 Problem D: Password Security 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | int n=scanner.nextInt(); 12 | String[] ans=new String[n]; 13 | for (int i=0;i arrayList=new ArrayList<>(); 16 | for (char c='A';c<='Z';c++) 17 | arrayList.add(String.valueOf(c)); 18 | Collections.shuffle(arrayList); 19 | String candidate=String.join("", arrayList); 20 | boolean able=true; 21 | for (String s: ans) { 22 | if (candidate.contains(s)) { 23 | able=false; 24 | break; 25 | } 26 | } 27 | if (able) return candidate; 28 | } 29 | return "IMPOSSIBLE"; 30 | } 31 | 32 | public static void main(String[] args) throws Exception { 33 | System.setOut(new PrintStream("output.txt")); 34 | Scanner scanner=new Scanner(System.in); 35 | int times=scanner.nextInt(); 36 | long start=System.currentTimeMillis(); 37 | for (int t=1;t<=times;t++) { 38 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 39 | } 40 | long end=System.currentTimeMillis(); 41 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 42 | 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /IO for Women/2016/README.md: -------------------------------------------------------------------------------- 1 | # Codejam to I/O for Women 2016 2 | https://code.google.com/codejam/contest/8274486/dashboard 3 | 4 | ## Problem A: Cody's Jams 5 | 6 | Simple. Each time, choose a smallest number x, remove x and 4x/3 in the array. 7 | 8 | ## Problem B: Dance Around The Clock 9 | 10 | Simple. For people on odd position, it will turn right every exchange, where 11 | people on even position will turn left. 12 | 13 | ## Problem C: Polynesiaglot 14 | 15 | DP. dp[n] means valid words number. 16 | Then, dp[0]=1, dp[1]=v, dp[n]=v*(dp[n-1]+c*dp[n-2]). 17 | 18 | ## Problem D: Password Security 19 | 20 | Random to generate 100000 strings! 21 | -------------------------------------------------------------------------------- /IO for Women/2017/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam to I/O for Women 2017 Problem A: Ticket Trouble 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | int f=scanner.nextInt(), s=scanner.nextInt(); 12 | int[][] seats=new int[f][2]; 13 | for (int i=0;i arrayList=new ArrayList<>(); 16 | int three=Math.min(13*7, n/3); 17 | int one=n-three*3; 18 | for (int line=0;line<15;line++) { 19 | if (line%4==0) arrayList.add(new String(allIs)); 20 | else if (line%4==2) arrayList.add(new String(allOs)); 21 | else { 22 | char[] tmp=Arrays.copyOf(allIs, 15); 23 | if (one-->0) 24 | tmp[0]='/'; 25 | for (int j=1;j<14;j++) { 26 | if (three-->0) 27 | tmp[j]='/'; 28 | } 29 | if (one-->0) 30 | tmp[14]='/'; 31 | arrayList.add(new String(tmp)); 32 | } 33 | } 34 | return "\n"+String.join("\n", arrayList); 35 | } 36 | 37 | public static void main(String[] args) throws Exception { 38 | System.setOut(new PrintStream("output.txt")); 39 | Scanner scanner=new Scanner(System.in); 40 | int times=scanner.nextInt(); 41 | long start=System.currentTimeMillis(); 42 | for (int t=1;t<=times;t++) { 43 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 44 | } 45 | long end=System.currentTimeMillis(); 46 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 47 | 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /IO for Women/2017/README.md: -------------------------------------------------------------------------------- 1 | # Codejam to I/O for Women 2017 2 | https://code.google.com/codejam/contest/12224486/dashboard 3 | 4 | ## Problem A: Ticket Trouble 5 | 6 | Simple. For each row, greedy to get the maximum number. 7 | 8 | ## Problem B: Understudies 9 | 10 | Sort all probabilities. Then match the smallest one with the biggest one. 11 | 12 | ## Problem C: Word Search 13 | 14 | Not so simple. 15 | Just consider of this pattern: 16 | ``` 17 | IIIIIIIIIIIIII 18 | ////////////// 19 | OOOOOOOOOOOOOO 20 | ////////////// 21 | IIIIIIIIIIIIII 22 | ... 23 | ``` 24 | For each `/` on border, it will create 1 `I/O`. 25 | For each `/` in middle, it will create 3 `I/O`. 26 | 27 | ## Problem D: Where Ya Gonna Call? 28 | 29 | This problem is hard, very hard, maybe the hardest problem in Codejam to I/O for Women. 30 | Using Floyd algorithm to calculate distance between any nodes. 31 | Then, for each edge, for each possible position, check the minimax. 32 | The problem is, what's the possible position? 33 | 34 | Assume the verticals of the edge is `u, v` and its length is `l`. 35 | Assume the distance between u and any point is `a[]`, distance 36 | between v and any point is `b[]`. 37 | Then, all possible positions x should satisfies `a[i]+x = b[j]+(l-x)`. 38 | So, just iterate i and j, to calculate all possible x, and find the minimax. 39 | 40 | -------------------------------------------------------------------------------- /apac/2015/RoundA/D.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2015 Round A Problem D: Cut Tiles 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | int n=scanner.nextInt(), m=scanner.nextInt(); 12 | int[] size=new int[32]; 13 | for (int i=0;i=0;index--) { 27 | if (b<(1< a+b=3.5 (output 3)`. 23 | Check my code [C.java](C.java) for more details. A really hard problem. 24 | 25 | ## Problem D: Cut Tiles 26 | 27 | Greedy problem. For each tile, try to cut it to satisfy tiles as much as possible. 28 | Harder than A/B, but easier than C, I think. 29 | 30 | -------------------------------------------------------------------------------- /apac/2015/RoundB/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2015 Round B Problem A: Password Attacker 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | int m=scanner.nextInt(), n=scanner.nextInt(); 12 | long mod=1000000007; 13 | long[][] c=new long[n+1][n+1]; 14 | for (int j=0;j<=n;j++) c[0][j]=1; 15 | for (int i=1;i<=n;i++) { 16 | for (int j=i;j<=n;j++) { 17 | c[i][j]=(c[i-1][j-1]+c[i][j-1])%mod; 18 | } 19 | } 20 | long[][] dp=new long[m+1][n+1]; 21 | dp[0][0]=1; 22 | for (int i=1;i<=m;i++) { 23 | for (int j=i;j<=n;j++) { 24 | for (int k=1;k<=j-i+1;k++) { 25 | dp[i][j]+=(c[k][j]*dp[i-1][j-k])%mod; 26 | dp[i][j]%=mod; 27 | } 28 | } 29 | } 30 | return String.valueOf(dp[m][n]); 31 | } 32 | 33 | public static void main(String[] args) throws Exception { 34 | System.setOut(new PrintStream("output.txt")); 35 | Scanner scanner=new Scanner(System.in); 36 | int times=scanner.nextInt(); 37 | long start=System.currentTimeMillis(); 38 | for (int t=1;t<=times;t++) { 39 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 40 | } 41 | long end=System.currentTimeMillis(); 42 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /apac/2015/RoundB/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2015 Round B Problem B: New Years Eve 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | int b=scanner.nextInt(), l=scanner.nextInt(), n=scanner.nextInt(); 12 | double[] curr=new double[2]; 13 | curr[1]=750.*b; 14 | for (int i=1;irow*(row+1)/2) row++; 20 | if (curr[j]>250.) { 21 | double overflow=curr[j]-250; 22 | nxt[j]+=overflow/3; 23 | nxt[j+row]+=overflow/3; 24 | nxt[j+row+1]+=overflow/3; 25 | } 26 | } 27 | curr=nxt; 28 | } 29 | return String.format("%.9f", Math.min(250., curr[n])); 30 | } 31 | 32 | public static void main(String[] args) throws Exception { 33 | System.setOut(new PrintStream("output.txt")); 34 | Scanner scanner=new Scanner(System.in); 35 | int times=scanner.nextInt(); 36 | long start=System.currentTimeMillis(); 37 | for (int t=1;t<=times;t++) { 38 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 39 | } 40 | long end=System.currentTimeMillis(); 41 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.)); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /apac/2015/RoundB/C.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2015 Round B Problem C: Card Game 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | int n=scanner.nextInt(), k=scanner.nextInt(); 12 | int[] nums=new int[n]; 13 | for (int i=0;ii;b--) { 25 | if (nums[b-1]==nums[j-1]-k && dp[b+1][j-1]==0) { 26 | // find a 27 | for (int a=b-1;a>=i;a--) { 28 | if (nums[a-1]==nums[b-1]-k && dp[a+1][b-1]==0) 29 | dp[i][j]=Math.min(dp[i][j], dp[i][a-1]); 30 | } 31 | } 32 | } 33 | } 34 | } 35 | return String.valueOf(dp[1][n]); 36 | } 37 | 38 | public static void main(String[] args) throws Exception { 39 | System.setOut(new PrintStream("output.txt")); 40 | Scanner scanner=new Scanner(System.in); 41 | int times=scanner.nextInt(); 42 | long start=System.currentTimeMillis(); 43 | for (int t=1;t<=times;t++) { 44 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 45 | } 46 | long end=System.currentTimeMillis(); 47 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.)); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /apac/2015/RoundB/README.md: -------------------------------------------------------------------------------- 1 | # APAC 2015 Round B 2 | https://code.google.com/codejam/contest/4214486/dashboard 3 | 4 | ## Problem A: Password Attacker 5 | 6 | Simple DP problem. 7 | dp[i][j] = sigma_(k) {jCk * dp[i-1][j-k]} 8 | Just return dp[m][n]. 9 | 10 | ## Problem B: New Years Eve 11 | 12 | Simple problem. Brute force is enough. 13 | Check each level, one by one. If one is overflowed, flow it to the next level. 14 | 15 | ## Problem C: Card Game 16 | 17 | DP. 18 | dp[i][j] means how many cards can be left in range [i, j]. 19 | dp[i][j] = min(1+dp[i][j-1], dp[i][k-1]) if [k, j] can be discarded. 20 | Just return dp[1][n] 21 | 22 | ## Problem D: Parentheses Order 23 | 24 | For small inputs, we can generate all possible parentheses, and get the kth smallest. 25 | As for large, we can use DP. 26 | dp[i][j] means how many possible parentheses with i pairs, and has j '(' in the beginning. 27 | For example, 28 | dp[1][1]=1 `()` 29 | dp[2][2]=1 `(())`, dp[2][1]=2 `(()), ()()` 30 | dp[3][3]=1 `((()))`, dp[3][2]=3 `((())), (())(), (()())`, dp[3][3]=5 31 | `((())), (())(), (()()), ()(()), ()()()` 32 | ... 33 | 34 | And then, we need to find, for n pairs, 35 | how many '(' are there in the k-th smallest one in the beginning. 36 | Find that index, and remove a pair of `()`, to get sub-problem of n-1. 37 | 38 | For example, for n=3 and k=4, as dp[3][2]data[i][j])) { 23 | ans=step[i][j];id=data[i][j]; 24 | } 25 | } 26 | } 27 | return id+" "+ans; 28 | } 29 | 30 | private int dfs(int n, int[][] data, int[][] step, int x, int y) { 31 | if (step[x][y]!=0) return step[x][y]; 32 | step[x][y]=1; 33 | for (int[] dir: new int[][] {{-1,0},{0,-1},{1,0},{0,1}}) { 34 | int dx=x+dir[0], dy=y+dir[1]; 35 | if (dx>=0 && dx=0 && dy ans=new ArrayList<>(); 17 | while (q-->0) { 18 | int x=scanner.nextInt(), cnt=0; 19 | for (int[] bus: buses) { 20 | if (bus[0]<=x && bus[1]>=x) cnt++; 21 | } 22 | ans.add(String.valueOf(cnt)); 23 | } 24 | return String.join(" ", ans); 25 | } 26 | 27 | public static void main(String[] args) throws Exception { 28 | System.setOut(new PrintStream("output.txt")); 29 | Scanner scanner=new Scanner(System.in); 30 | int times=scanner.nextInt(); 31 | long start=System.currentTimeMillis(); 32 | for (int t=1;t<=times;t++) { 33 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 34 | } 35 | long end=System.currentTimeMillis(); 36 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.)); 37 | } 38 | } -------------------------------------------------------------------------------- /apac/2015/RoundD/C.js: -------------------------------------------------------------------------------- 1 | /* 2 | * APAC 2015 (University Graduates Test) 3 | * Round D 4 | * Problem C: Sort a scrambled itinerary 5 | * Passed: both tests 6 | */ 7 | const fs= require('fs'), 8 | input= fs.readFileSync(0,'utf8').trim().split(/^\d+\n/m).filter(Boolean) 9 | .map(e=>e.trim().split('\n')); 10 | console.log(input.map((e,i)=>`Case #${i+1}: ${solve(e)}`).join('\n')); 11 | 12 | // fast solution 13 | function solve(arr) { 14 | let departure= [], dest=[]; 15 | let h= {}; 16 | let r= []; 17 | 18 | arr.forEach((city,i)=>{ 19 | i%2? 20 | (dest.push(city), h[city]=true) 21 | :departure.push(city) 22 | }); 23 | 24 | let city= departure.find(e=>!h[e]); 25 | let times= departure.length; 26 | 27 | departure.forEach((e,i)=>h[e]=dest[i]); 28 | 29 | while (times--) r.push(city+'-'+(city=h[city])); 30 | 31 | return r.join(' '); 32 | } 33 | 34 | // slow solution 35 | function solve(arr){ 36 | let dep= arr.filter((_,i)=>i%2===0); 37 | let dest= arr.filter((_,i)=>i%2); 38 | let first= dep.findIndex(e=>!dest.includes(e)); 39 | let tickets= dep.map((e,i)=>e+'-'+dest[i]); 40 | let r= tickets.splice(first,1); 41 | while (tickets.length) { 42 | let dep= r[r.length-1].slice(-3); 43 | let idx= tickets.findIndex(e=>e.slice(0,3)===dep); 44 | r.push(tickets.splice(idx,1)[0]); 45 | } 46 | return r.join(' '); 47 | } 48 | -------------------------------------------------------------------------------- /apac/2015/RoundD/README.md: -------------------------------------------------------------------------------- 1 | # APAC 2015 Round D 2 | https://code.google.com/codejam/contest/6214486/dashboard 3 | 4 | ## Problem A: Cube IV 5 | 6 | Simple. DP. 7 | 8 | ## Problem B: GBus count 9 | 10 | Simple. For each point, for each gBus, check if it's covered. 11 | 12 | ## Problem C: Sort a scrambled itinerary 13 | 14 | Simple. Loop to find a city with no flight to it. 15 | 16 | ## Problem D: Itz Chess 17 | 18 | Simple. Take care of, it P in E3, then it can kill F2/F4 (not D4/F4). 19 | -------------------------------------------------------------------------------- /apac/2016/Practice Round/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2016 Practice Round Problem B: Captain Hammer 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | double v=scanner.nextDouble(), d=scanner.nextDouble(); 12 | double x=(d*9.8)/(v*v); 13 | if (x>=1) x=1; 14 | return String.format("%.9f", Math.asin(x)*.5*180/Math.PI); 15 | } 16 | 17 | public static void main(String[] args) throws Exception { 18 | System.setOut(new PrintStream("output.txt")); 19 | Scanner scanner=new Scanner(System.in); 20 | int times=scanner.nextInt(); 21 | long start=System.currentTimeMillis(); 22 | for (int t=1;t<=times;t++) { 23 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 24 | } 25 | long end=System.currentTimeMillis(); 26 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 27 | 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /apac/2016/Practice Round/C.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2016 Practice Round Problem C: Moist 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | int n=scanner.nextInt(), cnt=0; 12 | scanner.nextLine(); 13 | String max=""; 14 | for (int i=0;i arrayList=new ArrayList<>(); 19 | while (m-->0) { 20 | int l=scanner.nextInt(), r=scanner.nextInt(), d=r-l+1; 21 | double ans=1.; 22 | for (int i=l;i<=r;i++) 23 | ans*=Math.pow(a[i], 1./d); 24 | arrayList.add(String.format("%.9f", ans)); 25 | } 26 | return "\n"+String.join("\n", arrayList); 27 | } 28 | 29 | public static void main(String[] args) throws Exception { 30 | System.setOut(new PrintStream("output.txt")); 31 | Scanner scanner=new Scanner(System.in); 32 | int times=scanner.nextInt(); 33 | long start=System.currentTimeMillis(); 34 | for (int t=1;t<=times;t++) { 35 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 36 | } 37 | long end=System.currentTimeMillis(); 38 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 39 | 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /apac/2016/RoundA/README.md: -------------------------------------------------------------------------------- 1 | # APAC 2016 Round A 2 | https://code.google.com/codejam/contest/4284486/dashboard 3 | 4 | ## Problem A: Googol String 5 | 6 | Simple. Nothin to tell. 7 | 8 | ## Problem B: gCube 9 | 10 | Simple. `V=PI_(i,l,r) {a[i]}, Edge=pow(V, 1/d).` 11 | 12 | ## Problem C: gCampus 13 | 14 | Floyd to calculate the distance of any two offices. 15 | Then, for each road, if the distance between its two verticals is smaller than 16 | its length, then it's an inefficient road. 17 | 18 | ## Problem D: gSnake 19 | 20 | Brute force. As X<=1000000, we can simulate at most 1100000 seconds. 21 | for every second in [1, 1100000], check if the snake will die, or it can eat food. 22 | Using HashSet to remember all bodies, and all foods eaten. 23 | -------------------------------------------------------------------------------- /apac/2016/RoundB/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ckcz123/codejam/e0632ddf91ed987b7e3ecb540ffd1f88499ee6f5/apac/2016/RoundB/README.md -------------------------------------------------------------------------------- /apac/2016/RoundC/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * APAC 2016 Round C Problem B: gFiles 7 | * Check README.md for explanation. 8 | */ 9 | public class Main { 10 | 11 | public String solve(Scanner scanner) { 12 | long start=1, end=1000000000000000000L; 13 | int n=scanner.nextInt(); 14 | while (n-->0) { 15 | long p=scanner.nextLong(), f=scanner.nextLong()*100; 16 | long e=(p==0?1000000000000000000L:f/p); 17 | long s=(p==100?f/100:f/(p+1)+1); 18 | start=Math.max(start, s); 19 | end=Math.min(end, e); 20 | } 21 | return start==end?String.valueOf(start):"-1"; 22 | } 23 | 24 | public static void main(String[] args) throws Exception { 25 | System.setOut(new PrintStream("output.txt")); 26 | Scanner scanner=new Scanner(System.in); 27 | int times=scanner.nextInt(); 28 | long start=System.currentTimeMillis(); 29 | for (int t=1;t<=times;t++) { 30 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 31 | } 32 | long end=System.currentTimeMillis(); 33 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.)); 34 | } 35 | } -------------------------------------------------------------------------------- /apac/2016/RoundC/README.md: -------------------------------------------------------------------------------- 1 | # APAC 2016 Round C 2 | https://code.google.com/codejam/contest/4284487/dashboard 3 | 4 | ## Problem A: gRanks 5 | 6 | Brute force. Simple. 7 | 8 | ## Problem B: gFiles 9 | 10 | Intervals intersection. 11 | Take care of p=0 and p=100. Use `long` instead of `double`. 12 | 13 | ## Problem C: gGames 14 | 15 | DP/DFS with memorization. 16 | dp[i][x] means, can we put x in first i rounds. Here x records people 17 | (one in a bit). 18 | To solve this, just break x into two numbers y and z, then check dp[i-1][y] 19 | and dp[i-1][z]. Check all possible subsets. 20 | Return `dp[n][(1< set=new HashSet<>(); 16 | if (i>0) set.add(s.charAt(i-1)); 17 | set.add(s.charAt(i)); 18 | if (i map=new HashMap<>(); 18 | for (int x: num[0]) { 19 | for (int y: num[1]) { 20 | int v=x^y; 21 | map.put(v, map.getOrDefault(v, 0)+1); 22 | } 23 | } 24 | long val=0; 25 | for (int x: num[2]) { 26 | for (int y: num[3]) { 27 | val+=map.getOrDefault(x^y^k, 0); 28 | } 29 | } 30 | return String.valueOf(val); 31 | } 32 | 33 | public static void main(String[] args) throws Exception { 34 | System.setOut(new PrintStream("output.txt")); 35 | Scanner scanner=new Scanner(System.in); 36 | int times=scanner.nextInt(); 37 | long start=System.currentTimeMillis(); 38 | for (int t=1;t<=times;t++) { 39 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 40 | } 41 | long end=System.currentTimeMillis(); 42 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 43 | 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /apac/2016/RoundE/C.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2016 Round E Problem C: Not So Random 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | int n=scanner.nextInt(), x=scanner.nextInt(), k=scanner.nextInt(), 12 | a=scanner.nextInt(), b=scanner.nextInt(), c=scanner.nextInt(); 13 | HashMap map=new HashMap<>(); 14 | map.put(x, 1d); 15 | for (int i=0;i map1=new HashMap<>(); 17 | for (Map.Entry entry: map.entrySet()) { 18 | add(map1, entry.getKey()&k, entry.getValue()*a/100.); 19 | add(map1, entry.getKey()|k, entry.getValue()*b/100.); 20 | add(map1, entry.getKey()^k, entry.getValue()*c/100.); 21 | } 22 | map=map1; 23 | } 24 | return String.format("%.9f", map.entrySet().stream().mapToDouble(en->en.getKey()*en.getValue()).sum()); 25 | } 26 | 27 | private void add(HashMap map, int key, double value) { 28 | map.put(key, map.getOrDefault(key, 0d)+value); 29 | } 30 | 31 | public static void main(String[] args) throws Exception { 32 | System.setOut(new PrintStream("output.txt")); 33 | Scanner scanner=new Scanner(System.in); 34 | int times=scanner.nextInt(); 35 | long start=System.currentTimeMillis(); 36 | for (int t=1;t<=times;t++) { 37 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 38 | } 39 | long end=System.currentTimeMillis(); 40 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 41 | 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /apac/2016/RoundE/README.md: -------------------------------------------------------------------------------- 1 | # APAC 2016 Round E 2 | https://code.google.com/codejam/contest/8264486/dashboard 3 | 4 | ## Problem A: Lazy Spelling Bee 5 | 6 | Simple. For each position, check how many letter can it spell. 7 | 8 | ## Problem B: Robot Rock Band 9 | 10 | Simple. For each number a in A and each number b in B, calculate 11 | a^b and rememeber into a HashMap. 12 | Then for each number c in C and each number d in D, calculate 13 | how many pairs are there in HashMap where a^b=K^c^d. 14 | 15 | ## Problem C: Not So Random 16 | 17 | Another HashMap problem. 18 | For each loop, calculate all possible values generated and its possibility. 19 | 20 | ## Problem D: Sums of Sums 21 | 22 | Hard, extremely hard problem. 23 | Please read this [Quora Explanation](https://www.quora.com/How-can-problem-D-Sums-of-sums-from-Round-E-of-the-Google-APAC-Test-2016-be-solved-for-the-large-dataset) 24 | if you don't have any idea. 25 | Check [D.java](D.java) for my implementation. 26 | -------------------------------------------------------------------------------- /apac/2017/Practice Round/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2017 Practice Round Problem A: Lazy Spelling Bee 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | String s=scanner.next(); 12 | int l=s.length(); 13 | long cnt=1, mod=1000000007; 14 | for (int i=0;i set=new HashSet<>(); 16 | if (i>0) set.add(s.charAt(i-1)); 17 | set.add(s.charAt(i)); 18 | if (i map=new HashMap<>(); 18 | for (int x: num[0]) { 19 | for (int y: num[1]) { 20 | int v=x^y; 21 | map.put(v, map.getOrDefault(v, 0)+1); 22 | } 23 | } 24 | long val=0; 25 | for (int x: num[2]) { 26 | for (int y: num[3]) { 27 | val+=map.getOrDefault(x^y^k, 0); 28 | } 29 | } 30 | return String.valueOf(val); 31 | } 32 | 33 | public static void main(String[] args) throws Exception { 34 | System.setOut(new PrintStream("output.txt")); 35 | Scanner scanner=new Scanner(System.in); 36 | int times=scanner.nextInt(); 37 | long start=System.currentTimeMillis(); 38 | for (int t=1;t<=times;t++) { 39 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 40 | } 41 | long end=System.currentTimeMillis(); 42 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 43 | 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /apac/2017/Practice Round/C.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2017 Practice Round Problem C: Not So Random 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | int n=scanner.nextInt(), x=scanner.nextInt(), k=scanner.nextInt(), 12 | a=scanner.nextInt(), b=scanner.nextInt(), c=scanner.nextInt(); 13 | HashMap map=new HashMap<>(); 14 | map.put(x, 1d); 15 | for (int i=0;i map1=new HashMap<>(); 17 | for (Map.Entry entry: map.entrySet()) { 18 | add(map1, entry.getKey()&k, entry.getValue()*a/100.); 19 | add(map1, entry.getKey()|k, entry.getValue()*b/100.); 20 | add(map1, entry.getKey()^k, entry.getValue()*c/100.); 21 | } 22 | map=map1; 23 | } 24 | return String.format("%.9f", map.entrySet().stream().mapToDouble(en->en.getKey()*en.getValue()).sum()); 25 | } 26 | 27 | private void add(HashMap map, int key, double value) { 28 | map.put(key, map.getOrDefault(key, 0d)+value); 29 | } 30 | 31 | public static void main(String[] args) throws Exception { 32 | System.setOut(new PrintStream("output.txt")); 33 | Scanner scanner=new Scanner(System.in); 34 | int times=scanner.nextInt(); 35 | long start=System.currentTimeMillis(); 36 | for (int t=1;t<=times;t++) { 37 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 38 | } 39 | long end=System.currentTimeMillis(); 40 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 41 | 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /apac/2017/Practice Round/README.md: -------------------------------------------------------------------------------- 1 | # APAC 2017 Practice Round 2 | https://code.google.com/codejam/contest/5254486/dashboard 3 | 4 | ## Problem A: Lazy Spelling Bee 5 | 6 | Simple. For each position, check how many letter can it spell. 7 | 8 | ## Problem B: Robot Rock Band 9 | 10 | Simple. For each number a in A and each number b in B, calculate 11 | a^b and rememeber into a HashMap. 12 | Then for each number c in C and each number d in D, calculate 13 | how many pairs are there in HashMap where a^b=K^c^d. 14 | 15 | ## Problem C: Not So Random 16 | 17 | Another HashMap problem. 18 | For each loop, calculate all possible values generated and its possibility. 19 | 20 | ## Problem D: Sums of Sums 21 | 22 | Hard, extremely hard problem. 23 | Please read this [Quora Explanation](https://www.quora.com/How-can-problem-D-Sums-of-sums-from-Round-E-of-the-Google-APAC-Test-2016-be-solved-for-the-large-dataset) 24 | if you don't have any idea. 25 | Check [D.java](D.java) for my implementation. 26 | -------------------------------------------------------------------------------- /apac/2017/RoundA/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2017 Round A Problem A: Country Leader 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | class Node { 11 | String s; 12 | int different; 13 | public Node(String _s) { 14 | s=_s; 15 | boolean[] chars=new boolean[26]; 16 | different=0; 17 | for (char c: s.toCharArray()) { 18 | if (c!=' ' && !chars[c-'A']) { 19 | different++; 20 | chars[c-'A']=true; 21 | } 22 | } 23 | } 24 | } 25 | 26 | private String solve(Scanner scanner) { 27 | int n=scanner.nextInt(); scanner.nextLine(); 28 | PriorityQueue queue=new PriorityQueue<>( 29 | (o1,o2)->o1.different==o2.different?o1.s.compareTo(o2.s):o2.different-o1.different); 30 | for (int i=0;i1e-12) { 19 | double mid=(start+end)/2; 20 | double vm=cal(c, mid); 21 | if (vm==0) {start=mid;break;} 22 | if (vm*vs<0) end=mid; 23 | else start=mid; 24 | } 25 | return String.format("%.12f",start); 26 | } 27 | 28 | private double cal(double[] c, double r) { 29 | double x=0; 30 | for (double v: c) x=x*(1+r)+v; 31 | return x; 32 | } 33 | 34 | public static void main(String[] args) throws Exception { 35 | System.setOut(new PrintStream("E:\\desktop\\output.txt")); 36 | Scanner scanner=new Scanner(System.in); 37 | int times=scanner.nextInt(); 38 | for (int t=1;t<=times;t++) { 39 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 40 | } 41 | 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /apac/2017/RoundA/README.md: -------------------------------------------------------------------------------- 1 | # APAC 2017 Round A 2 | https://code.google.com/codejam/contest/11274486/dashboard 3 | 4 | ## Problem A: Country Leader 5 | 6 | Simple. Nothing to tell. 7 | 8 | ## Problem B: Rain 9 | 10 | For each point, calculate the `real height` of it. 11 | For points on board, real height equals to its height. 12 | Then, using bfs (with priorityQueue) to calculate 13 | all real heights. 14 | 15 | ## Problem C: Jane's Flower Shop 16 | 17 | Binary Search. If mid*start>0 then start=mid, else end=mid. 18 | As f(1) and/or f(-1) may be zero, we need to calculate [-1+1e-12, 1-1e-12]. 19 | 20 | ## Problem D: Clash Royale 21 | 22 | DFS. 23 | For each card, each level, dfs to find the max attack. 24 | Prune if `currAtk+maxAtkLeft<=currMaxAtk`. That is, if the sum 25 | of attack current and max attacks left is smaller than the max attack 26 | we have found, just return immediately. 27 | Check [D.java](D.java) for more details. 28 | 29 | -------------------------------------------------------------------------------- /apac/2017/RoundB/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2017 Round B Problem A: Sherlock and Parentheses 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | long l=scanner.nextLong(), r=scanner.nextLong(), x=Math.min(l, r); 12 | return String.valueOf(x*(x+1)/2); 13 | } 14 | 15 | public static void main(String[] args) throws Exception { 16 | System.setOut(new PrintStream("E:\\desktop\\output.txt")); 17 | Scanner scanner=new Scanner(System.in); 18 | int times=scanner.nextInt(); 19 | long start=System.currentTimeMillis(); 20 | for (int t=1;t<=times;t++) { 21 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 22 | } 23 | long end=System.currentTimeMillis(); 24 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 25 | 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /apac/2017/RoundB/D.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ckcz123/codejam/e0632ddf91ed987b7e3ecb540ffd1f88499ee6f5/apac/2017/RoundB/D.java -------------------------------------------------------------------------------- /apac/2017/RoundC/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2017 Round C Problem B: Safe Squares 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | int r=scanner.nextInt(), c=scanner.nextInt(); 12 | int[][] map=new int[r][c]; 13 | int k=scanner.nextInt(); 14 | while (k-->0) 15 | map[scanner.nextInt()][scanner.nextInt()]=1; 16 | int[][] edge=new int[r][c]; 17 | long ans=0; 18 | for (int i=0;i nums=new ArrayList<>(); 13 | for (int i=0;i nums) { 20 | int mx1=0, mx2=0, n=nums.size(); 21 | if (n==0) return false; 22 | for (int[] num: nums) { 23 | mx1=Math.max(mx1, num[0]); 24 | mx2=Math.max(mx2, num[1]); 25 | } 26 | for (int[] num: nums) { 27 | if (num[0]==mx1 && num[1]==mx2) 28 | return true; 29 | } 30 | ArrayList ans=new ArrayList<>(); 31 | for (int[] num: nums) { 32 | if (num[0]!=mx1 && num[1]!=mx2) 33 | ans.add(num); 34 | } 35 | return able(ans); 36 | } 37 | 38 | public static void main(String[] args) throws Exception { 39 | System.setOut(new PrintStream("output.txt")); 40 | Scanner scanner=new Scanner(System.in); 41 | int times=scanner.nextInt(); 42 | long start=System.currentTimeMillis(); 43 | for (int t=1;t<=times;t++) { 44 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 45 | } 46 | long end=System.currentTimeMillis(); 47 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.)); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /apac/2017/RoundC/README.md: -------------------------------------------------------------------------------- 1 | # APAC 2017 Round C 2 | https://code.google.com/codejam/contest/6274486/dashboard 3 | 4 | ## Problem A: Monster Path 5 | 6 | DFS. 7 | When we reach a point in the k-th time, the possibility to get score 8 | is `pow(1-p, k)*p`. 9 | 10 | ## Problem B: Safe Squares 11 | 12 | Simple. 13 | For each point, 14 | calculate the max square length (as the bottom-right point). 15 | Then sum them. 16 | 17 | ## Problem C: Evaluation 18 | 19 | Topological sort. Simple. 20 | We only add one variable to the set iff it's can be calculate. (That is, 21 | `x=f(...)` where `...` can all be calculated.) 22 | 23 | ## Problem D: Soldiers 24 | 25 | Let's assume the max attack is x and the max defense is y. 26 | If there is one soldier s where `s.a=x and s.d=y`, the Alice can pick s and she win! 27 | If no soldiers have max attack and max defense, then Alice cannot pick one 28 | with max attack (or Bob will choose one with max defense, and game over!). 29 | Thus, we can remove all soldiers with max attack or max defense, and a new game starts! 30 | -------------------------------------------------------------------------------- /apac/2017/RoundD/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2017 Round D Problem A: Vote 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | double n=scanner.nextDouble(), m=scanner.nextDouble(); 12 | return String.format("%.9f", (n-m)/(n+m)); 13 | } 14 | 15 | public static void main(String[] args) throws Exception { 16 | System.setOut(new PrintStream("output.txt")); 17 | Scanner scanner=new Scanner(System.in); 18 | int times=scanner.nextInt(); 19 | long start=System.currentTimeMillis(); 20 | for (int t=1;t<=times;t++) { 21 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 22 | } 23 | long end=System.currentTimeMillis(); 24 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /apac/2017/RoundD/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2017 Round D Problem B: Sitting 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | int r=scanner.nextInt(), c=scanner.nextInt(); 12 | if (r>c) {int tmp=r;r=c;c=tmp;} 13 | // ##.##.##.## 14 | int v1=c/3*2+c%3; 15 | // #.##.##.##. 16 | int v2=c/3*2+(c%3==0?0:1); 17 | // .##.##.##.# 18 | int v3=c/3*2+(c%3<=1?0:1); 19 | if (r<=2) return String.valueOf(r*v1); 20 | return String.valueOf(r/3*(v1+v2+v3)+(r%3>=1?v1:0)+(r%3==2?v2:0)); 21 | } 22 | 23 | public static void main(String[] args) throws Exception { 24 | System.setOut(new PrintStream("output.txt")); 25 | Scanner scanner=new Scanner(System.in); 26 | int times=scanner.nextInt(); 27 | long start=System.currentTimeMillis(); 28 | for (int t=1;t<=times;t++) { 29 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 30 | } 31 | long end=System.currentTimeMillis(); 32 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.)); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /apac/2017/RoundD/C.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2017 Round D Problem C: Codejamon Cipher 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | int n=scanner.nextInt(), v=scanner.nextInt(), ml=0; 12 | HashMap map=new HashMap<>(); 13 | for (int i=0;i ans=new ArrayList<>(); 19 | while (v-->0) { 20 | String s=scanner.next(); 21 | int l=s.length(); 22 | long[] dp=new long[l+1]; 23 | dp[0]=1; 24 | for (int i=1;i<=l;i++) { 25 | for (int j=Math.max(0, i-ml);j (n+m, n-m)`, totals numbers are `(n+m)Cm`. 9 | However, the first vote must be A, and afterwards the way cannot 10 | reach the horizontal line. That is, `(1, 1) -> (n+m, n-m)` where the whole route 11 | is above the horizontal line. 12 | All possible routes number of `(1, 1) -> (n+m, n-m)` is `(n+m-1)C(n-1)`. 13 | All invalid routes number is `(n+m-1)C(m-1)` (Reflection, 14 | check [here](https://en.wikipedia.org/wiki/Catalan_number#Second_proof).) 15 | Finally, the result is 16 | `((n+m-1)C(n-1)-(n+m-1)C(m-1))/((n+m)Cn) = (n-m)/(n+m)`. 17 | 18 | ## Problem B: Sitting 19 | 20 | Assume `row<=col`. 21 | If `row<=2` then sit as `## ## ## ## ##...`. 22 | Else, sit as: 23 | ``` 24 | ## ## ## ## ## ... 25 | # ## ## ## ## ... 26 | ## ## ## ## # ... 27 | ## ## ## ## ## ... 28 | ... 29 | ``` 30 | 31 | ## Problem C: Codejamon Cipher 32 | 33 | Sort each word, save to HashMap. 34 | Then for each string, DP to find the number of ways. 35 | 36 | ## Problem D: Stretch Rope 37 | 38 | DP & Segment Tree. 39 | dp[i][j] means the minimal money needed, to get length j in first i ropes. 40 | As the i-th rope may has [A[i], B[i]] length, 41 | `dp[i][j]=min(dp[i-1][j], p[i]+dp[i-1][k]) where j-B[i]<=k<=j-A[i]`. 42 | So, we can build a segment tree for dp[i-1], to query the 43 | minimal of `dp[i-1][j-A[i]] ~ dp[i-1][j-B[i]]` in O(log l) time. 44 | Total time complexity: `O(n)*(O(l*log(l))+O(l*log(l)))=O(n*l*log(l))`. 45 | Check [D.java](D.java) for more details. -------------------------------------------------------------------------------- /apac/2017/RoundE/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2017 Round E Problem A: Diwali lightings 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | String s=scanner.next(); 12 | return String.valueOf(-cal(s, scanner.nextLong()-1)+cal(s, scanner.nextLong())); 13 | } 14 | 15 | private long cal(String s, long k) { 16 | int l=s.length(); 17 | long cnt=0; 18 | char[] chars=s.toCharArray(); 19 | for (char c: chars) 20 | if (c=='B') cnt++; 21 | cnt*=k/l; 22 | k%=l; 23 | for (int i=1;i<=k;i++) { 24 | if (chars[i-1]=='B') cnt++; 25 | } 26 | return cnt; 27 | } 28 | 29 | public static void main(String[] args) throws Exception { 30 | System.setOut(new PrintStream("output.txt")); 31 | Scanner scanner=new Scanner(System.in); 32 | int times=scanner.nextInt(); 33 | long start=System.currentTimeMillis(); 34 | for (int t=1;t<=times;t++) { 35 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 36 | } 37 | long end=System.currentTimeMillis(); 38 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.)); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /apac/2017/RoundE/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2017 Round E Problem B: Beautiful Numbers 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | long n=scanner.nextLong(); 12 | for (int b=64;b>=2;b--) { 13 | long candidate=(long)(Math.pow(n, 1./b)); 14 | if (candidate!=1 && cal(candidate, b)==n) 15 | return String.valueOf(candidate); 16 | } 17 | return String.valueOf(n-1); 18 | } 19 | 20 | private long cal(long v, int b) { 21 | long ans=1, p=1; 22 | for (int i=1;i<=b;i++) { 23 | p*=v; 24 | ans+=p; 25 | } 26 | return ans; 27 | } 28 | 29 | 30 | public static void main(String[] args) throws Exception { 31 | System.setOut(new PrintStream("output.txt")); 32 | Scanner scanner=new Scanner(System.in); 33 | int times=scanner.nextInt(); 34 | long start=System.currentTimeMillis(); 35 | for (int t=1;t<=times;t++) { 36 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 37 | } 38 | long end=System.currentTimeMillis(); 39 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /apac/2017/RoundE/C.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * APAC 2017 Round E Problem C: Partioning Number 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | int n=scanner.nextInt(), d=scanner.nextInt(); 12 | long ans=0; 13 | for (int x=d;x<=n;x+=d) { 14 | for (int p=1;p<=n/x;p++) { 15 | int t=n-p*x, range=t/(x+2); 16 | ans+=range/(x+1)+(range%(x+1)>=t%(x+1)?1:0); 17 | } 18 | } 19 | return String.valueOf(ans); 20 | } 21 | 22 | public static void main(String[] args) throws Exception { 23 | System.setOut(new PrintStream("output.txt")); 24 | Scanner scanner=new Scanner(System.in); 25 | int times=scanner.nextInt(); 26 | long start=System.currentTimeMillis(); 27 | for (int t=1;t<=times;t++) { 28 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 29 | } 30 | long end=System.currentTimeMillis(); 31 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.)); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /apac/2017/RoundE/README.md: -------------------------------------------------------------------------------- 1 | # APAC 2017 Round E 2 | https://code.google.com/codejam/contest/5264487/dashboard 3 | 4 | ## Problem A: Diwali lightings 5 | 6 | Simple. Just calculate the number of blue lights between [1, k]. 7 | 8 | ## Problem B: Beautiful Numbers 9 | 10 | Read this [leetcode discuss topic](https://discuss.leetcode.com/topic/76368/python-solution-with-detailed-mathematical-explanation-and-derivation) 11 | for explanation. 12 | 13 | ## Problem C: Partioning Number 14 | 15 | If the smallest number is x. 16 | Then, just calculate the number of pairs `(p,q,r) where px+q(x+1)+r(x+2)=n`. 17 | for each p in [1, n/x], `q(x+1)+r(x+2)=n-px`. 18 | Obviously, `(q+r)(x+1)+r=n-px`, that is, `r%(x+1)=(n-px)%(x+1)`. 19 | So, we just need to calculate how many r in [0, (n-px)/(x+1)] satisfies `r%(x+1)=(n-px)%(x+1)`. 20 | 21 | ## Problem D: Sorting Array 22 | 23 | This problem is hard, really hard. 24 | I don't know how to solve or explain it... 25 | If you can solve this problem, please contact me at [ckcz12345@gmail.com](mailto:ckcz12345@gmail.com). 26 | -------------------------------------------------------------------------------- /codejam/2008/Qualification Round/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.math.BigInteger; 3 | import java.util.*; 4 | import java.util.stream.Collectors; 5 | 6 | /** 7 | * Codejam 2008 Qualification Round 8 | * Problem A. Saving the Universe 9 | */ 10 | public class Main { 11 | 12 | public String solve(Scanner scanner) { 13 | int n=Integer.parseInt(scanner.nextLine()); 14 | HashSet set=new HashSet<>(); 15 | for (int i=0;i able=new HashSet<>(set); 18 | while (m-->0) { 19 | String s=scanner.nextLine(); 20 | able.remove(s); 21 | if (able.isEmpty()) { 22 | cnt++; 23 | able=new HashSet<>(set); 24 | able.remove(s); 25 | } 26 | } 27 | return String.valueOf(cnt); 28 | } 29 | 30 | public static void main(String[] args) throws Exception { 31 | System.setOut(new PrintStream("output.txt")); 32 | Scanner scanner=new Scanner(System.in); 33 | int times=Integer.parseInt(scanner.nextLine()); 34 | long start=System.currentTimeMillis(); 35 | for (int t=1;t<=times;t++) { 36 | try { 37 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 38 | } 39 | catch (Throwable e) { 40 | System.err.println("ERROR in case #"+t); 41 | e.printStackTrace(); 42 | } 43 | } 44 | long end=System.currentTimeMillis(); 45 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 46 | 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /codejam/2008/Round1A/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.math.BigInteger; 3 | import java.util.*; 4 | import java.util.stream.Collectors; 5 | 6 | /** 7 | * Codejam 2008 Round 1A 8 | * Problem A. Minimum Scalar Product 9 | */ 10 | public class Main { 11 | 12 | public String solve(Scanner scanner) { 13 | int n=scanner.nextInt(); 14 | long[] a=new long[n], b=new long[n]; 15 | for (int i=0;i cache; 13 | 14 | public String solve(Scanner scanner) { 15 | int n=scanner.nextInt(); 16 | String s=String.valueOf((cal(n)+999)%1000); 17 | while (s.length()<3) s="0"+s; 18 | return s; 19 | } 20 | 21 | private int cal(int n) { 22 | if (cache==null) { 23 | cache=new TreeMap<>(); 24 | cache.put(0, 2); 25 | cache.put(1, 6); 26 | } 27 | if (cache.containsKey(n)) return cache.get(n); 28 | int u=cache.lowerKey(n), v=u-1; 29 | int a=cache.get(v), b=cache.get(u); 30 | for (int i=u;i<=n;i++) { 31 | int c=(6*b-4*a+1000)%1000; 32 | a=b;b=c; 33 | } 34 | cache.put(n, a); 35 | cache.put(n+1, b); 36 | return a; 37 | } 38 | 39 | public static void main(String[] args) throws Exception { 40 | System.setOut(new PrintStream("output.txt")); 41 | Scanner scanner=new Scanner(System.in); 42 | int times=Integer.parseInt(scanner.nextLine()); 43 | long start=System.currentTimeMillis(); 44 | for (int t=1;t<=times;t++) { 45 | try { 46 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 47 | } 48 | catch (Throwable e) { 49 | System.err.println("ERROR in case #"+t); 50 | e.printStackTrace(); 51 | } 52 | } 53 | long end=System.currentTimeMillis(); 54 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 55 | 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /codejam/2008/Round1B/C.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2008 Round 1B 7 | * Problem C. Mousetrap 8 | */ 9 | public class Main { 10 | 11 | public String solve(Scanner scanner) { 12 | int k=scanner.nextInt(), n=scanner.nextInt(); 13 | int[] queries=new int[n]; 14 | String[] ans=new String[n]; 15 | for (int i=0;ipos+1) { 24 | queries[j]--; 25 | } 26 | } 27 | } 28 | return String.join(" ", ans); 29 | } 30 | 31 | public static void main(String[] args) throws Exception { 32 | System.setOut(new PrintStream("output.txt")); 33 | Scanner scanner=new Scanner(System.in); 34 | int times=Integer.parseInt(scanner.nextLine()); 35 | long start=System.currentTimeMillis(); 36 | for (int t=1;t<=times;t++) { 37 | try { 38 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 39 | } 40 | catch (Throwable e) { 41 | System.err.println("ERROR in case #"+t); 42 | e.printStackTrace(); 43 | } 44 | } 45 | long end=System.currentTimeMillis(); 46 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 47 | 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /codejam/2008/Round1C/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2008 Round 1C 7 | * Problem A. Text Messaging Outrage 8 | */ 9 | public class Main { 10 | 11 | public String solve(Scanner scanner) { 12 | int p=scanner.nextInt(), k=scanner.nextInt(), l=scanner.nextInt(); 13 | int[] a=new int[l]; 14 | for (int i=0;i queue=new PriorityQueue<>(); 17 | for (int i=0;i=0;i--) { 20 | int q=queue.poll()+1; 21 | ans+=q*a[i]; 22 | queue.offer(q); 23 | } 24 | return String.valueOf(ans); 25 | } 26 | 27 | public static void main(String[] args) throws Exception { 28 | System.setOut(new PrintStream("output.txt")); 29 | Scanner scanner=new Scanner(System.in); 30 | int times=Integer.parseInt(scanner.nextLine()); 31 | long start=System.currentTimeMillis(); 32 | for (int t=1;t<=times;t++) { 33 | try { 34 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 35 | } 36 | catch (Throwable e) { 37 | System.err.println("ERROR in case #"+t); 38 | e.printStackTrace(); 39 | } 40 | } 41 | long end=System.currentTimeMillis(); 42 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 43 | 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /codejam/2008/Round2/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | public class Main { 6 | 7 | public String solve(Scanner scanner) { 8 | int m = scanner.nextInt(), n = scanner.nextInt(), a = scanner.nextInt(); 9 | if (a>m*n) return "IMPOSSIBLE"; 10 | if (a%m==0) 11 | return String.format("0 0 %d %d %d %d", m, 0, 0, a/m); 12 | int ceil=a/m*m+m, delta=ceil-a; 13 | return String.format("0 0 %d %d %d %d", m, 1, delta, ceil/m); 14 | } 15 | 16 | public static void main(String[] args) throws Exception { 17 | System.setOut(new PrintStream("output.txt")); 18 | Scanner scanner=new Scanner(System.in); 19 | int times=Integer.parseInt(scanner.nextLine()); 20 | long start=System.currentTimeMillis(); 21 | for (int t=1;t<=times;t++) { 22 | try { 23 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 24 | } 25 | catch (Throwable e) { 26 | System.err.println("ERROR in case #"+t); 27 | e.printStackTrace(); 28 | } 29 | } 30 | long end=System.currentTimeMillis(); 31 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 32 | 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /codejam/2008/Round2/C.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2008 Round 2 7 | * Problem C. Star Wars 8 | */ 9 | public class Main { 10 | 11 | public String solve(Scanner scanner) throws Exception { 12 | int n=scanner.nextInt(); 13 | int[][] data=new int[n][4]; 14 | for (int i=0;i map=new HashMap<>(); 13 | int last=0; 14 | char[] chars=s.toCharArray(); 15 | for (int i=0;i())); 15 | } 16 | 17 | private int dfs(int start, int end, int[] a, HashMap map) { 18 | if (end=start && v<=end) 23 | ans=Math.min(ans, end-start+dfs(start, v-1, a, map)+dfs(v+1, end, a, map)); 24 | } 25 | if (ans==Integer.MAX_VALUE) ans=0; 26 | map.put(state, ans); 27 | return ans; 28 | } 29 | 30 | public static void main(String[] args) throws Exception { 31 | System.setOut(new PrintStream("output.txt")); 32 | Scanner scanner=new Scanner(System.in); 33 | int times=Integer.parseInt(scanner.nextLine()); 34 | long start=System.currentTimeMillis(); 35 | for (int t=1;t<=times;t++) { 36 | try { 37 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 38 | } 39 | catch (Throwable e) { 40 | System.err.println("ERROR in case #"+t); 41 | e.printStackTrace(); 42 | } 43 | } 44 | long end=System.currentTimeMillis(); 45 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 46 | 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /codejam/2009/Round2/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2009 Round 2 7 | * Problem A. Crazy Rows 8 | */ 9 | public class Main { 10 | 11 | private String solve(Scanner scanner) { 12 | int n=scanner.nextInt(); 13 | int[] data=new int[n]; 14 | for (int i=0;i=0;j--) { 17 | if (s.charAt(j)=='1') { 18 | data[i]=j+1; 19 | break; 20 | } 21 | } 22 | } 23 | ArrayList list=new ArrayList<>( 24 | Arrays.stream(data).boxed().collect(Collectors.toList()) 25 | ); 26 | int ans=0; 27 | for (int i=1;i<=n;i++) { 28 | for (int j=0;j0) { 21 | int next=Arrays.binarySearch(sum[index], k); 22 | if (next<0) next=-next-2; 23 | ans+=sum[index][next]; 24 | index=(index+next+1)%n; 25 | } 26 | return String.valueOf(ans); 27 | } 28 | 29 | public static void main(String[] args) throws Exception { 30 | System.setOut(new PrintStream("output.txt")); 31 | Scanner scanner=new Scanner(System.in); 32 | int times=scanner.nextInt(); 33 | long start=System.currentTimeMillis(); 34 | for (int t=1;t<=times;t++) { 35 | try { 36 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 37 | } 38 | catch (Throwable e) { 39 | System.err.println("ERROR in case #"+t); 40 | e.printStackTrace(); 41 | } 42 | } 43 | long end=System.currentTimeMillis(); 44 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 45 | 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /codejam/2010/Round1A/C.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam 2010 Round 1A 6 | * Problem C. Number Game 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | long a1=scanner.nextLong(), a2=scanner.nextLong(), 12 | b1=scanner.nextLong(), b2=scanner.nextLong(), ans=0; 13 | for (long a=a1;a<=a2;a++) ans+=b2-Math.min(b2+1, Math.max(b1, find(a)))+1; 14 | for (long b=b1;b<=b2;b++) ans+=a2-Math.min(a2+1, Math.max(a1, find(b)))+1; 15 | return String.valueOf(ans); 16 | } 17 | 18 | private long find(long x) { 19 | long start=x, end=2*x; 20 | while (start set=new HashSet<>(); 13 | int n=scanner.nextInt(), m=scanner.nextInt(), ans=0; 14 | while (n-->0) set.addAll(translate(scanner.next())); 15 | while (m-->0) 16 | for (String s: translate(scanner.next())) 17 | if (set.add(s)) ans++; 18 | return String.valueOf(ans); 19 | } 20 | 21 | private List translate(String path) { 22 | ArrayList list = new ArrayList<>(); 23 | int index=0; 24 | while (true) { 25 | index=path.indexOf('/', index+1); 26 | if (index<0) index=path.length(); 27 | list.add(index); 28 | if (index>=path.length()) break; 29 | } 30 | return list.stream().map(i->path.substring(0, i)).collect(Collectors.toList()); 31 | } 32 | 33 | public static void main(String[] args) throws Exception { 34 | System.setOut(new PrintStream("output.txt")); 35 | Scanner scanner=new Scanner(System.in); 36 | int times=scanner.nextInt(); 37 | long start=System.currentTimeMillis(); 38 | for (int t=1;t<=times;t++) { 39 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 40 | } 41 | long end=System.currentTimeMillis(); 42 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 43 | 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /codejam/2010/Round1B/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2010 Round 1B 7 | * Problem B. Picking Up Chicks 8 | */ 9 | public class Main { 10 | 11 | private String solve(Scanner scanner) { 12 | int n=scanner.nextInt(), k=scanner.nextInt(); 13 | long b=scanner.nextLong(); int t=scanner.nextInt(); 14 | long[] x=new long[n], v=new long[n]; 15 | for (int i=0;i=0;i--) { 19 | if (k==0) break; 20 | if (v[i]*t0) { 19 | ans++; 20 | cnt/=2; 21 | } 22 | return String.valueOf(ans); 23 | } 24 | 25 | public static void main(String[] args) throws Exception { 26 | System.setOut(new PrintStream("output.txt")); 27 | Scanner scanner=new Scanner(System.in); 28 | int times=scanner.nextInt(); 29 | long start=System.currentTimeMillis(); 30 | for (int t=1;t<=times;t++) { 31 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 32 | } 33 | long end=System.currentTimeMillis(); 34 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 35 | 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /codejam/2011/Qualification Round/C.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2011 Qualification Round 7 | * Problem C. Candy Splitting 8 | */ 9 | public class Main { 10 | 11 | public String solve(Scanner scanner) { 12 | int n=scanner.nextInt(), sum=0, xor=0, mn=Integer.MAX_VALUE; 13 | for (int i=0;i list=new ArrayList<>(); 15 | while (c-->0) { 16 | long p=scanner.nextLong(); int v=scanner.nextInt(); 17 | while (v-->0) list.add(2*p); 18 | } 19 | long start=0, end=1L<<60; 20 | while (end-start>1e-9) { 21 | long mid=(start+end)/2; 22 | long can=-(1L<<60); 23 | boolean able=true; 24 | for (long v: list) { 25 | if (can>v+mid) { 26 | able=false;break; 27 | } 28 | can=Math.max(can, v-mid)+d; 29 | } 30 | if (able) end=mid; 31 | else start=mid+1; 32 | } 33 | return start/2+(start%2==0?"":".5"); 34 | } 35 | 36 | public static void main(String[] args) throws Exception { 37 | System.setOut(new PrintStream("output.txt")); 38 | Scanner scanner=new Scanner(System.in); 39 | int times=scanner.nextInt(); 40 | long start=System.currentTimeMillis(); 41 | for (int t=1;t<=times;t++) { 42 | try { 43 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 44 | } 45 | catch (Throwable e) { 46 | System.err.println("ERROR in case #"+t); 47 | e.printStackTrace(); 48 | } 49 | } 50 | long end=System.currentTimeMillis(); 51 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 52 | 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /codejam/2011/Round3/D.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam 2011 Round 3 6 | * Problem D. Mystery Square 7 | * Only small solved. 8 | */ 9 | public class Main { 10 | 11 | public String solve(Scanner scanner) { 12 | char[] chars = scanner.next().toCharArray(); 13 | dfs(chars, 0); 14 | return new String(chars); 15 | } 16 | 17 | private boolean dfs(char[] chars, int curr) { 18 | if (curr==chars.length) 19 | return able(new String(chars)); 20 | if (chars[curr]!='?') return dfs(chars, curr+1); 21 | for (char c: "01".toCharArray()) { 22 | chars[curr]=c; 23 | if (dfs(chars, curr+1)) return true; 24 | } 25 | return false; 26 | } 27 | 28 | private boolean able(String s) { 29 | long v=Long.parseLong(s, 2), x=(long)Math.sqrt(v); 30 | return x*x==v; 31 | } 32 | 33 | public static void main(String[] args) throws Exception { 34 | System.setOut(new PrintStream("output.txt")); 35 | Scanner scanner=new Scanner(System.in); 36 | int times=scanner.nextInt(); 37 | long start=System.currentTimeMillis(); 38 | for (int t=1;t<=times;t++) { 39 | try { 40 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 41 | } 42 | catch (Throwable e) { 43 | System.err.println("ERROR in case #"+t); 44 | e.printStackTrace(); 45 | } 46 | } 47 | long end=System.currentTimeMillis(); 48 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 49 | 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /codejam/2012/Qualification Round/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2012 Qualification Round 7 | * Problem B. Dancing With the Googlers 8 | */ 9 | public class Main { 10 | 11 | private String solve(Scanner scanner) { 12 | int n=scanner.nextInt(), s=scanner.nextInt(), p=scanner.nextInt(); 13 | int[] t=new int[n]; 14 | for (int i=0;i=p?1:0)+dp[i-1][j], 23 | 24 | // is a surprise 25 | j==0?0:(((t[i-1]+4)/3>=Math.max(p, 2)?1:0)+dp[i-1][j-1]) 26 | ); 27 | 28 | } 29 | } 30 | return String.valueOf(dp[n][s]); 31 | } 32 | 33 | public static void main(String[] args) throws Exception { 34 | System.setOut(new PrintStream("output.txt")); 35 | Scanner scanner=new Scanner(System.in); 36 | long times=Integer.parseInt(scanner.nextLine()); 37 | long start=System.currentTimeMillis(); 38 | for (long t=1;t<=times;t++) { 39 | try { 40 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 41 | } 42 | catch (Throwable e) { 43 | System.err.println("ERROR in case #"+t); 44 | e.printStackTrace(); 45 | } 46 | } 47 | long end=System.currentTimeMillis(); 48 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 49 | } 50 | } 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /codejam/2012/Qualification Round/C.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2012 Qualification Round 7 | * Problem C. Recycled Numbers 8 | */ 9 | public class Main { 10 | 11 | private String solve(Scanner scanner) { 12 | int a=scanner.nextInt(), b=scanner.nextInt(), cnt=0; 13 | for (int x=a;x<=b;x++) { 14 | String s=Integer.toString(x); 15 | HashSet set=new HashSet<>(); 16 | for (int i=0;ix && y<=b) set.add(y); 20 | } 21 | cnt+=set.size(); 22 | } 23 | return String.valueOf(cnt); 24 | } 25 | 26 | public static void main(String[] args) throws Exception { 27 | System.setOut(new PrintStream("output.txt")); 28 | Scanner scanner=new Scanner(System.in); 29 | long times=Integer.parseInt(scanner.nextLine()); 30 | long start=System.currentTimeMillis(); 31 | for (long t=1;t<=times;t++) { 32 | try { 33 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 34 | } 35 | catch (Throwable e) { 36 | System.err.println("ERROR in case #"+t); 37 | e.printStackTrace(); 38 | } 39 | } 40 | long end=System.currentTimeMillis(); 41 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 42 | } 43 | } 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /codejam/2012/Round1A/A.java: -------------------------------------------------------------------------------- 1 | import java.io.File; 2 | import java.io.PrintStream; 3 | import java.util.*; 4 | import java.util.stream.Collectors; 5 | 6 | /** 7 | * Codejam 2012 Round 1A 8 | * Problem A. Password Problem 9 | */ 10 | public class Main { 11 | 12 | private String solve(Scanner scanner) { 13 | int a=scanner.nextInt(), b=scanner.nextInt(); 14 | double[] probs=new double[a]; 15 | for (int i=0;i list=new ArrayList<>(); 18 | while (list.size()v+1e-9) { 25 | v=v1; index=i; 26 | } 27 | } 28 | used[index]=true; 29 | list.add(String.valueOf(index)); 30 | } 31 | return String.join(" ", list); 32 | } 33 | 34 | public static void main(String[] args) throws Exception { 35 | System.setOut(new PrintStream("output.txt")); 36 | Scanner scanner=new Scanner(System.in); 37 | int times=scanner.nextInt(); 38 | long start=System.currentTimeMillis(); 39 | for (int t=1;t<=times;t++) { 40 | try { 41 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 42 | } 43 | catch (Throwable e) { 44 | System.err.println("ERROR in case #"+t); 45 | e.printStackTrace(); 46 | } 47 | } 48 | long end=System.currentTimeMillis(); 49 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 50 | 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /codejam/2013/Qualification Round/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2013 Qualification Round 7 | * Problem B. Lawnmower 8 | */ 9 | public class Main { 10 | 11 | private String solve(Scanner scanner) throws Throwable { 12 | int r=scanner.nextInt(), c=scanner.nextInt(); 13 | int[][] a=new int[r][c]; 14 | for (int i=0;ia[i][j]) { 20 | able=false; 21 | } 22 | } 23 | if (able) continue; 24 | able=true; 25 | for (int w=0;wa[i][j]) { 27 | able=false; 28 | } 29 | } 30 | if (able) continue; 31 | return "NO"; 32 | } 33 | } 34 | return "YES"; 35 | } 36 | 37 | public static void main(String[] args) throws Exception { 38 | System.setOut(new PrintStream("output.txt")); 39 | Scanner scanner=new Scanner(System.in); 40 | int times=scanner.nextInt(); 41 | long start=System.currentTimeMillis(); 42 | for (int t=1;t<=times;t++) { 43 | try { 44 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 45 | } 46 | catch (Throwable e) { 47 | System.err.println("ERROR in case #"+t); 48 | } 49 | } 50 | long end=System.currentTimeMillis(); 51 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 52 | 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /codejam/2013/Round1A/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.math.BigInteger; 3 | import java.util.*; 4 | import java.util.stream.Collectors; 5 | 6 | /** 7 | * Codejam 2013 Round 1A 8 | * Problem A. Bullseye 9 | */ 10 | public class Main { 11 | 12 | private String solve(Scanner scanner) throws Throwable { 13 | long r=scanner.nextLong(), t=scanner.nextLong(); 14 | long a=2, b=2*r+3, c=2*r+1-t; 15 | long start=0, end=4000000000000000000L; 16 | while (start set=new TreeSet<>(); 16 | int[] cnt=new int[l+1]; 17 | cnt[l]=0; 18 | for (int i=l-1;i>=0;i--) { 19 | if (isConsonant(s.charAt(i))) cnt[i]=cnt[i+1]+1; 20 | } 21 | for (int i=0;i=n) set.add(i); 23 | } 24 | long ans=0; 25 | for (int i=l-1;i>=n-1;i--) { 26 | Integer v=set.floor(i-n+1); 27 | if (v!=null) 28 | ans+=v+1; 29 | } 30 | return String.valueOf(ans); 31 | } 32 | 33 | private boolean isConsonant(char c) { 34 | return c!='a' && c!='e' && c!='i' && c!='o' && c!='u'; 35 | } 36 | 37 | public static void main(String[] args) throws Exception { 38 | System.setOut(new PrintStream("output.txt")); 39 | // Scanner scanner=new Scanner(System.in); 40 | Scanner scanner=new Scanner(new File("input.txt")); 41 | int times=scanner.nextInt(); 42 | long start=System.currentTimeMillis(); 43 | for (int t=1;t<=times;t++) { 44 | try { 45 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 46 | } 47 | catch (Exception e) {System.err.println("ERROR #"+t);e.printStackTrace();} 48 | } 49 | long end=System.currentTimeMillis(); 50 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.)); 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /codejam/2013/Round1C/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam 2013 Round 1C 6 | * Problem B. Pogo 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner sc) { 11 | int x=sc.nextInt(), y=sc.nextInt(); 12 | int sum=0, n=0; 13 | while (sum0) { 18 | if (Math.abs(x)>=Math.abs(y)) { 19 | if (x<0) { 20 | builder.append('W');x+=n; 21 | } 22 | else { 23 | builder.append('E');x-=n; 24 | } 25 | } 26 | else { 27 | if (y<0) { 28 | builder.append('S'); y+=n; 29 | } 30 | else { 31 | builder.append('N');y-=n; 32 | } 33 | } 34 | n--; 35 | } 36 | return builder.reverse().toString(); 37 | } 38 | 39 | public static void main(String[] args) throws Exception{ 40 | System.setOut(new PrintStream("output.txt")); 41 | Scanner sc = new Scanner(System.in); 42 | int times = sc.nextInt(); 43 | for(int i=1; i<=times; i++){ 44 | // System.out.println(String.format("Case #%d: %s", i, new Main().solve(sc))); 45 | try { 46 | System.out.println(String.format("Case #%d: %s", i, new Main().solve(sc))); 47 | }catch (Exception e){ 48 | System.err.println(String.format("error at case#%d", i)); 49 | } 50 | } 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /codejam/2013/Round2/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2013 Round 2 7 | * Problem B. Many Prizes 8 | */ 9 | public class Main { 10 | 11 | public String solve(Scanner scanner) { 12 | int n=scanner.nextInt(); long p=scanner.nextLong(); 13 | return String.valueOf(must(n, p)-1)+" "+String.valueOf(may(n, p)); 14 | } 15 | 16 | private long must(int n, long p) { 17 | if (p==(1L<0) ans=-1; 26 | if (ans==0) ans=i+1; 27 | } 28 | } 29 | if (ans==0) return "Volunteer cheated!"; 30 | if (ans==-1) return "Bad magician!"; 31 | return String.valueOf(ans); 32 | } 33 | 34 | 35 | public static void main(String[] args) throws Exception { 36 | System.setOut(new PrintStream("output.txt")); 37 | Scanner scanner=new Scanner(System.in); 38 | int times=scanner.nextInt(); 39 | long start=System.currentTimeMillis(); 40 | for (int t=1;t<=times;t++) { 41 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 42 | } 43 | long end=System.currentTimeMillis(); 44 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 45 | 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /codejam/2014/Qualification Round/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam 2014 Qualification Round 6 | * Problem B. Cookie Clicker Alpha 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | double c=scanner.nextDouble(), f=scanner.nextDouble(), x=scanner.nextDouble(); 12 | double speed=2.0, t=x/speed, curt=.0; 13 | while (true) { 14 | curt+=c/speed; 15 | speed+=f; 16 | if (t>curt+x/speed) 17 | t=curt+x/speed; 18 | else break; 19 | } 20 | return String.format("%.9f", t); 21 | } 22 | 23 | 24 | public static void main(String[] args) throws Exception { 25 | System.setOut(new PrintStream("output.txt")); 26 | Scanner scanner=new Scanner(System.in); 27 | int times=scanner.nextInt(); 28 | long start=System.currentTimeMillis(); 29 | for (int t=1;t<=times;t++) { 30 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 31 | } 32 | long end=System.currentTimeMillis(); 33 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 34 | 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /codejam/2014/Round1A/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2014 Round 1A 7 | * Problem B. Full Binary Tree 8 | */ 9 | public class Main { 10 | 11 | private String solve(Scanner scanner) { 12 | int n=scanner.nextInt(); 13 | ArrayList[] arrayLists=new ArrayList[n+1]; 14 | for (int i=1;i<=n;i++) arrayLists[i]=new ArrayList<>(); 15 | for (int i=1;i<=n-1;i++) { 16 | int x=scanner.nextInt(), y=scanner.nextInt(); 17 | arrayLists[x].add(y); 18 | arrayLists[y].add(x); 19 | } 20 | int max=0; 21 | for (int i=1;i<=n;i++) 22 | max=Math.max(max, dfs(arrayLists, i, 0)); 23 | return String.valueOf(n-max); 24 | } 25 | 26 | private int dfs(ArrayList[] arrayLists, int current, int parent) { 27 | ArrayList arrayList=arrayLists[current]; 28 | if (arrayList.size()<2 || (parent!=0 && arrayList.size()==2)) 29 | return 1; 30 | ArrayList next=new ArrayList<>(); 31 | for (int x: arrayList) { 32 | if (x!=parent) 33 | next.add(dfs(arrayLists, x, current)); 34 | } 35 | next.sort((o1,o2)->o2-o1); 36 | return next.get(0)+next.get(1)+1; 37 | } 38 | 39 | public static void main(String[] args) throws Exception { 40 | System.setOut(new PrintStream("output.txt")); 41 | Scanner scanner=new Scanner(System.in); 42 | int times=scanner.nextInt(); 43 | long start=System.currentTimeMillis(); 44 | for (int t=1;t<=times;t++) { 45 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 46 | } 47 | long end=System.currentTimeMillis(); 48 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 49 | 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /codejam/2014/Round1B/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2014 Round 1B 7 | * Problem B. New Lottery Game 8 | */ 9 | public class Main { 10 | 11 | private String solve(Scanner scanner) { 12 | long a=scanner.nextLong(), b=scanner.nextLong(), k=scanner.nextLong(); 13 | return String.valueOf(solve(a,b,k,new HashMap<>())); 14 | } 15 | 16 | private long solve(long a, long b, long k, HashMap map) { 17 | if (map.containsKey(a+","+b+","+k)) return map.get(a+","+b+","+k); 18 | if (a==0 || b==0 || k==0) return 0; 19 | if (a==1 && b==1) return 1; 20 | long ans=solve(a/2, b/2, k/2, map) 21 | +solve((a+1)/2, b/2, (k+1)/2, map) 22 | +solve(a/2, (b+1)/2, (k+1)/2, map) 23 | +solve((a+1)/2, (b+1)/2, (k+1)/2, map); 24 | map.put(a+","+b+","+k, ans); 25 | return ans; 26 | } 27 | 28 | public static void main(String[] args) throws Exception { 29 | System.setOut(new PrintStream("output.txt")); 30 | Scanner scanner=new Scanner(System.in); 31 | int times=scanner.nextInt(); 32 | long start=System.currentTimeMillis(); 33 | for (int t=1;t<=times;t++) { 34 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 35 | } 36 | long end=System.currentTimeMillis(); 37 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 38 | 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /codejam/2014/Round1C/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2014 Round 1C 7 | * Problem A. Part Elf 8 | */ 9 | public class Main { 10 | 11 | private String solve(Scanner scanner) { 12 | String s=scanner.next(); 13 | String[] ss=s.split("/"); 14 | long p=Long.parseLong(ss[0]), q=Long.parseLong(ss[1]), g=gcd(p, q); 15 | p/=g; q/=g; 16 | if ((q&(q-1))!=0) return "impossible"; 17 | int cnt=0; 18 | while (p linkedList=new LinkedList<>(Arrays.stream(s).boxed().collect(Collectors.toList())); 17 | int cnt=0; 18 | while (linkedList.size()>0) { 19 | int mx=linkedList.pollLast(); 20 | cnt++; 21 | if (linkedList.size()>0 && linkedList.peekFirst()+mx<=x) 22 | linkedList.pollFirst(); 23 | } 24 | return String.valueOf(cnt); 25 | } 26 | 27 | public static void main(String[] args) throws Exception { 28 | System.setOut(new PrintStream("output.txt")); 29 | Scanner scanner=new Scanner(System.in); 30 | int times=scanner.nextInt(); 31 | long start=System.currentTimeMillis(); 32 | for (int t=1;t<=times;t++) { 33 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 34 | } 35 | long end=System.currentTimeMillis(); 36 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 37 | 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /codejam/2014/Round2/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2014 Round 2 7 | * Problem B. Up and Down 8 | */ 9 | public class Main { 10 | 11 | private String solve(Scanner scanner) { 12 | int n=scanner.nextInt(); 13 | ArrayList arrayList=new ArrayList<>(); 14 | for (int i=0;i arrayList) { 23 | int min=Integer.MAX_VALUE, index=-1; 24 | for (int i=0;iarrayList.get(i)) { 26 | min=arrayList.get(i);index=i; 27 | } 28 | } 29 | arrayList.remove(index); 30 | return Math.min(index, arrayList.size()-index); 31 | } 32 | 33 | public static void main(String[] args) throws Exception { 34 | System.setOut(new PrintStream("output.txt")); 35 | Scanner scanner=new Scanner(System.in); 36 | int times=scanner.nextInt(); 37 | long start=System.currentTimeMillis(); 38 | for (int t=1;t<=times;t++) { 39 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 40 | } 41 | long end=System.currentTimeMillis(); 42 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 43 | 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /codejam/2014/Round3/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2014 Round 3 7 | * Problem A. Magical, Marvelous Tour 8 | */ 9 | public class Main { 10 | 11 | public String solve(Scanner scanner) { 12 | int n=scanner.nextInt(); 13 | long p=scanner.nextLong(), q=scanner.nextLong(), r=scanner.nextLong(), 14 | s=scanner.nextLong(), sum=0; 15 | long[] a=new long[n]; 16 | for (int i=0;icandidate) return false; 35 | if (sum+a[i]>candidate) { 36 | cnt++; 37 | sum=a[i]; 38 | } 39 | else { 40 | sum+=a[i]; 41 | } 42 | if (cnt>m) break; 43 | } 44 | return cnt<=m; 45 | } 46 | 47 | public static void main(String[] args) throws Exception { 48 | System.setOut(new PrintStream("output.txt")); 49 | Scanner scanner=new Scanner(System.in); 50 | int times=scanner.nextInt(); 51 | long start=System.currentTimeMillis(); 52 | for (int t=1;t<=times;t++) { 53 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 54 | } 55 | long end=System.currentTimeMillis(); 56 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.)); 57 | } 58 | } -------------------------------------------------------------------------------- /codejam/2015/Qualification Round/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam 2015 Qualification Round 6 | * Problem A. Standing Ovation 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | int n=scanner.nextInt(), x=0, cnt=0; 12 | String s=scanner.next(); 13 | for (char c: s.toCharArray()) { 14 | x+=c-'1'; 15 | if (x<0) { 16 | cnt++; 17 | x=0; 18 | } 19 | } 20 | return String.valueOf(cnt); 21 | } 22 | 23 | 24 | public static void main(String[] args) throws Exception { 25 | System.setOut(new PrintStream("E:\\desktop\\output.txt")); 26 | Scanner scanner=new Scanner(System.in); 27 | int times=scanner.nextInt(); 28 | for (int t=1;t<=times;t++) { 29 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 30 | } 31 | 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /codejam/2015/Qualification Round/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam 2015 Qualification Round 6 | * Problem B. Infinite House of Pancakes 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | int n=scanner.nextInt(); 12 | int[] num=new int[n]; 13 | for (int i=0;i=12) x=x%4+8; 14 | StringBuilder builder=new StringBuilder(); 15 | String s=scanner.next().replace('i','2').replace('j','3').replace('k','4'); 16 | for (int i=0;i=7) return false; 16 | if (r=2; 19 | if (x==4) return c>=3; 20 | if (x==5) return c>=3 && !(c==3 && r==5); 21 | return c>=4; 22 | } 23 | 24 | public static void main(String[] args) throws Exception { 25 | System.setOut(new PrintStream("E:\\desktop\\output.txt")); 26 | Scanner scanner=new Scanner(System.in); 27 | int times=scanner.nextInt(); 28 | for (int t=1;t<=times;t++) { 29 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 30 | } 31 | 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /codejam/2015/Round1A/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam 2015 Round 1A 6 | * Problem A. Mushroom Monster 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | int n=scanner.nextInt(); 12 | int[] a=new int[n]; 13 | for (int i=0;ia[i+1]) { 18 | x+=a[i]-a[i+1]; 19 | maxgap=Math.max(maxgap, a[i]-a[i+1]); 20 | } 21 | } 22 | for (int i=0;i=n) end=mid; 19 | else start=mid+1; 20 | } 21 | n-=cal(start-1, m); 22 | for (int i=0;i=d || ans[index]>curr) { 19 | res++; 20 | curr*=(c+1); 21 | } 22 | else 23 | curr+=c*ans[index++]; 24 | } 25 | return String.valueOf(res); 26 | } 27 | 28 | public static void main(String[] args) throws Exception { 29 | System.setOut(new PrintStream("E:\\desktop\\output.txt")); 30 | Scanner scanner=new Scanner(System.in); 31 | int times=scanner.nextInt(); 32 | long start=System.currentTimeMillis(); 33 | for (int t=1;t<=times;t++) { 34 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 35 | } 36 | long end=System.currentTimeMillis(); 37 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 38 | 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /codejam/2015/Round3/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam 2015 Round 3 6 | * Problem B. Smoothing Window 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | int n=scanner.nextInt(), k=scanner.nextInt(); 12 | int[] s=new int[n-k+1]; 13 | for (int i=0;i<=n-k;i++) s[i]=scanner.nextInt(); 14 | int[] a=new int[n]; 15 | for (int i=0;i priorityQueue=new PriorityQueue<>(); 20 | for (int i=0;i0) 32 | priorityQueue.offer(priorityQueue.poll()+1); 33 | int ans=0; 34 | for (int e: priorityQueue) 35 | ans=Math.max(ans, e); 36 | return String.valueOf(ans); 37 | } 38 | 39 | public static void main(String[] args) throws Exception { 40 | System.setOut(new PrintStream("E:\\desktop\\output.txt")); 41 | Scanner scanner=new Scanner(System.in); 42 | int times=scanner.nextInt(); 43 | long start=System.currentTimeMillis(); 44 | for (int t=1;t<=times;t++) { 45 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 46 | } 47 | long end=System.currentTimeMillis(); 48 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 49 | 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /codejam/2016/Qualification Round/A.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam 2016 Qualification Round 6 | * Problem A. Counting Sheep 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | long n=scanner.nextLong(); 12 | if (n==0) return "INSOMNIA"; 13 | boolean[] chars=new boolean[10]; 14 | int cnt=0; 15 | for (long v=n;;v+=n) { 16 | for (char c: String.valueOf(v).toCharArray()) { 17 | if (!chars[c-'0']) { 18 | chars[c-'0']=true;cnt++; 19 | } 20 | } 21 | if (cnt==10) return String.valueOf(v); 22 | } 23 | } 24 | 25 | public static void main(String[] args) throws Exception { 26 | System.setOut(new PrintStream("output.txt")); 27 | Scanner scanner=new Scanner(System.in); 28 | int times=scanner.nextInt(); 29 | long start=System.currentTimeMillis(); 30 | for (int t=1;t<=times;t++) { 31 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 32 | } 33 | long end=System.currentTimeMillis(); 34 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 35 | 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /codejam/2016/Qualification Round/B.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam 2016 Qualification Round 6 | * Problem B. Revenge of the Pancakes 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | String s=scanner.next(); 12 | return String.valueOf(solve(s.toCharArray())); 13 | } 14 | 15 | private int solve(char[] chars) { 16 | if (chars[0]=='+') { 17 | for (int i=0;i ans=new ArrayList<>(); 14 | for (int curr=1;curr<=k;curr+=c) { 15 | long p=1; 16 | for (int j=0;j0 && c>=builder.charAt(0)) 15 | builder.insert(0, c); 16 | else builder.append(c); 17 | } 18 | return builder.toString(); 19 | } 20 | 21 | public static void main(String[] args) throws Exception { 22 | System.setOut(new PrintStream("output.txt")); 23 | Scanner scanner=new Scanner(System.in); 24 | int times=scanner.nextInt(); 25 | long start=System.currentTimeMillis(); 26 | for (int t=1;t<=times;t++) { 27 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 28 | } 29 | long end=System.currentTimeMillis(); 30 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 31 | 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /codejam/2016/Round1A/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2016 Round 1A 7 | * Problem B. Rank and File 8 | */ 9 | public class Main { 10 | 11 | private String solve(Scanner scanner) { 12 | int n=scanner.nextInt(); 13 | HashSet set=new HashSet<>(); 14 | for (int i=1;i<2*n;i++) { 15 | for (int j=1;j<=n;j++) { 16 | int x=scanner.nextInt(); 17 | if (!set.add(x)) set.remove(x); 18 | } 19 | } 20 | return String.join(" ", set.stream().sorted().map(String::valueOf).collect(Collectors.toList())); 21 | } 22 | 23 | public static void main(String[] args) throws Exception { 24 | System.setOut(new PrintStream("output.txt")); 25 | Scanner scanner=new Scanner(System.in); 26 | int times=scanner.nextInt(); 27 | long start=System.currentTimeMillis(); 28 | for (int t=1;t<=times;t++) { 29 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 30 | } 31 | long end=System.currentTimeMillis(); 32 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 33 | 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /codejam/2016/Round1B/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * Codejam 2016 Round 1B 6 | * Problem A. Getting the Digits 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | String s=scanner.next().toLowerCase(); 12 | int[] cnt=new int[128]; 13 | for (char c: s.toCharArray()) { 14 | cnt[c]++; 15 | } 16 | int[] num=new int[10]; 17 | num[0]=cnt['z']; 18 | num[2]=cnt['w']; 19 | num[4]=cnt['u']; 20 | num[6]=cnt['x']; 21 | num[8]=cnt['g']; 22 | num[7]=cnt['s']-num[6]; 23 | num[5]=cnt['f']-num[4]; 24 | num[3]=cnt['h']-num[8]; 25 | num[1]=cnt['o']-num[0]-num[2]-num[4]; 26 | num[9]=cnt['i']-num[5]-num[6]-num[8]; 27 | StringBuilder builder=new StringBuilder(); 28 | for (int i=0;i<10;i++) 29 | for (int j=1;j<=num[i];j++) 30 | builder.append(i); 31 | return builder.toString(); 32 | } 33 | 34 | public static void main(String[] args) throws Exception { 35 | System.setOut(new PrintStream("output.txt")); 36 | Scanner scanner=new Scanner(System.in); 37 | int times=scanner.nextInt(); 38 | long start=System.currentTimeMillis(); 39 | for (int t=1;t<=times;t++) { 40 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 41 | } 42 | long end=System.currentTimeMillis(); 43 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 44 | 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /codejam/2016/Round1C/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | import java.util.stream.IntStream; 5 | 6 | /** 7 | * Codejam 2016 Round 1C 8 | * Problem B. Slides! 9 | */ 10 | public class Main { 11 | 12 | private String solve(Scanner scanner) { 13 | int b=scanner.nextInt(); 14 | long m=scanner.nextLong(); 15 | if (m>(1L<<(b-2))) return "IMPOSSIBLE"; 16 | char[][] map=new char[b][b]; 17 | for (int i=0;i arrayList=new ArrayList<>(); 15 | arrayList.add(String.valueOf(a*b*Math.min(c, k))); 16 | for (int i=1;i<=a;i++) { 17 | for (int j=1;j<=b;j++) { 18 | for (int p=0;p set=new HashSet<>(); 13 | for (int i=0;i=0?String.valueOf(v):"IMPOSSIBLE"; 15 | } 16 | 17 | private int solve(char[] c, int n, int k) { 18 | if (n==0) return 0; 19 | if (c[n-1]=='+') return solve(c, n-1, k); 20 | if (n=n-k;i--) 22 | c[i]=(char)('+'+'-'-c[i]); 23 | return 1+solve(c, n-1, k); 24 | } 25 | 26 | 27 | public static void main(String[] args) throws Exception { 28 | System.setOut(new PrintStream("output.txt")); 29 | Scanner scanner=new Scanner(System.in); 30 | int times=scanner.nextInt(); 31 | long start=System.currentTimeMillis(); 32 | for (int t=1;t<=times;t++) { 33 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 34 | } 35 | long end=System.currentTimeMillis(); 36 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 37 | 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /codejam/2017/Round1B/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2017 Round 1B 7 | * Problem A. Steed 2: Cruise Control 8 | */ 9 | public class Main { 10 | 11 | private String solve(Scanner scanner) throws Throwable { 12 | double d=scanner.nextDouble(); 13 | int n=scanner.nextInt(); 14 | double tmax=0.; 15 | while (n-->0) { 16 | double k=scanner.nextDouble(), s=scanner.nextDouble(); 17 | tmax=Math.max(tmax, (d-k)/s); 18 | } 19 | return String.format("%.9f", d/tmax); 20 | } 21 | 22 | 23 | 24 | public static void main(String[] args) throws Exception { 25 | System.setOut(new PrintStream("output.txt")); 26 | Scanner scanner=new Scanner(System.in); 27 | int times=scanner.nextInt(); 28 | long start=System.currentTimeMillis(); 29 | for (int t=1;t<=times;t++) { 30 | try { 31 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 32 | } 33 | catch (Throwable e) { 34 | System.err.println("ERROR in case #"+t); 35 | } 36 | } 37 | long end=System.currentTimeMillis(); 38 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 39 | 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /codejam/2017/Round1C/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * Codejam 2017 Round 1C 7 | * Problem A. Ample Syrup 8 | */ 9 | public class Main { 10 | 11 | class PanCake { 12 | long r, h; 13 | public PanCake(long _r, long _h) {r=_r;h=_h;} 14 | public long getArea() {return r*h;} 15 | } 16 | 17 | public String solve(Scanner scanner) { 18 | int n=scanner.nextInt(), k=scanner.nextInt(); 19 | PanCake[] panCakes=new PanCake[n]; 20 | for (int i=0;i list=new ArrayList<>(); 26 | long r=panCakes[i].r, sum=r*r+2*panCakes[i].getArea(); 27 | for (int j=0;j-p.getArea())); 32 | for (int j=0;javg+1e-10) { 31 | ans*=p[index-1]; 32 | index--; 33 | } 34 | if (index{if (str.length>len) len=str.length;}); 11 | 12 | arr=arr.map(str=>{ 13 | while (str.lengthset.add(str[i])); 21 | 22 | if (set.size===3) return 'IMPOSSIBLE'; 23 | 24 | if (set.size===1) { 25 | r.push(set.has('P')?'S':set.has('R')?'P':'R'); 26 | return r.join(''); 27 | } 28 | 29 | r.push(set.has('P')&&set.has('R')?'P':set.has('P')&&set.has('S')?'S':'R'); 30 | const latest=r[r.length-1]; 31 | arr=arr.filter(str=>str[i]===latest); 32 | } 33 | return 'IMPOSSIBLE'; 34 | } 35 | 36 | // an input handling/parsing 37 | const fs=require('fs'); 38 | const input=fs.readFileSync(0,'utf8').trim(); 39 | const tests=input.split(/\d+\s/).filter(Boolean); 40 | console.log(tests.map((t,i)=> { 41 | const arg= t.split('\n').filter(Boolean); 42 | return `Case #${i+1}: ${solve(arg)}`; 43 | }).join('\n')); -------------------------------------------------------------------------------- /kickstart/2017/Practice Round/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * KickStart 2017 Practice Round Problem A: Country Leader 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | class Node { 11 | String s; 12 | int num; 13 | public Node(String _s) { 14 | s=_s; 15 | boolean[] cnt=new boolean[26]; 16 | for (char c: s.toCharArray()) 17 | if (c!=' ') 18 | cnt[c-'A']=true; 19 | num=0; 20 | for (boolean b: cnt) 21 | if (b) 22 | num++; 23 | } 24 | } 25 | 26 | private String solve(Scanner scanner) { 27 | int n=scanner.nextInt(); 28 | scanner.nextLine(); 29 | Node[] nodes=new Node[n]; 30 | for (int i=0;io1.num==o2.num?o1.s.compareTo(o2.s):o2.num-o1.num); 34 | return nodes[0].s; 35 | } 36 | 37 | public static void main(String[] args) throws Exception { 38 | System.setOut(new PrintStream("output.txt")); 39 | long start=System.currentTimeMillis(); 40 | Scanner scanner=new Scanner(System.in); 41 | int times=scanner.nextInt(); 42 | for (int t=1;t<=times;t++) { 43 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 44 | } 45 | long end=System.currentTimeMillis(); 46 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 47 | 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /kickstart/2017/Practice Round/A.js: -------------------------------------------------------------------------------- 1 | /* 2 | KickStart 2017 3 | Practice Round 4 | Problem A: Country Leader 5 | Both solved 6 | */ 7 | const fs= require('fs'); 8 | const input= fs.readFileSync(0,'utf8').trim().split(/\d+\s+/).filter(Boolean); 9 | 10 | console.log(input.map((e,i)=>`Case #${i+1}: ${solve(e)}`).join('\n')); 11 | 12 | function solve(strOfNames) { 13 | const allNames= strOfNames.split('\n'); 14 | const longestNames= new Set; 15 | let max= 0; 16 | allNames.forEach((name)=>{ 17 | const uniqueChars= new Set(name); 18 | uniqueChars.delete(' '); 19 | if(uniqueChars.size>max) { 20 | max= uniqueChars.size; 21 | longestNames.clear(); 22 | } 23 | if (uniqueChars.size===max) longestNames.add(name); 24 | }); 25 | return [...longestNames].sort()[0]; 26 | } 27 | -------------------------------------------------------------------------------- /kickstart/2017/Practice Round/B.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * KickStart 2017 Practice Round Problem B: Vote 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | double n=scanner.nextDouble(), m=scanner.nextDouble(); 12 | return String.format("%.9f", (n-m)/(n+m)); 13 | } 14 | 15 | public static void main(String[] args) throws Exception { 16 | System.setOut(new PrintStream("output.txt")); 17 | long start=System.currentTimeMillis(); 18 | Scanner scanner=new Scanner(System.in); 19 | int times=scanner.nextInt(); 20 | for (int t=1;t<=times;t++) { 21 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 22 | } 23 | long end=System.currentTimeMillis(); 24 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 25 | 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /kickstart/2017/Practice Round/C.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * KickStart 2017 Practice Round Problem C: Sherlock and Parentheses 6 | * Check README.md for explanation. 7 | */ 8 | public class Main { 9 | 10 | private String solve(Scanner scanner) { 11 | long l=scanner.nextLong(), r=scanner.nextLong(), m=Math.min(l, r); 12 | return String.valueOf(m*(m+1)/2); 13 | } 14 | 15 | public static void main(String[] args) throws Exception { 16 | System.setOut(new PrintStream("output.txt")); 17 | long start=System.currentTimeMillis(); 18 | Scanner scanner=new Scanner(System.in); 19 | int times=scanner.nextInt(); 20 | for (int t=1;t<=times;t++) { 21 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 22 | } 23 | long end=System.currentTimeMillis(); 24 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /kickstart/2017/Practice Round/README.md: -------------------------------------------------------------------------------- 1 | # KickStart 2017 Practice Round 2 | https://code.google.com/codejam/contest/6304486/dashboard 3 | 4 | ## Problem A: Country Leader 5 | 6 | Simple. Nothing to tell. 7 | 8 | ## Problem B: Vote 9 | 10 | [Catalan Number](https://en.wikipedia.org/wiki/Catalan_number). 11 | Consider of a way with n upstrokes and m downstrokes. 12 | It's a route from `(0, 0) -> (n+m, n-m)`, totals numbers are `(n+m)Cm`. 13 | However, the first vote must be A, and afterwards the way cannot 14 | reach the horizontal line. That is, `(1, 1) -> (n+m, n-m)` where the whole route 15 | is above the horizontal line. 16 | All possible routes number of `(1, 1) -> (n+m, n-m)` is `(n+m-1)C(n-1)`. 17 | All invalid routes number is `(n+m-1)C(m-1)` (Reflection, 18 | check [here](https://en.wikipedia.org/wiki/Catalan_number#Second_proof).) 19 | Finally, the result is 20 | `((n+m-1)C(n-1)-(n+m-1)C(m-1))/((n+m)Cn) = (n-m)/(n+m)`. 21 | 22 | ## Problem C: Sherlock and Parentheses 23 | 24 | `x*(x+1)/2, where x=min(l, r)`. 25 | -------------------------------------------------------------------------------- /kickstart/2017/RoundB/A.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | import java.util.stream.Collectors; 4 | 5 | /** 6 | * KickStart 2017 Round B Problem A: Math Encoder 7 | * Check README.md for explanation. 8 | */ 9 | public class Main { 10 | 11 | private long pow(long x, int k, long mod) { 12 | if (k==0) return 1; 13 | long p=pow(x, k/2, mod)%mod; 14 | return p*p%mod*(k%2!=0?x:1)%mod; 15 | } 16 | 17 | private String solve(Scanner scanner) throws Throwable { 18 | int n=scanner.nextInt(); 19 | long[] a=new long[n]; 20 | for (int i=0;i map=new HashMap<>(); 24 | for (int i=0;i<=10000;i++) map.put(i, pow(2L, i, mod)); 25 | for (int i=0;i list=new ArrayList<>(); 13 | for (int i=0;i list=new LinkedList<>(); 13 | for (int i=0;ilist.peekFirst()) { 18 | e-=list.pollFirst(); ans++; 19 | } 20 | else if (ans>0 && list.size()>1) { 21 | e+=list.pollLast()-list.pollFirst(); 22 | } 23 | else break; 24 | } 25 | return String.valueOf(ans); 26 | } 27 | 28 | public static void main(String[] args) throws Exception { 29 | System.setOut(new PrintStream("output.txt")); 30 | Scanner scanner=new Scanner(System.in); 31 | int times=Integer.parseInt(scanner.nextLine()); 32 | long start=System.currentTimeMillis(); 33 | for (int t=1;t<=times;t++) { 34 | try { 35 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 36 | } 37 | catch (Throwable e) { 38 | System.err.println("ERROR in case #"+t); 39 | e.printStackTrace(); 40 | } 41 | } 42 | long end=System.currentTimeMillis(); 43 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 44 | 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /kickstart/2017/RoundF/D.java: -------------------------------------------------------------------------------- 1 | import java.io.PrintStream; 2 | import java.util.*; 3 | 4 | /** 5 | * KickStart 2017 Round F 6 | * Problem D. Eat Cake 7 | */ 8 | public class Main { 9 | 10 | public String solve(Scanner scanner) { 11 | int n=scanner.nextInt(); 12 | if (check1(n)) return "1"; 13 | if (check2(n)) return "2"; 14 | if (check3(n)) return "3"; 15 | return "4"; 16 | } 17 | 18 | private boolean check1(int n) { 19 | for (int i=0;i<=100;i++) 20 | if (i*i==n) return true; 21 | return false; 22 | } 23 | 24 | private boolean check2(int n) { 25 | for (int i=0;i<=100;i++) 26 | for (int j=0;j<=100;j++) 27 | if (i*i+j*j==n) 28 | return true; 29 | return false; 30 | } 31 | 32 | private boolean check3(int n) { 33 | for (int i=0;i<=100;i++) 34 | for (int j=0;j<=100;j++) 35 | for (int k=0;k<=100;k++) 36 | if (i*i+j*j+k*k==n) 37 | return true; 38 | return false; 39 | } 40 | 41 | 42 | public static void main(String[] args) throws Exception { 43 | System.setOut(new PrintStream("output.txt")); 44 | Scanner scanner=new Scanner(System.in); 45 | int times=Integer.parseInt(scanner.nextLine()); 46 | long start=System.currentTimeMillis(); 47 | for (int t=1;t<=times;t++) { 48 | try { 49 | System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner))); 50 | } 51 | catch (Throwable e) { 52 | System.err.println("ERROR in case #"+t); 53 | e.printStackTrace(); 54 | } 55 | } 56 | long end=System.currentTimeMillis(); 57 | System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0)); 58 | 59 | } 60 | 61 | } -------------------------------------------------------------------------------- /kickstart/2017/RoundG/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ckcz123/codejam/e0632ddf91ed987b7e3ecb540ffd1f88499ee6f5/kickstart/2017/RoundG/README.md -------------------------------------------------------------------------------- /kickstart/2018/RoundA/A.js: -------------------------------------------------------------------------------- 1 | /** 2 | * KickStart 2018 Round A 3 | * Problem A. Even Digits 4 | * Both solved 5 | */ 6 | let fs= require('fs'); 7 | let input= fs.readFileSync(0,'utf8').trim().split('\n').slice(1); 8 | console.log(input.map((e,i)=>`Case #${i+1}: ${solve(e)}`).join('\n')); 9 | 10 | function solve(str){ 11 | let up= '', down= ''; 12 | for (let i=0; i '11', getDiff('5','8')-> '03' etc. 47 | 48 | */ 49 | 50 | function getDiff(a, b) 51 | { 52 | const len= Math.max(a.length, b.length); 53 | let r= '', c= 0, d, diff; 54 | [a,b]= [a,b].map(e=>e.padStart(len,'0')); 55 | if (a < b) [a,b]= [b,a]; 56 | 57 | for (let i=len-1; i>=0; i--) 58 | { 59 | d= +b[i] + c; 60 | 61 | +a[i]>=d ? 62 | (c= 0, diff= a[i]-d) 63 | :(c= 1, diff= '1'+a[i]-d) 64 | 65 | r = diff + r; 66 | } 67 | return r; 68 | } 69 | -------------------------------------------------------------------------------- /kickstart/2018/RoundA/B.java: -------------------------------------------------------------------------------- 1 | import java.io.FileInputStream; 2 | import java.io.PrintStream; 3 | import java.util.*; 4 | 5 | /** 6 | * KickStart 2018 Round A 7 | * Problem B. Lucky Dip 8 | * Only small solved. 9 | */ 10 | public class Main { 11 | 12 | public String solve(Scanner scanner) { 13 | return solveSmall(scanner); 14 | } 15 | 16 | private String solveSmall(Scanner scanner) { 17 | int n=scanner.nextInt(), k=scanner.nextInt(); 18 | long[] values=new long[n]; 19 | for (int i=0;ii%2); 10 | 11 | console.log(input.map((e,i)=>`Case #${i+1}: ${solve(e)}`).join('\n')); 12 | 13 | function solve(str) { 14 | const len= str.length, 15 | waste= Math.floor(len/2); 16 | let score = 0; 17 | for (let i= waste; i maxScore) maxScore= score; 22 | } 23 | return maxScore; 24 | } -------------------------------------------------------------------------------- /kickstart/2019/Practice_Round/B.js: -------------------------------------------------------------------------------- 1 | /* 2 | KickStart 2019 Practice Round 3 | Problem B. Mural 4 | Passed: Small dataset and Large dataset 5 | */ 6 | const fs= require('fs'); 7 | 8 | const input= fs.readFileSync(0,'utf8').trim().split('\n').slice(1) 9 | .filter((_,i)=>i%2); 10 | 11 | console.log(input.map((e,i)=>`Case #${i+1}: ${solve(e)}`).join('\n')); 12 | 13 | function solve(str) { 14 | const len= str.length, 15 | waste= Math.floor(len/2); 16 | let score = 0; 17 | for (let i= waste; i maxScore) maxScore= score; 22 | } 23 | return maxScore; 24 | } 25 | -------------------------------------------------------------------------------- /kickstart/2019/RoundA/A.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Round A - Kick Start 2019 3 | * Problem A Training 4 | * Test set 1 passed 5 | * Test set 2 passed 6 | * */ 7 | 8 | const fs= require('fs'), 9 | input= fs.readFileSync(0,'utf8').trim().split('\n').slice(1), 10 | np= input.filter((_,i)=>i%2===0).map(e=>+e.split(' ')[1]), 11 | sk= input.filter((_,i)=>i%2).map(e=>e.split(' ').map(Number)); 12 | 13 | console.log(np.map((e,i)=>`Case #${i+1}: ${solve(e, sk[i])}`).join('\n')); 14 | 15 | function solve(n,arr) { 16 | arr.sort((a,b)=>a-b); 17 | let lowSkill= arr[0], 18 | highSkill= arr[n-1], 19 | highLowDiff= highSkill-lowSkill, 20 | hours= arr.slice(0,n).reduce((r,e)=>r+highSkill-e,0), 21 | minHours= hours; 22 | for (let i=1; i<=arr.length-n; i++) { 23 | hours += (arr[i+n-1]-highSkill)*(n-1) - highLowDiff; 24 | highSkill= arr[i+n-1]; 25 | lowSkill= arr[i]; 26 | highLowDiff= highSkill-lowSkill; 27 | if (hourse.split('\n').filter(Boolean)); 16 | 17 | console.log(chars 18 | .map((e, i) => 19 | `Case #${i + 1}: ${solve(e, questions[i])}`) 20 | .join('\n')); 21 | 22 | // fast solution 23 | function solve(str, ranges) { 24 | let positiveAnswers= 0; 25 | const list= []; 26 | const frequency= Array(26).fill(0); 27 | for (let c of str) { 28 | frequency[c.charCodeAt()-65]++; 29 | list.push([...frequency]); 30 | } 31 | 32 | ranges.forEach((range)=> { 33 | const [start, end] = range.split(' ').map(Number); 34 | let answer= 1; 35 | let odds= 0; 36 | 37 | for (let i=0; i<26; i++) { 38 | let occurrencies = list[end-1][i] - (start>1 ? list[start-2][i] : 0); 39 | if(occurrencies%2 && ++odds>1) { answer= 0; break; } 40 | } 41 | positiveAnswers+=answer; 42 | }); 43 | return positiveAnswers; 44 | } 45 | 46 | // slow solution (sufficient olnly for Test set 1) 47 | function solve(str, ranges) { 48 | let positiveAnswers= 0; 49 | for (let range of ranges) { 50 | const [start, end]= range.split(' '); 51 | let counter= Array(26).fill(0); 52 | 53 | for (let i=start-1; i1) { 62 | answer= 0; 63 | break; 64 | } 65 | } 66 | } 67 | positiveAnswers += answer; 68 | } 69 | return positiveAnswers; 70 | } --------------------------------------------------------------------------------