File tree Expand file tree Collapse file tree 2 files changed +56
-1
lines changed
Expand file tree Collapse file tree 2 files changed +56
-1
lines changed Original file line number Diff line number Diff line change 1+ const fs = require ( "fs" ) ;
2+ const filePath = process . platform === "linux" ? "/dev/stdin" : "./서정우/input.txt" ;
3+ const input = fs
4+ . readFileSync ( filePath )
5+ . toString ( )
6+ . trim ( )
7+ . split ( "\n" )
8+ . map ( ( el ) => el . trim ( ) ) ;
9+ const N = parseInt ( input [ 0 ] ) ;
10+ const A = input [ 1 ] . split ( " " ) . map ( ( x ) => parseInt ( x ) - 1 ) ;
11+
12+ const count = [ 0 , 0 , 0 ] ;
13+ for ( const x of A ) count [ x ] ++ ;
14+
15+ const perms = [
16+ [ 0 , 1 , 2 ] ,
17+ [ 0 , 2 , 1 ] ,
18+ [ 1 , 0 , 2 ] ,
19+ [ 1 , 2 , 0 ] ,
20+ [ 2 , 0 , 1 ] ,
21+ [ 2 , 1 , 0 ] ,
22+ ] ;
23+
24+ let answer = Infinity ;
25+
26+ for ( const perm of perms ) {
27+ const B = [ ] ;
28+ for ( const x of perm ) {
29+ for ( let i = 0 ; i < count [ x ] ; i ++ ) B . push ( x ) ;
30+ }
31+
32+ const wrong = Array . from ( { length : 3 } , ( ) => [ 0 , 0 , 0 ] ) ;
33+ for ( let i = 0 ; i < N ; i ++ ) {
34+ if ( A [ i ] !== B [ i ] ) wrong [ A [ i ] ] [ B [ i ] ] ++ ;
35+ }
36+
37+ let numChanges = 0 ;
38+
39+ for ( let i = 0 ; i < 3 ; i ++ ) {
40+ for ( let j = i + 1 ; j < 3 ; j ++ ) {
41+ const tmp = Math . min ( wrong [ i ] [ j ] , wrong [ j ] [ i ] ) ;
42+ numChanges += tmp ;
43+ wrong [ i ] [ j ] -= tmp ;
44+ wrong [ j ] [ i ] -= tmp ;
45+ }
46+ }
47+
48+ numChanges += 2 * wrong [ 0 ] [ 1 ] ;
49+ numChanges += 2 * wrong [ 0 ] [ 2 ] ;
50+
51+ answer = Math . min ( answer , numChanges ) ;
52+ }
53+
54+ console . log ( answer ) ;
Original file line number Diff line number Diff line change 1- 40 1 100
1+ 8
2+ 1 3 3 2 1 1 3 2
You can’t perform that action at this time.
0 commit comments