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
QHEAP1
This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element
View Solution →Jesse and Cookies
Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t
View Solution →Find the Running Median
The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.
View Solution →Minimum Average Waiting Time
Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h
View Solution →Merging Communities
People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w
View Solution →Components in a graph
There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu
View Solution →