Skip to content

Instantly share code, notes, and snippets.

@dolpen
Last active June 1, 2017 08:17
Show Gist options
  • Save dolpen/232d1c31c8042749b538e339238a95f6 to your computer and use it in GitHub Desktop.
Save dolpen/232d1c31c8042749b538e339238a95f6 to your computer and use it in GitHub Desktop.
import java.io.PrintWriter;
import java.util.Arrays;
public class Main {
static byte[] b = new byte[40000];
static int c, i, j, k, l, m, n, w, x, y = 0;
public static void main(String[] a) throws Exception {
l = System.in.read(b);
int[] s = new int[m = r()], p = new int[n = r()], d;
for (c = i = j = k = 0; j < m; c = Math.max(c, s[j++] = r())) ;
for (d = new int[c + 1]; k < n; p[k++] = r()) ;
Arrays.fill(d, w = 0xfffff);
for (Arrays.sort(p); i < n; ++i)
for (j = 0; j <= i; d[x] = x) {
if ((x = p[i] * p[j++]) >= c) {
if (d[c] > x) d[c] = x;
break;
}
}
for (i = k = c; --i >= 0; d[i] = d[k]) if (d[i] < w) k = i;
PrintWriter o = new PrintWriter(System.out);
for (; ++i < m; o.println(d[s[i]] - s[i])) ;
o.close();
}
static int r() {
w = 0;
while ((x = b[y++]) > 0 && x < 48) ;
do {
w = w * 10 + (x - 48);
} while ((x = b[y++]) > 0 && x >= 48);
return w;
}
}
import java.io.*;import java.util.*;public class Main{static byte[] b=new byte[40000];static int c,i,j,k,l,m,n,w,x,y=0;public static void main(String[] a)throws Exception{l=System.in.read(b);int[] s=new int[m=r()],p=new int[n=r()],d;for(c=i=j=k=0;j<m;c=Math.max(c,s[j++]=r()));for(d=new int[c+1];k<n;p[k++]=r());Arrays.fill(d,w=0xfffff);for(Arrays.sort(p);i<n;++i)for(j=0;j<=i;d[x]=x){if((x=p[i]*p[j++])>=c){if(d[c]>x)d[c]=x;break;}}for(i=k=c;--i>=0;d[i]=d[k])if(d[i]<w)k=i;PrintWriter o=new PrintWriter(System.out);for(;++i<m;o.println(d[s[i]]-s[i]));o.close();}static int r(){w=0;while((x=b[y++])>0&&x<48);do{w=w*10+(x-48);}while((x=b[y++])>0&&x>=48);return w;}}
package poh;
import java.io.PrintWriter;
import java.util.Arrays;
public class Main {
static byte[] b = new byte[40000];
static int c, i, j, k, l, m, n, q, w, x, y, z = 0xfffff;
public static void main(String[] a) throws Exception {
l = System.in.read(b);
c = i = j = k = y = 0;
int[] s = new int[m = r()], p = new int[n = r()], d;
for (; j < m; c = Math.max(c, s[j++] = r())) ;
for (; k < n; p[k++] = r()) ;
d = new int[c + 1];
Arrays.sort(p);
Arrays.fill(d, z);
for (; i < n; ++i)
for (j = 0; j <= i; d[q] = q) {
if ((q = p[i] * p[j++]) >= c) {
d[c] = d[c] > q ? q : d[c];
break;
}
}
for (i = k = c; --i >= 0; d[i] = d[k]) if (d[i] < z) k = i;
PrintWriter o = new PrintWriter(System.out);
for (; ++i < m; o.println(d[s[i]] - s[i])) ;
o.close();
}
static int r() {
w = 0;
while ((x = b[y++]) > 0 && x < 48) ;
do {
w = w * 10 + (x - 48);
} while ((x = b[y++]) > 0 && x >= 48);
return w;
}
}
@dolpen
Copy link
Author

dolpen commented Apr 26, 2017

@dolpen
Copy link
Author

dolpen commented May 1, 2017

minimize processor : work -> min

package poh.utils;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Minifier {


    public static String[] trimmed = new String[]{
        "if", "for", "while", "do", ";", ",", "=", ">=", "<=", "==", ">", "<",
        "&&", "&", "\\|\\|", "\\|", "\\^", "\\+", "-", "\\*",
        "\\{", "\\}", "\\[", "\\]", "\\(", "\\)", "\\?", ":", ";"
    };

    public static String[] paTypes = new String[]{
        "int", "long", "String", "char", "byte"
    };

    public static void main(String[] a) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader(new File("path/to/Main.java")));
        String o = br.lines().filter(s -> {
            return !s.startsWith("package ");
        }).map(s -> {
            if(!s.startsWith("import ")){
                return s;
            }
            String[] tokens = s.split("\\.");
            tokens[tokens.length-1] = "*;";
            return Arrays.stream(tokens).collect(Collectors.joining("."));
        }).map(s -> {
            s = s.replaceAll("^\\s+", "");
            for (String trim : trimmed) {
                s = s.replaceAll("\\s*" + trim + "\\s*", trim);
            }
            for (String type : paTypes) {
                s = s.replaceAll("(" + type + "\\[\\])", "$1 ");
            }
            return s;
        }).collect(Collectors.joining());
        System.out.println("["+o.length()+"bytes]");
        System.out.println(o);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment