UVALive - 4621 Cav

题意:在长n的地方,求能完全能放下的有的面积

思路:先从左到右,然后在从右到左统计每个位置能有的高度,最后再相加就是了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1000010;
const int INF = 0x3f3f3f3f;

int a[MAXN],b[MAXN];
int n,h[MAXN];

int main(){
    int t;
    scanf("%d",&t);
    while (t--){
        scanf("%d",&n);
        for (int i = 1; i <= n; i++)
            scanf("%d",&a[i]);
        for (int i = 1; i <= n; i++)
            scanf("%d",&b[i]);
        a[0] = b[0] = INF;
        a[n+1] = b[n+1] = INF;
        int ah = INF;
        for (int i = 1; i <= n; i++){
            if (a[i-1] > ah)
                ah = a[i-1];
            if (a[i] > ah)
                ah = a[i];
            if (b[i] < ah)
                ah = b[i];
            h[i] = ah;
        }
        ah = INF;
        for (int i = n; i >= 1; i--){
            if (a[i+1] > ah)
                ah = a[i+1];
            if (a[i] > ah)
                ah = a[i];
            if (b[i] < ah)
                ah = b[i];
            if (ah < h[i])
                h[i] = ah;
            ah = h[i];
        }
        int ans = 0;
        for (int i = 1; i <= n; i++)
            ans += h[i] - a[i];
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(UVALive - 4621 Cav)