题意:给出一个数列,下标为0 - n - 1。然后求x,y,z,使得sum[0,x) - sum[x,y) + sum[y,z) - sum[z,n)的值最大。
思路:相当于数列的所有的和sum - 2 * ( sum[x,y) + sum[z,n) )最大,所以要使得sum[x,y) + sum[z,n)最小。
这样的话先预处理一下从后往前的最小,即处理出对于i来说sum[z,n)最小的那个下标z。然后跑二维枚举x,y.这个时候看x,y,z,形成的是不是最小的。
PS:下标转换有点蛋疼,所以调了很久。
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define clr(x,y) memset(x,y,sizeof x)
#define PI acos(-1.0)
#define ITER set<int>::iterator
const int Mod = 1e9 + 7;
const int maxn = 5e3 + 10;
const int N = 2;
int n,a[maxn];
ll sum[maxn];
ll b[maxn];int c[maxn];
int pos1[maxn],pos2[maxn];
int main()
{
while( ~ scanf("%d",&n))
{
sum[0] = 0;
for(int i = 1; i <= n; i ++)scanf("%d",&a[i]),sum[i] = sum[i - 1] + a[i];
bool flag = true;for(int i = 1; i <= n; i ++)if(a[i] >= 0){flag = false;break;}
if(flag){cout << 0 << " " << 0 << " " << 0 << endl;continue;}
b[n + 1] = 0;
ll mins = 0;int pos = n;c[n + 1] = n;
for(int i = n;i >= 1;i --)
{
b[i] = b[i + 1] + a[i];
if(b[i] < mins)
{
mins = b[i];c[i] = pos = i - 1;
}
else c[i] = pos;
}
mins = 0;int pos1 = 1,pos2 = 1,pos3 = n;
for(int i = 1; i <= n; i ++)
{
for(int j = i;j <= n; j ++)
{
pos = c[j + 1] + 1;
ll t = sum[j - 1] - sum[i - 1] + b[pos];
if(i == j) t = b[pos];
if(t < mins)
{
mins = t;
pos1 = i - 1,pos2 = j - 1,pos3 = pos - 1;
}
}
}
cout << pos1<< " " << pos2 << " " << pos3 << endl;
}
return 0;
}