有关ACM题目超出内存限制问题

physics1988 2009-03-09 10:18:36
原题连接:http://acm.pku.edu.cn/JudgeOnline/problem?id=3685
我写了一个程序。测试数据没错,但是提交就是说我超内存。我感觉到很困惑我只用了两个vector而已,难道vector类占的内存真的那么大吗?
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool isSmaller(const int &s1,const int &s2){
return s1<s2;
}
int main(){
vector<long int> result;//用于存放每次输入应该输出的结果,读入完成后一次输出
vector<long int> temp_result;//因为每次输入的N都是按升序的,矩阵元素不用每次全部重新生成,所以声明为全局变量
temp_result.push_back(3);//为了防止第一个输入N不是1的情况,先将1的情况压栈,以便使用上一条解析的内存节省
int i;
cin>>i;
int last_N=1;
for(int j=0;j<i;j++){
int N;
long int M;
cin>>N>>M;
long int temp;
if(N!=1){//N等于1的情况不同于其他数字
for(int q=last_N+1;q<=N;q++){//对多出的列元素进行压栈
for(int p=1;p<=last_N;p++){
temp=p*p+100000*p+q*q-100000*q+p*q;
temp_result.push_back(temp);
}
}
for(int p=last_N+1;p<=N;p++){//对多出的行元素进行压栈
for(int q=1;q<=N;q++){
temp=p*p+100000*p+q*q-100000*q+p*q;
temp_result.push_back(temp);
}
}
}
stable_sort(temp_result.begin(),temp_result.end(),isSmaller);//对该矩阵数据进行升序排序
temp=temp_result[M-1];
result.push_back(temp);
last_N=N;
}
for(vector<long int>::const_iterator iter=result.begin();
iter!=result.end();iter++)
cout<<*iter<<endl;
return 0;
}
希望各位高手不吝赐教,一起将问题找出来
...全文
1602 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
arong1234 2009-03-11
  • 打赏
  • 举报
回复
100,000也太少了哦,1M都不到
[Quote=引用 6 楼 snailman 的回复:]
2500000000
这么多?vector 相对于 内置的数组,在ms的编译器数组元素有限制,最多 好像是 100000个
[/Quote]
arong1234 2009-03-11
  • 打赏
  • 举报
回复
不考虑vector,假如你用不占额外内存的任何数据结构存储,单个元素最大大概3*50,000 * 50,000=7,500,000这需要一个int保存,占用4字节,总共2,500,000,000个元素,总共需要4*2,500,000,000=10,000,000,000大概10G,在32位平台上,用户态内存总共才2G,还要给代码和其他数据结构用,你怎么可能房得下

所以不要想着把着所有得matrix都放内存找到最小得方法,这是不可能做到得。你自己要是稍微算一下就知道这个和vector没啥关系,是你算法太差了。

在帖子http://topic.csdn.net/u/20090310/08/90f0a4c7-b544-4993-be17-7510d462daa6.html里,已经有些建议,我觉得在这个基础上进行研究也许有点前途

[Quote=引用 5 楼 physics1988 的回复:]
我试了一下,原来vector类根本就放不下2500000000个元素,在vector <long int> temp_result(2500000000);这句初始化的语句就报错,内存访问违法
[/Quote]
海枫 2009-03-11
  • 打赏
  • 举报
回复
题目要求程序的范围是: N(1 ≤ N ≤ 50,000) and M(1 ≤ M ≤ N × N).
所以你的程序必须要这些情况下,正确运行,性能和效率必须满足出题者的要求(如线性的解决方法,你用平方的算法,肯定是通不过的)。

否则不可能Pass的!
physics1988 2009-03-10
  • 打赏
  • 举报
回复
各位高手出来帮一下忙啊。难道vector类占的内存空间真的那么大吗?
snailman 2009-03-10
  • 打赏
  • 举报
回复
2500000000
这么多?vector 相对于 内置的数组,在ms的编译器数组元素有限制,最多 好像是 100000个
physics1988 2009-03-10
  • 打赏
  • 举报
回复
我试了一下,原来vector类根本就放不下2500000000个元素,在vector<long int> temp_result(2500000000);这句初始化的语句就报错,内存访问违法
海枫 2009-03-10
  • 打赏
  • 举报
回复
我怀疑是你程序算法不好,占用太多的内存,不要把问题推到vector里面去。你在本机测试一下下面的用例,并把用时贴上来让大家看看

5
1000 500
10000 5000
50000 2400000000
50000 1200000000
50000 1

我觉得这个测试用例会考验你程序的效率。

附原网页的问题,大家可以讨论一下!

Description

Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that equals i2 + 100000 × i + j2 - 100000 × j + i × j, you are to find the M-th smallest element in the matrix.

Input

The first line of input is the number of test case.
For each test case there is only one line contains two integers, N(1 ≤ N ≤ 50,000) and M(1 ≤ M ≤ N × N). There is a blank line before each test case.

Output

For each test case output the answer on a single line.

Sample Input

12

1 1

2 1

2 2

2 3

2 4

3 1

3 2

3 8

3 9

5 1

5 25

5 10

Sample Output

3
-99993
3
12
100007
-199987
-99993
100019
200013
-399969
400031
-99939
jichre 2009-03-10
  • 打赏
  • 举报
回复
要求内存限制是多少
physics1988 2009-03-10
  • 打赏
  • 举报
回复
请各高手帮帮忙啊

65,208

社区成员

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

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