整数の配列間で一致する最小コストを見つける


12

それぞれサイズがおよび、の整数Xおよびの2つのソートされた配列を考えます。たとえば、、ます。Ymnm<nX=(1,4)Y=(2,10,11)

我々は、マッチングの各要素ペアのいくつかの方法であると言う元素とのない二つの要素となるように、同じ要素と対にされていない 。マッチングのコストは、ペアの差の絶対値の合計です。Y X YXYXY

たとえば、、場合、ペアを作成できます。このペアのコストはです。ペア作成した場合、コストはます。ペア作成した場合、コストはます。Y = 2 10 11 7 2 11 10 5 + 1 = 6 7 10 11 11 3 + 0 = 3 7 11 11 10 4X=(7,11)Y=(2,10,11)(7,2),(11,10)5+1=6(7,10),(11,11)3+0=3(7,11),(11,10)4+1=5

別の例として、ます。コストでペアを作成できます。ペアコストはです。バツ=71114Y=(2,10,11,18)(7,2),(11,10),(14,11)9(7,10),(11,11),(14,18)7

タスクは、整数および 2つの並べ替えられた配列が与えられると、最小コストの一致を計算するコードを記述することです。XY

テストケース

[1, 4],      [2, 10, 11]     => [[1, 2], [4, 10]]
[7, 11],     [2, 10, 11]     => [[7, 10], [11, 11]]
[7, 11, 14], [2, 10, 11, 18] => [[7, 10], [11, 11], [14, 18]]

XまたはYに値が繰り返されることはありますか?

@ニーモニックいいえ、彼らはしません
Anush

2
明確にするために、最小コストではなく最小コストでマッチングを返します。
ジュゼッペ

1
他の例がありますか?
ディルナン

最小限のコストのマッチングが1つだけあると想定できますか?
ディルナン

回答:


4

Brachylog、16バイト

∧≜I&pᵐz₀.-ᵐȧᵐ+I∧

オンラインでお試しください!

説明

∧
 ≜I                   Take an integer I = 0, 1, -1, 2, -2, 3, -3, …
   &pᵐ                Permute each sublist
      z₀.             Zip the sublists together. The result of the zip is the output
         -ᵐȧᵐ         Absolute differences of each pair
             +I       The sum of these differences must be I
               ∧

I最初に整数に統一するので、の小さい値Iから大きい値までを試してみますI。つまり、最初に成功するのは、絶対差が最小のペアリングに必ずなります。


4

ゼリー15 14 12 11バイト

Œ!ż€IASƊÞḢṁ

オンラインでお試しください!

  • ジョナサン・アランのおかげで-1バイト
  • Xcoder氏のおかげで-1バイト
  • 匿名エディターのおかげで-2バイト

強引な。入力をとして、次にXとして受け取ります。Yバツ

Œ!ż€IASƊÞḢṁ
Œ!                 All permutations of Y.
  ż€               Zip each of the permutations with X.

       ƊÞ          Sort by:
    I              Difference of each pair.
     A             Absolute value.
      S            Sum.
         Ḣ         Take the first matching.
          ṁ        Mold the result like X. Keeps only values up to the length 
                   of X which removes unpaired values from Y.

L}の代わりに働きますか⁹L¤
ミスターXcoder

@ Mr.Xcoderはい、ありがとう!
ディルナン

ÐṂḢ-> ÞḢバイトを保存します。
ジョナサンアラン

3

Haskell、78 77 76バイト

import Data.Lists
(argmin(sum.map(abs.uncurry(-))).).(.permutations).map.zip

TIOにはないData.Listsので、リンクはありません。

基本的に@dylnanの答えに見られるものと同じアルゴリズム。

編集:@BMOのおかげで-1バイト。



2

J、24バイト

