Gridland Metro

Sort by

recency

|

420 Discussions

|

  • + 0 comments

    This was the last task on this stupid site. Fortunately, there are many good sites with a similar focus.

  • + 0 comments

    What kind of idiotic condition is this? How can this be?

    k = 3
    r   c1  c2
    1   1   4
    2   2   4
    3   1   2
    4   2   3
    

    `Why k=3 if I see 4 tracks??? I have already tried several solutions, but none of them work.

  • + 0 comments
    import java.util.Scanner;
    import java.util.HashMap;
    
    public class GridlandMetro {
    
         static long gridlandMetro(int n, int m, int k, int[][] track) {
            long total = (long) n * m;
            HashMap<Integer, int[]> d = new HashMap<>();
    
            for (int i = 0; i < k; i++) {
                int r = track[i][0];
                int c1 = track[i][1];
                int c2 = track[i][2];
    
                if (!d.containsKey(r)) {
                    d.put(r, new int[]{c1, c2});
    > >             } else if (c1 > d.get(r)[1]) {
                    total -= c2 - c1 + 1;
                } else if (c2 > d.get(r)[1]) {
                    d.get(r)[1] = c2;
                }
            }
    
            long tracks = 0;
            for (int[] range : d.values()) {
                tracks += range[1] - range[0] + 1;
            }
    
            long lamps = total - tracks;
            return lamps;
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            // Read input
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            int k = scanner.nextInt();
    
            int[][] track = new int[k][3];
            for (int i = 0; i < k; i++) {
                track[i][0] = scanner.nextInt();
                track[i][1] = scanner.nextInt();
                track[i][2] = scanner.nextInt();
            }
    
            // Calculate and print the result
            long result = gridlandMetro(n, m, k, track);
            System.out.println(result);
    
            scanner.close();
        }
    }
    
  • + 0 comments
    def gridlandMetro(n, m, k, track):
        bins = defaultdict(list)
        # match each track interval to its row
        for t, s, e in track:
            bins[t].append([s, e])
        # initially we have the whole grid available
        ans = m * n
        for b in bins.values():
            # sort the intervals by start time/position
            b.sort()
            gap = 0
            l_bound, r_bound = b[0]
            for i in range(1, len(b)):
                # the intervals overlap - combine them
                if b[i][0] <= r_bound:
                    l_bound = min(l_bound, b[i][0])
                    r_bound = max(r_bound, b[i][1])
                # the intervals don't overlap - we need to remove space from the grid
                else:
                    gap += r_bound - l_bound + 1
                    l_bound, r_bound = b[i]
            # the final interval won't be handled in the loop
            gap += r_bound - l_bound + 1
            ans -= gap
            
        return ans
    
  • + 0 comments

    The int in the boilercode needs to be changed to long. Some of the test cases have large n and m, and result will exceed 32bits (int size).