九度online judge-最大连续子序列
来源:互联网 发布:sas软件怎么使用 编辑:程序博客网 时间:2024/06/03 14:52
题目来源:http://ac.jobdu.com/problem.php?pid=1011
我的代码:超时
//c语言中,数学函数除了求整数的绝对值函数abs()之外<abs() 定义在stdlib.h中>,其余的函数都在头文件 math.h 中定义,包括对浮点数求绝对值的函数fabs()。
//c++中,包含的相应的头文件为,原则是前面加c,同时去掉.h 。
//例如:
//#include <cstdlib>对应 #include <stdlib.h>
//#include <cmath>对应 #include <math.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int m;
int a[10000];
int lesss,moree;
int maxx;
//int sum=0;
int pick(int low,int high,int& les,int& mor){//总是假设序列从一个正数开始
if (low==high&&a[low]<0){
m=0;
les=0;
mor=high;
return 0;
}
if(low>high) return0;
int ma=a[low];
int sum=0;
int less=low;
int more=low;
while(a[++more]>=0&&more<=high){
// more=low+1;
ma+=a[more];
}
more--;
int i=more+1;
while (a[more]==0&&more>low) more--;
while (a[less]==0&&(less+1)<=more) less++;
for(;i<=high;i++){
if(a[i]>=0)break;
sum+=a[i];
}
maxx=pick(i,high,lesss,moree);
int summ=abs(sum);
if(maxx>summ){
if((ma-summ+maxx)>=maxx){
ma=ma-summ+maxx;
more=moree;
}
else if((ma-summ+maxx)<maxx){
less=lesss;
more=moree;
ma=maxx;
}
}
else if(maxx<=summ)
if(ma<maxx){
less=lesss;
more=moree;
ma=maxx;
}
les=less;
mor=more;
return ma;
}
int main(){
int k;
while(cin>>k){
if (k==0) return 0;
for(int i=0;i<k;i++)
cin>>a[i];
int less=0,more=k-1;
int i;
for(i=0;i<k;i++)
if(a[i]>=0)break;
m= pick(i,k-1,less,more);
cout<<m<<" "<<a[less]<<" "<<a[more]<<endl;
}
return 0;
}
#include<stdio.h>
#define Max 10000
int
main(){
int
maxsofar;
int
maxendinghere;
int
begin,end,temp;
int
x[Max];
int
i,n;
int
count;
while
(
scanf
(
"%d"
,&n),n>0){
for
(i=0;i<n;i++)
scanf
(
"%d"
,&x[i]);
count = 0;
maxsofar = maxendinghere = 0;
begin = end = temp = 0;
for
(i=0;i<n;i++){
if
(x[i]<0) count++;
maxendinghere += x[i];
if
(maxendinghere<=0){
maxendinghere = 0;
temp = i+1;
}
if
(maxendinghere>maxsofar){
maxsofar = maxendinghere;
begin = temp;
end = i;
}
if
(maxendinghere==0&&maxsofar==0&&x[i]==0)
end = begin = i;
}
if
(count==n)
printf
(
"0 %d %d\n"
,x[0],x[n-1]);
else
printf
(
"%d %d %d\n"
,maxsofar,x[begin],x[end]);
}
}
- 九度online judge-最大连续子序列
- 杭州电子科技大学Online Judge 之 “最大连续子序列(ID1231)”解题报告
- 九度1011 最大连续子序列
- 九度1011 最大连续子序列
- 九度 1011 最大连续子序列
- 九度 1011 最大连续子序列
- 九度1011:最大连续子序列
- 九度[1011]-最大连续子序列
- 九度1011-最大连续子序列
- 九度online-题目1480:最大上升子序列和
- 九度Online Judge
- 九度笔记之 1501:最大连续子序列乘积
- 九度-1011 最大连续子序列(动态规划)
- 九度1011 最大连续子序列和
- 九度题目1011:最大连续子序列
- 九度oj-1011-最大连续子序列
- 九度题目1011:最大连续子序列
- 九度OJ—题目1011:最大连续子序列
- 深入理解Oracle中的shared pool与library cache组件及相关等待事件
- 软件工程第二次作业-3D游戏开发(3D版俄罗斯方块)
- Hibernate之双向一对多关系总结
- EasyUi基础
- vim简明教程(step 1)
- 九度online judge-最大连续子序列
- 一步一步教你使用CGI实现一个简单的后门
- Linux内核启动参数传递
- php 组合算法(从n个数中取出m个元素进行组合,不考虑排序和元素重复)
- Java复制文件
- mysql 锁
- 最长递增子序列
- 用户界面View之Chronometer
- win7 64位系统怎么使用debug命令