给定线段和一些点,对于每个点,找到覆盖该点的线段数。
A segment (l, r) covers a point x if and only if l < = x < = r.
例子:
Input: Segments = {{0, 3}, {1, 3}, {3, 8}},
Points = {-1, 3, 8}.
Output : {0, 3, 1}
Explanation :
- No segments passing through point -1
- All the segments passing through point 3
- Segment 3rd passing through point 8
Input: Segments = {{1, 3}, {2, 4}, {5, 7}},
Points = {0, 2, 5}.
Output: {0, 2, 1}
Explanation :
- No segments passing through point 0
- 1st and 2nd segment passing through point 2
- Segment 3rd passing through point 5
方法:
- 我们可以通过使用类似于前缀和的逻辑来做到这一点。
- 让我们用(l,r)表示一个线段。形成一个成对的向量,对于每个段,在向量中推入两对,其值分别为(l,+1)和(r + 1,-1)。
- 按升序对点进行排序,但我们还需要它的位置,因此将其与它的位置映射在一起。
- 对段向量进行降序排序,因为我们从后面对其进行了迭代。
- 对段进行可变计数,该计数最初为零。
- 然后,我们将迭代该点,并从线段向量中弹出该对,直到其第一个值小于当前点,然后将其第二个值添加到计数中。
- 最后,将count的值存储在数组中的相应位置并打印该数组。
下面是上述方法的实现。
C++
// C++ program to find the number of
// segments covering each points
#include
using namespace std;
// Function to print an array
void PrintArray(int n,int arr[])
{
for(int i = 0; i < n; i++)
{
cout< >segments,
vectorpoints, int s, int p)
{
vector< pair >pts, seg;
// Pushing points and index in
// vector as a pairs
for(int i = 0; i < p; i++)
{
pts.push_back({points[i], i});;
}
for(int i = 0; i < s; i++)
{
// (l,+1)
seg.push_back({segments[i].first, 1});
// (r+1,-1)
seg.push_back({segments[i].second+1, -1});
}
// Sort the vectors
sort(seg.begin(), seg.end(),
greater>());
sort(pts.begin(),pts.end());
int count = 0;
int ans[p];
for(int i = 0; i < p; i++)
{
int x = pts[i].first;
while(!seg.empty() &&
seg.back().first <= x)
{
count+= seg.back().second;
seg.pop_back();
}
ans[pts[i].second] = count;
}
// Print the answer
PrintArray(p, ans);
}
//Driver code
int main()
{
// Initializing vector of pairs
vector>seg;
// Push segments
seg.push_back({0, 3});
seg.push_back({1, 3});
seg.push_back({3, 8});
// Given points
vectorpoint{-1, 3, 7};
int s = seg.size();
int p = point.size();
NumberOfSegments(seg, point, s, p);
return 0;
}
Java
// Java program to find the number of
// segments covering each points
import java.util.*;
import java.lang.*;
class GFG{
// Function to print an array
static void PrintArray(int n,int arr[])
{
for(int i = 0; i < n; i++)
{
System.out.print(arr[i] + " ");
}
}
// Funtion prints number of segments
// covering by each points
static void NumberOfSegments(ArrayList segments,
int[] points, int s, int p)
{
ArrayList pts = new ArrayList<>(),
seg = new ArrayList<>();
// Pushing points and index in
// vector as a pairs
for(int i = 0; i < p; i++)
{
pts.add(new int[]{points[i], i});
}
for(int i = 0; i < s; i++)
{
// (l,+1)
seg.add(new int[]{segments.get(i)[0], 1});
// (r+1,-1)
seg.add(new int[]{segments.get(i)[1] + 1, -1});
}
// Sort the vectors
Collections.sort(seg, (a, b) -> b[0] - a[0]);
Collections.sort(pts, (a, b) -> a[0] - b[0]);
int count = 0;
int[] ans = new int[p];
for(int i = 0; i < p; i++)
{
int x = pts.get(i)[0];
while (seg.size() != 0 &&
seg.get(seg.size() - 1)[0] <= x)
{
count += seg.get(seg.size() - 1)[1];
seg.remove(seg.size() - 1);
}
ans[pts.get(i)[1]] = count;
}
// Print the answer
PrintArray(p, ans);
}
// Driver code
public static void main(String[] args)
{
// Initializing vector of pairs
ArrayListseg = new ArrayList<>();
// Push segments
seg.add(new int[]{0, 3});
seg.add(new int[]{1, 3});
seg.add(new int[]{3, 8});
// Given points
int[] point = {-1, 3, 7};
int s = seg.size();
int p = point.length;
NumberOfSegments(seg, point, s, p);
}
}
// This code is contributed by offbeat
Python3
# Python3 program to find the number
# of segments covering each point
# Function to print an array
def PrintArray(n, arr):
for i in range(n):
print(arr[i], end = " ")
# Funtion prints number of segments
# covering by each points
def NumberOfSegments(segments, points, s, p):
pts = []
seg = []
# Pushing points and index in
# vector as a pairs
for i in range(p):
pts.append([points[i], i])
for i in range(s):
# (l, +1)
seg.append([segments[i][0], 1])
# (r+1, -1)
seg.append([segments[i][1] + 1, -1])
# Sort the vectors
seg.sort(reverse = True)
pts.sort(reverse = False)
count = 0
ans = [0 for i in range(p)]
for i in range(p):
x = pts[i][0]
while(len(seg) != 0 and
seg[len(seg) - 1][0] <= x):
count += seg[len(seg) - 1][1]
seg.remove(seg[len(seg) - 1])
ans[pts[i][1]] = count
# Print the answer
PrintArray(p, ans)
# Driver code
if __name__ == '__main__':
# Initializing vector of pairs
seg = []
# Push segments
seg.append([ 0, 3 ])
seg.append([ 1, 3 ])
seg.append([ 3, 8 ])
# Given points
point = [ -1, 3, 7 ]
s = len(seg)
p = len(point)
NumberOfSegments(seg, point, s, p)
# This code is contributed by Bhupendra_Singh
输出:
0 3 1
时间复杂度:O(s + p)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。