Skip to content

Commit

Permalink
Finished Rectangular Pasture
Browse files Browse the repository at this point in the history
  • Loading branch information
skiwee45 committed Jan 12, 2022
1 parent 00d463e commit fe2e776
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/USACO_Bronze_2021December/UB_LonelyPhotosAnalysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.StringTokenizer;

//http://www.usaco.org/index.php?page=viewproblem2&cpid=1155
//Revised version that passed all test cases, completed after competition
public class UB_LonelyPhotosAnalysis {
public static void main(String[] args) {
MyScanner in = new MyScanner();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.StringTokenizer;

//http://www.usaco.org/index.php?page=viewproblem2&cpid=1155
//Version submitted during competition
public class UB_LonelyPhotosContestSubmission {
public static void main(String[] args) {
MyScanner in = new MyScanner();
Expand Down
95 changes: 95 additions & 0 deletions src/USACO_Practice/US_RectangularPasture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package USACO_Practice;

import java.util.*;

//http://usaco.org/index.php?page=viewproblem2&cpid=1063
public class US_RectangularPasture {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] cows = new int[n][2];
Integer[] xRankToIndex = new Integer[n]; //holds indexes of cows in sorted x order
Integer[] yRankToIndex = new Integer[n]; //same thing but x order
for (int i = 0; i < n; i++) {
cows[i] = new int[] {in.nextInt(), in.nextInt()};
xRankToIndex[i] = i;
yRankToIndex[i] = i;
}

class CowsXSort implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return cows[o1][0] - cows[o2][0];
}
}

class CowsYSort implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return cows[o1][1] - cows[o2][1];
}
}

Arrays.sort(xRankToIndex, new CowsXSort());
Arrays.sort(yRankToIndex, new CowsYSort());

int[] indexToXRank = new int[n];
int[] indexToYRank = new int[n];
for (int i = 0; i < n; i++) {
indexToXRank[xRankToIndex[i]] = i;
indexToYRank[yRankToIndex[i]] = i;
}

int[][] aboveLists = new int[n][n+1]; //in xRank order, prefix sums
int[][] belowLists = new int[n][n+1];
for (int i = 0; i < n; i++) {
aboveLists[i][0] = 0;
belowLists[i][0] = 0;
}
for (int i = 0; i < n; i++) {
int cowIndexI = xRankToIndex[i];
for (int j = 0; j < n; j++) {
int cowIndexJ = xRankToIndex[j];
boolean above = indexToYRank[cowIndexJ] > indexToYRank[cowIndexI];
if (i == j) {
aboveLists[i][j + 1] = aboveLists[i][j];
belowLists[i][j + 1] = belowLists[i][j];
} else if (above) {
aboveLists[i][j + 1] = aboveLists[i][j] + 1;
belowLists[i][j + 1] = belowLists[i][j];
} else {
aboveLists[i][j + 1] = aboveLists[i][j];
belowLists[i][j + 1] = belowLists[i][j] + 1;
}
}
}
// for(int[] list : aboveLists) {
// System.out.println(Arrays.toString(list));
// }
// System.out.println("\n");
// for(int[] list : belowLists) {
// System.out.println(Arrays.toString(list));
// }

long total = 1;
for (int p = 0; p < n; p++) {
total++;
for (int o = p + 1; o < n; o++) {
total++;
//if point o is below point p, aboveNum = points above p, belowNum = points below o
int aboveNum;
int belowNum;
if (indexToYRank[xRankToIndex[o]] > indexToYRank[xRankToIndex[p]]) {
aboveNum = aboveLists[o][o] - aboveLists[o][p+1];
belowNum = belowLists[p][o] - belowLists[p][p+1];
} else {
aboveNum = aboveLists[p][o] - aboveLists[p][p+1];
belowNum = belowLists[o][o] - belowLists[o][p+1];
}
//System.out.println("P- " + p + " O- " + o + " Above- " + aboveNum + " Below- " + belowNum);
total += aboveNum + belowNum + aboveNum * belowNum;
}
}
System.out.print(total);
}
}

0 comments on commit fe2e776

Please sign in to comment.