fork download
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. class Position {
  5. int x, y, time;
  6.  
  7. public Position(int x, int y, int time) {
  8. this.x = x;
  9. this.y = y;
  10. this.time = time;
  11. }
  12. }
  13.  
  14. class problemC {
  15. public static void main(String[] args) {
  16. Scanner scanner = new Scanner(System.in);
  17. int numPositions = scanner.nextInt();
  18. int GPSTimeInterval = scanner.nextInt();
  19. ArrayList<Position> positions = new ArrayList<>();
  20.  
  21. for (int i = 0; i < numPositions; i++) {
  22. positions.add(new Position(scanner.nextInt(), scanner.nextInt(), scanner.nextInt()));
  23. }
  24.  
  25. double actualDistance = getDistance(positions);
  26. ArrayList<Position> GPSPositions = new ArrayList<>();
  27. int currentTime = 0;
  28.  
  29. for (int i = 0; i < numPositions - 1; i++) {
  30. Position current = positions.get(i);
  31. Position next = positions.get(i + 1);
  32.  
  33. while (currentTime <= next.time) {
  34. if (currentTime < current.time) {
  35. currentTime += GPSTimeInterval;
  36. continue;
  37. }
  38. double fraction = (double)(currentTime - current.time) / (next.time - current.time);
  39. int GPSx = (int)Math.round(current.x + fraction * (next.x - current.x));
  40. int GPSy = (int)Math.round(current.y + fraction * (next.y - current.y));
  41. GPSPositions.add(new Position(GPSx, GPSy, currentTime));
  42. currentTime += GPSTimeInterval;
  43. }
  44. }
  45.  
  46. Position lastPosition = positions.get(numPositions - 1);
  47. if (currentTime != lastPosition.time) {
  48. GPSPositions.add(new Position(lastPosition.x, lastPosition.y, lastPosition.time));
  49. }
  50.  
  51. double GPSDistance = getDistance(GPSPositions);
  52. double lostPercentage = ((actualDistance - GPSDistance) / actualDistance) * 100;
  53.  
  54. for(Position p : positions) {
  55. System.out.println(p.x + " " + p.y + " - " + p.time);
  56. }
  57. System.out.println("-----------");
  58. for(Position d : GPSPositions) {
  59. System.out.println(d.x + " " + d.y + " - " + d.time);
  60. }
  61.  
  62. System.out.println(lostPercentage);
  63. scanner.close();
  64. }
  65.  
  66. private static double getDistance(ArrayList<Position> positions) {
  67. double distance = 0.0;
  68. for (int i = 0; i < positions.size() - 1; i++) {
  69. Position current = positions.get(i);
  70. Position next = positions.get(i + 1);
  71. distance += Math.sqrt(Math.pow(next.x - current.x, 2) + Math.pow(next.y - current.y, 2));
  72. }
  73. return distance;
  74. }
  75. }
Success #stdin #stdout 0.23s 58908KB
stdin
6 3
0 0 0
0 3 4
-2 5 8
0 7 12
2 5 16
0 3 20

stdout
0 0 - 0
0 3 - 4
-2 5 - 8
0 7 - 12
2 5 - 16
0 3 - 20
-----------
0 0 - 0
0 2 - 3
-1 4 - 6
-1 6 - 9
0 7 - 12
2 6 - 15
1 4 - 18
0 3 - 20
5.42890364013152