http://hihocoder.com/contest/mstest2016april1/problems
import java.lang.*;
import java.util.*;
import java.applet.*;
import java.awt.*;
import java.beans.*;
import java.io.*;
import java.math.*;
import java.net.*;
A Font Size
import java.util.*;
public class main {
public static int check(int[] p, int w, int h, int s){
int temp = 0;
for (int i = 0; i < p.length; i++){
temp += p[i] / (w / s);
}
int single = h / s;
return temp / single;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num = in.nextInt();
while(num > 0){
int n = in.nextInt();
int p = in.nextInt();
int w = in.nextInt();
int h = in.nextInt();
int[] ps = new int[n];
for (int i = 0; i < n; i++){
ps[i] = in.nextInt();
}
int low = 1;
int high = Math.min(w, h);
while(low <= high){
int mid = (low + high) / 2;
int temp = check(ps, w, h, mid);
if (temp == p){
System.out.println(mid);
break;
} else if (temp > p){
high = mid - 1;
} else {
if (check(ps, w, h,mid + 1) > p){
System.out.println(mid);
break;
}
low = mid + 1;
}
}
num--;
}
}
}
B 403 Forbidden
注意, Scanner自动过滤空格,然后可以使用nextInt(),nextDouble(),nextFloat(),next()。
在涉及到规则的时候,要考虑服从规则的代码编写
import java.util.*;
public class main{
public static boolean check(long ip, Map[] rules){
int i = 0;
for (Map r : rules){
i++;
if (((int)r.get("mask") == 0 && (long)r.get("ip") == ip) ){
return (boolean)r.get("res");
}
if ((((long)r.get("ip") ^ ip) >> (32 - (int)r.get("mask"))) == 0){
return (boolean)r.get("res");
}
}
return true;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
Map[] rules = new Map[n];
for (int i = 0; i < n; i++){
String temp = in.nextLine();
while (temp.length() < 10){
temp = in.nextLine().trim();
}
String[] strarray1 = temp.split(" ");
boolean res = false;
long ip = 0;
int mask = 32;
if (strarray1[0].equals("allow")){
res = true;
}
String []strarray2 = strarray1[1].split("/");
if (strarray2.length > 1){
mask = Integer.valueOf(strarray2[1]);
}
String[] strarray3 = strarray2[0].split("\\.");
ip = (Long.valueOf(strarray3[0]) << 24) | (Long.valueOf(strarray3[1]) << 16)
| (Long.valueOf(strarray3[2]) << 8) | Long.valueOf(strarray3[3]);
Map map = new HashMap();
map.put("res", res);
map.put("ip", ip);
map.put("mask", mask);
rules[i] = map;
}
for (int j = 0; j < m; j++){
String ips = in.nextLine().trim();
String[] inips = ips.split("\\.");
long nowip = (Long.valueOf(inips[0]) << 24) | (Long.valueOf(inips[1]) << 16)
| (Long.valueOf(inips[2]) << 8) | Long.valueOf(inips[03]);
if (check(nowip, rules)){
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}
C Demo Day
思路: 这个运用递归进行求解,暂时未想到动态规划进行缓存的方法,
递归爬坑记录 : 明确递归的三个重要组成部分,在处理单个位置的时候,一定要从各特殊点进行分析,先判断处于边界条件的,可以及早退出的。一定要在单个点处理逻辑上进行到下一个点,动作保持完全一致。
import java.util.*;
public class main{
public static int getChangenum(boolean[][] roots, int x, int y, boolean tar){
if (x == (roots.length -1) && y == (roots[0].length - 1)){
return 0;
}
int temp = Integer.MAX_VALUE;
if (y == roots[0].length - 1){
if (roots[x + 1][y] == false){
temp = Math.min(temp, getChangenum(roots, x + 1, y, false) + 1);
} else {
temp = Math.min(temp, getChangenum(roots, x + 1, y, false));
}
return temp;
}
if (x == roots.length - 1){
if (roots[x][y + 1] == false){
temp = Math.min(temp, getChangenum(roots, x, y + 1, true) + 1);
} else {
temp = Math.min(temp, getChangenum(roots, x, y + 1, true));
}
return temp;
}
if (tar){
if (roots[x][y + 1] == false){
temp = Math.min(temp, getChangenum(roots, x, y + 1, true) + 1);
if (roots[x + 1][y] == false){
temp = Math.min(temp, getChangenum(roots, x + 1, y, false) + 1);
} else {
temp = Math.min(temp, getChangenum(roots, x + 1, y, false));
}
}else {
temp = Math.min(temp, getChangenum(roots, x, y + 1, true));
}
} else {
if (roots[x + 1][y] == false){
temp = Math.min(temp, getChangenum(roots, x + 1, y, false) + 1);
if (roots[x][y + 1] == false){
temp = Math.min(temp, getChangenum(roots, x, y + 1, true) + 1);
} else {
temp = Math.min(temp, getChangenum(roots, x, y + 1, true));
}
} else {
temp = Math.min(temp, getChangenum(roots, x + 1, y, false));
}
}
return temp;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
boolean [][] roots = new boolean [n][m];
for (int i = 0; i < n; i++){
String str = in.next();
for (int j = 0; j < m; j++){
char temp = str.charAt(j);
if (temp == 'b'){
roots[i][j] = false;
} else {
roots[i][j] = true;
}
}
}
int result = Math.min(getChangenum(roots, 0, 0, true), getChangenum(roots, 0, 0, false));
System.out.println(result);
}
}
D Building in Sandbox
这个程序是依据数据题目规则进行编写的,这要卡在了点的存储形式上,按照题目给定范围x, y, z均处在0 到 100之间, 切总点数处在100000以内,为了方便记录,采用了HashMap
package reseversting;
import java.util.*;
public class main{
public static boolean rule1(int x, int y ,int z, int pre){
if (x == 1 && y == 1){
return true;
}
if (pre == -1){
return false;
}
if (x == getx(pre) && y == gety(pre)){
return true;
}
if (y == gety(pre) && z == getz(pre)){
return true;
}
if (x == getx(pre) && z == getz(pre)){
return true;
}
return false;
}
public static boolean rule2(int x, int y , int z, Map map){
if (null == map.get(convertindex(x, y , z + 1))){
return true;
}
if (z == 1){
} else {
if (null == map.get(convertindex(x, y , z - 1))){
return true;
}
}
if (null == map.get(convertindex(x - 1, y, z))){
return true;
}
if (null == map.get(convertindex(x + 1, y, z))){
return true;
}
if (null == map.get(convertindex(x, y - 1, z))){
return true;
}
if (null == map.get(convertindex(x, y + 1, z))){
return true;
}
return false;
}
public static int convertindex(int x, int y, int z){
return (x << 16) | (y << 8) | z;
}
public static int getx (int index){
return index >> 16;
}
public static int gety (int index){
return (index & 0xff00) >> 8;
}
public static int getz (int index){
return index & 0xff;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int t = in.nextInt();
int x = 0;
int y = 0;
int z = 0;
int pre = -1;
for (int i = 0; i < t; i++){
Map map = new HashMap();
boolean flag = true;
int num = in.nextInt();
for (int j = 0; j < num; j++){
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
if (!rule1(x, y, z, pre)){
flag = false;
break;
}
if (!rule2(x, y, z, map)){
flag = false;
break;
}
pre = convertindex(x, y, z);
map.put(pre, true);
}
if (flag){
System.out.println("Yes");
}
}
}
}