zoukankan      html  css  js  c++  java
  • HDU 1231:最大连续子序列(DP)

    最大连续子序列

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 38001    Accepted Submission(s): 17114


    Problem Description
    给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., 
    Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个, 
    例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和 
    为20。 
    在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该 
    子序列的第一个和最后一个元素。
     

    Input
    测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。
     

    Output
    对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元 
    素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。 
     

    Sample Input
    6 -2 11 -4 13 -5 -2 10 -10 1 2 3 4 -5 -23 3 7 -21 6 5 -8 3 2 5 0 1 10 3 -1 -5 -2 3 -1 0 -2 0
     

    Sample Output
    20 11 13 10 1 4 10 3 5 10 10 10 0 -1 -2 0 0 0
    Hint
    Hint
    Huge input, scanf is recommended.

    一道简单的DP,但是多了个开头结尾数字的输出。我已开始想的竟然是用前缀和来写,emmmm也不知道是哪根筋抽了,数据处理好之后一脸懵逼,最后还是找了百度QAQ。

    找了两种写法,一种是先找出最大的连续子段和,然后倒退出开头和结尾的数字。

    还有一种是在寻找最大连续子段的时候就记录下来开头结尾的数字。

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <math.h>
    #include <limits.h>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #define ll long long
    #define INF 0x3f3f3f3f
    const int maxn=1e6+10;
    int a[maxn];
    int dp[maxn];
    int main(int argc, char const *argv[])
    {
    	int n;
    	while(scanf("%d",&n)&&n)
    	{
    		memset(dp,0,sizeof(dp));
    		memset(a,0,sizeof(a));
    		for(int i=1;i<=n;i++)
    			scanf("%d",&a[i]);
    		int ans=a[1];
    		int l=1,r=1;
    		//找出最大的连续子段和
    		for(int i=2;i<=n;i++)
    		{
    			dp[i]=std::max(a[i],dp[i-1]+a[i]);
    			if(dp[i]>ans)
    			{
    				ans=dp[i];
    				r=i;
    			}
    		}
    		int sum=0;
            //然后通过逆过程,找到最大值所对应的左下标
            for(int i=r;i>0;i--)
            {
            	//从最大数开始往前加,直到与子段和相等时结束
                sum+=a[i];
                if(sum==ans)
                {
                    l=i;
                    break;
                }
            }
            if(ans<0)
            {
                printf("0 %d %d
    ",a[1],a[n]);
                continue;
            }
            printf("%d %d %d
    ",ans,a[l],a[r]);
    	}
    	return 0;
    }
    #include <stdio.h>
    int a[100000];
    int main()
    {
        int n;
        while(~scanf("%d",&n)&&n)
        {
            for(int i=0;i<n;i++)
                scanf("%d",&a[i]);
            int thissum=0;
            int maxsum=a[0];
            int stop=a[0];
            int temp=a[0];
            int start=a[0];
            for(int i=0;i<n;i++)
            {
                thissum+=a[i];
                if(thissum>maxsum)
                {
                    maxsum=thissum;
                    start=temp;
                    stop=a[i];
                }
                if(thissum<0)
                {
                    thissum=0;
                    temp=a[i+1];
                }
            }
            if(maxsum<0)
                printf("0 %d %d
    ",a[0],a[n-1]);
            else
                printf("%d %d %d
    ",maxsum,start,stop);
        }
        return 0;
    }
  • 相关阅读:
    Faster-RCNN Pytorch实现的minibatch包装
    RuntimeError: cuda runtime error (10) : invalid device ordinal
    pip使用技巧
    Pytorch model saving and loading 模型保存和读取
    vs 附加包含目录不起作用
    微服务杂谈
    spring 学习笔记
    java 发送 http 请求
    PHP7 错误及异常机制
    Mac OS 下定制终端颜色
  • 原文地址:https://www.cnblogs.com/Friends-A/p/9308992.html
Copyright © 2011-2022 走看看