zoukankan      html  css  js  c++  java
  • [HDU1003]最长子序列和

    http://acm.hdu.edu.cn/showproblem.php?pid=1003

    解题关键:1、最大连续子序列和模板

        2、max、end不能使用,在oj中会显示编译错误

        3、注意模板中没有else if

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cstdlib>
     5 #define INF 10000000 
     6 using namespace std;
     7 typedef long long ll;
     8 int n,a[1000002],st,end1;
     9 int max_sub(){
    10     int max1=-INF,temp_sum=0,k=0;
    11     for(int i=0;i<n;i++){
    12         temp_sum+=a[i];
    13         if(temp_sum>max1){ max1=temp_sum;st=k;end1=i;}
    14         if(temp_sum<0){ temp_sum=0;k=i+1;}
    15     }
    16     return max1;
    17 }
    18 int main(){
    19     int t;
    20     scanf("%d",&t);
    21     for(int i=1;i<=t;i++){
    22         scanf("%d",&n);
    23         for(int j=0;j<n;j++){
    24             scanf("%d",a+j);
    25         }
    26         int ans=max_sub();
    27         printf("Case %d:
    %d %d %d
    ",i,ans,st+1,end1+1);
    28         if(i!=t) printf("
    ");
    29     }    
    30     return 0;
    31 }
     1 #include<bits/stdc++.h>
     2 #define INF 0x3f3f3f3f
     3 using namespace std;
     4 typedef long long ll;
     5 int a[50002];
     6 int main(){
     7     int n;
     8     cin>>n;
     9     ll sum=0,max=0;
    10     for(int i=0;i<n;i++){
    11         cin>>a[i];
    12         //a[i]=-a[i];
    13         sum+=a[i];
    14         if(sum>max) max=sum;
    15         if(sum<0) sum=0;
    16     }
    17     printf("%lld
    ",max);
    18 }
  • 相关阅读:
    深入理解计算机系统
    Python基础知识点
    贝叶斯分类器
    matplotlib 库的使用
    Linux 学习笔记
    支持向量机
    神经网络
    决策树
    k近邻法的实现
    智能过滤:九眼过滤管应对千变万化
  • 原文地址:https://www.cnblogs.com/elpsycongroo/p/6837924.html
Copyright © 2011-2022 走看看