最大值最小化

完全个人想法,欢迎拍砖。。指导优化

#include <iostream> using namespace std; int main() { int n; cout << "输入子序列长度:"; cin >> n; int *seq = new int[n]; int m; cout << "输入要划分成多少个序列:"; cin >> m; int *par = new int[m]; //划分的标志位 int sum = 0; int maxone = 0; cout << "输入子序列:"; for(int i = 0; i < n; i++) { cin >> seq[i]; sum += seq[i]; if(maxone < seq[i]) maxone = seq[i]; } int left = n; int beg = sum / m; if(beg < maxone) beg = maxone; int _max = 0; while(left != 0) { _max = beg; left = n; int mTmp = 0; int curSum = 0; for(int i = 0; i < n; i++) { if(curSum + seq[i] <= _max) { curSum += seq[i]; }else { curSum = seq[i]; mTmp++; if(mTmp == m) break; } left--; } beg++; } cout << _max << endl; return 0; }  

你可能感兴趣的:(优化)