http://codeforces.com/contest/540A. Combination Lock
n = input()
s1 = raw_input()
s2 = raw_input()
ans=0
for i in range(n):
tmp1 = abs((int)(s1[i])-(int)(s2[i]))
ans += min(tmp1, 10-tmp1)
print ans
B. School Marks
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.plaf.synth.SynthScrollBarUI;
public class Test
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n, k, p, x, y;
n = sc.nextInt();
k = sc.nextInt();
p = sc.nextInt();
x = sc.nextInt();
y = sc.nextInt();
int sum = 0, sum1 = 0, sum2 = 0;
int[] a = new int[n];
int[] b = new int[n];
int l = 0;
for(int i = 0; i < k; i++) {
b[i] = sc.nextInt();
sum += b[i];
if(b[i] >= y)
sum1++;
else if(b[i] < y)
sum2++;
}
Arrays.sort(b, 0, k);
int ans1 = -1;
if(sum2 > n/2) {
}
else if(sum1 > n/2) {
sum += n-k;
if(sum <= x) {
for(int i = 0; i < n-k; i++) {
a[l++] = 1;
}
}
}
else {
sum += n/2-sum2;
sum += y*(n/2-sum1+1);
if(sum <= x) {
for(int i = 0; i < n/2-sum2; i++) {
a[l++] = 1;
}
for(int i = 0; i < n/2-sum1+1; i++) {
a[l++] = y;
}
}
}
if(l == 0) {
System.out.println(-1);
}
else {
for(int i = 0; i < l; i++) {
System.out.println(a[i]);
}
}
}
}
C. Ice Cave广搜
import java.util.LinkedList;
import java.util.Scanner;
public class Test {
public static char[][] a;
public static int[][] vis;
public static int[][] dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
public static int n, m;
static class node {
int x, y;
public node(int x, int y) {
this.x = x;
this.y = y;
}
}
public static boolean BFS(int s1, int e1, int s2, int e2) {
LinkedList<node> Q = new LinkedList<>();
Q.add(new node(s1, e1));
while(!Q.isEmpty()) {
node u = Q.removeFirst();
if(u.x == s2 && u.y == e2 && a[u.x][u.y] == '*') {
return true;
}
for(int i = 0; i < 4; i++) {
int x = u.x + dir[i][0];
int y = u.y + dir[i][1];
if(x < 0 || x >= n || y < 0 || y >= m) {
continue;
}
if(a[x][y] == '.') {
Q.add(new node(x, y));
a[x][y] = 'X';
}
else if(x == s2 && y == e2) {
Q.add(new node(x, y));
a[x][y] = '*';
}
}
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
a = new char[n][m];
sc.nextLine();
for(int i = 0; i < n; i++) {
a[i] = sc.nextLine().toCharArray();
}
int s1 = sc.nextInt(), e1 = sc.nextInt();
int s2 = sc.nextInt(), e2 = sc.nextInt();
vis = new int[n][m];
if(BFS(s1-1, e1-1, s2-1, e2-1)) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
D. Bad Luck Island概率DP
import java.util.LinkedList;
import java.util.Scanner;
public class Test {
static int r, s, p;
static double dp[][][];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
r = sc.nextInt();
s = sc.nextInt();
p = sc.nextInt();
dp = new double[r+10][s+10][p+10];
dp[r][s][p] = 1.0;
for(int i = r; i >= 0; i--) {
for(int j = s; j >= 0; j--) {
for(int k = p; k >=0; k--) {
//if(i+j == 0 || j+k == 0 || i+k == 0)
//continue;
if(i == r && j == s && k == p)
continue;
dp[i][j][k] = 0;
if(i > 0) {
dp[i][j][k] += dp[i][j+1][k]*i*(j+1)/(i*(j+1)+(j+1)*k+i*k);
}
if(j > 0) {
dp[i][j][k] += dp[i][j][k+1]*j*(k+1)/(i*j+j*(k+1)+i*(k+1));
}
if(k > 0) {
dp[i][j][k] += dp[i+1][j][k]*k*(i+1)/((i+1)*j+j*k+(i+1)*k);
}
}
}
}
double ans1 = 0, ans2 = 0, ans3 = 0;
for(int i = 1; i <= r; i++) {
ans1 += dp[i][0][0];
}
for(int i = 1; i <= s; i++) {
ans2 += dp[0][i][0];
}
for(int i = 1; i <= p; i++) {
ans3 += dp[0][0][i];
}
System.out.println(ans1+" "+ans2+" "+ans3);
}
}
E. Infinite Inversions