给两个序列, 一一对应相乘, 求最大和.
0 不算数, 输入时按正负共分为 4 个数组.
- #include<cstdio>
- #include<algorithm>
- #include<iostream>
- using namespace std;
- const int maxn=100010;
- int coupon1[maxn],coupon2[maxn];
- int product1[maxn],product2[maxn];
- bool cmp(int a,int b){
- return a>b;
- }
- int main(){
- int n,m,c1=0,c2=0,p1=0,p2=0;
- long long cx,px,ans=0;
- scanf("%d", &n);
- for(int i=0;i<n;i++){
- scanf("%lld",&cx);
- if(cx>=0) {
- coupon1[c1++]=cx;
- }
- else {
- coupon2[c2++]=cx;
- }
- }
- scanf("%d",&m);
- for(int i=0;i<m;i++){
- scanf("%lld",&px);
- if(px>=0) {
- product1[p1++]=px;
- }
- else {
- product2[p2++]=px;
- }
- }
- sort(coupon1,coupon1+c1,cmp);
- sort(coupon2,coupon2+c2);
- sort(product1,product1+p1,cmp);
- sort(product2,product2+p2);
- for(int i=0;i<c1;i++){
- ans+=coupon1[i]*product1[i];
- }
- for(int i=0;i<c2;i++){
- ans+=coupon2[i]*product2[i];
- }
- printf("%lld",ans);
- return 0;
- }
- A1037
来源: http://www.bubuko.com/infodetail-2958002.html