其他算法(待更新)
考点:在线处理算法
参考的视频链接:https://www.icourse163.org/learn/ZJU-93001?tid=1466830443#/learn/content?type=detail&id=1247077010&cid=1272668040
#include<iostream>
using namespace std;
int List[10010];
int main()
{
int N;
cin >> N;
int flag1 = 0, flag2 = 0;
int ThisSum = 0, MaxSum = 0;
int Maxleft = 0, Maxright = 0, left = 0, right = 0;
for(int i = 0; i < N; i++)
{
cin >> List[i];
if(List[i] > 0)
flag1 = 1;
}
if(flag1 == 1)
{
for(int i = 0; i < N; i++)
{
ThisSum += List[i];
if(ThisSum > MaxSum)
{
MaxSum = ThisSum;
Maxright = right;
Maxleft = left;
}
else if(ThisSum < 0)
{
ThisSum = 0;
left = i + 1;
}
right++;
}
cout << MaxSum << ' ' << List[Maxleft] << ' ' << List[Maxright] << endl;
}
else
{
for(int i = 0; i < N; i++)
{
if(List[i] == 0)
{
cout << MaxSum << ' ' << List[i] << ' ' << List[i] << endl;
flag2 = 1;
break;
}
}
if(flag2 == 0)
{
cout<< MaxSum << ' ' << List[0] << ' ' << List[N - 1] << endl;
}
}
return 0;
}