Skip to content

Commit a66041f

Browse files
committed
pushing lots of code
1 parent bf5a61b commit a66041f

File tree

13 files changed

+946
-86
lines changed

13 files changed

+946
-86
lines changed

Blogs/MyJourney

Whitespace-only changes.

Competitive Programming.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
</content>
1212
<orderEntry type="inheritedJdk" />
1313
<orderEntry type="sourceFolder" forTests="false" />
14+
<orderEntry type="library" name="chelper" level="project" />
1415
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
1516
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
16-
<orderEntry type="library" name="chelper" level="project" />
1717
</component>
1818
</module>

src/main/java/main/java/BruteForceSolver.java

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main.java;
2+
3+
public class MaxSumArray {
4+
public static void main(String[] args) {
5+
int n = 10;
6+
int a[] = new int[]{-1, -3, -7, -1, -10, -2, -3, -2, -1, -3};
7+
int max_sum = Integer.MIN_VALUE, current_sum = 0;
8+
for (int i = 0; i < n; i++) {
9+
current_sum = current_sum + a[i];
10+
if (current_sum < 0) {
11+
current_sum = a[i];
12+
}
13+
max_sum = Math.max(current_sum, max_sum);
14+
System.out.print(max_sum + " ");
15+
}
16+
System.out.println("\n" + max_sum);
17+
}
18+
}

src/main/java/main/java/Solution.java

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,54 @@
77

