连通器
向左向右扫描两次即可
每一段有水的连通区域,高度必须相同,且不超过最低天花板高度
if(p[i] > level) level = p[i]; 被隔断,要上升(隔断后,之前的就不变了,之后的从p【i】开始)
if(s[i] < level) level = s[i];
h[i] = level;
左右分别扫描一次,可以满足条件,取从左向右的h[i]和从右向左的level的min
ans += min(h[i], level) - p[i];
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 1000000 + 5; int n, p[maxn], s[maxn], h[maxn]; int main() { int T; scanf("%d", &T); while(T--) { scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d", &p[i]); for(int i = 0; i < n; i++) scanf("%d", &s[i]); int ans = 0, level = s[0]; for(int i = 0; i < n; i++) { if(p[i] > level) level = p[i]; if(s[i] < level) level = s[i]; h[i] = level; } level = s[n-1]; for(int i = n-1; i >= 0; i--) { if(p[i] > level) level = p[i]; if(s[i] < level) level = s[i]; ans += min(h[i], level) - p[i]; } printf("%d ", ans); } return 0; }