Last active
April 9, 2017 02:25
-
-
Save zeptometer/c76a8ee8b2f66f7a0ce4d5fc3ae2f174 to your computer and use it in GitHub Desktop.
Googole Code Jam
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.lang.*; | |
public class Main { | |
static int doit(String ps, int fsiz) { | |
int len = ps.length(); | |
boolean bs[] = new boolean[len]; | |
for (int i = 0; i < len; i++) { | |
bs[i] = ((ps.charAt(i) == '+')?true:false); | |
} | |
int cnt = 0; | |
for (int i = 0; i + fsiz <= len; i++) { | |
if (bs[i]) continue; | |
cnt++; | |
for (int j = 0; j < fsiz; j++) { | |
bs[i+j] = !bs[i+j]; | |
} | |
} | |
for (int i = 0; i < len; i++) { | |
if (!bs[i]) { | |
return -1; | |
} | |
} | |
return cnt; | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int t = sc.nextInt(); | |
for (int i = 0; i < t; i++) { | |
int result = doit(sc.next(), sc.nextInt()); | |
System.out.printf("Case #%d: ", i + 1); | |
if (result >= 0) { | |
System.out.println("" + result); | |
} else { | |
System.out.println("IMPOSSIBLE"); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.lang.*; | |
public class Main { | |
static long toLong(int[] ds) { | |
long n = 0; | |
for (int i = 0; i < ds.length; i++) { | |
n *= 10; | |
n += ds[i]; | |
} | |
return n; | |
} | |
static long findMinTidy(int[] ds, int c) { | |
if (c == 0 || (ds[c] != 0 && ds[c-1] <= ds[c] - 1)) { | |
ds[c]--; | |
return toLong(ds); | |
} else { | |
ds[c] = 9; | |
return findMinTidy(ds, c - 1); | |
} | |
} | |
static long doit (String str) { | |
int len = str.length(); | |
int[] ds = new int[len]; | |
for (int i = 0; i < len; i++) { | |
ds[i] = Character.getNumericValue(str.charAt(i)); | |
} | |
for (int i = 0; i < len - 1; i++) { | |
if (ds[i] > ds[i+1]) { | |
for (int j = i+1; j < len; j++) { | |
ds[j] = 9; | |
} | |
return findMinTidy(ds, i); | |
} | |
} | |
return Long.parseLong(str); | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int t = sc.nextInt(); | |
for (int i = 0; i < t; i++) { | |
System.out.printf("Case #%d: %d\n", i+1, doit(sc.next())); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.lang.*; | |
class Pair { | |
public long l, r; | |
public Pair(long l, long r) { | |
this.l = l; | |
this.r = r; | |
} | |
} | |
public class Main { | |
static long dmx (long a) {return a / 2;} | |
static long dmn (long a) {return (a - 1) / 2;} | |
static Pair rec(long m, long l, long nm, long nl, long k) { | |
if (nm >= k) { | |
return new Pair(dmx(m), dmn(m)); | |
} else if (nm + nl >= k) { | |
return new Pair(dmx(l), dmn(l)); | |
} else if (m % 2 == 1) { | |
return rec(dmx(m), dmn(l), nm * 2 + nl, nl, k - (nm + nl)); | |
} else { | |
return rec(dmx(m), dmn(l), nm, nm + nl * 2, k - (nm + nl)); | |
} | |
} | |
static Pair doit(long n, long k) { | |
return rec(n, n-1, 1, 0, k); | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int t = sc.nextInt(); | |
for (int i = 0; i < t; i++) { | |
Pair result = doit(sc.nextLong(), sc.nextLong()); | |
System.out.printf("Case #%d: %d %d\n", i+1, result.l, result.r); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment