CSU2084: Lunch War with the Donkey

Description

Jingze is a big figure in California State University for his stubbornness. Because of his new failure in the last CET-4, he has decided not to solve problems with English discription any more. In yesterday‘s warm-up contest,he protested to the organizing committee for the appearance of long English discription and threatened to eat all the bread of the contestants during coding. Undoubtedly, this behavior will seriously affect the entire competition. In order to help him get rid of English phobias, good-hearted seniors decided to help him regain his confidence with simple English problems. This problem is a gift that we gave to all contestants who are in the same boat with him.

In today's campus coding match, the contestants brought a variety of bread and milk, and each kind of bread and milk has its own deliciousness. Because Jingze can't just eat bread, he will also wipe out a carton of milk when eating a loaf of bread. If only bread or only milk leaves to have, his appetite will be zero. Since bread and milk are amazing lunch partners, different combinations bring different tastes. Everyone knows the deliciousness of some combination is defined to be its product. Of course the maximum sum of deliciousness is desired for Jingze while the minimum for us, although in both cases we seem to be disadvantaged.

In fact, the Lunch War will never happen and the following gif may be your live picture of today's early reading time and closing ceremony.

Input

There are at most 20 test case.

For each test cases, you are given the number of kinds of bread and milk, N, M for each and 1 ≤ N, M ≤ 105.

In the following two lines are N + M positive integers denoting the deliciousness for each kind of bread and milk.

The deliciousness is guaranteed within 100.

Output

Two integers per line for each test: The maximum and minimum sum of deliciousness for Jingze and us.

Sample Input

1 4
23 
67 50 25 2 

10 6
95 30 2 18 96 6 5 52 99 89 
24 6 83 53 67 17 

Sample Output

1541 46
22884 2073

水题:理解题意就ok了。两两配对后得到的最大值和最小值

  对于无法搭配食用的单一食物,我们可以假想它和0搭配。统一补齐成相同种数后,问题变为:

max/min~~a_1b_1+a_2b_2+…+a_k*b_k=max/min~~_{i=1}^{k} a_i*b_i

附ac代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn=100005;
int n,m;
int a[maxn];
int b[maxn];
int main()
{
    while(cin>>n>>m)
    {
        for(int i=0;i<n;i++)
            cin>>a[i];
        for(int i=0;i<m;i++)
            cin>>b[i];
        sort(a,a+n);
        sort(b,b+m);
        long long maxans=0,minans=0;
        int cnt=max(n,m);
        if(n<m)
        {
            for(int i=0;i<n;i++)
                {
                    maxans+=(a[n-1-i]*b[m-1-i]);
                    minans+=(a[n-1-i]*b[i]);
                }

        }
        else
        {
             for(int i=0;i<m;i++)
               {
                   maxans+=(a[n-1-i]*b[m-1-i]);
                   minans+=(a[i]*b[m-1-i]);

               }
        }
        cout<<maxans<<" "<<minans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/abandoninged/article/details/80086867
war