Advertisement
Guest User

parkzones

a guest
Mar 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. package com.company;
  2. import java.lang.reflect.Array;
  3. import java.util.*;
  4. import java.util.regex.*;
  5. import java.util.stream.Stream;
  6.  
  7. public class Main {
  8. public static void main(String[] args) {
  9. Scanner scan = new Scanner(System.in);
  10. int n = Integer.parseInt(scan.nextLine());
  11. HashMap<String, List<Integer>> Zones = new HashMap<>();
  12.  
  13. for (int i = 0; i < n; i++) {
  14. String[] input = scan.nextLine().split(":");
  15. String name = input[0];
  16. String[] att = input[1].split(",");
  17. ArrayList<Integer> attributes = new ArrayList<>(5);
  18. for (int j = 0; j < att.length; j++) {
  19. att[j] = att[j].trim();
  20. attributes.add(Integer.parseInt(att[j]));
  21. }
  22. attributes.add(0);
  23. attributes.add(0);
  24. Zones.put(name, attributes);
  25. }
  26.  
  27. String[] parkSpaces = scan.nextLine().split(";");
  28. String[] shopXY = scan.nextLine().split(",");
  29.  
  30. int shopX = Integer.parseInt(shopXY[0]);
  31. int shopY = Integer.parseInt(shopXY[1]);
  32.  
  33. int k = Integer.parseInt(scan.nextLine());
  34.  
  35. for (String space : parkSpaces
  36. ) {
  37. int spaceX = Integer.parseInt(space.split(",")[0]);
  38. int spaceY = Integer.parseInt(space.split(",")[1]);
  39. for (int i = 0; i < Zones.size(); i++) {
  40.  
  41. List<Integer> attributes = Zones.get((Zones.keySet().toArray())[i]);
  42. int zoneX = attributes.get(0);
  43. int zoneY = attributes.get(1);
  44. int w = attributes.get(2);
  45. int l = attributes.get(3);
  46. int bestX = attributes.get(5);
  47. int bestY = attributes.get(6);
  48.  
  49.  
  50. if (spaceX > zoneX && spaceX < zoneX + w
  51. && spaceY > zoneY && spaceY < zoneY + l) {
  52. bestDistance(attributes, bestX, bestY, shopX, shopY,
  53. spaceX, spaceY);
  54. }
  55. }
  56. }
  57.  
  58. double bestPrice = Double.MAX_VALUE;
  59. double bestTime = Double.MAX_VALUE;
  60. String zoneName = null;
  61. int finalX = 0;
  62. int finalY = 0;
  63. for (int i = 0; i < Zones.size(); i++) {
  64. List<Integer> attributes = Zones.get((Zones.keySet().toArray())[i]);
  65. int bestX = attributes.get(5);
  66. int bestY = attributes.get(6);
  67. int price = attributes.get(4);
  68. int distance = checkDistance(bestX, bestY,shopX, shopY);
  69. int currentTime = k*2*distance;
  70. double temp = Math.ceil((double)currentTime/60);
  71. int currentPrice = (int)temp;
  72. currentPrice *= price;
  73. if (currentPrice<bestPrice || (currentPrice==bestPrice && currentTime<bestTime)){
  74. bestPrice = currentPrice;
  75. bestTime = currentTime;
  76. zoneName = Zones.keySet().toArray()[i].toString();
  77. finalX = bestX;
  78. finalY = bestY;
  79. }
  80. }
  81. System.out.println("Zone Type: " + zoneName + "; ");
  82. System.out.printf("X: %s; Y: %s; Price: %s", finalX, finalY, bestPrice);
  83. }
  84. private static void bestDistance (List < Integer > att,int bestX, int bestY, int shopX, int shopY, int spaceX,
  85. int spaceY){
  86. int currentDistance = checkDistance(spaceX, spaceY, shopX, shopY);
  87. int bestDistance = checkDistance(bestX, bestY, shopX, shopY);
  88. if (currentDistance > bestDistance) {
  89. att.set(5, spaceX);
  90. att.set(6, spaceY);
  91. }
  92. }
  93.  
  94.  
  95. private static int checkDistance ( int spaceX, int spaceY, int shopX, int shopY){
  96. int result = Math.abs(spaceX - shopX) + Math.abs(spaceY - shopY);
  97. return result;
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement