当前位置: 代码迷 >> 综合 >> pat a 1037
  详细解决方案

pat a 1037

热度:51   发布时间:2024-01-26 07:18:43.0

狂用vector和sort 将两组数据再分别分成正的一组(按从大到小排列),负的一组(从小到大排列),然后看两个数组大小,将两个数组相乘的结果累加即可

#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;vector<long long int> coupon;
vector<long long int> value;
vector<long long int> cz;
vector<long long int> cf;
vector<long long int> vz;
vector<long long int> vf;bool cmp1(long long int a,long long int b){return a<b;
}bool cmp2(long long int a,long long int b){return a>b;
}int main(){int nc,np;scanf("%d",&nc);for(int i=0;i<nc;i++){long int temp;scanf("%lld",&temp);coupon.push_back(temp);}scanf("%d",&np);for(int i=0;i<np;i++){long int temp;scanf("%lld",&temp);value.push_back(temp);}	for(int i=0;i<coupon.size();i++){if(coupon[i]<0){cf.push_back(coupon[i]);}else{cz.push_back(coupon[i]);}}for(int i=0;i<value.size();i++){if(value[i]<0){vf.push_back(value[i]);}else{vz.push_back(value[i]);}}sort(cf.begin(),cf.end(),cmp1);sort(vf.begin(),vf.end(),cmp1);sort(cz.begin(),cz.end(),cmp2);sort(vz.begin(),vz.end(),cmp2);long long int sum=0;for(int i=0;i<cf.size()&&i<vf.size();i++){sum=sum+cf[i]*vf[i];}for(int i=0;i<cz.size()&&i<vz.size();i++){sum=sum+cz[i]*vz[i];}printf("%lld",sum);return 0;} 
  相关解决方案