1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #define MAXN 150005 5 #include <algorithm> 6 7 using namespace std; 8 9 struct node 10 { 11 int num; 12 int sum; 13 bool boo; 14 }; 15 16 node _m[MAXN]; 17 int n; 18 void init(); 19 20 int main() 21 { 22 //freopen("acm.acm","r",stdin); 23 int i; 24 int j; 25 int max = -1; 26 cin>>n; 27 init(); 28 for(i = 0; i < n; ++ i) 29 { 30 cin>>_m[i].num; 31 } 32 _m[0].boo = true; 33 _m[0].sum = _m[0].num; 34 for(i = 1; i < n; ++ i) 35 { 36 j = i - 1; 37 if(_m[j].boo == true) 38 { 39 if(_m[i].num >= _m[j].num) 40 { 41 _m[i].boo = true; 42 _m[i].sum = _m[j].sum + _m[i].num - _m[j].num; 43 if(_m[i].sum > max) 44 { 45 max = _m[i].sum; 46 } 47 } 48 else 49 { 50 _m[i].boo = false; 51 _m[i].sum = _m[j].sum - _m[i].num; 52 } 53 } 54 else 55 { 56 if(_m[i].num <= _m[j].num) 57 { 58 _m[i].boo = false; 59 _m[i].sum = _m[j].sum + _m[j].num - _m[i].num; 60 } 61 else 62 { 63 _m[i].boo = true; 64 _m[i].sum = _m[i].num + _m[j].sum; 65 if(_m[i].sum > max) 66 { 67 max = _m[i].sum; 68 } 69 } 70 } 71 } 72 73 cout<<max<<endl; 74 } 75 76 void init() 77 { 78 int i; 79 int j; 80 for(i = 0; i < n; ++ i) 81 { 82 _m[i].boo = false; 83 _m[i].sum = 0; 84 _m[i].num = 0; 85 } 86 }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com