-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathP186.java
80 lines (68 loc) · 1.87 KB
/
P186.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
186. The Chain
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard input
output: standard output
Smith has N chains. Each chain is the sequence of successively connected links.
The length of each chain is known: the first chain contains L1 links, the second
- L2, ..., the last one - LN.
He can make a following series of actions in a minute:
1. to unchain one link
2. to remove or to put into the unchained link some other links of any chain
3. to chain the link
Your task is to determine the minimum time which will take the smith to connect
all the chains in one line, i.e. the chain will look like a chain made up of
successively connected links.
Input
The first line contains natural number N<=100. The second line contains L1, L2,
..., LN (1<=Li<=100, for all i = 1..N).
Output
Output the only integer number - the solution to the problem.
Sample test(s)
Input
2
3 4
Output
1
*
*/
import java.util.*;
public class P186 {
public static int process1(int[] a) {
Arrays.sort(a);
int cnt = 0;
int linkSum = 0;
int n = a.length;
for(int i = 0; i < n; i++) {
linkSum += a[i];
if(linkSum >= n - 1 - i)
break;
cnt++;
}
return n - 1 - cnt;
}
public static int process2(int[] a) {
Arrays.sort(a);
int time = 0;
int n = a.length;
int i = 0;
int j = n - 1;
while(i < j) {
a[i]--;
j--;
time++;
if(a[i] == 0)
i++;
}
return time;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] chains = new int[n];
for(int i = 0; i < n; i++)
chains[i] = in.nextInt();
System.out.println(process1(chains));
}
}