Skip to content

Commit 6c49ab2

Browse files
committed
google java style
1 parent dc1ee34 commit 6c49ab2

File tree

399 files changed

+21147
-21611
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

399 files changed

+21147
-21611
lines changed
File renamed without changes.

Java/ACMContestBlackout.java

+107-107
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,128 @@
1-
/** https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1541
2-
* tag: #prim algorithms
3-
* run Prim algorithms double times
1+
/**
2+
* https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1541
3+
* tag: #prim algorithms run Prim algorithms double times
44
*/
5-
6-
import java.util.Scanner;
75
import java.util.ArrayList;
86
import java.util.Arrays;
97
import java.util.PriorityQueue;
8+
import java.util.Scanner;
109

1110
class ACMContestBlackout {
12-
public static void Prim(ArrayList<ArrayList<Node>> graph, int[] costArr, int[] pathArr, int src) {
13-
int sz = graph.size();
14-
boolean[] visitedArr = new boolean[sz];
15-
Arrays.fill(visitedArr, false);
16-
costArr[src] = 0;
17-
18-
PriorityQueue<Node> pq = new PriorityQueue<>();
19-
Node node;
20-
int id, w;
21-
pq.add(new Node(src, 0));
22-
while (!pq.isEmpty()) {
23-
node = pq.poll();
24-
id = node.id;
25-
visitedArr[id] = true;
26-
for (Node neighbour : graph.get(id)) {
27-
w = neighbour.cost;
28-
if (!visitedArr[neighbour.id] && w < costArr[neighbour.id]) {
29-
costArr[neighbour.id] = w;
30-
pathArr[neighbour.id] = id;
31-
pq.add(new Node(neighbour.id, w));
32-
}
33-
}
11+
public static void Prim(ArrayList<ArrayList<Node>> graph, int[] costArr, int[] pathArr, int src) {
12+
int sz = graph.size();
13+
boolean[] visitedArr = new boolean[sz];
14+
Arrays.fill(visitedArr, false);
15+
costArr[src] = 0;
16+
17+
PriorityQueue<Node> pq = new PriorityQueue<>();
18+
Node node;
19+
int id, w;
20+
pq.add(new Node(src, 0));
21+
while (!pq.isEmpty()) {
22+
node = pq.poll();
23+
id = node.id;
24+
visitedArr[id] = true;
25+
for (Node neighbour : graph.get(id)) {
26+
w = neighbour.cost;
27+
if (!visitedArr[neighbour.id] && w < costArr[neighbour.id]) {
28+
costArr[neighbour.id] = w;
29+
pathArr[neighbour.id] = id;
30+
pq.add(new Node(neighbour.id, w));
3431
}
32+
}
3533
}
34+
}
3635

37-
public static long getCost(int[] costArr) {
38-
long ans = 0;
39-
for (int cost : costArr) {
40-
ans += cost;
41-
}
42-
return ans;
36+
public static long getCost(int[] costArr) {
37+
long ans = 0;
38+
for (int cost : costArr) {
39+
ans += cost;
4340
}
41+
return ans;
42+
}
43+
44+
public static void main(String[] args) {
45+
Scanner sc = new Scanner(System.in);
46+
int testcases = sc.nextInt();
47+
int n, m, a, b, c;
48+
for (int t = 0; t < testcases; t++) {
49+
n = sc.nextInt();
50+
m = sc.nextInt();
51+
ArrayList<ArrayList<Node>> graph = new ArrayList<>();
52+
int sz = n + 1;
53+
for (int i = 0; i < sz; i++) {
54+
graph.add(new ArrayList<>());
55+
}
56+
for (int i = 0; i < m; i++) {
57+
a = sc.nextInt();
58+
b = sc.nextInt();
59+
c = sc.nextInt();
60+
graph.get(a).add(new Node(b, c));
61+
graph.get(b).add(new Node(a, c));
62+
}
63+
64+
// first time
65+
int[] costArr1 = new int[sz];
66+
int[] pathArr1 = new int[sz];
67+
Arrays.fill(pathArr1, -1);
68+
Arrays.fill(costArr1, Integer.MAX_VALUE);
69+
costArr1[0] = 0;
70+
Prim(graph, costArr1, pathArr1, 1);
71+
int val1 = (int) getCost(costArr1);
72+
73+
// second time
74+
int cost_tmp = -1;
75+
int val2 = Integer.MAX_VALUE;
76+
int[] pathArr2 = new int[sz];
77+
Arrays.fill(pathArr2, -1);
78+
int[] costArr2 = new int[sz];
79+
for (int id = 1; id < sz; id++) {
4480

45-
public static void main(String[] args) {
46-
Scanner sc = new Scanner(System.in);
47-
int testcases = sc.nextInt();
48-
int n, m, a, b, c;
49-
for (int t=0; t < testcases; t++) {
50-
n = sc.nextInt();
51-
m = sc.nextInt();
52-
ArrayList<ArrayList<Node>> graph = new ArrayList<>();
53-
int sz = n+1;
54-
for (int i=0; i < sz; i++) {
55-
graph.add(new ArrayList<>());
81+
Arrays.fill(costArr2, Integer.MAX_VALUE);
82+
costArr2[0] = 0;
83+
84+
if (pathArr1[id] != -1) {
85+
cost_tmp = costArr1[id];
86+
for (Node node : graph.get(id)) {
87+
if (node.id == pathArr1[id]) {
88+
node.cost = Integer.MAX_VALUE;
5689
}
57-
for (int i=0; i < m; i++) {
58-
a = sc.nextInt();
59-
b = sc.nextInt();
60-
c = sc.nextInt();
61-
graph.get(a).add(new Node(b, c));
62-
graph.get(b).add(new Node(a, c));
90+
}
91+
for (Node node : graph.get(pathArr1[id])) {
92+
if (node.id == id) {
93+
node.cost = Integer.MAX_VALUE;
6394
}
95+
}
6496

65-
// first time
66-
int[] costArr1 = new int[sz];
67-
int[] pathArr1 = new int[sz];
68-
Arrays.fill(pathArr1, -1);
69-
Arrays.fill(costArr1, Integer.MAX_VALUE);
70-
costArr1[0] = 0;
71-
Prim(graph, costArr1, pathArr1, 1);
72-
int val1 = (int)getCost(costArr1);
73-
74-
// second time
75-
int cost_tmp = -1;
76-
int val2 = Integer.MAX_VALUE;
77-
int[] pathArr2 = new int[sz];
78-
Arrays.fill(pathArr2, -1);
79-
int[] costArr2 = new int[sz];
80-
for (int id = 1; id < sz; id++) {
81-
82-
Arrays.fill(costArr2, Integer.MAX_VALUE);
83-
costArr2[0] = 0;
84-
85-
if (pathArr1[id] != -1) {
86-
cost_tmp = costArr1[id];
87-
for (Node node : graph.get(id)) {
88-
if (node.id == pathArr1[id]) {
89-
node.cost = Integer.MAX_VALUE;
90-
}
91-
}
92-
for (Node node : graph.get(pathArr1[id])) {
93-
if (node.id == id) {
94-
node.cost = Integer.MAX_VALUE;
95-
}
96-
}
97+
Prim(graph, costArr2, pathArr2, 1);
98+
val2 = (int) Math.min(getCost(costArr2), val2);
9799

98-
Prim(graph, costArr2, pathArr2, 1);
99-
val2 = (int)Math.min(getCost(costArr2), val2);
100-
101-
for (Node node : graph.get(id)) {
102-
if (node.id == pathArr1[id]) {
103-
node.cost = cost_tmp;
104-
}
105-
}
106-
for (Node node : graph.get(pathArr1[id])) {
107-
if (node.id == id) {
108-
node.cost = cost_tmp;
109-
}
110-
}
111-
}
100+
for (Node node : graph.get(id)) {
101+
if (node.id == pathArr1[id]) {
102+
node.cost = cost_tmp;
103+
}
104+
}
105+
for (Node node : graph.get(pathArr1[id])) {
106+
if (node.id == id) {
107+
node.cost = cost_tmp;
112108
}
113-
System.out.println(val1 + " " + val2);
109+
}
114110
}
111+
}
112+
System.out.println(val1 + " " + val2);
115113
}
116-
114+
}
117115
}
118116

119117
class Node implements Comparable<Node> {
120-
int id, cost;
121-
public Node(int id, int cost) {
122-
this.id = id;
123-
this.cost = cost;
124-
}
125-
public int compareTo(Node other) {
126-
return this.cost - other.cost;
127-
}
128-
}
118+
int id, cost;
119+
120+
public Node(int id, int cost) {
121+
this.id = id;
122+
this.cost = cost;
123+
}
124+
125+
public int compareTo(Node other) {
126+
return this.cost - other.cost;
127+
}
128+
}

Java/AGGRCOW.java

+47-47
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
1-
/** https://www.spoj.com/problems/AGGRCOW/
2-
* tag: #binary-search
3-
* minimum distance between two position in a range.
1+
/**
2+
* https://www.spoj.com/problems/AGGRCOW/ tag: #binary-search minimum distance between two position
3+
* in a range.
44
*/
5-
6-
import java.util.Scanner;
75
import java.util.Arrays;
6+
import java.util.Scanner;
87

98
class AGGRCOW {
109

11-
static int getResult(int[] source, int countPos) {
12-
Arrays.sort(source);
13-
int startVal = 0;
14-
int endVal = source[source.length - 1] - source[0];
15-
int ans = 0;
16-
int midVal;
17-
18-
while (startVal <= endVal) {
19-
midVal = startVal + (endVal - startVal) / 2;
20-
int prev = source[0];
21-
int count = 0;
22-
for (int i=1; i < source.length; i++) {
23-
if (source[i] - prev >= midVal) {
24-
count++;
25-
prev = source[i];
26-
}
27-
}
28-
if (count >= countPos - 1) {
29-
if (midVal > ans) {
30-
ans = midVal;
31-
}
32-
startVal = midVal + 1;
33-
} else {
34-
endVal = midVal - 1;
35-
}
10+
static int getResult(int[] source, int countPos) {
11+
Arrays.sort(source);
12+
int startVal = 0;
13+
int endVal = source[source.length - 1] - source[0];
14+
int ans = 0;
15+
int midVal;
16+
17+
while (startVal <= endVal) {
18+
midVal = startVal + (endVal - startVal) / 2;
19+
int prev = source[0];
20+
int count = 0;
21+
for (int i = 1; i < source.length; i++) {
22+
if (source[i] - prev >= midVal) {
23+
count++;
24+
prev = source[i];
3625
}
37-
return ans;
38-
}
39-
public static void main(String[] args) {
40-
Scanner sc = new Scanner(System.in);
41-
int testcases = sc.nextInt();
42-
int n, c;
43-
for (int t=0; t < testcases; t++) {
44-
n = sc.nextInt();
45-
c = sc.nextInt();
46-
int[] source = new int[n];
47-
int tmp;
48-
for (int i=0; i < n; i++) {
49-
tmp = sc.nextInt();
50-
source[i] = tmp;
51-
}
52-
int result = getResult(source, c);
53-
System.out.println(result);
26+
}
27+
if (count >= countPos - 1) {
28+
if (midVal > ans) {
29+
ans = midVal;
5430
}
31+
startVal = midVal + 1;
32+
} else {
33+
endVal = midVal - 1;
34+
}
35+
}
36+
return ans;
37+
}
38+
39+
public static void main(String[] args) {
40+
Scanner sc = new Scanner(System.in);
41+
int testcases = sc.nextInt();
42+
int n, c;
43+
for (int t = 0; t < testcases; t++) {
44+
n = sc.nextInt();
45+
c = sc.nextInt();
46+
int[] source = new int[n];
47+
int tmp;
48+
for (int i = 0; i < n; i++) {
49+
tmp = sc.nextInt();
50+
source[i] = tmp;
51+
}
52+
int result = getResult(source, c);
53+
System.out.println(result);
5554
}
55+
}
5656
}

0 commit comments

Comments
 (0)