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;
}
时间: 2024-08-04 09:07:54

PAT (Advanced Level) 1037. Magic Coupon (25)的相关文章

【PAT甲级】1037 Magic Coupon (25 分)

题意: 输入一个正整数N(<=1e5),接下来输入N个整数.再输入一个正整数M(<=1e5),接下来输入M个整数.每次可以从两组数中各取一个,求最大的两个数的乘积的和. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[100007],b[100007];long long c[100007],d[100007];int cnta,cntb,cntc,cntd

PAT 1037. Magic Coupon (25)

1037. Magic Coupon (25) The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product back! What is more, the sho

PAT Advanced 1037 Magic Coupon (25) [贪?算法]

题目 The magic shop in Mars is ofering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product back! What is more, the shop also ofers some bonu

PAT甲题题解-1037. Magic Coupon (25)-贪心,水

题目说了那么多,就是给你两个序列,分别选取元素进行一对一相乘,求得到的最大乘积. 将两个序列的正和负数分开,排个序,然后分别将正1和正2前面的相乘,负1和负2前面的相乘,累加和即可. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <cmath> #include <vector> using namespa

1037. Magic Coupon (25)

题目例如以下: The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product back! What is more, the shop also offers so

PAT (Advanced Level) 1083. List Grades (25)

简单排序. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std

PAT (Advanced Level) 1021. Deepest Root (25)

先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; struct Edge { int a,b; }e[20000]; int n,sz

PAT (Advanced Level) 1020. Tree Traversals (25)

递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int maxn=40; int a[maxn],b[maxn]; int n,tot; struc

PAT (Advanced Level) 1114. Family Property (25)

简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct X { int id; int Father,Mother; int k;