Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package deposit;
- /**
- *
- * @author 100622796
- */
- import java.util.*;
- public class Deposit {
- /**
- * @param args the command line arguments
- */
- int x0, x1, y;
- boolean extracted;
- public Deposit(int x0, int x1, int y) {
- this.x0 = x0;
- this.x1 = x1;
- this.y = y;
- }
- public static boolean isBetween(int startX, int startY, int endX, int endY, Deposit deposit) {
- double slope, b;
- if (startY != endY) {
- slope = (double) (endY - startY) / (double) (endX - startX); // calculate slope of the well
- b = startY - (double) (slope * startX); // calculate intercept
- double depoPoint = (deposit.y - b) / slope;
- depoPoint = Math.round(depoPoint);
- return (depoPoint >= deposit.x0 && depoPoint <= deposit.x1);
- }
- return false;
- }
- public static void drillWell(Deposit[] depoArray) {
- int totalOil = 0;
- ArrayList<Integer> oilList = new ArrayList<Integer>();
- int pointArray[] = new int[depoArray.length * 2];
- for (int i = 0; i < pointArray.length; i = i + 2) {
- pointArray[i] = depoArray[i / 2].x0;
- pointArray[i + 1] = depoArray[i / 2].x1;
- }
- for (int i = 0; i < pointArray.length; i++) {
- totalOil = 0;
- if (i % 2 == 0) {
- for (int j = i + 2; j < pointArray.length; j++) {
- totalOil = 0;
- for (int k = 0; k < depoArray.length; k++) {
- if (isBetween(pointArray[i], depoArray[(i / 2)].y, pointArray[j], depoArray[j / 2].y, depoArray[k]) && !(depoArray[i / 2].y == depoArray[j / 2].y)) {
- totalOil = totalOil + (depoArray[k].x1 - depoArray[k].x0);
- oilList.add(totalOil);
- }
- }
- }
- } else {
- for (int j = i + 1; j < pointArray.length; j++) {
- totalOil = 0;
- for (int k = 0; k < depoArray.length; k++) {
- if (isBetween(pointArray[i], depoArray[(i / 2)].y, pointArray[j], depoArray[j / 2].y, depoArray[k]) && !(depoArray[i / 2].y == depoArray[j / 2].y)) {
- totalOil = totalOil + (depoArray[k].x1 - depoArray[k].x0);
- oilList.add(totalOil);
- }
- }
- }
- }
- }
- System.out.println("largest value = " + Collections.max(oilList));
- }
- /*
- public static void sort(Deposit arr[]) {
- int n = arr.length;
- for (int i = 1; i < n; ++i) {
- int key = arr[i].y;
- int j = i - 1;
- /* Move elements of arr[0..i-1], that are
- greater than key, to one position ahead
- of their current position
- while (j >= 0 && arr[j].y > key) {
- arr[j + 1].y = arr[j].y;
- j = j - 1;
- }
- arr[j + 1].y = key;
- }
- } */
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.println("Enter the number of deposits.");
- int numOfDeposits = input.nextInt();
- Deposit[] deposits = new Deposit[numOfDeposits];
- int start, finish, temp;
- System.out.println("Enter the information of each deposit.");
- for (int i = 0; i < numOfDeposits; i++) {
- start = input.nextInt();
- finish = input.nextInt();
- if (start > finish) {
- temp = start;
- start = finish;
- finish = temp;
- }
- deposits[i] = new Deposit(start, finish, input.nextInt());
- }
- drillWell(deposits);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment