我尝试了正常的方式
typedef struct
{
int distance; //distance to the next stop
int fuel; //the fuel at the stop
}stopInfo;
int main()
{
vector<stopInfo> swapper;
int numCases,i,initFuel,distance,numStops;
stopInfo *I;
scanf("%d",&numCases);
for(i=0;i<numCases;i++)
{
scanf("%d",&numStops);
I=(stopInfo *)malloc(sizeof(stopInfo)*numStops+2);
for(i=0;i<numStops;i++) scanf("%d %d",&I[i].distance,&I[i].fuel);
scanf("%d %d",&distance,&initFuel);
I[numStops].fuel=initFuel; I[numStops].distance=distance; //for starting point
I[numStops+1].fuel=0; I[numStops+1].distance=0; //for destination
//sort in decresing order of distance.
vector<stopInfo> v; v.assign(I,I+numStops+2);
sort(v.begin(),v.end(),customCompare);
for(i=0;i<numStops+2;i++) //copy back in array
{
I[i].distance=v[i].distance;
I[i].fuel=v[i].fuel;
}
for(i=0;i<numStops+1;i++)
I[i].distance=I[i].distance-I[i+1].distance;
printf("%d\n",fxMemoWrap(I,numStops,initFuel));
}
return 0;
}
但它给出了一个错误
a.out: malloc.c:2451: sSYSMALLOc: 断言`(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof (size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' 失败。