poj2479,poj 2593 动归
来源:互联网 发布:笔记本thinkpad和mac 编辑:程序博客网 时间:2024/04/29 23:25
poj 2479
如题:http://poj.org/problem?id=2479
题目很简单 就不翻译了 大概说是求一个数组中2段最大的和
这个题目数据量是50000,但时间只有1000ms
在输入的同时 将之前可能出现的最大连续求出,保存在另一个数组,然后在从后向前遍历一次,求出从后向前连续可能出现最大和,后向前遍历的同时,和前面求出数组中的和相加,匹配,寻找最终的结果。
注意两点:1.输入数 组中每个数据时用scanf 不然可能超时
2.ans初始化要为MIN
#include<iostream>
using namespace std;
#define MIN -999999999
int num[50001];
int count[50001];
int main()
{
int ans;
int N;
cin>>N;
int max,sum,n,i;
while(N--)
{
ans=MIN;
max=MIN;
sum=0;
cin>>n;
for(i=1;i<=n;i++)
{
scanf("%d",&num[i]);
sum+=num[i];
if(sum>max)
max=sum;
count[i]=max;
if(sum<0)
sum=0;
}
max=MIN;
sum=0;
for(i=n;i>1;i--)
{
sum+=num[i];
if(sum>max)
max=sum;
if(ans<max+count[i-1])
ans=max+count[i-1];
if(sum<0)
sum=0;
}
cout<<ans<<endl;
}
return 0;
}
poj 2593
http://poj.org/problem?id=2593
题目和2479一样 只是时间要求相对宽松
#include<iostream>
using namespace std;
#define MIN -999999999
#define MAXN 100002
int num[MAXN];
int count[MAXN];
int main()
{
int N,i,sum,max,ans;
while(1)
{
sum=0;
max=MIN;
cin>>N;
if(N==0) break;
for(i=1;i<=N;i++)
{
scanf("%d",&num[i]);
sum+=num[i];
if(sum>max)
max=sum;
count[i]=max;
if(sum<0)
sum=0;
}
max=MIN;
sum=0;
ans=MIN;
for(i=N;i>1;i--)
{
sum+=num[i];
if(sum>max)
max=sum;
if(ans<max+count[i-1])
ans=max+count[i-1];
if(sum<0)
sum=0;
}
cout<<ans<<endl;
}
return 0;
}
- poj2479,poj 2593 动归
- poj(1260,动归)
- poj 2250 Compromise(动归)
- 【DP动归】【DFS】POJ-1088 滑雪
- POJ - 3846 Mountain Road 动归
- POJ - 3847 Moving to Nuremberg 动归
- 动归
- Poj2479(&poj2593)(动规)
- [DP动归]-POJ-2479-最大两段子串和
- POJ 1018 Communication System(搜索/贪心/动归)
- poj 1157 little shop of flowers(动归)
- poj 1625 AC自动机+动归+大数加法
- 【精】POJ 3267 The Cow Lexicon(区间动归)
- POJ 2750 Potted Flower (线段树+动归)
- POJ 1159 Palindrome(区间动归 滚动数组)
- POJ -1037-A decorative fence(动归 +计数)
- 一道动归问题
- 动归_poj2479
- ERROR hive.ql.metadata.Hive - NoSuchObjectException(message:default.hive_born table not found)
- monodb 聚合框架 Aggregation
- 官方文档关于sqlt工具的介绍
- sqlserver存储过程
- 给程序员推荐一些提高工作效率的工具
- poj2479,poj 2593 动归
- 如何学习嵌入式
- 八大排序算法
- cocos2dx3.2学习之路之新建项目
- IOS视频软件开发流程
- Android 编程下字库的使用
- Bash中多条shell命令的连接后,一次性发送
- IOS开发:导航栏添加多个按钮以及在左边添加logo
- eclipse svn插件安装方法