wenbao与分治
啦啦啦啦啦啦啦啦啦!!!!!!!!!!!
现在想想其实线段树就是运用分治的思想。。。
1 #include <iostream> 2 using namespace std; 3 const int maxn = 1e5+10; 4 int Max(int x, int y){ 5 return x > y ? x : y; 6 } 7 int a[maxn]; 8 int fenzhi(int *a, int l, int r){ 9 if(r-l == 1) return a[l]; 10 int mid = l + (r - l) / 2; 11 int ma = Max(fenzhi(a, l, mid), fenzhi(a, mid, r)); 12 int v = 0, lx = a[mid-1], rx = a[mid]; 13 for(int i = mid-1; i >= l; i--) lx = Max(lx, v += a[i]); 14 v = 0; 15 for(int i = mid; i < r; i++) rx = Max(rx, v += a[i]); 16 return Max(ma, lx + rx); 17 } 18 int main(){ 19 int t, n; 20 cin >> t; 21 while(t--){ 22 cin >> n; 23 for(int i = 0; i < n; i++) cin >> a[i]; 24 cout<<fenzhi(a, 0, n)<<endl; 25 } 26 return 0; 27 }
只有不断学习才能进步!