最小标量积


16

最小标量积

此代码高尔夫球问题的灵感来自Google的代码堵塞竞赛。问题的前提是,在输入两个长度不同的向量的情况下,找到最小可能的标量。可以使用以下公式找到标量:

x1 * y1 + x2 * y2 + ... + xn * yn

然而,问题在于,取决于输入情况下数字的顺序(如下所示),可以找到标量的多个值。您的目标是通过将输入用例编号插入方程式并求解来确定最小可能的标量整数解。您只能在输入中使用每个数字一次,并且必须使用所有数字。

请允许我提供一个带有以下向量的示例。

输入值

3
1 3 -5
-2 4 1

输出量

-25

该行上的第一个整数表示每个向量中的数字n。在这种情况下,每个向量有三个数字。

数字n可能会随每个测试用例而变化,但是总会有两个向量。

在示例输入中,最小标量积为-25。

(-5 * 4) + (1 * 1) + (3 * -2) = 25

规则

  • 您只能在两个向量中使用每个整数一次。
  • 您必须在向量中使用所有整数。
  • 您的输出必须仅包含最终产品
  • 我将以最少的语言选择使用最少语言的解决方案,该解决方案应使用任何语言,都遵循上面列出的所有规范!

提示:您无需强求这个问题,除非它会使您的代码更短。找到最小跨度标量有一种特定的方法:)。


我真的不想破坏任何人,所以除非您已经知道答案,否则不要打开它。这是众所周知的,很有趣。en.m.wikipedia.org/wiki/Rearrangement_inequality
骄傲的haskeller

Answers:


8

果冻,6个字节

ṢṚ×Ṣ}S

在线尝试!

使用蛮力同样短:

Œ!×S€Ṃ

怎么运行的

ṢṚ×Ṣ}S  Main link. Arguments: u (vector), v (vector)

Ṣ       Sort the components of u.
 Ṛ      Reverse.
   Ṣ}   Sort the components of v.
  ×     Multiply the results, element by element.
     S  Compute the sum of the products.


5

APL,15个字节

{+/⍺[⍒⍺]×⍵[⍋⍵]}

这是一个二元函数,它接受左右数组,并返回一个整数。它使用与我的Julia 答案相同的方法:排序数组的点积,一个降序,一个升序。

在这里尝试


5

MATL,6个字节

码:

SiSP*s

我的第一个MATL回答:)

说明:

S       # Sort the first array
 iS     # Take the second array and sort it
   P    # Flip the array
    *   # Multiply both arrays with each other
     s  # Sum of the result

在线尝试!


1
看到这个我很高兴!:-)
Luis Mendo

4

Mathematica,30个 17字节

墨菲-13个字节

Sort@#.-Sort@-#2&

函数,输入为vector1(list),vector2(list)几个修订:

Plus@@(Sort@#*Reverse@Sort@#2)&(*me*)
Total[Sort@#*Reverse@Sort@#2]& 
Sort@#.Reverse@Sort@#2&        (*alephalpha*)
Sort@#.Sort[#2,#>#2&]&         (*murphy*)
Sort@#.SortBy[#2,-#&]          (*me*)
Sort@#.-Sort@-#2&              (*murphy*)

聪明的解决方案!
baseman101 '16

2
Sort@#.Reverse@Sort@#2&
alephalpha

Sort@#.Sort[#2,#>#2&]&
墨菲

1
Sort@#.-Sort@-#2&
墨菲(Murphy)2016年

或为您的解决方案1,Sort@#.SortBy[#2,-#&]
CalculatorFeline


2

朱莉娅,32 25字节

x->y->-sort(-x)⋅sort(y)

这是一个匿名函数,它接受两个数组并返回一个整数。要调用它,请将其分配给变量并执行f(x)(y)

对于输入xy,我们只需计算以相反顺序排序的xy排序的点积。通过取反所有值,进行排序然后再次取反,可以得到反向排序的x

感谢Dennis,节省了7个字节!


2

Javascript ES6,69个字节

a=>b=>a.sort((x,y)=>x-y).map((x,y)=>i+=b.sort((x,y)=>y-x)[y]*x,i=0)|i

哇,这太久了。


我认为尝试重用sort函数会花费3个字节。
Neil

我打高尔夫球更多。更好?
Mama Fun Roll

您可能可以用保存一个字节,|i而不是&&i
ETHproductions

Thx @ETHproductions
Mama Fun Roll

是的,这就是我的想法。
尼尔



1

Python,139个字节

def mdp(n, a, b):
    a = list(reversed(sorted(a)))
    b = sorted(b)
    res = sum([a[i] * b[i] for i in range(len(a))])
    return res

1
您可以通过删除等号旁边的空格来节省一些字节,例如b = sorted(b)变成b=sorted(b)(保存了2个字节)。您还可以通过用分号将多个语句放在同一行上,例如a=list(reversed(sorted(a)));b=sorted(b);res=0
charredgrass

@charredgrass我是新来的。保存每个可能的字节有什么需要?我试图使其可读。
rebelliard

欢迎来到PPCG!这个问题是一场代码高尔夫竞赛,其目标是编写代码以尽可能少的字节来完成挑战,这通常意味着可读性较低的代码。
charredgrass

@charredgrass知道了!
rebelliard

2
更短:lambda a,b,s=sorted:sum(x*y for x,y in zip(s(a)[::-1],s(b)))。我们不需要命名函数提交(因此未命名的lambda是有效的),并且该n参数是不必要的(许多其他提交完全省略了它)。
Mego

1

C ++,124个字节

#include<algorithm>
int m(int*a,int*b,int n){std::sort(a,a+n);std::sort(b,b+n);int r=0;while(--n>=0)r+=a[n]**b++;return r;}

松散

#include<algorithm>
int m(int*a,int*b,int n){
 std::sort(a,a+n);
 std::sort(b,b+n);
 int r=0;
 while(--n>=0)
  r+=a[n]*(*b++);
return r;
}

起初,我使用std::greater<int>()了排序,b但求和总和的顺序更容易。


1

Haskell,59个字节

import Data.List
v?u=sum$zipWith(*)(sort v)$reverse$sort u

0

返回 29字节

[{␆␃}\{␆}␄␅[¤¥][×␌]#}␁[¤][+]#]

Try it here.

更换任何 ␆␃␄␇其为无法打印的副本。

留下的匿名lambda结果在stack2上。用法:

""{1 3 0 5-}""{0 2- 4 1}[{␆␃}\{␆}␄␅[¤¥][×␌]#}␁[¤][+]#]!

说明

[                                 ]  lambda
 {␆␃}                              sort and reverse first stack
       \{␆}                         sort second stack
            ␄␅                     transpose and flatten
               [  ][  ]#             while loop
                ¤¥                     check if 2 items exist in stack
                    ×                  if so, multiply top 2 items
                     ␌                 and push to stack2
                        }␁          switch to stack2
                           [¤][+]#   sum stack2

0

J,14个字节

+/@(*|.)&(/:~)

使用与其他相同的原理。

说明

+/@(*|.)&(/:~)  Input: x on LHS and y on RHS
        &(/:~)  Sort both x and y
     |.         Reverse the sorted y
    *           Multiply the sorted x and reversed sorted y elementwise
+/@             Reduce the products using addition and return
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.