-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularBarn.java
More file actions
75 lines (74 loc) · 1.84 KB
/
CircularBarn.java
File metadata and controls
75 lines (74 loc) · 1.84 KB
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
import java.io.*;
import java.util.*;
public class CircularBarn {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader br = new BufferedReader(new FileReader("cbarn.in"));
int N = Integer.parseInt(br.readLine());
int[] barn = new int[N];
for (int i = 0; i < N; i++)
barn[i] = Integer.parseInt(br.readLine());
int[] counts = new int[N];
int cows = 0;
int start = -1;
for (int i = 0; i < N; i++)
{
if (barn[i] == 0)
start = i;
}
if (start == -1)
{
System.out.println(0); return;
}
for (int i = start; i < N + start; i++)
{
if (barn[i%N] == 0 && cows != 0)
cows--;
else if (barn[i%N] > 1)
cows += barn[i%N] - 1;
counts[i%N] = cows;
}
for (int i = 0; i < N; i++)
{
if (counts[i] > counts[start])
start = i;
}
start = (start - barn[start] + N)%N;
System.out.println(start);
Comparator<Integer> c= new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) { return o2-o1; }
};
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(c);
ArrayList<Integer> temp = new ArrayList<Integer>();
int total = 0;
for (int i = start; i < N+start; i++)
{
while (!pq.isEmpty())
temp.add(pq.remove()+1);
while (!temp.isEmpty())
pq.add(temp.remove(temp.size()-1));
if (pq.isEmpty())
{
if (barn[i%N] > 1)
for (int j = 1; j < barn[i%N]; j++)
pq.add(0);
}
else
{
total += Math.pow(pq.remove(), 2);
if (barn[i%N] > 0)
for (int j = 0; j < barn[i%N]; j++)
pq.add(0);
}
System.out.println(i);
System.out.println(pq);
System.out.println(total);
}
System.out.println(total);
//PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("cbarn.out")));
//pw.println(total);
//pw.close();
br.close();
}
}