编程语言
首页 > 编程语言> > [Java] Barn Repair

[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();
	}
}

标签:Repair,Java,int,end,start,flag,stall,new,Barn
来源: https://blog.csdn.net/weixin_41714373/article/details/112094459