File tree Expand file tree Collapse file tree 1 file changed +81
-0
lines changed
Expand file tree Collapse file tree 1 file changed +81
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+
3+ import java.util.* ;
4+ import java.io.* ;
5+
6+ class Main {
7+
8+ // IO field
9+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
10+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
11+ static StringTokenizer st;
12+
13+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
14+ static int nextInt() {return Integer . parseInt(st. nextToken());}
15+ static long nextLong() {return Long . parseLong(st. nextToken());}
16+ static void bwEnd() throws Exception {bw. flush();bw. close();}
17+
18+ // Additional field
19+ static int T , N , PA , PB ;
20+ static long [] A ;
21+
22+ public static void main(String [] args) throws Exception {
23+
24+ ready();
25+ // solve();
26+
27+ bwEnd();
28+ }
29+
30+ static void ready() throws Exception {
31+
32+ T = Integer . parseInt(br. readLine());
33+ while (T -- > 0 ) {
34+ nextLine();
35+ N = nextInt();
36+ PA = nextInt();
37+ PB = nextInt();
38+ if (PA > PB ) {
39+ int temp = PA ;
40+ PA = PB ;
41+ PB = temp;
42+ }
43+ A = new long [N ];
44+ nextLine();
45+ for (int i= 0 ;i< N ;i++ ) A [i] = nextInt();
46+ solve();
47+ }
48+
49+ }
50+
51+ static void solve() throws Exception {
52+
53+ Arrays . sort(A );
54+ long ans = 0 , res = 0 , mid = - 1 , cnt = 0 ;
55+ for (long i: A ) {
56+ if (i- PA < PB - i) {
57+ ans += (i- PA ) * 2 ;
58+ res += (i- PA ) * 2 ;
59+ }
60+ else if (i- PA == PB - i) {
61+ ans += (i- PA ) * 2 ;
62+ mid = i;
63+ cnt++ ;
64+ }
65+ else {
66+ ans += (PB - i) * 2 ;
67+ res -= (PB - i) * 2 ;
68+ }
69+ }
70+ if (mid != - 1 ) {
71+ long res2 = Long . MAX_VALUE ;
72+ for (long i= 0 ;i<= cnt;i++ ) res2 = Math . min(res2, Math . abs(res+ 2 * (mid- PA )* (2 * i- cnt)));
73+ res = res2;
74+ }
75+ res = Math . abs(res);
76+ bw. write(ans + " " + res + " \n " );
77+ }
78+
79+ }
80+
81+ ```
You can’t perform that action at this time.
0 commit comments