zoukankan      html  css  js  c++  java
  • HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)

    C - 最大连续子序列
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    Appoint description:

    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.
            
            
     状态转移方程:sum = sum > 0 ? sum + a[i] : a[i] ; 第一个sum是前i个数的和,后面的两个sum是前(i-1)个是的和;如果i前面的和是小于0的,那么加上第i个数肯定比i要小,所以只取第i个数即 a[i],如果是大于0的,则就加上。
     
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=10005;
    int a[maxn];
    int main(){
        int n;
        while(scanf("%d",&n)!=EOF){
            if(!n)
                break;
            bool flag=true;
        //    memset(a,0,sizeof(a));
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
                if(a[i]>=0)
                    flag=false;
            }
             if(flag){
                 printf("0 %d %d
    ",a[1],a[n]);
                 continue;
             }
            int ans,sum,head,tail,ans_head,ans_tail;
            ans=sum=a[1];
                ans_head=ans_tail=head=tail=1;
            for(int i=2;i<=n;i++){
                  if(sum>0){
                      sum+=a[i];
                      tail=i;
                  }
                  if(sum<=0){
                       sum=a[i];
                       head=tail=i;
                  }
                  if(sum>ans){
                      ans=sum;
                      ans_head=head;
                      ans_tail =tail;
                  }
            }
    
            printf("%d %d %d
    ",ans,a[ans_head],a[ans_tail]);
    
        }
        return 0;
    }
    D - Max Sum
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    Appoint description:

    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
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=100005;
    int a[maxn];
    int main(){
        int n;
        int t;
        int cnt=0;
        scanf("%d",&t);
        while(t--){
          cnt++;
            scanf("%d",&n);
        //    bool flag=true;
        //    memset(a,0,sizeof(a));
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
            
            }
            int ans,sum,head,tail,ans_head,ans_tail;
            ans=sum=a[1];
                ans_head=ans_tail=head=tail=1;
            for(int i=2;i<=n;i++){
                  if(sum>=0){
                      sum+=a[i];
                      tail=i;
                  }
                  if(sum<0){
                       sum=a[i];
                       head=tail=i;
                  }
                  if(sum>ans){
    
                      ans=sum;
                      ans_head=head;
                      ans_tail =tail;
                  }
            }
                printf("Case %d:
    ",cnt);
            printf("%d %d %d
    ",ans,ans_head,ans_tail);
    
            if(t!=0)
    
            printf("
    ");
    
        }
        return 0;
    }
     
  • 相关阅读:
    现代软件工程_第一周练习_第12题
    [assembly: AssemblyVersion("1.0.1.*")] 指定版本字符串不符合所需格式
    C#中结构体与字节流互相转换
    字节转化为结构体BytesToStruct
    C#调用Microsoft.DirectX.DirectSound问题记录及解决
    C# Wpf集合双向绑定
    <转载>XML操作
    <转载>提升程序的特权(AdjustTokenPrivileges)
    <转载>批处理之FOR语句祥解
    Effective STL 笔记: Item 6--Be alert for C++'s most vexing parse
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/4983978.html
Copyright © 2011-2022 走看看