File tree 6 files changed +191
-0
lines changed
6 files changed +191
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+ import java .util .*;
3
+
4
+ public class Main {
5
+
6
+ public static void main (String [] args ) throws IOException {
7
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
8
+ int T = Integer .parseInt (br .readLine ());
9
+ StringBuffer sb = new StringBuffer ();
10
+ for (int i =0 ; i <T ; i ++) {
11
+ int N = Integer .parseInt (br .readLine ());
12
+ int [] arr = new int [N ];
13
+ StringTokenizer st = new StringTokenizer (br .readLine ());
14
+ for (int j =0 ; j <N ; j ++) {
15
+ arr [j ] = Integer .parseInt (st .nextToken ());
16
+ }
17
+ Arrays .sort (arr );
18
+
19
+ Deque <Integer > q = new LinkedList <>();
20
+ q .add (arr [N -1 ]);
21
+ for (int j =N -2 ; j >=1 ; j -=2 ) {
22
+ q .addFirst (arr [j ]);
23
+ q .addLast (arr [j -1 ]);
24
+ }
25
+ if (N %2 == 0 ) {
26
+ q .addFirst (arr [0 ]);
27
+ }
28
+
29
+ int answer = Math .abs (q .getFirst () - q .getLast ());
30
+ for (int j =0 ; j <N -1 ; j ++) {
31
+ int num = q .removeFirst ();
32
+ answer = Math .max (answer , Math .abs (num -q .getFirst ()));
33
+ }
34
+ sb .append (answer ).append ('\n' );
35
+ }
36
+ System .out .println (sb .toString ());
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+
3
+ public class Main {
4
+
5
+ public static void main (String [] args ) throws IOException {
6
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
7
+ String [] num = br .readLine ().split (" " );
8
+ int N = Integer .parseInt (num [0 ]);
9
+ int M = Integer .parseInt (num [1 ]);
10
+ int [][] coin = new int [N ][M ];
11
+ for (int i = 0 ; i < N ; i ++) {
12
+ String input = br .readLine ();
13
+ for (int j = 0 ; j < M ; j ++) {
14
+ coin [i ][j ] = input .charAt (j ) - '0' ;
15
+ }
16
+ }
17
+ int count = 0 ;
18
+ for (int i = N -1 ; i >= 0 ; i --) {
19
+ for (int j = M -1 ; j >= 0 ; j --) {
20
+ if (coin [i ][j ] == 1 ) {
21
+ flipCoin (coin , i , j );
22
+ count ++;
23
+ }
24
+ }
25
+ }
26
+ System .out .println (count );
27
+ }
28
+
29
+ public static void flipCoin (int [][] coin , int a , int b ) {
30
+ for (int i =0 ; i <=a ; i ++) {
31
+ for (int j =0 ; j <=b ; j ++) {
32
+ if (coin [i ][j ] == 0 ) {
33
+ coin [i ][j ] = 1 ;
34
+ }else {
35
+ coin [i ][j ] = 0 ;
36
+ }
37
+ }
38
+ }
39
+ }
40
+ }
Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+ import java .util .*;
3
+
4
+ public class Main {
5
+
6
+ public static void main (String [] args ) throws IOException {
7
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
8
+ String [] input = br .readLine ().split (" " );
9
+ int n = Integer .parseInt (input [0 ]);
10
+ int m = Integer .parseInt (input [1 ]);
11
+
12
+ StringTokenizer st = new StringTokenizer (br .readLine ());
13
+ long [] cards = new long [n ];
14
+ for (int i =0 ; i <n ; i ++) {
15
+ cards [i ] = Integer .parseInt (st .nextToken ());
16
+ }
17
+ Arrays .sort (cards );
18
+ for (int i =0 ; i <m ; i ++) {
19
+ mergeCard (cards );
20
+ }
21
+ long sum = 0 ;
22
+ for (long num : cards ) {
23
+ sum += num ;
24
+ }
25
+ System .out .println (sum );
26
+ }
27
+
28
+ public static void mergeCard (long [] cards ) {
29
+ long num = cards [0 ] + cards [1 ];
30
+ cards [0 ] = cards [1 ] = num ;
31
+ if (cards .length >= 3 && num > cards [2 ]) {
32
+ Arrays .sort (cards );
33
+ }
34
+ }
35
+ }
Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+
3
+ public class Main {
4
+
5
+ public static void main (String [] args ) throws IOException {
6
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
7
+ String [] input = br .readLine ().split (" " );
8
+ int N = Integer .parseInt (input [0 ]);
9
+ int M = Integer .parseInt (input [1 ]);
10
+ if (N == 1 ) {
11
+ System .out .println ("1" );
12
+ }else if (N == 2 ) {
13
+ System .out .println (Math .min (4 , (M +1 )/2 ));
14
+ }else {
15
+ if (M <= 6 ) {
16
+ System .out .println (Math .min (4 , M ));
17
+ }else {
18
+ System .out .println (M -2 );
19
+ }
20
+ }
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+ import java .util .*;
3
+
4
+ public class Main {
5
+
6
+ public static void main (String [] args ) throws IOException {
7
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
8
+ int N = Integer .parseInt (br .readLine ());
9
+ StringTokenizer st = new StringTokenizer (br .readLine ());
10
+ PriorityQueue <Integer > q = new PriorityQueue <>();
11
+ for (int i =0 ; i <N ; i ++) {
12
+ q .offer (Integer .parseInt (st .nextToken ()));
13
+ }
14
+
15
+ int count = 0 ;
16
+ int link = q .size () - 1 ;
17
+ while (true ) {
18
+ int chain = q .peek ();
19
+ if (link > chain ) {
20
+ link -= chain + 1 ;
21
+ count += q .poll ();
22
+ }else {
23
+ count += link ;
24
+ break ;
25
+ }
26
+ }
27
+ System .out .println (count );
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+
3
+ public class Main {
4
+
5
+ public static void main (String [] args ) throws IOException {
6
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
7
+ int K = Integer .parseInt (br .readLine ());
8
+ int square = 1 ;
9
+ while (square < K ) {
10
+ square *= 2 ;
11
+ if (square == K ) {
12
+ System .out .println (square + " 0" );
13
+ return ;
14
+ }
15
+ }
16
+ int count = 0 ;
17
+ int num = square ;
18
+ while (K >= 1 ) {
19
+ num /= 2 ;
20
+ if (num <= K ) {
21
+ K -= num ;
22
+ }
23
+ count ++;
24
+ }
25
+ System .out .println (square + " " + count );
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments