跳过正文
  1. Posts/

最大连续区间和的算法总结

·2 分钟

最大连续区间和是一个经典的问题。给定一个长度为 n 的序列 a[1],a[2]…a[n-1],a[n],求一个连续的子序列 a[i],a[i+1]…a[j-1],a[j],使得 a[i]+a[i+1]…a[j-1]+a[j]最大。

① 最简单最容易想到的就是根据定义来枚举。 枚举上下界{i,j | 0<=i<=j<=n},维护一个 max 值即可。 其中枚举上下界的时间复杂度为 O(n^2),求区间和的复杂度为 O(n),所以总时间复杂度为 O(n^3)。

1for ( i = 1 ; i <= n ; i++ )
2for ( j = i ; j <= n ; j++ )
3ans = max(ans,accumulate(a+i,a+j+1,0));

② 其实就是第一种方法的优化。 这里有个很容易想到的优化,即预处理出前缀和 sum[i]=a[0]+a[1]+…+a[i-1]+a[i],算区间和的时候即可将求区间和的复杂度降到 O(1),枚举上下界的复杂度不变,所以总时间复杂度为 O(n^2)。

1for ( i = 1 ; i <= n ; i++ )
2sum[i]=sum[i-1]+a[i];
3for ( i = 1 ; i <= n ; i++ )
4for ( j = i ; j <= n ; j++ )
5ans = max(ans,sum[j]-sum[i-1]);

③ 可以利用动态规划的思维来继续优化,得到一个线性的算法,也是最大连续区间和的标准算法 定义 maxn[i]为以 i 为结尾的最大连续和,则很容易找到递推关系:maxn[i]=max{0,maxn[i-1]}+a[i]。 所以只需要扫描一遍即可,总时间复杂度为 O(n)。

1for ( i = 1 ; i <= n ; i++ )
2{
3last = max(0,last)+a[i];
4ans = max(ans,last);
5}

④ 同样用到类似的思维。 首先也需要预处理出前缀和 sum[i],可以推出 ans=max{sum[i]-min{sum[j] } | 0<=j<i},而最小前缀和可以动态维护,所以总时间复杂度为 O(n)。

1for ( i = 1 ; i <= n ; i++ )
2    sum[i]=sum[i-1]+a[i];
3for ( i = 1 ; i <= n ; i++ )
4{
5    ans = max(ans,sum[i]-minn);
6    minn = min(minn,sum[i]);
7}

总结:虽然朴素的O(n^3)和前缀和优化的O(n^2)算法很容易想到,但代码实现却反而比方法三麻烦,第四个方法虽然有和方法三相同的复杂度,但需要一个预处理和多出的O(n)的空间,所以,方法三很好很强大。

相关文章

cf 535B Tavas and SaDDas

·1 分钟
B. Tavas and SaDDas time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Once again Tavas started eating coffee mix without water! Keione told him that it smells awful, but he didn’t stop doing that. That’s why Keione told his smart friend, SaDDas to punish him! SaDDas took Tavas’ headphones and told him: “If you solve the following problem, I’ll return it to you.”

poj 3278 catch that cow

·1 分钟
http://poj.org/problem?id=3278 bfs,用到了stl的queue 1 2 3 /* *********************************************** 4 Author :111qqz 5 Created Time :2016年02月19日 星期五 15时45分05秒 6 File Name :3278.cpp 7 ************************************************ */ 8 9 #include <algorithm> 10 #include <cstdio> 11 #include <iostream> 12 #include <cstring> 13 #include <string> 14 #include <cmath> 15 #include <map> 16 #include <stack> 17 #include <queue> 18 19 using namespace std; 20 typedef long long LL; 21 const int inf = 8E8; 22 const int N=2E5+7; 23 int d[N]; 24 int n,k; 25 void bfs() 26 { 27 queue<int> q; 28 memset(d,-1,sizeof(d)); 29 q.push(n); 30 d[n]=0; 31 while (!q.empty()) 32 { 33 int x = q.front(); 34 q.pop(); 35 if ( x==k ) 36 { 37 break; 38 } 39 int next[10]; 40 next[1]=x-1; 41 next[2]=x+1; 42 next[3]=2*x; 43 for ( int i = 1; i <= 3 ; i++ ) 44 { 45 if (next[i]>=0&&next[i]<=100000&&d[next[i]]==-1) 46 { 47 d[next[i]]=d[x]+1; 48 q.push(next[i]); 49 } 50 } 51 } 52 53 54 } 55 int main() 56 { 57 while (scanf("%d %d",&n,&k)!=EOF) 58 { 59 bfs(); 60 cout<<d[k]<<endl; 61 } 62 return 0; 63 }

POJ 1028 Web Navigation

·1 分钟
http://poj.org/problem?id=1028 1 2 3 4 /* *********************************************** 5 Author :111qqz 6 Created Time :2016年02月19日 星期五 15时45分01秒 7 File Name :1028.cpp 8 ************************************************ */ 9 10 #include <algorithm> 11 #include <cstdio> 12 #include <iostream> 13 #include <cstring> 14 #include <string> 15 #include <cmath> 16 #include <map> 17 #include <stack> 18 #include <queue> 19 20 using namespace std; 21 typedef long long LL; 22 const int inf = 8E8; 23 stack<string> backstack; 24 stack<string> forwardstack; 25 string cur; 26 string cmd; 27 int main() 28 { 29 cur ="http://www.acm.org/"; 30 31 while (cin>>cmd) 32 { 33 if (cmd=="QUIT") 34 { 35 break; 36 } 37 if (cmd=="BACK") 38 { 39 if (backstack.empty()) 40 { 41 cout<<"Ignored"<<endl; 42 continue; 43 } 44 forwardstack.push(cur); 45 cur=backstack.top(); 46 backstack.pop(); 47 cout<<cur<<endl; 48 } 49 if (cmd=="FORWARD") 50 { 51 if (forwardstack.empty()) 52 { 53 cout<<"Ignored"<<endl; 54 continue; 55 } 56 backstack.push(cur); 57 cur=forwardstack.top(); 58 forwardstack.pop(); 59 cout<<cur<<endl; 60 } 61 if (cmd=="VISIT") 62 { 63 backstack.push(cur); 64 65 cin>>cur; 66 while (!forwardstack.empty()) forwardstack.pop(); 67 cout<<cur<<endl; 68 69 } 70 } 71 72 73 return 0; 74 }