在线求解 交了好几遍超时

qq_31803271 2016-09-06 03:01:29
DESCRIPTION
Given an integer array, how will you find out the increasing subsequence which gives the largest sum. For example, 4, 2, 1, 5, 3 in this subsequence a few possible increasing subsequences are 1) 4 2) 2 3) 1 4) 5 5) 3 6) 4, 5 7) 2, 5 8) 2, 3 9) 1, 5 10) 1, 3 but 4, 5 gievs the maximum sum. Your task is how to find such the maximum sum in a given integer array.
INPUT
Multiply Cases, For each case: The first line contains N(0< N <=1000000), which is the number of the integer array. The second line contains N integer numbers not exceeding 2000000 by the absolute value.
OUTPUT
For each case, output only one integer, which is the maximum sum of increasing subsequences.
SAMPLE INPUT
5
4 2 1 5 3
SAMPLE OUTPUT
9
运行了好多遍超时 求解:
#include<iostream>
using namespace std;
int p1[1000000];
int main()
{
int n;
while(cin>>n)
{

for(int k=0;k<n;k++)
cin>>p1[k];
int temp;
for(int i=1;i<n;i++)
{
for(int j=0;j<n-i;j++)
if(p1[j]>p1[j+1])
{
temp=p1[j];
p1[j]=p1[j+1];
p1[j+1]=temp;
}
}
cout<<p1[n-2]+p1[n-1]<<endl;
}
}
...全文
233 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 版主 2016-09-06
  • 打赏
  • 举报
回复
引用 2 楼 dustpg 的回复:
[quote=引用 1 楼 paschen 的回复:] 不宜在栈上分配这么大的数组,如果需要很大容量的数组,应该动态申请内存
这是全局啊, 不是栈上的, 这种在ACM上其实很常见[/quote] 不好意思,没仔细看他代码
赵4老师 2016-09-06
  • 打赏
  • 举报
回复
在占用内存空间较大的局部数组声明的前面加static将其从堆栈数据段挪到全局数据段即可避开因局部数组大小超过默认堆栈大小1MB造成程序不能正常运行的问题。 容量大小从小到大:栈≤全局数据≤堆≤文件≤硬盘≤磁盘阵列≤云存储 当程序需要使用比如2GB~1TB左右的存储时,最简单的办法恐怕得是用文件读写模拟内存读写了吧。windows参考_fseeki64函数,linux参考fseeko64函数。
小灸舞 2016-09-06
  • 打赏
  • 举报
回复
你这个算法不够优,你没必要用冒泡进行排序。
仅仅只是找出2个最大的数的话,一次遍历就行了,复杂度是O(n)
dustpg 2016-09-06
  • 打赏
  • 举报
回复
引用 1 楼 paschen 的回复:
不宜在栈上分配这么大的数组,如果需要很大容量的数组,应该动态申请内存
这是全局啊, 不是栈上的, 这种在ACM上其实很常见
paschen 版主 2016-09-06
  • 打赏
  • 举报
回复
不宜在栈上分配这么大的数组,如果需要很大容量的数组,应该动态申请内存

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