1. 程式人生 > >洛谷 P1318 積水面積

洛谷 P1318 積水面積

技術分享 class == 這一 lose bits show lan def

P1318 積水面積

將每層可積水的面積加起來

每層可積水的面積=左邊第一個比他大右邊的所有空位+右邊第一個比他大的左邊的所有空位-這一層的所有空位(他指當前高度

技術分享
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define maxn 1000000
 4 
 5 int n,h[maxn],tot,maxx;
 6 int l[maxn],r[maxn],ans;
 7 bool if_;
 8 char ch;
 9 inline void read(int &now)
10 {
11     ch=getchar(); now=0
; 12 while(ch>9||ch<0) ch=getchar(); 13 while(ch>=0&&ch<=9) now=now*10+ch-0,ch=getchar(); 14 } 15 16 int main() 17 { 18 read(n); 19 for(int i=1;i<=n;i++) read(h[i]),maxx=max(maxx,h[i]); 20 for(int k=maxx;k>=1;k--) 21 { 22 int
s=0; 23 if_=false; 24 for(int i=1;i<=n;i++) 25 { 26 if(if_==false) 27 { 28 if(h[i]>=k) if_=true; 29 else if(h[i]<k) s--; 30 } 31 } 32 if_=false; 33 for(int i=n;i>=1
;i--) 34 { 35 if(if_==false) 36 { 37 if(h[i]>=k) if_=true; 38 else if(h[i]<k) s--; 39 } 40 } 41 for(int i=1;i<=n;i++) 42 if(h[i]<k) s++; 43 ans+=s; 44 } 45 printf("%d\n",ans); 46 return 0; 47 }
View Code

洛谷 P1318 積水面積