zoukankan      html  css  js  c++  java
  • HDU1231最大连续子序列DP

    状态方程:sum[i]=max(sum[i-1]+a[i],a[i]);

    找出动态转移方程就可以直接ac掉了.

     

    #include<iostream> 
    using namespace std; 
    int main(void

        int n,i,flag,num[10000],temp,start,end; 
        int sum,max; 
        while(cin>>n,n) 
        { 
            flag=0
            for(i=0;i<n;i++) 
            { 
                cin>>num[i]; 
                if(num[i]>=0
                { 
                    flag=1
                } 
            } 
            if(flag==0)     
            { 
                cout<<'0'<<" "<<num[0]<<" "<<num[n-1]<<endl; 
                continue
            } 
            start=0
            temp=0
            for(i=0,max=-1,sum=0;i<n;i++) 
            { 
                sum+=num[i]; 
                if(sum>max) 
                { 
                    start=temp; 
                    end=i; 
                    max=sum; 
                } 
                if(sum<0
                { 
                    sum=0
                    temp=i+1
                } 
            } 
            cout<<max<<" "<<num[start]<<" "<<num[end]<<endl; 
        } 
        return 0

  • 相关阅读:
    Python 三级菜单
    linux 下按文件类型删除
    linux 做内网端口映射
    ss
    fio
    libXtst.so.6 is needed by teamviewer-12.0.76279-0.i686
    copy 浅复制 与深复制
    Git 使用方法
    关于 爬虫使用 urllib.urlopen 提交默认 User-Agent值
    Python 官方模块文档
  • 原文地址:https://www.cnblogs.com/cchun/p/2520108.html
Copyright © 2011-2022 走看看