Minimum Scalar Product HRBUST - 1754

2024-04-11 15:18

本文主要是介绍Minimum Scalar Product HRBUST - 1754,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

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

题意:

给你一个数字n,代表有n个数字,后面跟着n个数字,让你求出来sum=a[1]*b[1]+a[2]*b[2]+.......+a[n]*b[n];

其中,n个数字的数据你可以随意排序,求出来sum的最小值;

思路:

直接把数组按照由小到大的顺序进行排序,然后sum=a[ i ] * b[ n-i ]  (i=1,2,3,4,5),求出的sum就是最小值;

需要注意的是:数据范围(long long);

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;long long int a[1010],b[1010];int main()
{int n;while(~scanf("%d",&n)){for(int i=0; i<n; i++)scanf("%lld",&a[i]);for(int i=0; i<n; i++)scanf("%lld",&b[i]);sort(a,a+n);sort(b,b+n);long long int sum=0;for(int i=0; i<n; i++){sum+=a[i]*b[n-i-1];}printf("%lld\n",sum);}return 0;
}

 

这篇关于Minimum Scalar Product HRBUST - 1754的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/894444

相关文章

Leetcode 3195. Find the Minimum Area to Cover All Ones I

Leetcode 3195. Find the Minimum Area to Cover All Ones I 1. 解题思路2. 代码实现 题目链接:3195. Find the Minimum Area to Cover All Ones I 1. 解题思路 这一题还是挺简单的,只要找到所有1所在的元素的上下左右4个边界,作为目标矩形的四个边即可。 2. 代码实现 给出python

【轨迹规划论文整理(1)】UAV轨迹规划的开山之作Minimum Snap Trajectory

【轨迹规划论文整理(1)】UAV轨迹规划的开山之作Minimum Snap Trajectory Generation and Control for Quadrotors 本系列主要是对精读的一些关于无人机、无人车的轨迹搜索论文的整理,包括了论文所拓展的其他一些算法的改进思路。 这是本系列的第一篇文章,也是UAV轨迹规划的开山之作,是所有学习无人机方向的需要精读的第一篇文章,两个作者来自于宾夕

Minimum Absolute Difference in BST问题及解法

问题描述: Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. 示例: nput:1\3/2Output:1Explanation:The minimum absolute differenc

Largest Palindrome Product问题及解法

问题描述: Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. 示例: Input: 2 Output: 987 E

产品为何总是做不好 (六): Product Owner 惯性的行为

2016.9.3,  北京, Ken Fang 回顾这近二十年的敏捷、软件工程的旅程,我的收获是相当的丰富的;尤其是我面对面了许多不同层级的部门领导、数千位的团队成员, 使我能不断的验证了 “人类惯性的行为“ 对团队开发效率与产品质量 (品味)的影响。 1. 当 Product Owner 惯性的行为, 只是希望能在某月某日交付版本。 2. 当 Product Owner 惯性的行为,

【线段树】hdu 1394 Minimum Inversion Number

题意:求Inversion后的最小逆序数 思路:用O(nlogn)复杂度求出最初逆序数后,就可以用O(1)的复杂度分别递推出其他解 线段树功能:update:单点增减 query:区间求和 #include<iostream>#include<algorithm>using namespace std;int sum[10001],x[5001];void pushup(int

【PAT】【Advanced Level】1009. Product of Polynomials (25)

1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This time, you are supposed to find A*B where A and B are two pol

OpenCV之cv::Scalar

在 OpenCV 中,cv::Scalar 是一个模板类,用于表示多通道的值。常用来表示颜色或其他具有多个分量的数据。在图像处理中,cv::Scalar 经常用于指定颜色。 cv::Scalar(255, 255, 255) 具体如何理解,取决于图像的颜色空间: 对于灰度图像(单通道): 只使用第一个值,忽略其他值。因此,cv::Scalar(255) 表示最大亮度值,即白色。 对于 RGB

LeetCode contest 183 5376. 非递增顺序的最小子序列 Minimum Subsequence in Non-Increasing Order

Table of Contents 一、中文版 二、英文版 三、My answer 四、解题报告 一、中文版 给你一个数组 nums,请你从中抽取一个子序列,满足该子序列的元素之和 严格 大于未包含在该子序列中的各元素之和。 如果存在多个解决方案,只需返回 长度最小 的子序列。如果仍然有多个解决方案,则返回 元素之和最大 的子序列。 与子数组不同的地方在于,「数组的子序列」不强

Magento相关产品(Related Product)推荐销售(Up-sells)和交叉销售(Cross-sells)

http://www.hicoogle.com/magento-products-related-product-sale-up-sells-and-cross-selling-cross-sells.html