UVA 1442 Cav
#include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef long long ll; const int maxn=1000100; const int INF=1<<29; int n; int p[maxn],s[maxn]; int L[maxn],R[maxn],d[maxn]; void solve() { L[1]=s[1]; REP(i,2,n){ L[i]=max(L[i-1],p[i]); L[i]=min(L[i],s[i]); } R[n]=s[n]; for(int i=n-1;i>=1;i--){ R[i]=max(R[i+1],p[i]); R[i]=min(R[i],s[i]); } REP(i,1,n) d[i]=min(L[i],R[i]); int ans=0; REP(i,1,n) ans+=d[i]-p[i]; cout<<ans<<endl; /* REP(i,1,n) printf("%2d ",i);cout<<endl; REP(i,1,n) printf("%2d ",p[i]);cout<<endl; REP(i,1,n) printf("%2d ",s[i]);cout<<endl; REP(i,1,n) printf("%2d ",L[i]);cout<<endl; REP(i,1,n) printf("%2d ",R[i]);cout<<endl; */ } int main() { freopen("in.txt","r",stdin); int T;cin>>T; while(T--){ scanf("%d",&n); REP(i,1,n) scanf("%d",&p[i]); REP(i,1,n) scanf("%d",&s[i]); solve(); } return 0; } /** 虽然看起来很难实现,但是画个图很容易理解。 codeforces上也看过一次这种题,第一眼竟然没有看出解法...why am i so stupid... */
没有AC不了的题,只有不努力的ACMER!