GCJ Round 1A 2008 Problem A. Minimum Scalar Product(YY)

12 篇文章 0 订阅

题目链接:https://code.google.com/codejam/contest/32016/dashboard

题意:给两个整型数组,两个数组各项相乘,数组内的数可以任意调换位置,求乘积最小

思路:先对这两个数组进行排序,然后用数组1的最小值乘以数组2的最大值,然后加上数组1的次小值乘以数组2的次大值,依此类推,得到的最终值就是那个内积的最小值。

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
ll a[1000],b[1000];
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    freopen("A-large-practice.in","r",stdin);
    freopen("A-large-practice.out","w",stdout);
    int n,m;
    scanf("%d",&n);
    for(int cas=1;cas<=n;cas++){
        ll ans=0;
        scanf("%d",&m);
        for(int i=1;i<=m;i++) scanf("%I64d",&a[i]);
        for(int i=1;i<=m;i++) scanf("%I64d",&b[i]);
        sort(a+1,a+m+1);
        sort(b+1,b+m+1,cmp);
        for(int i=1;i<=m;i++){
            ans+=a[i]*b[i];
        }
        printf("Case #%d: %I64d\n",cas,ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值