练习三1001
2016-04-27 13:04
281 查看
Problem A
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 142 Accepted Submission(s) : 21
[align=left]Problem Description[/align]
Given a sequence a[1],a[2],a[3]......a
, 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.<br>
[align=left]Input[/align]
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).<br>
[align=left]Output[/align]
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.<br>
[align=left]Sample Input[/align]
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
[align=left]Sample Output[/align]
Case 1:
14 1 4
Case 2:
7 1 6
Statistic |
Submit |
Back
题意:
给出一串数列,求其中和最大的子序列的值及其始末位置。
思路:
刚开始想以每一个数作为起始,计算以他开始的所有子序列的长度存入数组,但超时了。应该以他为末位置向前加,如果他前面的最大和小于零,则它变为起点。注意题目中说输出时每组后空一行,一开始没看到。
代码:
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
for(int k=1;k<=t;k++)
{
int n,a[100001],b[100001],c[100001];
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
b[1]=a[1];
c[1]=1;
for(int i=2;i<=n;i++)
{
if(b[i-1]>=0)
{
b[i]=b[i-1]+a[i];
c[i]=c[i-1];
}
else
{
b[i]=a[i];
c[i]=i;
}
}
int sum=b[1],sta=c[1],end=1;
for(int i=2;i<=n;i++)
{
if(b[i]>sum)
{
sum=b[i];
sta=c[i];
end=i;
}
}
cout<<"Case "<<k<<":"<<endl<<sum<<" "<<sta<<" "<<end<<endl;
if(k!=t) cout<<endl;
}
return 0;
}
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 142 Accepted Submission(s) : 21
[align=left]Problem Description[/align]
Given a sequence a[1],a[2],a[3]......a
, 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.<br>
[align=left]Input[/align]
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).<br>
[align=left]Output[/align]
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.<br>
[align=left]Sample Input[/align]
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
[align=left]Sample Output[/align]
Case 1:
14 1 4
Case 2:
7 1 6
Statistic |
Submit |
Back
题意:
给出一串数列,求其中和最大的子序列的值及其始末位置。
思路:
刚开始想以每一个数作为起始,计算以他开始的所有子序列的长度存入数组,但超时了。应该以他为末位置向前加,如果他前面的最大和小于零,则它变为起点。注意题目中说输出时每组后空一行,一开始没看到。
代码:
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
for(int k=1;k<=t;k++)
{
int n,a[100001],b[100001],c[100001];
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
b[1]=a[1];
c[1]=1;
for(int i=2;i<=n;i++)
{
if(b[i-1]>=0)
{
b[i]=b[i-1]+a[i];
c[i]=c[i-1];
}
else
{
b[i]=a[i];
c[i]=i;
}
}
int sum=b[1],sta=c[1],end=1;
for(int i=2;i<=n;i++)
{
if(b[i]>sum)
{
sum=b[i];
sta=c[i];
end=i;
}
}
cout<<"Case "<<k<<":"<<endl<<sum<<" "<<sta<<" "<<end<<endl;
if(k!=t) cout<<endl;
}
return 0;
}
相关文章推荐
- 为什么科技公司会信神信教信风水?
- cvNormalizeHist()函数详解!
- Redis为什么这么快?
- Scrum团队成立及《构建之法》第六、七章读后感
- 【VS开发】C++ opencv Mat基础
- 循环中读取数据库、嵌套循环引起的性能问题
- Vmstat 2 详解
- 学习进度条
- 解决 Ubuntu 无法调节屏幕亮度的问题(转)
- 301跳转
- redis的一个操作的代码执行流程
- 精通CSS滤镜(filter)
- 如何刷新K/3 Cloud Oracle 数据库的物化视图
- 150. Evaluate Reverse Polish Notation
- 长方柱体类
- android的token
- Android ListView中 每一项都有不同的布局
- 正则表达式知识详解之贪婪模式和非贪婪模式 (java版示例)
- 如何修改数据库的快照隔离级别
- Jquery 判断 checked 是否选中