88
public class Solution {
99
public static void main(String[] args) throws IOException {
10-
final BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
11-
final StringBuilder stringBuilder = new StringBuilder();
12-
final int t = 1;
13-
final Solution solution = new Solution();
14-
for (int test = 0; test < t; test++) {
15-
final long a[] = new long[Integer.parseInt(inputReader.readLine())];
16-
final String[] split = inputReader.readLine().split(" ");
17-
for (int i = 0; i < split.length; i++) {
18-
a[i] = Integer.parseInt(split[i]);
10+
int n = 5, m = 5;
11+
int matrix[][] = new int[n][m];
12+
int sum[][] = new int[n][m];
13+
for (int i = 0; i < n; i++) {
14+
for (int j = 0; j < n; j++) {
15+
matrix[i][j] = i * n + j + 1;
16+
int sumOfPreviousColumn;
17+
if (j == 0)
18+
sumOfPreviousColumn = 0;
19+
else
20+
sumOfPreviousColumn = sum[i][j - 1];
21+
int sumOfPreviousRow;
22+
if (i == 0)
23+
sumOfPreviousRow = 0;
24+
else
25+
sumOfPreviousRow = sum[i - 1][j];
26+
int sumOfDiagonal;
27+
if (i == 0 || j == 0) {
28+
sumOfDiagonal = 0;
29+
} else {
30+
sumOfDiagonal = sum[i - 1][j - 1];
31+
}
32+
sum[i][j] = matrix[i][j]
33+
+ sumOfPreviousColumn
34+
+ sumOfPreviousRow
35+
- sumOfDiagonal;
1936
}
20-
stringBuilder.append(solution.getAnswer(a)).append('\n');
2137
}
22-
System.out.println(stringBuilder);
23-
}
24-
25-
public long getAnswer(final long[] a) {
26-
Arrays.sort(a);
27-
final long powersOf2[] = new long[a.length];
28-
final long mod = 1000000007;
29-
powersOf2[0] = 1;
30-
for (int i = 1; i < a.length; i++) {
31-
powersOf2[i] = (powersOf2[i - 1] << 1) % mod;
38+
for (int i = 0; i < n; i++) {
39+
System.out.println(Arrays.toString(matrix[i]));
40+
}
41+
System.out.println();
42+
System.out.println();
43+
for (int i = 0; i < n; i++) {
44+
System.out.println(Arrays.toString(sum[i]));
3245
}
33-
long ans = 0;
34-
for (int i = 0; i < a.length; i++) {
35-
ans = (ans + (powersOf2[i] - powersOf2[a.length - i - 1]) * a[i]) % mod;
46+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
47+
String s = reader.readLine();
48+
while (!s.equalsIgnoreCase("Quit")) {
49+
int r1 = Integer.parseInt(reader.readLine()) - 1,
50+
r2 = Integer.parseInt(reader.readLine()) - 1,
51+
c1 = Integer.parseInt(reader.readLine()) - 1,
52+
c2 = Integer.parseInt(reader.readLine()) - 1;
53+
System.out.println(sum[r2][c2]
54+
- sum[r1 - 1][c2]
55+
- sum[r2][c1 - 1]
56+
+ sum[r1 - 1][c1 - 1]);
57+
s = reader.readLine();
3658
}
37-
return ans;
3859
}
3960
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main.java.codechef;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
7+
import static java.lang.Integer.parseInt;
8+
9+
public class BalancedGame {
10+
public static void main(String[] args) throws IOException {
11+
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
12+
final StringBuilder stringBuilder = new StringBuilder();
13+
for (int t = parseInt(bufferedReader.readLine()); t > 0; t--) {
14+
String line[] = bufferedReader.readLine().split(" ");
15+
final int n = parseInt(line[0]), cMax = parseInt(line[1]), hMax = parseInt(line[2]);
16+
final long seeds[][] = new long[2][n];
17+
for (int i = 0; i < n; i++) {
18+
line = bufferedReader.readLine().split(" ");
19+
seeds[1][i] = Long.parseLong(line[0]);
20+
seeds[2][i] = Long.parseLong(line[1]);
21+
}
22+
}
23+
System.out.print(stringBuilder);
24+
}
25+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package main.java.codechef;
2+
3+
import java.util.Arrays;
4+
import java.util.Random;
5+
6+
public class BruteForceSolver {
7+
private final int[][][] w;
8+
private final int[] starts;
9+
private final int[] ends;
10+
private final int[][] cycleLengths;
11+
12+
public BruteForceSolver(int n, int[][] weights, final int[] cycleWeights, final int[] starts, int[] ends) {
13+
w = getWeights(weights, n);
14+
cycleLengths = getCycleLengths(n, w, cycleWeights, starts, ends);
15+
this.starts = starts;
16+
this.ends = ends;
17+
}
18+
19+
public static void main(String[] args) {
20+
final Random random = new Random();
21+
int n = random.nextInt(20) + 1;
22+
int a[] = new int[n];
23+
int[][] weights = new int[n][];
24+
int[] starts = new int[n];
25+
int[] ends = new int[n];
26+
int[] cycleWeights = new int[n];
27+
for (int i = 0; i < n; i++) {
28+
a[i] = random.nextInt(20) + 1;
29+
weights[i] = new int[a[i]];
30+
for (int j = 0; j < a[i]; j++) {
31+
weights[i][j] = random.nextInt(20) + 1;
32+
}
33+
starts[i] = random.nextInt(a[i]);
34+
ends[i] = random.nextInt(a[i]);
35+
cycleWeights[i] = random.nextInt(20) + 1;
36+
}
37+
}
38+
39+
public int solve(int v1, int v2, int c1, int c2) {
40+
if (c1 > c2) {
41+
int temp = v1;
42+
v1 = v2;
43+
v2 = temp;
44+
temp = c1;
45+
c1 = c2;
46+
c2 = temp;
47+
}
48+
final int cycleDistance1 = Math.abs(cycleLengths[c1][0] - cycleLengths[c2][0]),
49+
excess = findDistance(starts[c1], ends[c1], w[c1]),
50+
first1 = findDistance(v1, ends[c1], w[c1]),
51+
second1 = findDistance(starts[c2], v2, w[c2]);
52+
final int cycleDistance2 = Math.abs(cycleLengths[c1][1] - cycleLengths[c2][1]),
53+
first2 = findDistance(v1, starts[c1], w[c1]),
54+
second2 = findDistance(ends[c2], v2, w[c2]);
55+
final int possibilityOne = cycleDistance1 - excess + first1 + second1;
56+
final int possibilityTwo = cycleDistance2 - excess + first2 + second2;
57+
return Math.min(possibilityOne, possibilityTwo);
58+
}
59+
60+
public int[][] getCycleLengths(final int n, final int[][][] w, final int[] cycleWeights, final int[] starts, final int[] ends) {
61+
final int[][] lengths = new int[n][2];
62+
final int[] distances = new int[n];
63+
for (int i = 0; i < n; i++) {
64+
distances[i] = findDistance(starts[i], ends[i], w[i]);
65+
}
66+
for (int j = 1; j < n; j++) {
67+
lengths[j][0] = lengths[j - 1][0] + distances[j - 1] + cycleWeights[j - 1];
68+
}
69+
for (int j = n - 1; j > 0; j--) {
70+
lengths[j][1] = lengths[(j + 1) % n][1] + distances[(j + 1) % n] + cycleWeights[j];
71+
}
72+
return lengths;
73+
}
74+
75+
public int[][][] getWeights(final int weights[][], final int n) {
76+
final int w[][][] = new int[n][][];
77+
for (int i = 0; i < n; i++) {
78+
final int size = weights[i].length;
79+
w[i] = new int[size][2];
80+
for (int j = 1; j < size; j++) {
81+
w[i][j][0] = w[i][j - 1][0] + weights[i][j - 1];
82+
}
83+
w[i][size - 1][1] = weights[i][size - 1];
84+
for (int j = size - 2; j > 0; j--) {
85+
w[i][j][1] = w[i][j + 1][1] + weights[i][j];
86+
}
87+
}
88+
return w;
89+
}
90+
91+
private int findDistance(final int start, final int end, final int[][] weights) {
92+
final int clockwise = Math.abs(weights[start][0] - weights[end][0]);
93+
final int antiClockwise = weights[weights.length - 1][1] + weights[weights.length - 1][0] - clockwise;
94+
return clockwise > antiClockwise ? antiClockwise : clockwise;
95+
}
96+
}

0 commit comments

Comments
 (0)