
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to count number of cities we can visit from each city with given operations
Suppose we have a list of N coordinate points P in the form (xi, yi). The x and y values are permutation of first N natural numbers. For each k in range 1 to N. We are at city k. We can apply the operations arbitrarily many times. The operation: We move to another city that has a smaller x-coordinate and a smaller y-coordinate or larger x or larger y coordinate than the city we are currently in. We have to find the number of cities we can reach from city k.
So, if the input is like P = [[1, 4], [2, 3], [3, 1], [4, 2]], then the output will be [1, 1, 2, 2]
Steps
To solve this, we will follow these steps −
n := size of P Define one 2D array lst for initialize i := 0, when i < n, update (increase i by 1), do: v := { P[i, 0], P[i, 1], i } insert v at the end of lst sort the array lst y_min := 1e9 Define one set se Define an array ans of size n and fill with 0 for initialize i := 0, when i < n, update (increase i by 1), do: y_min := minimum of y_min and lst[i, 1] insert lst[i, 2] into se if y_min + i is same as n, then: for each element j in se ans[j] := size of se clear the set se if i is same as n - 1, then: for each element j in se ans[j] := size of se for initialize i := 0, when i < n, update (increase i by 1), do: display ans[i]
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(vector<vector<int>> P){ int n = P.size(); vector<vector<int>> lst; for (int i = 0; i < n; i++){ vector<int> v = { P[i][0], P[i][1], i }; lst.push_back(v); } sort(lst.begin(), lst.end()); int y_min = 1e9; set<int> se; vector<int> ans(n, 0); for (int i = 0; i < n; i++){ y_min = min(y_min, lst[i][1]); se.insert(lst[i][2]); if (y_min + i == n){ for (auto j : se) ans[j] = se.size(); se.clear(); } if (i == n - 1){ for (auto j : se) ans[j] = se.size(); } } for (int i = 0; i < n; i++){ cout << ans[i] << ", "; } } int main(){ vector<vector<int>> P = { { 1, 4 }, { 2, 3 }, { 3, 1 }, { 4, 2 } }; solve(P); }
Input
{ { 1, 4 }, { 2, 3 }, { 3, 1 }, { 4, 2 } }
Output
1, 1, 2, 2,
- Related Articles
- Program to check we can visit any city from any city or not in Python
- Program to count number of words we can generate from matrix of letters in Python
- Count the number of operations required to reduce the given number in C++
- C++ program to count number of dodecagons we can make of size d
- Total Distance to Visit City Blocks in Python
- Program to count number of ways we can throw n dices in Python
- Program to count how many times we can find "pizza" with given string characters in Python
- Program to count number of ways we can distribute coins to workers in Python
- C++ program to count minimum number of operations needed to make number n to 1
- Program to count maximum number of strings we can generate from list of words and letter counts in python
- Program to count number of strings we can make using grammar rules in Python
- Program to count number of palindromes of size k can be formed from the given string characters in Python
- How can we reduce traffic in Cities?
- How can we get the count of all MySQL event-related operations collectively?
- C++ program to count expected number of operations needed for all node removal

Advertisements