합계가 가장 작은 합동 목록 생성


24

두 개의 목록이 A있고 B길이가 같으면 동일하고 A비교 하는 요소가에서 같음을 비교 B합니다.

다시 말해, 두 개의 유효한 지수 xy:

  • 만약 A[x] = A[y]다음, B[x] = B[y].
  • 만약 A[x] != A[y]다음, B[x] != B[y].

예를 들어, 목록 [1, 2, 1, 4, 5][0, 1, 0, 2, 3]합동입니다.

작업

의 비어 있지 않은 목록을 감안할 때 음이 아닌 정수 A의 새 목록 반환 음이 아닌 정수 B에 합동 등을 A의 정수의 합을 최소화하면서 B.

가능한 많은 유효한 출력이있을 수 있습니다. 예를 들어 list [12, 700, 3]에서 순열은 [0, 1, 2]유효한 출력으로 간주됩니다.

테스트 사례

Format:
input ->
one possible valid output

[1 2 1 4 5] ->
[0 1 0 2 3] (this is the example given above)

[3 2 2 1 5 7 2] ->
[1 0 0 2 3 4 0]

[8 8 8 8 8] ->
[0 0 0 0 0]

[2] ->
[0]

[8 6 8 4 6 8 2 4 6 8 0 2 4 6 8] ->
[0 1 0 2 1 0 3 2 1 0 4 3 2 1 0]

[14 1] ->
[1 0]

[19 6 4 9 14 17 10 9 6 14 8 14 6 15] ->
[8 0 3 2 1 7 5 2 0 1 4 1 0 6]

[15] ->
[0]

[1 18 4 8 6 19 12 17 6 13 7 6 8 1 6] ->
[1 8 3 2 0 9 5 7 0 6 4 0 2 1 0]

[9 10 11 9 7 11 16 17 11 8 7] ->
[2 4 0 2 1 0 5 6 0 3 1]

[1 3 16 19 14] ->
[0 1 3 4 2]

[18 8] ->
[1 0]

[13 4 9 6] ->
[3 0 2 1]

[16 16 18 6 12 10 4 6] ->
[1 1 5 0 4 3 2 0]

[11 18] ->
[0 1]

[14 18 18 11 9 8 13 3 3 4] ->
[7 1 1 5 4 3 6 0 0 2]

[20 19 1 1 13] ->
[3 2 0 0 1]

[12] ->
[0]

[1 14 20 4 18 15 19] ->
[0 2 6 1 4 3 5]

[13 18 20] ->
[0 1 2]

[9 1 12 2] ->
[2 0 3 1]

[15 11 2 9 10 19 17 10 19 11 16 5 13 2] ->
[7 2 0 5 1 3 9 1 3 2 8 4 6 0]

[5 4 2 2 19 14 18 11 3 12 20 14 2 19 7] ->
[5 4 0 0 2 1 9 7 3 8 10 1 0 2 6]

[9 11 13 13 13 12 17 8 4] ->
[3 4 0 0 0 5 6 2 1]

[10 14 16 17 7 4 3] ->
[3 4 5 6 2 1 0]

[2 4 8 7 8 19 16 11 10 19 4 7 8] ->
[4 1 0 2 0 3 7 6 5 3 1 2 0]

[15 17 20 18 20 13 6 10 4 19 9 15 18 17 5] ->
[0 1 3 2 3 9 6 8 4 10 7 0 2 1 5]

[15 14 4 5 5 5 3 3 19 12 4] ->
[5 4 2 0 0 0 1 1 6 3 2]

[7 12] ->
[0 1]

[18 5 18 2 5 20 8 8] ->
[2 0 2 3 0 4 1 1]

[4 6 10 7 3 1] ->
[2 3 5 4 1 0]

[5] ->
[0]

[6 12 14 18] ->
[0 1 2 3]

[7 15 13 3 4 7 20] ->
[0 4 3 1 2 0 5]

[10 15 19 14] ->
[0 2 3 1]

[14] ->
[0]

[19 10 20 12 17 3 6 16] ->
[6 2 7 3 5 0 1 4]

