我编写了这个程序,它按第3列对2d向量进行排序,并进行一些基本操作来提供输出。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<bits/stdc++.h>
#include<vector>
using namespace std;
bool sortcol( const vector<long long int>& v1,const vector<long long int>& v2 )
{
return v1[2] > v2[2];
}
int main()
{
long long int n,d,i,j;
cin>>n>>d;
long long int sad=0;
vector< vector<long long int> > stuff;
vector<int> day(d,1);
for(i=0;i<n;i++)
{
vector<long long int> tstuff;
for(j=0;j<3;j++)
{
long long int temp;
cin>>temp;
tstuff.push_back(temp);
}
sad+=tstuff[1]*tstuff[2];
if(tstuff[1]>d - tstuff[0]+1) tstuff[1]=d - tstuff[0]+1;
stuff.push_back(tstuff);
}
sort(stuff.begin(),stuff.end(),sortcol);
for (i = 0; i < stuff.size();i++)
{
long long int a = stuff[i][0]+stuff[i][1];
for (long long int j =stuff[i][0]; j <a ; j++)
{
if(day[j-1] == 0) continue;
else
{
sad=sad - stuff[i][2];
day[j-1]=0;
}
}
}
cout<<sad<<"\n";
return 0;
}
当我针对自定义输出运行它时,它会提供所需的结果。但是在提交它到在线时它会显示以下内容:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
我认为它与某些内存管理问题有关,但我无法识别它。请指出错误。 谢谢!