Gridland Metro
Problem Statement :
The city of Gridland is represented as an n x m matrix where the rows are numbered from 1 to n and the columns are numbered from 1 to m. Gridland has a network of train tracks that always run in straight horizontal lines along a row. In other words, the start and end points of a train track are and , where represents the row number, represents the starting column, and represents the ending column of the train track. The mayor of Gridland is surveying the city to determine the number of locations where lampposts can be placed. A lamppost can be placed in any cell that is not occupied by a train track. Given a map of Gridland and its train tracks, find and print the number of cells where the mayor can place lampposts. Note: A train track may overlap other train tracks within the same row. In this case, there are five open cells (red) where lampposts can be placed. Function Description Complete the gridlandMetro function in the editor below. gridlandMetro has the following parameter(s): int n:: the number of rows in Gridland int m:: the number of columns in Gridland int k:: the number of tracks track[k][3]: each element contains integers that represent , all 1-indexed Returns int: the number of cells where lampposts can be installed Input Format The first line contains three space-separated integers and , the number of rows, columns and tracks to be mapped. Each of the next lines contains three space-separated integers, and , the row number and the track column start and end.
Solution :
Solution in C :
In C++ :
#include <bits/stdc++.h>
using namespace std;
int main() {
long long h, w, K;
cin >> h >> w >> K;
long long ans = h * w;
map<int, vector<pair<int, int>>> mp;
for (int i = 0; i < K; i++) {
int y, l, r;
scanf("%d %d %d", &y, &l, &r);
mp[y].emplace_back(l, r);
}
for (auto &kv : mp) {
vector<pair<int, int>> evs;
for (auto p : kv.second) {
evs.emplace_back(p.first, 0);
evs.emplace_back(p.second, 1);
}
sort(evs.begin(), evs.end());
int cnt = 0;
int l = 0;
for (auto e : evs) {
if (e.second == 0) {
if (cnt == 0) l = e.first;
cnt++;
} else {
cnt--;
if (cnt == 0) ans -= e.first - l + 1;
}
}
}
cout << ans << endl;
}
In Java :
import java.util.*;
import java.awt.*;
public class Solution {
ArrayList<Point> locs;
public Solution(){
locs = new ArrayList<>();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long rows = in.nextInt(), cols = in.nextInt();
int tracks = in.nextInt();
ArrayList<Integer> check = new ArrayList<>();
HashMap<Integer, Solution> found = new HashMap<>();
for(int i = 0; i<tracks; i++){
int curRow = in.nextInt(), start = in.nextInt(), end = in.nextInt();
if(!found.containsKey(curRow)){
Solution sol = new Solution();
sol.locs.add(new Point(start,end));
check.add(curRow);
found.put(curRow,sol);
}else{
Solution sol = found.get(curRow);
sol.locs.add(new Point(start, end));
}
}
//Computing the total # of tracks - those of train tracks
long total = rows * cols;
for(int r : check){
// System.out.println(r);
Solution sol = found.get(r);
ArrayList<Point> myPoints = sol.locs;
Collections.sort(myPoints, new myComparator());
Point first = myPoints.get(0);
total -= (first.y - first.x+1);
int lastEnd = first.y+1;
for(int i = 1; i<myPoints.size(); i++){
Point curPoint = myPoints.get(i);
if(curPoint.y< lastEnd) continue;
int begin = Math.max(curPoint.x, lastEnd);
total -=(curPoint.y - begin + 1);
lastEnd = curPoint.y+1;
}
}
System.out.println(total);
}
}
class myComparator implements Comparator<Point>{
public int compare(Point p1, Point p2){
return p1.x - p2.x;
}
}
In C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct points
{
long int r;
long int c1;
long int c2;
};
typedef struct points points;
int comp(const void* a,const void* b)
{
points* a1=(points*)a;
points* b1=(points*)b;
if((a1->r)!=(b1->r))
return (a1->r)-(b1->r);
else
{
if((a1->c1)!=(b1->c1))
return (a1->c1)-(b1->c1);
else
return (a1->c2)-(b1->c2);
}
}
long int min(long int a,long int b)
{
if(a<b)
return a;
return b;
}
long int max(long int a,long int b)
{
if(a>b)
return a;
return b;
}
int main() {
long int n,m,r,c1,c2,temp,r_s,r_e;
int k,i,j;
scanf("%ld %ld %d",&n,&m,&k);
points p[k];
for(i=0;i<k;i++)
scanf("%ld %ld %ld",&p[i].r,&p[i].c1,&p[i].c2);
qsort(p,k,sizeof(p[0]),comp);
int visited[k];
memset(visited,0,sizeof(visited));
long long ans=n*m;
for(i=0;i<k;i++)
{
long int sub=0;
if(visited[i]==0){
for(j=i+1;j<k;j++)
{
if(p[i].r==p[j].r)
{
if(p[i].c2<p[j].c1)
{
sub+=p[i].c2-p[i].c1+1;
p[i].c1=p[j].c1;
p[i].c2=p[j].c2;
}
else
{
r_s=min(p[i].c1,p[j].c1);
r_e=max(p[i].c2,p[j].c2);
p[i].c1=r_s;
p[i].c2=r_e;
}
visited[j]=1;
}
else
break;
}
ans-=sub;
ans-=(p[i].c2-p[i].c1+1);
}
}
printf("%lld",ans);
return 0;
}
In Python3 :
from collections import defaultdict
n, m, k = map(int, input().split())
tracks = defaultdict(list)
for _ in range(k):
row, c1, c2 = map(int, input().split())
tracks[row].append((c1, -1))
tracks[row].append((c2 + 1, 1))
ans = 0
for row in tracks:
tracks[row].sort()
prev = tracks[row][0][0]
depth = 1
for event in tracks[row][1:]:
if depth > 0:
ans += (event[0] - prev)
depth -= event[1]
prev = event[0]
print(n * m - ans)
View More Similar Problems
Kitty's Calculations on a Tree
Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a
View Solution →Is This a Binary Search Tree?
For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a
View Solution →Square-Ten Tree
The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the
View Solution →Balanced Forest
Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a
View Solution →Jenny's Subtrees
Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .
View Solution →Tree Coordinates
We consider metric space to be a pair, , where is a set and such that the following conditions hold: where is the distance between points and . Let's define the product of two metric spaces, , to be such that: , where , . So, it follows logically that is also a metric space. We then define squared metric space, , to be the product of a metric space multiplied with itself: . For
View Solution →