标签:iostream 优化 eclipse har 好的 ext lin 定义 mil
给出一个整数数组a(正负数都有),如何找出一个连续子数组(可以一个都不取,那么结果为0),使得其中的和最大?
for(int i = 1; i <= n; i++) { for(int j = i; j <= n; j++) { int sum = 0; for(int k = i; k <= j; k++) sum += a[k]; max = Max(max, sum); } }
for(int i = 1; i <= n; i++) { int sum = 0; for(int j = i; j <= n; j++) { sum += a[j]; max = Max(max, sum); } }
endmax = answer = a[1] for i = 2 to n do endmax = max(endmax, 0) + a[i] answer = max(answer, endmax) endfor
start = 1 answerstart = asnwerend = 1 endmax = answer = a[1] for end = 2 to n do if endmax > 0 then endmax += a[end] else endmax = a[end] start = end endif if endmax > answer then answer = endmax answerstart = start answerend = end endif endfor
#include<iostream> #include<cstdio> #include<cstring> using namespace std; long long n,a[60000],dp,answer; int main() { cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; dp=answer=a[1]; for(int i=2;i<=n;i++){ dp=max(dp,0LL)+a[i]; answer=max(dp,answer); } cout<<answer; }
如果对你有所帮助,别忘了加好评哦;么么哒!!下次见!88
标签:iostream 优化 eclipse har 好的 ext lin 定义 mil
原文地址:http://www.cnblogs.com/cangT-Tlan/p/6219001.html