PAT (Advanced Level) 1037. Magic Coupon (25)
简单题。
#include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<vector> using namespace std; const int maxn=100000+10; int n,m; long long a[maxn]; long long b[maxn]; bool cmp(const long long &a,const long long &b) { return a>b; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); scanf("%d",&m); for(int i=1;i<=m;i++) scanf("%lld",&b[i]); sort(a+1,a+1+n); sort(b+1,b+1+m); long long ans=0; for(int i=1;i<=min(m,n);i++) { if(a[i]>=0) break; if(b[i]>=0) break; ans=ans+a[i]*b[i]; } sort(a+1,a+1+n,cmp); sort(b+1,b+1+m,cmp); for(int i=1;i<=min(m,n);i++) { if(a[i]<=0) break; if(b[i]<=0) break; ans=ans+a[i]*b[i]; } printf("%lld\n",ans); return 0; }