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

相关文章

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

ural 1014. Product of Digits贪心

1014. Product of Digits Time limit: 1.0 second Memory limit: 64 MB Your task is to find the minimal positive integer number  Q so that the product of digits of  Q is exactly equal to  N. Inpu

【Hdu】Minimum Inversion Number(逆序,线段树)

利用线段树在nlogn的时间复杂度内求一段数的逆序。 由于给的序列是由0 ~ n -1组成的,求出初始的逆序之后可以递推出移动之后的逆序数。 #include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const in

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

如何将Product依赖的LibraryModule导出成jar

在Android Studio新建Module时可以选择创建的module是工程module还是Android Library。 或者可以在工程module中的build.gradle文件中将 apply plugin: 'com.android.application'改为apply plugin: 'com.android.library' 同时将applicati

[LeetCode] 238. Product of Array Except Self

题:https://leetcode.com/problems/product-of-array-except-self/description/ 题目 Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all

[LeetCode] 64. Minimum Path Sum

题:https://leetcode.com/problems/minimum-path-sum/description/ 题目 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers

【HDU】 1754 I Hate It 线段树

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 34940    Accepted Submission(s): 13752 Problem Description 很多学校流行一种比较的习惯。

CodeForces 487C Prefix Product Sequence

题意: 构造一个1~n的排列  使得n个前缀积%n是一个0~n-1的排列 思路: 首先确定n一定放最后  要不然会有%n会有多个0  这时n-1位置的前缀积为(n-1)! 接着讨论n  n为合数时只有n=4有解  因为如果n为合数一定可以拆成p*q的形式  明显pq|(n-1)! 然后构造ai=(i+1)*inv[i]  因为(i+1)*inv[i] == (j+1)*inv[j]时一

Path With Maximum Minimum Value

Given a matrix of integers A with R rows and C columns, find the maximum score of a path starting at [0,0] and ending at [R-1,C-1]. The score of a path is the minimum value in that path.  For example