hdu1003(蒟蒻在成长)

</pre>Max Sum</h1><strong><span style="font-family:Arial;font-size:12px;color:green;font-weight:bold">Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 224167    Accepted Submission(s): 52732</span></strong><div class="panel_title" align="left">Problem Description</div><div class="panel_content">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.</div><div class="panel_bottom"> </div><div class="panel_title" align="left">Input</div><div class="panel_content">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).</div><div class="panel_bottom"> </div><div class="panel_title" align="left">Output</div><div class="panel_content">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.</div><div class="panel_bottom"> </div><div class="panel_title" align="left">Sample Input</div><div class="panel_content"><pre><div style="font-family:Courier New,Courier,monospace">2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5</div>
 

Sample Output
   
   
Case 1: 14 1 4 Case 2: 7 1 6
 

最大子数列和 内容不再详述

暴力算法优化:除去负数为开头的子数列  对于大数据依然TLE

还是把目光投向最不擅长的DP。。。


分析问题:对于a[1]---a[n] 

以第一个数为结尾:maxsum[1]=a[1]

以第二个数:maxsum[2]=max(a[1]+a[2],a[2])

以第三个数:maxsum[3]=max(a[1]+a[2]+a[3],a[2]+a[3],a[3])=max(maxsum[2]+a[3],a[3])

不难推理出maxsum[n]=max(maxsum[n-1]+a[n],a[n])


那么以max记录当前最大子数列和,以ans表示maxsum。显然若ans<0,加上a[n]后一定小于a[n],故令ans=0即可使ans=a[n],若ans>0则直接加上a[n]一定大于a[n].


最后是记录开头和结尾位置,显然我们需要先使开头为1,在max被更新时,结尾即+1,而在ans<0时,开头将变成当前位的下一位。temp存在的目的是在存在多组相同数据时,记录第一次最大值得出现位置。避免出现开头是最后一次最大子数列的开头,而结尾是第一次最大子数列的结尾。(即开头结尾要同时更新)


源代码:

package sduoj无限滚动;
import java.util.Scanner;
import java.math.*;
public class haaha
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        int T=s.nextInt();
        int cout=1;
        int[] a=new int[100000];
        for (int i=1;i<=T;i++)
        {
            int ans=0;int max=-1000000000;
            int k=s.nextInt();
            int start=1,end=k,temp=1;
            for (int j=1;j<=k;j++)
            {
                a[j]=s.nextInt();
                ans+=a[j];
                if (ans>max)
                {
                    max=ans;
                    start=temp;
                    end=j;
                }
                if (ans<0)
                {
                    ans=0;
                    temp=j+1;
                }
            }
             if(cout == 1)
             {
                 System.out.println("Case "+i+":");
                 System.out.println(max+" "+start+" "+end);
                 cout = 0;
             }
             else
             {
                 System.out.println();
                 System.out.println("Case "+i+":");
                 System.out.println(max+" "+start+" "+end);
             }
        }
    }
}




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
信息数据从传统到当代,是一直在变革当中,突如其来的互联网让传统的信息管理看到了革命性的曙光,因为传统信息管理从时效性,还是安全性,还是可操作性等各个方面来讲,遇到了互联网时代才发现能补上自古以来的短板,有效的提升管理的效率和业务水平。传统的管理模式,时间越久管理的内容越多,也需要更多的人来对数据进行整理,并且数据的汇总查询方面效率也是极其的低下,并且数据安全方面永远不会保证安全性能。结合数据内容管理的种种缺点,在互联网时代都可以得到有效的补充。结合先进的互联网技术,开发符合需求的软件,让数据内容管理不管是从录入的及时性,查看的及时性还是汇总分析的及时性,都能让正确率达到最高,管理更加的科学和便捷。本次开发的医院后台管理系统实现了病房管理、病例管理、处方管理、字典管理、公告信息管理、患者管理、药品管理、医生管理、预约医生管理、住院管理、管理员管理等功能。系统用到了关系型数据库中王者MySql作为系统的数据库,有效的对数据进行安全的存储,有效的备份,对数据可靠性方面得到了保证。并且程序也具备程序需求的所有功能,使得操作性还是安全性都大大提高,让医院后台管理系统更能从理念走到现实,确确实实的让人们提升信息处理效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值