例题8-18(uva-1442)

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int maxn = 1e6 + 7;
int low[maxn], high[maxn], res[maxn],n;
int main()
{
    
    
	int t;
	cin >> t;
	while (t--) {
    
    
		cin >> n;
		for (int i = 0; i < n; i++) cin >> low[i];
		for (int i = 0; i < n; i++) cin >> high[i];

		int ans = 0, h = high[0];
		for (int i = 0; i < n; i++) {
    
    
			if (low[i] > h) h = low[i];
			if (high[i] < h) h = high[i];
			res[i] = h;
		}

		h = high[n - 1];
		for (int i = n - 1; i >= 0; i--) {
    
    
			if (low[i] > h) h = low[i];
			if (high[i] < h) h = high[i];
			ans += min(res[i], h) - low[i];
		}
		cout << ans << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/seanbill/article/details/116403705
今日推荐