题目链接:

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

Max Sum

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
#### 问题描述
> 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.
#### 输入
> The first line of the input contains an integer T(1 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.

样例输入

2

5 6 -1 5 4 -7

7 0 6 -1 1 -6 7 -5

样例输出

Case 1:

14 1 4

Case 2:

7 1 6

题意

求最大连续子段和,多解时求最左边那个。

题解

1、写wa了。。

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=101010; int arr[maxn],n; int main() {
int tc,kase=0;
scf("%d",&tc);
while(tc--){
scf("%d",&n);
for(int i=1;i<=n;i++) scf("%d",&arr[i]); int l=1,r=1,Ma=arr[1];
for(int i=1;i<=n;i++){
int tmp=arr[i];
if(tmp>Ma){ Ma=tmp,l=r=i; }
int ed=i+1;
//这里其实强行把第i个塞进去了,这是错的。
while(ed<=n&&tmp+arr[ed]>=0){
tmp+=arr[ed];
if(tmp>Ma){
Ma=tmp;
l=i,r=ed;
}
ed++;
}
i=ed-1;
} prf("Case %d:\n",++kase);
prf("%d %d %d\n",Ma,l,r);
if(tc) prf("\n");
}
return 0;
}

2、改的很挫,终于过了。。

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=101010; int arr[maxn],n; int main() {
// freopen("data_in.txt","r",stdin);
// freopen("data_out.txt","w",stdout);
int tc,kase=0;
scf("%d",&tc);
while(tc--){
scf("%d",&n);
for(int i=1;i<=n;i++) scf("%d",&arr[i]); int l=0,r=0,Ma=-INF;
for(int i=1;i<=n;i++){
if(arr[i]<0){
if(arr[i]>Ma){
Ma=arr[i];
l=r=i;
}
continue;
}
int tmp=arr[i];
if(arr[i]>Ma){
Ma=arr[i];
l=r=i;
}
int ed=i+1;
while(ed<=n&&tmp+arr[ed]>=0){
tmp+=arr[ed];
if(tmp>Ma){
Ma=tmp;
l=i,r=ed;
}
ed++;
}
i=ed-1;
} prf("Case %d:\n",++kase);
prf("%d %d %d\n",Ma,l,r);
if(tc) prf("\n");
}
return 0;
} //end-----------------------------------------------------------------------

3、来个正常点的思路:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=101010; int arr[maxn],n; int main() {
// freopen("data_in.txt","r",stdin);
// freopen("data_out.txt","w",stdout);
int tc,kase=0;
scf("%d",&tc);
while(tc--){
scf("%d",&n);
for(int i=1;i<=n;i++) scf("%d",&arr[i]); int sum=0,l=1,r=1,tmp=1,Ma=-INF; for(int i=1;i<=n;i++){
if(sum<0){
sum=0; tmp=i;
}
sum+=arr[i];
if(sum>Ma){
Ma=sum;
l=tmp,r=i;
}
} prf("Case %d:\n",++kase);
prf("%d %d %d\n",Ma,l,r); if(tc) prf("\n"); }
return 0;
} //end-----------------------------------------------------------------------

HDU 1003 最大连续子段和的更多相关文章

  1. HDU 1003 最大连续和

    http://www.acmerblog.com/hdu-1003-Max-Sum-1258.html 这里难点只有求起始位置,把握状态变化就行.一般这种子序列问题,都可以用dp简化 #include ...

  2. hdu 1003 最大连续子串

    #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #defin ...

  3. HDU 1003:Max Sum(DP,连续子段和)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  4. HDU 1003(A - 最大子段和)

    HDU   1003(A - 最大子段和) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/A 题目: ...

  5. 【ToReadList】六种姿势拿下连续子序列最大和问题,附伪代码(以HDU 1003 1231为例)(转载)

    问题描述:       连续子序列最大和,其实就是求一个序列中连续的子序列中元素和最大的那个. 比如例如给定序列: { -2, 11, -4, 13, -5, -2 } 其最大连续子序列为{ 11, ...

  6. hdu 1003 hdu 1231 最大连续子序列【dp】

    HDU1003 HDU1231 题意自明.可能是真的进步了点,记得刚开始研究这个问题时还想了好长时间,hdu 1231还手推了很长时间,今天重新写干净利落就AC了. #include<iostr ...

  7. HDOJ-1003 Max Sum(最大连续子段 动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=1003 给出一个包含n个数字的序列{a1,a2,..,ai,..,an},-1000<=ai<=100 ...

  8. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  9. HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)

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

随机推荐

  1. redis 数据库随笔 (一)

    redis数据库的基本类型分析: 1.string 最基本的数据类型.只存贮一个值,key-value,最大值存储512M. 创建命令:hmset  读取命令:hget 2.hash 集合,存储为一个 ...

  2. linux命令--cp、tail、cd、mv、history、cd

    day1 cd命令 cd ../定位至上级目录 cd ./定位到当前目录 cd ~ 定位当前用户目录 cd / 定位系统根目录 cd - 返回进入此目录之前所在的目录 day2 mv命令: mv时,若 ...

  3. docker 设置映射端口 目录挂载

    docker run -p 3092:9092 -p 3093:9093 -p 3094:9094 -p 3181:2181 --name="kafka_map_port_3092_4_31 ...

  4. Docker vs Warden

    相同点: 都是依赖宿主操作系统内核的轻量级容器: 都采用了linux内核技术实现容器隔离(namespace)和资源限制(cgroup): 都使用了aufs文件系统: 不同点: 用途 warden是C ...

  5. Android远程擦除漏洞

    漏洞原理: 安卓浏览器内核有这样一个机制,当网页内嵌入了一个特定格式的字符串,就可以被识别为一个电话号码通过点击该字符串的方式拨打电话.但是没有对*#06#等具有特殊功能的拨号组合进行限制,由于很多厂 ...

  6. 20155222卢梓杰 课堂测试ch06补做

    20155222卢梓杰 课堂测试ch06补做 1.下面代码中,对数组x填充后,采用直接映射高速缓存,所有对x和y引用的命中率为() A . 1 B . 1/4 C . 1/2 D . 3/4 正确答案 ...

  7. 2460: [BeiJing2011]元素

    2460: [BeiJing2011]元素 链接 分析: 贪心的想:首先按权值排序,然后从大到小依次放,能放则放.然后用线性基维护是否合法. 代码: #include<cstdio> #i ...

  8. [HNOI2015]开店 树链剖分,主席树

    [HNOI2015]开店 LG传送门 蒟蒻表示不会动态淀粉质. 先把点按年龄排序, 设\(dis[i]\)表示\(i\)到根的距离. 把我们要算的东西稍微变下形:\(ans\) \[ = \sum \ ...

  9. js 删除url指定参数

    /** * 删除当前url中指定参数 * @param names 数组或字符串 * @returns {string} */ function funcUrlDel(names) { if(type ...

  10. 菜鸟vimer成长记——第4.2章、编程插件

    简介 这部分的插件是与编程相关的插件.主要涉及两大块:所有编程语言通用的插件,以及各个语言独有的插件.插件的数量和合理性,这可能是一个不断累积和修正的过程. 个人感觉,现在比较适合sh,html,ma ...