C++ 超过内存限制或时间限制
我试图创建一个程序,将向量1('V1')乘以3个不相等的位置,并找到最大乘法 我使用3个“for”循环进行计数和书写。程序获取位置数量“N”,然后获取“input.txt”中的所有“N”数字。之后,它获取最大位置“max”,并将其写入“output.exe” 但我需要尽可能保持程序的效率,16 MB内存限制和1秒时间限制(我得到1.004秒和33 MB)。有没有更有效的方法C++ 超过内存限制或时间限制,c++,algorithm,vector,C++,Algorithm,Vector,我试图创建一个程序,将向量1('V1')乘以3个不相等的位置,并找到最大乘法 我使用3个“for”循环进行计数和书写。程序获取位置数量“N”,然后获取“input.txt”中的所有“N”数字。之后,它获取最大位置“max”,并将其写入“output.exe” 但我需要尽可能保持程序的效率,16 MB内存限制和1秒时间限制(我得到1.004秒和33 MB)。有没有更有效的方法 #include <vector> #include <fstream> #include <
#include <vector>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int N;
long long max = -9223372036854775807;
int input;
vector<long long> V1;
ifstream file1;
file1.open("input.txt");
file1 >> N;
V1.resize(N);
for (int i = 0; i < N; i++)
{
file1 >> V1[i];
}
for (int i = 0; i < N; i++)
for (int j = 0; j < i; j++)
for (int k = 0; k < j; k++)
if (V1[i] * V1[j] * V1[k] > max)
{
max = V1[i] * V1[j] * V1[k];
}
ofstream file2;
file2.open("output.txt");
file2 << max;
file2.close();
}
首先,我想到了——为什么要存储这些值?您只需要一个最大值—不需要存储所有这些值、推送它们,而且还需要对它们进行排序 另一个重要注意事项:
- 向量为
,但读取的是long-long
s。由于您的输入中有大量的数字,请在任何地方使用int
long
- 推一个项目并弹出它回来是没有意义的-你应该在推之前检查它,以避免两个不必要的操作
- 无论如何,您根本不需要比较
、i
、j
来确定等价性-根据您的循环限制,它们永远不相等k
- 当您知道项目编号错误时,将项目推送到阵列中。扩展向量需要更多的时间。您可能需要将其调整为给定的大小
int N;
long long maximum = -9223372036854775807; // Subject to limits.h LLONG_MIN usage
vector<long long> V1;
ifstream file1;
file1.open("input.txt");
file1 >> N;
V1.resize(N);
for (int i = 0; i < N; i++){
file1 >> V1[i];
}
file1.close();
for (int i = 0; i < N; i++)
for (int j = 0; j < i; j++)
for (int k = 0; k < j; k++)
if (V1[i] * V1[j] * V1[k] > maximum)
maximum = V1[i] * V1[j] * V1[k];
ofstream file2;
file2.open("output.txt");
file2 << maximum;
file2.close();
intn;
长-长最大值=-9223372036854775807;//受限制。仅限使用
向量V1;
ifstream文件1;
file1.open(“input.txt”);
文件1>>N;
V1.调整大小(N);
对于(int i=0;i>V1[i];
}
file1.close();
对于(int i=0;i最大值)
最大值=V1[i]*V1[j]*V1[k];
流文件2;
file2.open(“output.txt”);
file2首先,我想到了-为什么要存储这些值?您只需要一个最大值—不需要存储所有这些值、推送它们,而且还需要对它们进行排序
另一个重要注意事项:
- 向量为
long-long
,但读取的是int
s。由于您的输入中有大量的数字,请在任何地方使用long
- 推一个项目并弹出它回来是没有意义的-你应该在推之前检查它,以避免两个不必要的操作
- 无论如何,您根本不需要比较
i
、j
、k
来确定等价性-根据您的循环限制,它们永远不相等
- 当您知道项目编号错误时,将项目推送到阵列中。扩展向量需要更多的时间。您可能需要将其调整为给定的大小
此代码可能会满足您的内存\时间要求:
int N;
long long maximum = -9223372036854775807; // Subject to limits.h LLONG_MIN usage
vector<long long> V1;
ifstream file1;
file1.open("input.txt");
file1 >> N;
V1.resize(N);
for (int i = 0; i < N; i++){
file1 >> V1[i];
}
file1.close();
for (int i = 0; i < N; i++)
for (int j = 0; j < i; j++)
for (int k = 0; k < j; k++)
if (V1[i] * V1[j] * V1[k] > maximum)
maximum = V1[i] * V1[j] * V1[k];
ofstream file2;
file2.open("output.txt");
file2 << maximum;
file2.close();
intn;
长-长最大值=-9223372036854775807;//受限制。仅限使用
向量V1;
ifstream文件1;
file1.open(“input.txt”);
文件1>>N;
V1.调整大小(N);
对于(int i=0;i>V1[i];
}
file1.close();
对于(int i=0;i最大值)
最大值=V1[i]*V1[j]*V1[k];
流文件2;
file2.open(“output.txt”);
文件2通过查看您所做的工作,您必须找到给定输入向量中3个数字乘积的最大值
只需对向量V1排序并输出最大值(前3个元素或前2个和后2个元素的乘积)。这在空间和时间上都是有效的
像这样:
sort(V1.begin(),V1.end(),greater<int>()) //sorts in descending order
int n = V1.size()-1;
output max(V1[0] * V1[1] * V1[2], V1[0] * V1[n] * V1[n-1])
排序(V1.begin(),V1.end(),greater())//按降序排序
int n=V1.size()-1;
输出最大值(V1[0]*V1[1]*V1[2],V1[0]*V1[n]*V1[n-1])
通过查看您所做的工作,您必须找到给定输入向量中3个数字乘积的最大值
只需对向量V1排序并输出最大值(前3个元素或前2个和后2个元素的乘积)。这在空间和时间上都是有效的
像这样:
sort(V1.begin(),V1.end(),greater<int>()) //sorts in descending order
int n = V1.size()-1;
output max(V1[0] * V1[1] * V1[2], V1[0] * V1[n] * V1[n-1])
排序(V1.begin(),V1.end(),greater())//按降序排序
int n=V1.size()-1;
输出最大值(V1[0]*V1[1]*V1[2],V1[0]*V1[n]*V1[n-1])
好吧,当我看到大小和时间减少时,我倾向于删除所有不必要的语言优点,因为它们确实有助于正确编程,但只会以资源为代价
所以,如果你真的想保留一个值列表中不同索引的所有乘积,我建议你扔掉向量,推送和弹出,使用固定大小的数组
但在进行低级优化之前,我们必须考虑所有可能的算法优化。您只希望从列表中的三个不同值中获得最大的产品。但是对于正数,a>=b a*c>=b*c,两个负数的乘积是正数
因此,最高的产品可能只来自:
- 三个最高正值的乘积
- 一个最高正值和两个最低负值的乘积(绝对值最高)
- 如果没有正值,则三个最高负值的乘积
- 三个最高的正值
- 三个最高的负值
- 两个最小的负值
#include <iostream>
#include <fstream>
#include <climits>
using namespace std;
class Max3 {
long long pmax[3];
long long nmax[3];
long long nmin[2];
void push(long long *record, long long val, size_t pos) {
for(size_t i=0; i<pos; i++) {
record[i] = record[i + 1];
}
record[pos] = val;
}
void set(long long *record, long long val, size_t sz) {
for (size_t i=1; i<sz; i++) {
if (val < record[i]) {
push(record, val, i - 1);
return;
}
}
push(record, val, sz - 1);
}
public:
Max3() {
size_t i;
for (i=0; i<sizeof(pmax)/sizeof(pmax[0]); i++)
pmax[i] = 0;
for (i=0; i<sizeof(nmin)/sizeof(nmin[0]); i++)
nmin[i] = 0;
for (i=0; i<sizeof(nmax)/sizeof(nmax[0]); i++)
nmax[i] = LLONG_MIN;
}
void test(long long val) {
if (val >= *pmax) {
set(pmax, val, 3);
}
else if (val <= 0) {
if (val <= *nmin) {
set(nmin, -val, 2);
}
if (val >= *nmax) {
set(nmax, val, 3);
}
}
}
long long getMax() {
long long max = 0, prod, pm;
if ((prod = pmax[0] * pmax[1] * pmax[2]) > max)
max = prod;
if (pmax[2] > 0)
pm = pmax[2];
else if (pmax[1] > 0)
pm = pmax[1];
else
pm = pmax[0];
if ((prod = nmin[0] * nmin[1] * pm) > max)
max = prod;
if ((prod = nmax[0] * nmax[1] * nmax[2]) > max)
max = prod;
return max;
}
};
int main() {
int N;
long long input;
Max3 m3;
ifstream file1;
file1.open("input.txt");
file1 >> N;
for (int i = 0; i < N; i++){
file1 >> input;
m3.test(input);
}
file1.close();
ofstream file2;
file2.open("output.txt");
file2 << m3.getMax();
file2.close();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
类Max3{
长pmax[3];
长nmax[3];
长nmin[2];
真空推力(lon)