【HDU 1003】 Max Sum 最大子串和 DP

Problem Description
Given a sequence a[1],a[2],a[3]…a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

Output
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

Sample Output
Case 1:
14 1 4

Case 2:
7 1 6

题意:找出和最大的一个子串

思路:

经典DP题了,直接说状态转移方程:
1.当前最优解为自身,即dp[i][0] = a[i],dp[i][0]代表i位置只选自己。
2.当前最优解由前i-1个的最优解转移过来,理解为接上上一个串。即dp[i][1] = max(dp[i-1][0], dp[i-1][1]),dp[i][1]代表i位置和之前接上的最优解。
3.对于起始位置和结束位置,也跟着一起转移即可。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'|ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll a[maxn];
ll dp[maxn][2];
ll s[maxn][2];
ll t[maxn][2];

int main()
{
    int kase;
    cin>>kase;
    int num = 1;
    while(kase--)
    {
        ll n = read();
        rep(i,1,n) a[i] = read();
        dp[0][0] = dp[0][1] = 0;
        ll ans = -inf;
        ll ss = 0 ,tt = 0;
        rep(i,1,n)
        {
            dp[i][0] = a[i]; s[i][0] = t[i][0] = i;
            if(i==1) s[i][1] = t[i][1] = i;
            else if(dp[i-1][0]> dp[i-1][1])
            s[i][1] = s[i-1][0], t[i][1] = i;
            else  s[i][1] = s[i-1][1], t[i][1] = i;
            dp[i][1] = max(dp[i-1][0],dp[i-1][1]) + a[i];
            if(ans < dp[i][0])
            {
                ans = dp[i][0];
                ss = s[i][0] , tt = t[i][0];
            }
            if(ans <= dp[i][1])
            {
                ans = dp[i][1];
                ss = s[i][1], tt = t[i][1];
            }
        }

       printf("Case %d:\n%lld %lld %lld\n",num++,ans,ss,tt);
       if(kase)
       cout<<'\n';
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45492531/article/details/107633575