[UCSD白板题] Points and Segments
Problem Introduction
The goal in this problem is given a set of segments on a line and a set of points on a line, to count, for each point, the number of segments which contain it.
Problem Description
Task.In this problem you are given a set of points on a line and a set of segments on a line. The goal is to compute, for each point, the number of segments that contain this point.
Input Format.The first line contains two non-negative integers \(s\) and \(p\) defining the number of segments and the number of points on a line, respectively. The next \(s\) lines contain two integers \(a_i, b_i\) defining the \(i\)-th segment \([a_i, b_i]\). The next line contains \(p\) integers defining points \(x_1,x_2,\cdots ,x_p\).
Constraints.\(1 \leq s,p \leq 50000;-10^8 \leq a_i \leq b_i \leq 10^8\) for all \(0 \leq i < s;-10^8 \leq x_j \leq 10^8\) for all \(0 \leq j < p\).
Output Format.Output \(p\) non-negative integers \(k_0,k_1,\cdots,k_{p-1}\) where \(k_i\) is the number of segments which contain \(x_i\). More formally,
\(k_i=|{j:a_j \leq x_i \leq b_j}|\)
Sample 1.
Input:
2 3
0 5
7 10
1 6 11
Output:
1 0 0
Sample 2.
Input:
1 3
-10 10
-100 100 0
Output:
0 0 1
Sample 3.
Input:
3 2
0 5
-3 2
7 10
1 6
Output:
2 0
Solution
# Uses python3
import sys
from itertools import chain
def fast_count_segments(starts, ends, points):
cnt = [0] * len(points)
a = zip(starts, [float('-inf')]*len(starts))
b = zip(ends, [float('inf')]*len(ends))
c = zip(points, range(len(points)))
sortedlist = sorted(chain(a,b,c), key=lambda a : (a[0], a[1]))
stack = []
for i, j in sortedlist:
if j == float('-inf'):
stack.append(j)
elif j == float('inf'):
stack.pop()
else:
cnt[j] = len(stack)
return cnt
def naive_count_segments(starts, ends, points):
cnt = [0] * len(points)
for i in range(len(points)):
for j in range(len(starts)):
if starts[j] <= points[i] <= ends[j]:
cnt[i] += 1
return cnt
if __name__ == '__main__':
input = sys.stdin.read()
data = list(map(int, input.split()))
n = data[0]
m = data[1]
starts = data[2:2 * n + 2:2]
ends = data[3:2 * n + 2:2]
points = data[2 * n + 2:]
#use fast_count_segments
cnt = fast_count_segments(starts, ends, points)
for x in cnt:
print(x, end=' ')
[UCSD白板题] Points and Segments的更多相关文章
- [UCSD白板题] Covering Segments by Points
Problem Introduction You are given a set of segments on a line and your goal is to mark as few point ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- [UCSD白板题] Maximize the Value of an Arithmetic Expression
Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- [UCSD白板题] Take as Much Gold as Possible
Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...
- [UCSD白板题] Primitive Calculator
Problem Introduction You are given a primitive calculator that can perform the following three opera ...
- [UCSD白板题] Number of Inversions
Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...
- [UCSD白板题] Sorting: 3-Way Partition
Problem Introduction The goal in this problem is to redesign a given implementation of the randomize ...
- [UCSD白板题] Majority Element
Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...
随机推荐
- Breakpoint is not hit
新拿到一个Silverlight项目,能够正常运行,但是一旦运行起来,断点处由实心点变成了空心的,并警告:The breakpoint will not currently be hit. No sy ...
- 修改maven默认的JDK编译版本
1.全局模式(settings.xml) <profiles> <profile> <id>jdk-1.8</id> <activation> ...
- jquery版本清单
PM> Install-Package jQuery -Version 1.11.2 jQuery 2.1.3 (latest stable) 315444 Wednesday, Decembe ...
- DataInputStream和DataOutputStream
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInp ...
- WebService基本概念及原理
一.Web Service基本概念 WebService是一种跨编程语言和跨操作系统平台的远程调用技术.Web Service也叫XML Web Service WebService是一种可以接收从I ...
- OO基本原则
1. 单一职责原则(SRP) 一个类应该最多只能有一个因素能够给导致其变化,类中的方法应该都是相关性很高的,即"高内聚" 2. 开放-封闭原则(OC) - 扩 ...
- kafka java实例
生产者 package com; import java.util.Properties; import java.util.concurrent.TimeUnit; import kafka.jav ...
- JavaWeb 学习003-简单登录页面功能实现
先说下题外话:学习不是看你学了多久,重点是学到多少: 这就要求 效率.我在这三个小时,但是有效率的又有多久?只是做了这么一点简单的事. 登录页面 跟数据库交互,进行判断是否登陆成功.我只是实现了一 ...
- Hibernate控制台显示创建数据库表语句
package cqvie.yjq.View; import org.hibernate.Session; import org.hibernate.Transaction; import org.h ...
- c语言冒泡排序
在C语言中,常用的排序算法有:冒泡排序.快速排序.插入排序.选择排序.希尔排序.堆排序以及归并排序等等. 冒泡排序基本概念: 依次比较相邻的两个数,将小数放在前面,大数放在后面. #include ...