题目传送门:https://www.patest.cn/contests/pat-a-practise/1037
AC 代码 因为一开始用erase 删除数组元素 倒置超时 后来用下标增加AC
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <stack>
#include <map>
using namespace std;
int main(){
vector<long long>cPos,cNeg;//优惠券正数 负数.
vector<long long>vPos,vNeg;//商品正数,负数.
int NC,NP;
long long t;
scanf("%d",&NC);
for (int i=0; i<NC; ++i) {
scanf("%lld",&t);
if(t<0)
cNeg.push_back(t);
else if(t>0)
cPos.push_back(t);
}
scanf("%d",&NP);
for (int i=0; i<NP; ++i) {
scanf("%lld",&t);
if(t<0)
vNeg.push_back(t);
else if(t>0)
vPos.push_back(t);
}
sort(cPos.begin(), cPos.end(),[](int a,int b){return a>b;});
sort(cNeg.begin(), cNeg.end(),[](int a,int b){return a<b;});
sort(vPos.begin(), vPos.end(),[](int a,int b){return a>b;});
sort(vNeg.begin(), vNeg.end(),[](int a,int b){return a<b;});
long long sum = 0;
int pos = 0;
int len1 = (int)cPos.size(),len2 = (int)vPos.size();
while (pos<len1&&pos<len2) {
sum = sum + cPos[pos]*vPos[pos];
++pos;
}
pos = 0;
len1 = (int)cNeg.size(),len2=(int)vNeg.size();
while (pos<len1&&pos<len2) {
sum = sum + cNeg[pos]*vNeg[pos];
pos++;
}
printf("%lld\n",sum);
return 0;
}