Cav

分别从左右找出最大能够储存的高度

#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(1000010);

int f[MAXN], c[MAXN];
int L[MAXN], R[MAXN];

int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		int n;
		scanf("%d", &n);
		for(int i = 1; i <= n; ++i)
			scanf("%d", f+i);
		for(int i = 1; i <= n; ++i)
			scanf("%d", c+i);
		L[0] = 2010;
		for(int i = 1; i <= n; ++i)
		{
			L[i] = L[i-1];
			if(f[i] > L[i])
				L[i] = f[i];
			if(c[i] < L[i])
				L[i] = c[i];    
		}
		R[n+1] = 2010;
		long long ans = 0LL;
		for(int i = n; i >= 1; --i)
		{
			R[i] = R[i+1];
			if(f[i] > R[i])
				R[i] = f[i];
			if(c[i] < R[i])
				R[i] = c[i];
			ans += min(L[i], R[i])-f[i];
		}
		printf("%lld\n", ans);
	}
	return 0;
}


你可能感兴趣的:(Cav)