1
+ package main .java .codechef ;
2
+
3
+ import java .util .Arrays ;
4
+ import java .util .Random ;
5
+
6
+ public class BruteForceSolver {
7
+ private final int [][][] w ;
8
+ private final int [] starts ;
9
+ private final int [] ends ;
10
+ private final int [][] cycleLengths ;
11
+
12
+ public BruteForceSolver (int n , int [][] weights , final int [] cycleWeights , final int [] starts , int [] ends ) {
13
+ w = getWeights (weights , n );
14
+ cycleLengths = getCycleLengths (n , w , cycleWeights , starts , ends );
15
+ this .starts = starts ;
16
+ this .ends = ends ;
17
+ }
18
+
19
+ public static void main (String [] args ) {
20
+ final Random random = new Random ();
21
+ int n = random .nextInt (20 ) + 1 ;
22
+ int a [] = new int [n ];
23
+ int [][] weights = new int [n ][];
24
+ int [] starts = new int [n ];
25
+ int [] ends = new int [n ];
26
+ int [] cycleWeights = new int [n ];
27
+ for (int i = 0 ; i < n ; i ++) {
28
+ a [i ] = random .nextInt (20 ) + 1 ;
29
+ weights [i ] = new int [a [i ]];
30
+ for (int j = 0 ; j < a [i ]; j ++) {
31
+ weights [i ][j ] = random .nextInt (20 ) + 1 ;
32
+ }
33
+ starts [i ] = random .nextInt (a [i ]);
34
+ ends [i ] = random .nextInt (a [i ]);
35
+ cycleWeights [i ] = random .nextInt (20 ) + 1 ;
36
+ }
37
+ }
38
+
39
+ public int solve (int v1 , int v2 , int c1 , int c2 ) {
40
+ if (c1 > c2 ) {
41
+ int temp = v1 ;
42
+ v1 = v2 ;
43
+ v2 = temp ;
44
+ temp = c1 ;
45
+ c1 = c2 ;
46
+ c2 = temp ;
47
+ }
48
+ final int cycleDistance1 = Math .abs (cycleLengths [c1 ][0 ] - cycleLengths [c2 ][0 ]),
49
+ excess = findDistance (starts [c1 ], ends [c1 ], w [c1 ]),
50
+ first1 = findDistance (v1 , ends [c1 ], w [c1 ]),
51
+ second1 = findDistance (starts [c2 ], v2 , w [c2 ]);
52
+ final int cycleDistance2 = Math .abs (cycleLengths [c1 ][1 ] - cycleLengths [c2 ][1 ]),
53
+ first2 = findDistance (v1 , starts [c1 ], w [c1 ]),
54
+ second2 = findDistance (ends [c2 ], v2 , w [c2 ]);
55
+ final int possibilityOne = cycleDistance1 - excess + first1 + second1 ;
56
+ final int possibilityTwo = cycleDistance2 - excess + first2 + second2 ;
57
+ return Math .min (possibilityOne , possibilityTwo );
58
+ }
59
+
60
+ public int [][] getCycleLengths (final int n , final int [][][] w , final int [] cycleWeights , final int [] starts , final int [] ends ) {
61
+ final int [][] lengths = new int [n ][2 ];
62
+ final int [] distances = new int [n ];
63
+ for (int i = 0 ; i < n ; i ++) {
64
+ distances [i ] = findDistance (starts [i ], ends [i ], w [i ]);
65
+ }
66
+ for (int j = 1 ; j < n ; j ++) {
67
+ lengths [j ][0 ] = lengths [j - 1 ][0 ] + distances [j - 1 ] + cycleWeights [j - 1 ];
68
+ }
69
+ for (int j = n - 1 ; j > 0 ; j --) {
70
+ lengths [j ][1 ] = lengths [(j + 1 ) % n ][1 ] + distances [(j + 1 ) % n ] + cycleWeights [j ];
71
+ }
72
+ return lengths ;
73
+ }
74
+
75
+ public int [][][] getWeights (final int weights [][], final int n ) {
76
+ final int w [][][] = new int [n ][][];
77
+ for (int i = 0 ; i < n ; i ++) {
78
+ final int size = weights [i ].length ;
79
+ w [i ] = new int [size ][2 ];
80
+ for (int j = 1 ; j < size ; j ++) {
81
+ w [i ][j ][0 ] = w [i ][j - 1 ][0 ] + weights [i ][j - 1 ];
82
+ }
83
+ w [i ][size - 1 ][1 ] = weights [i ][size - 1 ];
84
+ for (int j = size - 2 ; j > 0 ; j --) {
85
+ w [i ][j ][1 ] = w [i ][j + 1 ][1 ] + weights [i ][j ];
86
+ }
87
+ }
88
+ return w ;
89
+ }
90
+
91
+ private int findDistance (final int start , final int end , final int [][] weights ) {
92
+ final int clockwise = Math .abs (weights [start ][0 ] - weights [end ][0 ]);
93
+ final int antiClockwise = weights [weights .length - 1 ][1 ] + weights [weights .length - 1 ][0 ] - clockwise ;
94
+ return clockwise > antiClockwise ? antiClockwise : clockwise ;
95
+ }
96
+ }
0 commit comments