[9 4 7 18 18 15 3] ->
[4 2 3 0 0 5 1]

[7 4 13 7] ->
[0 1 2 0]

[19 1 10 3 1] ->
[3 0 2 1 0]

[8 14 20 4] ->
[1 2 3 0]

[17 20 18 11 1 15 7 2] ->
[5 7 6 3 0 4 2 1]

[11 4 3 17] ->
[2 1 0 3]

[1 9 15 1 20 8 6] ->
[0 3 4 0 5 2 1]

[16 13 10] ->
[2 1 0]

[17 20 20 12 19 10 19 7 8 5 12 19] ->
[7 2 2 1 0 6 0 4 5 3 1 0]

[18 11] ->
[1 0]

[2 16 7 12 10 18 4 14 14 7 15 4 8 3 14] ->
[3 9 2 7 6 10 1 0 0 2 8 1 5 4 0]

[5 7 2 2 16 14 7 7 18 19 16] ->
[3 0 1 1 2 4 0 0 5 6 2]

[8 6 17 5 10 2 14] ->
[3 2 6 1 4 0 5]

이것은 이므로 가장 짧은 유효한 제출 (바이트로 계산)이 이깁니다.


답변:



4

Pyth- 12 11 10 바이트

XQ_o/QN{QU

테스트 스위트 .


1
젠장, 그것은 빠르다! 나는 단지 우리에게 무엇을 요청했는지 알아낼 수 있었다!
Shaggy

로 바이트를 저장할 수 있습니다 mx_o/QN{Q.

4

Japt , 11 바이트

£â ñ@è¦XÃbX

온라인으로 테스트하십시오!

설명

 £   â ñ@  è¦ Xà bX
UmX{Uâ ñX{Uè!=X} bX}   Ungolfed
                       Implicit: U = input array
UmX{               }   Map each item X in the input to:
    Uâ                   Take the unique items of U.
       ñX{     }         Sort each item X in this by
          Uè!=X            how many items in U are not equal to X.
                         This sorts the items that occur most to the front of the list.
                 bX      Return the index of X in this list.
                       Implicit: output result of last expression

2

J , 11 바이트

i.~~.\:#/.~

온라인으로 사용해보십시오!

설명

i.~~.\:#/.~  Input: array A
       #/.~  Frequency of each unique character, sorted by first appearance
   ~.        Unique, sorted by first appearance
     \:      Sort down the uniques using their frequencies
i.~          First index in that for each element of A


2

하스켈 , 93 91 85 바이트

import Data.List
f a=[i|x<-a,(i,y:_)<-zip[0..]$sortOn((0-).length)$group$sort a,x==y]

온라인으로 사용해보십시오!

편집 : 6 바이트를 벗어난 @Laikoni에게 감사드립니다!

아주 짧지는 않지만 다른 것은 생각할 수 없습니다. 아이디어는 배열 ( x<-a) 을 반복 하고 튜플 목록 ( (i,y:_)<-... ,x==y) 에서 조회를 수행하여 얼마나 일반적인 지에 따라 입력의 각 고유 요소에 음이 아닌 정수를 할당하는 것입니다. 해당 튜플 목록은 sort입력 을 먼저 group동일한 요소의 하위 목록으로 입력 한 다음 하위 목록의 길이별로 목록을 정렬 한 다음 ( sortOn((0-).length)길이는 "내림차순"순서로 정렬되지 않음) 마지막으로 무한 목록으로 압축하여 생성됩니다. 패턴 일치를 사용하여 하위 목록에서 실제 요소를로 추출합니다 y.


1
패턴을 일치 (i,y:_)시키고 head<$>파트를 삭제 하고 괄호를로 대체 할 수 있습니다 $.
Laikoni


1

CJam, 17 14 바이트

Peter Taylor 덕분에 -3 바이트

이것은 테스트 케이스를 생성하는 데 사용한 프로그램의 골프 버전입니다.

{_$e`$W%1f=f#}

이것은 스택 위에 배열로 입력을 기대하고 스택 위에 배열을 출력하는 익명 블록입니다.

설명:

{_$e`$W%1f=f#} Stack:                  [1 2 1 4 5]
 _             Duplicate:              [1 2 1 4 5] [1 2 1 4 5]
  $            Sort:                   [1 2 1 4 5] [1 1 2 4 5]
   e`          Run-length encode:      [1 2 1 4 5] [[2 1] [1 2] [1 4] [1 5]]
     $         Sort lexicographically: [1 2 1 4 5] [[1 2] [1 4] [1 5] [2 1]]
      W%       Reverse:                [1 2 1 4 5] [[2 1] [1 5] [1 4] [1 2]]
        1f=    Second element of each: [1 2 1 4 5] [1 5 4 2]
           f#  Vectorized indexing:    [0 3 0 2 1]

세 개의 바이트 만 분리하면 역순으로 정렬 할 수 있습니다 $W%.
피터 테일러

@PeterTaylor 아, 배열의 사전 비교를 잊어 버리는 것은 계속됩니다. 감사.
Esolanging 과일

1

TI-BASIC, 66 바이트

Ans+max(Ans+1)seq(sum(Ans=Ans(I)),I,1,dim(Ans→A
cumSum(Ans→B
SortD(∟A,∟B
cumSum(0≠ΔList(augment({0},∟A→A
SortA(∟B,∟A
∟A-1

설명

seq(sum(Ans=Ans(I)),I,1,dim(Ans    Calculates the frequency of each element of Ans.
                                   Comparing a value to a list returns a list of booleans,
                                   so taking the sum will produce the number of matches.

Ans+max(Ans+1)                     Multiplies each frequency by one more than the max element,
                                   then adds each original value.
                                   This ensures that identical values with the same frequency
                                   will be grouped together when sorting.
                                   Additionally, all resulting values will be positive.

→A                                 Stores to ∟A.

cumSum(Ans→B                       Stores the prefix sum of the result into ∟B.
                                   Since ∟A has only positive values, ∟B is guaranteed
                                   to be strictly increasing.

SortD(∟A,∟B                        Sort ∟A in descending order (by frequency), grouping
                                   identical values together. Also, dependently sort ∟B
                                   so the original ordering can be restored.

       0≠ΔList(augment({0},∟A      Prepends a 0 to ∟A and compares each consecutive difference
                                   to 0. This places a 1 at each element that is different
                                   from the previous element, and 0 everywhere else.
                                   The first element is never 0, so it is considered different.

cumSum(                      →A    Takes the prefix sum of this list and stores to ∟A.
                                   Since there is a 1 at each element with a new value,
                                   the running sum will increase by 1 at each value change.
                                   As a result, we've created a unique mapping.

SortA(∟B,∟A                        Sorts ∟B in ascending order with ∟A as a dependent,
                                   restoring the original element ordering.

∟A-1                               Since we started counting up at 1 instead of 0,
                                   subtract 1 from each element in ∟A and return it.


1

자바 스크립트 (ES6), 91 바이트

빈도별로 정렬 된 고유 값 목록을 사용합니다.

x=>x.map(x=>Object.keys(C).sort((a,b)=>C[b]-C[a]).indexOf(x+''),C={},x.map(v=>C[v]=-~C[v]))

테스트

var F=
x=>x.map(x=>Object.keys(C).sort((a,b)=>C[b]-C[a]).indexOf(x+''),C={},x.map(v=>C[v]=-~C[v]))

Test=`[1 2 1 4 5] -> [0 1 0 2 3]
[3 2 2 1 5 7 2] -> [1 0 0 2 3 4 0]
[8 8 8 8 8] -> [0 0 0 0 0]
[2] -> [0]
[8 6 8 4 6 8 2 4 6 8 0 2 4 6 8] -> [0 1 0 2 1 0 3 2 1 0 4 3 2 1 0]
[14 1] -> [1 0]
[19 6 4 9 14 17 10 9 6 14 8 14 6 15] -> [8 0 3 2 1 7 5 2 0 1 4 1 0 6]
[15] -> [0]
[1 18 4 8 6 19 12 17 6 13 7 6 8 1 6] -> [1 8 3 2 0 9 5 7 0 6 4 0 2 1 0]
[9 10 11 9 7 11 16 17 11 8 7] -> [2 4 0 2 1 0 5 6 0 3 1]
[1 3 16 19 14] -> [0 1 3 4 2]
[18 8] -> [1 0]
[13 4 9 6] -> [3 0 2 1]
[16 16 18 6 12 10 4 6] -> [1 1 5 0 4 3 2 0]
[11 18] -> [0 1]
[14 18 18 11 9 8 13 3 3 4] -> [7 1 1 5 4 3 6 0 0 2]
[20 19 1 1 13] -> [3 2 0 0 1]
[12] -> [0]
[1 14 20 4 18 15 19] -> [0 2 6 1 4 3 5]
[13 18 20] -> [0 1 2]
[9 1 12 2] -> [2 0 3 1]
[15 11 2 9 10 19 17 10 19 11 16 5 13 2] -> [7 2 0 5 1 3 9 1 3 2 8 4 6 0]
[5 4 2 2 19 14 18 11 3 12 20 14 2 19 7] -> [5 4 0 0 2 1 9 7 3 8 10 1 0 2 6]
[9 11 13 13 13 12 17 8 4] -> [3 4 0 0 0 5 6 2 1]
[10 14 16 17 7 4 3] -> [3 4 5 6 2 1 0]
[2 4 8 7 8 19 16 11 10 19 4 7 8] -> [4 1 0 2 0 3 7 6 5 3 1 2 0]
[15 17 20 18 20 13 6 10 4 19 9 15 18 17 5] -> [0 1 3 2 3 9 6 8 4 10 7 0 2 1 5]
[15 14 4 5 5 5 3 3 19 12 4] -> [5 4 2 0 0 0 1 1 6 3 2]
[7 12] -> [0 1]
[18 5 18 2 5 20 8 8] -> [2 0 2 3 0 4 1 1]
[4 6 10 7 3 1] -> [2 3 5 4 1 0]
[5] -> [0]
[6 12 14 18] -> [0 1 2 3]
[7 15 13 3 4 7 20] -> [0 4 3 1 2 0 5]
[10 15 19 14] -> [0 2 3 1]
[14] -> [0]
[19 10 20 12 17 3 6 16] -> [6 2 7 3 5 0 1 4]
[9 4 7 18 18 15 3] -> [4 2 3 0 0 5 1]
[7 4 13 7] -> [0 1 2 0]
[19 1 10 3 1] -> [3 0 2 1 0]
[8 14 20 4] -> [1 2 3 0]
[17 20 18 11 1 15 7 2] -> [5 7 6 3 0 4 2 1]
[11 4 3 17] -> [2 1 0 3]
[1 9 15 1 20 8 6] -> [0 3 4 0 5 2 1]
[16 13 10] -> [2 1 0]
[17 20 20 12 19 10 19 7 8 5 12 19] -> [7 2 2 1 0 6 0 4 5 3 1 0]
[18 11] -> [1 0]
[2 16 7 12 10 18 4 14 14 7 15 4 8 3 14] -> [3 9 2 7 6 10 1 0 0 2 8 1 5 4 0]
[5 7 2 2 16 14 7 7 18 19 16] -> [3 0 1 1 2 4 0 0 5 6 2]
[8 6 17 5 10 2 14] -> [3 2 6 1 4 0 5]`

Test.split(`\n`).forEach(row => {
  row=row.match(/\d+/g)
  var nv = row.length/2
  var tc = row.slice(0,nv)
  var exp = row.slice(nv)
  var xsum = eval(exp.join`+`)
  var result = F(tc)
  var rsum = eval(result.join`+`)
  var ok = xsum == rsum
  console.log('Test ' + (ok ? 'OK':'KO')
  + '\nInput [' + tc 
  + ']\nExpected (sum ' + xsum + ') ['+ exp 
  + ']\nResult (sum ' + rsum + ') [' + result + ']')
  
})



당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.