Max Sum(HDU 1003) && 最大连续子序列(HDU 1231)

Max Sum(HDU 1003)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003

题意:给出一组长度为n的数组,求它的一串连续子序列,使其和最大,若有多个答案,取前边第一个。

思路:我们假设dp[i]表示以a[i]结尾的连续子序列最大和。那么显然如果dp[i - 1]是正的,那么对当前dp[i]是有贡献的,那么dp[i] = dp[i - 1] + a[i]; 如果是负的,那么dp[i] = a[i],我们直接重新开始一个序列。在dp的过程中记录答案即可。

#include <iostream>
#include <cstdio>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int read()
{
    int x = 0, f = 1; char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') f = -f; c = getchar(); }
    while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

const int maxN = 100005;
int n, a[maxN];

int main()
{
    int t; t = read();
    int cas = 0;
    while(t -- ) {
        if(cas) {
            cout << "\n";
        }
        n = read();
        for(int i = 1; i <= n; ++ i ) {
            a[i] = read();
        }
        cout << "Case " << ++ cas << ":\n";
        int l, r, ans = -INF, now = 1;
        for(int i = 1; i <= n; ++ i ) {
            if(a[i - 1] >= 0) { //如果前边的是正的说明对当前的有贡献
                a[i] += a[i - 1];
            } else { // 如果是负的对当前没贡献,更新区间
                now = i;
            }
            if(a[i] > ans) { //如果
                l = now, r = i;
                ans = a[i];
            }
        }
        cout << ans << ' ' << l << ' ' << r << endl;
    }
    return 0;
}

My idea:
这道题好多好多好多人都过了,好像也是挂的题里我做的第一道题,记不得了,反正昨天就看了,今天中午才刚刚过掉。好多人都说这是一道简单的DP,可是我就是没有想出来怎么做。
也没有什么没进入状态之类的借口,反正就是不会。
后来——
所有情况可以分为三种:
1、数组中有正数存在。
我们只需要从数组第一个数往后加,一直更新ans,如果加到<0了就从下一个元素重新开始相加。为什么呢?前边的子序列我们已经加到负数了,也就是说到目前为止的这个子序列,我们已经找到过它的最大和了,并且已经在ans中更新过了。还有就是为什么不是小于等于0,而是小于0?因为我们要找的是第一串最大和的子序列,这中间再有相加为0的小子序列是不影响它的最大值的,而且还会保证是第一串。
2、数组中没有正数,但是有0存在。
那么答案就是0.
3、数组中只有负数。
答案就是最大的那个负数,遍历一遍就可以了。
注意:关于目标子序列的起始位置想想就可以知道怎么处理,这个还是挺简单的。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <list>
#include <map>
#define P(x) x>0?x:0
#define INF 0x3f3f3f3f

using namespace std;
typedef long long ll;
const ll maxN=1e5+5;

set<int>st;

int T,N,a[maxN];
int ansl,ansr,ans,flag_posi,flag_zero,zero_first,zeroL,zeroR;

void init()
{
    ansl=1;
    ansr=1;
    ans=0;
    flag_posi=0;
    flag_zero=0;
    zero_first=0;
}

int main()
{
    scanf("%d",&T);
    for(int Case=1;Case<=T;Case++)
    {
        init();
        scanf("%d",&N);
        for(int i=1;i<=N;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]>0)
                flag_posi=1;
            if(a[i]==0&&zero_first==0)
            {
                zero_first=1;
                flag_zero=1;//为了保证是第一个0
                zeroL=i;
                zeroR=i;
            }
        }
        if(flag_posi==1)//数列中有正数存在
        {
            int tmp=0,l=1;
            for(int i=1;i<=N;)
            {
                tmp+=a[i];//tmp一直加
                if(tmp>ans)//大于ans是就替换ans,存起来起始位置
                {
                    ansl=l;
                    ansr=i;
                    ans=tmp;
                    i++;
                    continue;
                }
                if(tmp<0)//如果加到这一串子序列小于0了,也就是负数的时候,就直接从下一位数列元素开始重新相加
                {//不能是<=0是因为我们要找第一串子序列,所以相加为0后再加到最大和才是最终答案
                    i++;
                    l=i;//令左边界为下一个元素的位置
                    tmp=0;//令初始值为0
                    continue;
                }
                i++;//如果既没有大于ans,又没有小于零,继续跑下一个元素
            }
            printf("Case %d:\n%d %d %d\n",Case,ans,ansl,ansr);
        }
        else if(flag_zero==1)//
        {
            printf("Case %d:\n%d %d %d\n",Case,ans,zeroL,zeroR);
        }
        else//全是负数的时候最大的那个负数就是最终结果
        {
            ans=a[1];
            for(int i=2;i<=N;i++)
            {
                if(ans<a[i])
                {
                    ans=a[i];
                    ansl=i;
                    ansr=i;
                }
            }
            printf("Case %d:\n%d %d %d\n",Case,ans,ansl,ansr);
        }
        if(Case<T)
            printf("\n");
    }
    return 0;
}

最大连续子序列(HDU 1231)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1231

题意:和上题类似,稍有不同
题解:代码也稍稍改动以下就可以了

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <list>
#include <map>
#define P(x) x>0?x:0
#define INF 0x3f3f3f3f

using namespace std;
typedef long long ll;
typedef vector<int>:: iterator VITer;
const int maxN=1e4+5;
const int maxC=1e6+5;

int K;
int a[maxN];
int ans,ansl,ansr;
int first_zero,zerol,zeror,posi;

void init()
{
    first_zero=0;
    posi=0;
    ans=0;
}

int main()
{
    while(~scanf("%d",&K)&&K)
    {
        init();
        for(int i=1;i<=K;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]>0)
            {
                posi=1;
            }
            if(!first_zero&&a[i]==0)
            {
                first_zero=1;
                zerol=i;
                zeror=i;
            }
        }
        if(posi)//有正数
        {
            int tmp=0,l=1;
            for(int i=1;i<=K;)
            {
                tmp+=a[i];
                if(tmp<0)
                {
                    ++i;
                    tmp=0;
                    l=i;
                    continue;
                }
                if(ans<tmp)
                {
                    ans=tmp;
                    ansl=l;
                    ansr=i;
                }
                ++i;
            }
            printf("%d %d %d\n",ans,a[ansl],a[ansr]);
        }
        else if(first_zero)//没有正数但是有0
        {
            printf("0 %d %d\n",a[zerol],a[zeror]);
        }
        else//只有负数
        {
            printf("0 %d %d\n",a[1],a[K]);
        }

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值