很简单的贪心。
将输入的两种数据各分正负存到四个数组中,
正的和正的相乘,负的和负的相乘(绝对值大的和绝对值大的相乘)
# include <iostream>
# include <algorithm>
# include <functional>
# include <vector>
using namespace std;
vector<int> coupons_postive;
vector<int> coupons_negative;
vector<int> products_postive;
vector<int> products_negative;
int main()
{
// 分为正负各四个数组
// 输入数据
int Nc, Np;
cin >> Nc;
for(int i=0;i<Nc;++i){
int c;
cin >> c;
if(c >= 0)
coupons_postive.push_back(c);
else
coupons_negative.push_back(c);
}
cin >> Np;
for(int i=0;i<Np;++i){
int p;
cin >> p;
if(p >= 0)
products_postive.push_back(p);
else
products_negative.push_back(p);
}
sort(coupons_postive.begin() , coupons_postive.end() , greater<int>());
sort(coupons_negative.begin() , coupons_negative.end() , less<int>() );
sort(products_postive.begin() , products_postive.end() , greater<int>());
sort(products_negative.begin(), products_negative.end(), less<int>() );
int i, j;
int sum = 0;
for(i=0, j=0;i < coupons_postive.size() && j < products_postive.size();++i, ++j)
sum += coupons_postive[i]*products_postive[j];
for(i=0, j=0;i<coupons_negative.size() && j<products_negative.size();++i, ++j)
sum += coupons_negative[i]*products_negative[j];
cout << sum << endl;
return 0;
}