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

来源:互联网 发布:手机淘宝宝贝分类链接 编辑:程序博客网 时间:2024/04/30 15:06

题目链接: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
原创粉丝点击