问题 E: Equator
时间限制: 1 Sec 内存限制: 64 MB提交: 15 解决: 8
[ 提交][ 状态][ 讨论版]
题目描述
In a galaxy far away, the planet Equator is under attack! The evil gang Galatic Criminal People
Cooperation is planning robberies in Equator’s cities. Your help is needed! In order to complete your
training for becoming a lord of the dark side you should help them deciding which cities to rob.
As the name says, the desert planet Equator only can be inhabited on its equator. So the gang lands there at some point and travels into some direction robbing all cities on their way until leaving the planet again.
As the name says, the desert planet Equator only can be inhabited on its equator. So the gang lands there at some point and travels into some direction robbing all cities on their way until leaving the planet again.
But what is still open for them is to decide where to land, which direction to take, and when toleave. Maybe they shouldn’t even enter the planet at all? They do not consider costs for travelingor for running their ship, those are peanuts compared to the money made by robbery!
The cities differ in value: some are richer, some are poorer, some have better safety functions. So the gang assigned expected profits or losses to the cities. Help them deciding where to begin and where to end their robbery to maximize the money in total when robbing every city in between.输入
The input starts with the number of test cases T ≤ 30. Each test case starts a new line containing
the number of cities 1 ≤ n ≤ 1 000 000. In the same line n integers c i follow. Each c i (0 ≤ i < n,
−1000 ≤ c i ≤ +1000) describes the money obtained when robbing city i, a negative c i describes the
amount of money they would lose.
输出
For each test case print one integer describing the maximum money they can make in total.
样例输入
3
3 1 2 3
8 4 5 -1 -1 1 -1 -1 5
2 -1 -1
样例输出
6
14
0
循环序列的最长连续子串和。DP
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define PI acos(-1.0)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define Lowbit(x) (x & (-x))
#define mem(a,x) memset(a,x,sizeof(a))
#define Read() freopen("in.txt", "r", stdin);
#define Write() freopen("out.txt", "w", stdout);
#define bitnum(a) __builtin_popcount(a)
using namespace std;
typedef long long int ll;
inline ll in()
{
ll res=0;char c;ll f=1;
while((c=getchar())<'0' || c>'9')if(c=='-')f=-1;
while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
return res*f;
}
ll dp[1000010];
ll a[1000010];
int main(){
int T = in();
while(T--){
int n = in();
ll sum = 0; ll maxx = 0;
for(int i=1;i<=n;i++){
a[i] = in();
sum += a[i];
dp[i] = max(dp[i-1],sum); //用dp数组存储从头开始连续子串的最大的大小
}
sum = 0;
for(int i=1;i<=n;i++){ //不超过边界,正常的最大连续和
if(sum < 0) sum = 0;
sum += a[i];
maxx =max(maxx,sum);
}
sum = 0;
for(int i=n;i>=1;i--){ //超越的边界 dp[i-1] + sum 即是从后面的i倒着连上i-1的最大连续子串和,当然dp[i-1]是 从0开始连续的最大和。
sum += a[i];
maxx = max(maxx,dp[i-1] + sum);
}
printf("%lld\n",maxx);
}
return 0;
}