这个要用动态规划写...
然后合并果子又要用贪心...
石子和果子...
大概编题目的老师也有苦衷的吧...
1 #include2 using namespace std; 3 4 const int MAXN = 1e6 + 5 ; 5 6 int n , N , min_ans = MAXN , max_ans; 7 int a[401] ; 8 int f_max[401][401] , f_min[401][401]; 9 10 int read(){11 char c ;12 int sign = 1 ;13 while((c = getchar()) < '0' || c > '9') 14 if(c == '-') sign = -1 ;15 int Ans = c - '0' ;16 while((c = getchar()) >= '0' && c <= '9')17 Ans = Ans * 10 + c - '0' ;18 return Ans * sign ;19 }20 21 int main(){22 n = read() ;23 N = (n << 1) ;24 for(int i = 1 ; i <= n ; ++ i){25 a[i] = a[i + n] = read() ;26 }27 for(int i = 2 ; i <= N ; ++ i){28 a[i] += a[i - 1] ;29 }30 for(int i = 2 ; i <= n ; ++ i){31 for(int j = 1 ; j + i - 1 <= N ; ++ j){32 int end = j + i - 1 ;33 f_min[j][end] = MAXN , f_max[j][end] = 0 ;34 for(int k = j ; k <= end - 1 ; ++ k){35 f_min[j][end] = min(f_min[j][end] , f_min[j][k] + f_min[k + 1][end] + a[end] - a[j - 1]) ;36 f_max[j][end] = max(f_max[j][end] , f_max[j][k] + f_max[k + 1][end] + a[end] - a[j - 1]) ;37 }38 }39 }40 for(int i = 1 ; i <= n ; ++ i){41 min_ans = min(min_ans , f_min[i][i + n - 1]) ;42 max_ans = max(max_ans , f_max[i][i + n - 1]) ;43 }44 cout << min_ans << endl << max_ans ;45 return 0 ;46 }