[,.[-[:,@:(0{]#~1>])"1-/

オンラインでお試しください!

説明/デモ:

二項動詞、 x f y

-/ 違いを見つける

 7 11 14 -/ 2 10 11 18
 5 _3 _4 _11
 9  1  0  _7
12  4  3  _4

(0{]#~1>])"1 各行について、正でない値のみを保持し、最初の値を取得します。

   7 11 14 ([:(0{]#~1>])"1-/) 2 10 11 18
_3 0 _4

[:,@: リストを平坦化します(左の引数の形状に一致させるため)

[-分を引きます。左引数との違い

    7 11 14 ([-[:,@:(0{]#~1>])"1-/) 2 10 11 18
10
11
18

[,. それらを左の引数につなぎます:

   7 11 14 ([,.[-[:,@:(0{]#~1>])"1-/) 2 10 11 18
 7 10
11 11
14 18



1

Pyth、16バイト

hosaMNCM*.pQ.cEl

ここでオンライン試すか、ここですべてのテストケースを一度に検証します

hosaMNCM*.pQ.cEl   Implicit: Q=evaluated 1st input, E=evaluated 2nd input
               l   Length of 1st input (trailing Q inferred)
            .cE    All combinations of 2nd input of the above length
         .pQ       All permutations of 1st input
        *          Cartesian product
      CM           Transpose each of the above
 o                 Order the above using:
   aMN               Take the absolute difference of each pair
  s                  ... and take their sum
h                  Take the first element of the sorted list, implicit print

1

MATL、16バイト

yn&Y@yy&1ZP&X<Y)

入力はX、その後、Y

マッチングは、各ペアの最初の値(つまり、X)が最初の行に、各ペアの2番目の値が2行目に出力されます。

オンラインでお試しください!または、すべてのテストケースを確認します

説明

y       % Implicit inputs: X, Y. Duplicate from below
        % STACK: [7 11], [2 10 11], [7 11]
n       % Number of elements
        % STACK: [7 11], [2 10 11], 2
&Y@     % Variations without repetition
        % STACK: [7 11], [2 10; 2 11; 10 2; 10 11; 11 2; 11 10]
yy      % Duplicate top two elements
        % STACK: [7 11], [2 10; ...; 11 10], [7 11], [2 10; ...; 11 10]
&1ZP    % Compute cityblock distance between rows of the two input matrices
        % STACK: [7 11], [2 10;...; 11 10], [6 5 12 3 13 5]
&X<     % Argmin (first index of occurrences of the minimum)
        % STACK: [7 11], [2 10; 2 11; 10 2; 10 11; 11 2; 11 10], 4
Y)      % Row indexing. Implicit display
        % STACK: [7 11], 10 11]

1

ゼリー、(10?)12 バイト

Yの要素だけが必要な場合は10バイト(コメントを参照)-まだ仕様で許可されているかどうかはわかりません(他の回答が既にこの詳細を実装しているので、そうでないかもしれません)。
これは、末尾を削除する⁸żことで実現できます。

Lœc@ạS¥Þ⁸Ḣ⁸ż

左側のXと右側のYを受け入れるダイアディックリンク。
œc⁹L¤ạS¥ÞḢż@および10バイトœc⁹L¤ạS¥ÞḢは、左側のYと右側のXで同じことを行います)。

オンラインでお試しください!

どうやって?

Lœc@ạS¥Þ⁸Ḣ⁸ż - Link: sorted list of integers X, sorted list of integers Y
L            - length
   @         - with swapped arguments:
 œc          -   combinations (chosen as if picked left-to-right
             -      e.g. [2,5,7,9] œc 2 -> [[2,5],[2,7],[2,9],[5,7],[5,9],[7,9]] )
        ⁸    - chain's left argument (to be on right of the following...)
       Þ     -   sort by:
      ¥      -     last two links as a dyad:
    ạ        -       absolute difference (vectorises)
     S       -       sum
         Ḣ   - head (since sorted this is just the first minimal choices from Y)
          ⁸  - chain's left argument
           ż - zip with (the chosen Y elements)

1

JavaScript(ES7)、100バイト

ここで新しい; ヒント/修正をいただければ幸いです!以前の試みでは、NaN値を含む配列のソートの複雑さを見逃していたので、今回は何も見逃していないことを願っています。

(x,y,q=Infinity)=>y.map((u,j)=>(p=0,s=x.map((t,i)=>(u=y[i+j],p+=(t-u)**2,[t,u])),p)<q&&(q=p,r=s))&&r

2つの引数をそれぞれXYとして期待します。 オンラインでお試しください!

@Arnauldのソリューションに似ているように見える

説明

XYがソートされているという事実に依存し、すべてのペアがXの要素の順序を保持するように配置されている場合、配置内のすべてのY要素もその順序を保持する最小コスト一致のソリューションが存在します。

(x, y, q = Infinity) =>
    y.map((u, j) =>                   // iterate over indices of y
        (
            p=0,
            s=x.map((t, i) => (       // map each element of x to...
                    u = y[i+j],       // an element of y offset by j
                    p += (t-u)**2,    // accumulate the square of the difference
                    [t, u]            // new element of s
                )),
            p
        ) < q                         // if accumulated cost less than previous cost...
                                      // (if p is NaN, any comparison will return false and short circuit)
        && (q=p, r=s)                 // save cost, pair values respectively
    ) && r                            // return lowest-cost pairs
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.