Minimum Scalar Product (白书)

标签: bai shu / gui lv
6人阅读 评论(0) 收藏 举报
分类:

You are given two vectors v1=(x1,x2,...,xn) and v2=(y1,y2,...,yn). The scalar product of these vectors is a single number, calculated as x1y1+x2y2+...+xnyn.

Suppose you are allowed to permute the coordinates of each vector as you wish. Choose two permutations such that the scalar product of your two new vectors is the smallest possible, and output that minimum scalar product.

Input

There are multiple test cases.

For each test case, the first line contains integer number n. The next two lines contain n integers each (1<=n<=800), giving the coordinates of v1 and v2 respectively.

 Process to the end of file.

Output

For each test case, output a line X, where X is the minimum scalar product of all permutations of the two given vectors.

Sample Input
3
1 3 -5
-2 4 1
5
1 2 3 4 5
1 0 1 0 1

Sample Output

-25
6

求最小的内积:

首先我们将v1升序排,v2按降序排,最后跟据内积的规则乘下即可。

有个坑点就是:数据必须为long long

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

typedef long long LL;

const int maxn=805;

LL v1[maxn];
LL v2[maxn];

int cmp(const int a,const int b)
{
	return a>b;
}

int main()
{
	int n;
	while(~scanf("%d",&n)){
		for(int i=0;i<n;i++){
			scanf("%lld",&v1[i]);
		}
		for(int i=0;i<n;i++){
			scanf("%lld",&v2[i]);
		}
		sort(v1,v1+n,cmp);
		sort(v2,v2+n);
		LL res=0;
		for(int i=0;i<n;i++){
			res+=v1[i]*v2[i];
		}
		printf("%lld\n",res);
	}
	return 0;
}

查看评论

Minimum Scalar Product

Problem You are given two vectors v1=(x1,x2,...,xn) and v2=(y1,y2,...,yn). The scalar product of th...
  • u012259209
  • u012259209
  • 2014-04-19 11:08:37
  • 466

GCJ 2008 Round 1A Minimum Scalar Product

GCJ(google code jam)上的水题。下周二有比赛,来熟悉熟悉。 有两个向量V1=(x1,x2……xn)和v2=(y1,y2……yn),允许任意交换v1和v2各自的分量顺序,请计算v1和v...
  • murmured
  • murmured
  • 2014-03-06 23:52:19
  • 971

Minimum Scalar product

有两个向量v1=(x1,x2,x3,,,xn)和v2=( y1,y2,,,,yn),允许任意交换v1和v2各自的分量的顺序。请计算v1和v2的内积x1y1+,,,,+xnyn的最小值。 限制条件 ...
  • qq_qingtian
  • qq_qingtian
  • 2015-04-30 12:11:01
  • 537

TOJ 3508.Minimum Scalar Product

题目链接:http://acm.tju.edu.cn/toj/showp3508.html 3508.   Minimum Scalar Product Time Limit: 1...
  • muyujinxi
  • muyujinxi
  • 2016-08-16 23:12:26
  • 88

Minimum Scalar Product(GCJ 2008 Round1A A)数学问题+贪心

来自《挑战程序设计竞赛》 1.题目原文 https://code.google.com/codejam/contest/32016/dashboard#s=p0 Problem You are g...
  • qq_33929112
  • qq_33929112
  • 2016-09-24 12:35:44
  • 247

2008 Round1AA Minimum Scalar Product

用贪心法能实现,一个向量正序,一个向量逆序,相乘相加就能解决PS:用__int64时,在printf,scanf里面的格式化字符应该是%I64d,虽然%d也能用,但是如果数值范围过大就会溢出。 #in...
  • q547550831
  • q547550831
  • 2015-10-25 21:20:36
  • 14718

矩阵乘法和矩阵的逆的意义

注:目前开通个人网站朝思录,之后的博文将在上面更新,CSDN博客会滞后一点 矩阵乘法的意义 考虑最基本的矩阵乘法公式: b=Ax(1)\boldsymbol b = \boldsymbol ...
  • u014792304
  • u014792304
  • 2017-06-09 21:18:03
  • 314

scalarprod.pdf

  • 2013年03月25日 00:37
  • 251KB
  • 下载

(code jam)Problem A. Minimum Scalar Product

Problem You are given two vectors v1=(x1,x2,...,xn) and v2=(y1,y2,...,yn). The scalar product of th...
  • modiz
  • modiz
  • 2014-03-13 19:55:28
  • 684

Code Jam - Minimum Scalar Product for Python

这是Code Jam 2008, Round 1A的试题
  • u011333059
  • u011333059
  • 2014-07-29 15:36:32
  • 439
    个人资料
    持之以恒
    等级:
    访问量: 6123
    积分: 2677
    排名: 1万+
    文章存档
    最新评论