POJ - 1010 ~ 1014
1011 - Sticks
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = 0;
while((num = in.nextInt()) != 0) {
int[] sticks = new int[num];
int[] visit = new int[num];
int sumLen = 0;
for(int i = 0; i < num; i++) {
sticks[i] = in.nextInt();
sumLen += sticks[i];
}
Arrays.sort(sticks);
for(int i = 0, j = num - 1; i < j; i++, j--) {
int tmp = sticks[i];
sticks[i] = sticks[j];
sticks[j] = tmp;
}
for(int i = num; i > 0; i--) {
if(sumLen % i == 0 && (sumLen / i) >= sticks[0]) {
int len = sumLen / i;
if(dfs(sticks, visit, 0, 0, len, 0, num)) {
System.out.println(len);
break;
}
}
}
}
in.close();
}
private static boolean dfs(int[] sticks, int[] visit, int index, int curLen, int len, int curNum, int num) {
if(curNum == num) {
return true;
}
for(int i = index; i < num; i++) {
if(visit[i] == 1) {
continue;
}
if(curLen + sticks[i] < len) {
visit[i] = 1;
if(dfs(sticks, visit, i + 1, curLen + sticks[i], len, curNum + 1, num)) {
return true;
}
visit[i] = 0;
while(i + 1 < num && sticks[i] == sticks[i + 1]) {
i++;
}
} else if(curLen + sticks[i] == len) {
visit[i] = 1;
if(dfs(sticks, visit, 0, 0, len, curNum + 1, num)) {
return true;
}
visit[i] = 0;
return false;
}
if(curLen == 0) {
return false;
}
}
return false;
}
}
1012 - Joseph
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] res = new int[14];
int k = 0;
while((k = in.nextInt()) != 0) {
if(res[k] != 0) {
System.out.println(res[k]);
} else {
int m = k + 1;
while(!joseph(k, m)) {
m++;
}
res[k] = m;
System.out.println(res[k]);
}
}
in.close();
}
private static boolean joseph(int k, int m) {
int t = 0, badBoy = k * 2;
for(int i = 0; i < k; i++) {
t = (t + m - 1) % badBoy;
if(t < k) {
return false;
}
badBoy--;
}
return true;
}
}
1013 - Counterfeit Dollar
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int nCases = in.nextInt();
while(nCases-- != 0) {
boolean[] trueDollar = new boolean[12];
int[] dollar = new int[12];
for(int i = 0; i < 3; i++) {
String left = in.next();
String right = in.next();
String check = in.next();
if(check.equals("even")) {
for(int j = 0; j < left.length(); j++) {
trueDollar[(int)(left.charAt(j)) - 65] = true;
trueDollar[(int)(right.charAt(j)) - 65] = true;
}
} else if(check.equals("up")) {
for(int j = 0; j < left.length(); j++) {
dollar[(int)(left.charAt(j)) - 65]++;
dollar[(int)(right.charAt(j)) - 65]--;
}
} else {
for(int j = 0; j < left.length(); j++) {
dollar[(int)(left.charAt(j)) - 65]--;
dollar[(int)(right.charAt(j)) - 65]++;
}
}
}
int pos = 0, max = 0;
for(int i = 0; i < 12; i++) {
if(trueDollar[i]) {
continue;
}
if(dollar[i] != 0 && Math.abs(dollar[i]) > max) {
max = Math.abs(dollar[i]);
pos = i;
}
}
System.out.print((char)(pos + 65) + " is the counterfeit coin and it is ");
if(dollar[pos] > 0) {
System.out.println("heavy.");
} else {
System.out.println("light.");
}
}
in.close();
}
}
1014 - Dividing
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] array = new int[6];
int no = 1;
while(in.hasNext()) {
int total = 0;
for(int i = 0; i < 6; i++) {
array[i] = in.nextInt();
total += array[i] * (i + 1);
}
if(total == 0) {
break;
}
boolean flag = true;
if(total % 2 == 1) {
flag = false;
} else {
flag = canBeDivide(array, total / 2);
}
System.out.println("Collection #" + no++ + ":");
if(flag) {
System.out.println("Can be divided.");
} else {
System.out.println("Can't be divided.");
}
System.out.println();
}
in.close();
}
private static boolean canBeDivide(int[] array, int target) {
int[] V = new int[target + 1];
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < array.length; i++) {
int bit = 0;
while(array[i] != 0) {
if(array[i] >= (1 << bit)) {
list.add((1 << bit) * (i + 1));
array[i] -= (1 << bit);
} else if(array[i] != 0) {
list.add(array[i] * (i + 1));
array[i] = 0;
}
bit++;
}
}
Collections.sort(list);
for(Integer c : list) {
for(int i = target; i >= c; i--) {
if(c > target) {
return false;
}
V[i] = Math.max(V[i], V[i - c] + c);
if(i == target && V[target] == target) {
return true;
}
}
}
return false;
}
}