长城守卫 Beijing guards CERC 2004 LA3177 解题总结

在最小值,最大值的问题 或者没有办法确定答案时候,  可以使用二分答案法.

#include <cstdio>
#include <iostream>
#include <fstream>
using namespace std;

int main(){
	fstream f("E:\text.txt");

	int n;
	int gift[100];
	bool used[100];
	memset(used, 0, sizeof(used));
	int ans = 0;
	f >> n;
	for (int i = 0; i < n; i++){
		f >> gift[i];
	}
	ans = gift[0];
	for (int i = 0; i < gift[0]; i++) used[i] = true;
	
	for (int i = 1; i < n; i++){

		int g = gift[i];
		int j = 0;
		while (1){
			if (used[j]) used[j] = false;
			else {
				g--; used[j] = true;
				if (g == 0) break;
			}
			if (j+1>ans) ans = j+1;
			j++;
		}
	}
	int last=0;
	for (int i = gift[0]; i < ans; i++){
		if (!used[i]) last++;
	}
	for (int i = 0; i < gift[0]; i++){
		if (used[i]){
			if (last>0)
				last--;
			else
			 ans++;
		}
	}
	cout << ans;
}


原文地址:https://www.cnblogs.com/Pomodori/p/4316626.html