[Java] Barn Repair

/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: lincans1
LANG: JAVA
TASK: barn1
*/
import java.io.*;
import java.util.*;

public class barn1 {

	private int count(int M, int start, int end, boolean[] stall, boolean[] flag) {
		for (int i = start; i <= end; i++) {
			flag[i] = true;
		}
		while (M > 0) {
	    	int s = start, e = start;
	    	for (int i = start; i <= end; i++) {
	    		if (flag[i] && !stall[i]) {
	    			int start_temp = i;
	    			while (i <= end && flag[i] && !stall[i]) {
	    				i++;
	    			}
	    			int end_temp = i - 1;
	    			if (end_temp - start_temp > e - s) {
	    				s = start_temp;
	    				e = end_temp;
	    			}
	    		}
	    	}
	    	for (int i = s; i <= e; i++) {
	    		flag[i] = false;
	    	}
	    	M--;
		}
	    int ans = 0;
	    for (int i = start; i <= end; i++) {
	    	if (flag[i]) ans++;
	    }
	    return ans;
	}
	
	public barn1() throws IOException {
		// Use BufferedReader rather than RandomAccessFile; it's much faster
		BufferedReader f = new BufferedReader(new FileReader("barn1.in"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("barn1.out")));

		// Use StringTokenizer vs. readLine/split -- lots faster
		StringTokenizer st = new StringTokenizer(f.readLine());

	    int M = Integer.parseInt(st.nextToken()) - 1;
	    int S = Integer.parseInt(st.nextToken());
	    int C = Integer.parseInt(st.nextToken());
	    boolean[] stall = new boolean[S + 1];
	    boolean[] flag  = new boolean[S + 1];

	    int start = S, end = 0;
	    for (int i = 0; i < C; i++) {
	    	int stall_number = Integer.parseInt(f.readLine());
	    	stall[stall_number] = true;
	    	start = Math.min(start, stall_number);
	    	end   = Math.max(end, stall_number);
	    }
	    int ans = M > C ? C : count(M, start, end, stall, flag);
	    out.println(ans);
	    out.close();
	}
	
	public static void main (String [] args) throws IOException {
		new barn1();
	}
}

上一篇:cka考试 创建一个deployment并暴露Service


下一篇:Debian上使用TFTP