Airport Seoul 2009

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>
#include <ctime>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::greater;

const int MAXN(5010);

int suma[MAXN], sumb[MAXN];

bool legal(int goal, int n)
{
	int ta = 0, tb = 0, rem = 0;   //ta表示可以从A飞走的飞机数,tb类似,rem表示可以飞走的飞机总数
	for(int i = 1; i <= n; ++i)
	{
		int temp1 = max(suma[i]-goal, 0), temp2 = max(sumb[i]-goal, 0);
		if(temp1  > ta || temp2 > tb || temp1+temp2 > rem)
			return false;
		if(ta < suma[i])
			++ta;
		if(tb < sumb[i])
			++tb;
		if(rem < suma[i]+sumb[i])
			++rem;
	}
	return true;
}

int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		int n;
		scanf("%d", &n);
		for(int i = 1; i <= n; ++i)
		{
			scanf("%d%d", suma+i, sumb+i);
			suma[i] += suma[i-1];
			sumb[i] += sumb[i-1];
		}
		int l = max(suma[1], sumb[1]), r = max(suma[n], sumb[n]);
		while(l < r)
		{
			int m = (l+r) >> 1;
			if(legal(m, n))
				r = m;
			else
				l = m+1;
		}
		printf("%d\n", l? l-1: l);
	}
	return 0;
}


你可能感兴趣的:(Airport Seoul 2009)