File tree Expand file tree Collapse file tree 1 file changed +72
-0
lines changed
Expand file tree Collapse file tree 1 file changed +72
-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 = new StringTokenizer (" " );
12+
13+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
14+ static int nextInt() throws Exception {
15+ if (! st. hasMoreTokens()) nextLine();
16+ return Integer . parseInt(st. nextToken());
17+ }
18+ static long nextLong() throws Exception {
19+ if (! st. hasMoreTokens()) nextLine();
20+ return Long . parseLong(st. nextToken());
21+ }
22+ static void bwEnd() throws Exception {bw. flush();bw. close();}
23+
24+ // Additional field
25+
26+ static int N ;
27+ static long [] C ;
28+
29+ public static void main(String [] args) throws Exception {
30+
31+ ready();
32+ solve();
33+
34+ bwEnd();
35+
36+ }
37+
38+ static void ready() throws Exception {
39+
40+ N = nextInt();
41+ C = new long [10001 ];
42+ for (int i= 0 ;i< N ;i++ ) C [nextInt()]++ ;
43+
44+ }
45+
46+ static void solve() throws Exception {
47+
48+ long ans1 = - 1 , res = Long . MAX_VALUE ;
49+ long ans2 = - 1 , res2 = Long . MAX_VALUE ;
50+ for (int i= 1 ;i<= 10000 ;i++ ) {
51+ long temp1 = 0 , temp2 = 0 ;
52+ for (int j= 1 ;j<= 10000 ;j++ ) {
53+ temp1 += C [j] * Math . abs(i- j);
54+ temp2 += C [j] * (i- j) * (i- j);
55+ }
56+ if (temp1 < res) {
57+ res = temp1;
58+ ans1 = i;
59+ }
60+ if (temp2 < res2) {
61+ res2 = temp2;
62+ ans2 = i;
63+ }
64+ }
65+ bw. write(ans1 + " " + ans2);
66+
67+
68+ }
69+
70+ }
71+
72+ ```
You can’t perform that action at this time.
0 commit comments