Skip to content

Instantly share code, notes, and snippets.

@hamadu
Created November 15, 2011 09:13
Show Gist options
  • Save hamadu/1366531 to your computer and use it in GitHub Desktop.
Save hamadu/1366531 to your computer and use it in GitHub Desktop.
Codeforces #94(div2)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class ProblemA {
public static void main(String[] args) throws IOException {
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
String line = s.readLine();
int n = Integer.valueOf(line);
int[] a = new int[n];
int total = 0;
String[] sline = s.readLine().split(" ");
for (int i = 0 ; i < n ; i++) {
a[i] = Integer.valueOf(sline[i]);
total += a[i];
}
total = total % 2;
int ans = 0;
for (int i = 0 ; i < n ; i++) {
if (a[i] % 2 == total) {
ans++;
}
}
System.out.println(ans);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
public class ProblemC {
public static int[] dx = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
public static int[] dy = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
public static class State {
int cx, cy, turn;
public State (int _cx, int _cy, int _turn) {
cx = _cx;
cy = _cy;
turn = _turn;
}
}
public static void main(String[] args) throws IOException {
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
int[][][] map = new int[10][10][10];
for (int i = 0 ; i < 10 ; i++) {
for (int j = 0 ; j < 10 ; j++) {
for (int t = 0 ; t < 10 ; t++) {
map[0][i][j] = -1;
}
}
}
for (int i = 0 ; i < 8 ; i++) {
String line = s.readLine();
for (int j = 0 ; j < 8 ; j++) {
if (line.charAt(j) == 'S') {
map[0][i+1][j+1] = 1;
} else {
map[0][i+1][j+1] = 0;
}
}
}
for (int t = 1 ; t <= 9 ; t++) {
for (int i = 0 ; i < 8 ; i++) {
for (int j = 0 ; j < 8 ; j++) {
map[t][i+1][j+1] = 0;
if (i >= 1) {
map[t][i+1][j+1] = map[t-1][i][j+1];
}
}
}
}
PriorityQueue<State> q = new PriorityQueue<State>(10000, new Comparator<State>(){
public int compare(State a, State b) {
if (a.turn < b.turn) {
return 1;
}
return -1;
}
});
q.add(new State(1, 8, 0));
boolean win = false;
while (q.size() >= 1) {
State state = q.poll();
if (state.turn >= 10) {
win = true;
break;
}
int cx = state.cx, cy = state.cy;
int turn = state.turn;
for (int d = 0 ; d < 9 ; d++) {
int tx = cx + dx[d];
int ty = cy + dy[d];
if (tx <= 0 || ty <= 0 || tx >= 9 || ty >= 9) {
continue;
}
if (map[turn][ty][tx] == 0 && map[turn][ty-1][tx] != 1) {
q.add(new State(tx, ty, turn + 1));
}
}
}
if (win) {
System.out.println("WIN");
} else {
System.out.println("LOSE");
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ProblemE {
static int _n, _m, _k;
static long MOD = 1000000007;
static long[][] dp;
public static long dfs(int n, int k) {
if (n <= k * 2) {
return 0;
}
if (k == 0) {
return 1;
}
if (dp[n][k] >= 0) {
return dp[n][k];
}
dp[n][k] = 0;
for (int d = n - 2; d >= 1; d--) {
dp[n][k] = (dp[n][k] + (dfs(d, k - 1) * (((n - 2) - d) + 1))) % MOD;
}
return dp[n][k];
}
public static void main(String[] args) throws IOException {
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
String[] line = s.readLine().split(" ");
_n = Integer.valueOf(line[0]);
_m = Integer.valueOf(line[1]);
_k = Integer.valueOf(line[2]);
dp = new long[1001][1001];
if (Math.min(_n, _m) < _k * 2 + 1) {
System.out.println("0");
return;
}
for (int i = 0; i <= 1000; i++) {
for (int j = 0; j <= 1000; j++) {
dp[i][j] = -1;
}
}
long ret = (dfs(_n, _k) * dfs(_m, _k)) % MOD;
System.out.println(ret);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment