当前位置:网站首页>【ACWing】2983. 玩具
【ACWing】2983. 玩具
2022-07-26 04:58:00 【记录算法题解】
题目地址:
https://www.acwing.com/problem/content/description/2986/
计算玩具收纳盒中,每个分区内的玩具数量。约翰的父母有一个烦恼----约翰每次玩完玩具以后总会将玩具乱扔。他们为约翰准备了一个长方形的玩具收纳盒,用来放他的玩具。但是约翰非常调皮,每次都非常随意的将玩具扔进盒子中,使得所有玩具都随意混在一起,这让约翰难以找到他喜欢的玩具。对此,约翰的父母想出了一个对策,用若干个纸板将收纳盒分隔成若干个分区,这样至少扔到不同分区的玩具之间还是能分开的。下面是一个收纳盒的俯视图示例。
你的任务是,每当约翰将玩具扔进收纳盒中时,确定每个分区中有多少个玩具。
输入格式:
本题包含多组测试数据。
对于每组数据,第一行包含 6 6 6个整数 n , m , x 1 , y 1 , x 2 , y 2 n,m,x_1,y_1,x_2,y_2 n,m,x1,y1,x2,y2,表示共有 n n n个纸板, m m m个玩具,收纳盒的左上角坐标为 ( x 1 , y 1 ) (x_1,y_1) (x1,y1),右下角坐标为 ( x 2 , y 2 ) (x_2,y_2) (x2,y2)。
接下来 n n n行,每行包含两个整数 U i , L i U_i,L_i Ui,Li,表示第 i i i个纸板的两端点坐标分别为 ( U i , y 1 ) (U_i,y_1) (Ui,y1)和 ( L i , y 2 ) (L_i,y_2) (Li,y2)。数据保证纸板之间不相交,且按照从左至右顺序依次给出。
接下来 m m m行,每行包含两个整数 X j , Y j X_j,Y_j Xj,Yj,表示第 j j j个玩具的位置坐标。玩具的给出顺序是随机的。数据保证玩具不会恰好落在纸板上,也不会落在盒子外。
输入由包含单个 0 0 0的一行结束。
输出格式:
对于每组数据,输出 n + 1 n+1 n+1行。
n n n个纸板将收纳盒划分为了 n + 1 n+1 n+1个分区,从左到右编号为 0 ∼ n 0∼n 0∼n。
按照分区编号从小到大的顺序,每行输出一行信息 i : j i:\ j i: j,其中 i i i表示分区编号, j j j表示分区内玩具数量。
每组数据之间,用空行隔开。
数据范围:
每个测试点最多包含 10 10 10组数据。
1 ≤ n , m ≤ 5000 1≤n,m≤5000 1≤n,m≤5000
所有坐标取值范围 [ − 1 0 5 , 1 0 5 ] [−10^5,10^5] [−105,105]。
对于某个玩具 ( X i , Y i ) (X_i, Y_i) (Xi,Yi),和某个隔板 ( U i , y 1 ) − ( L i , y 2 ) (U_i,y_1)-(L_i,y_2) (Ui,y1)−(Li,y2),它在隔板左边当且仅当 ( ( U i , y 1 ) − ( L i , y 2 ) ) × ( ( X i , Y i ) − ( L i , y 2 ) ) = ( U i − L i , y 1 − y 2 ) × ( X i − L i , Y i − y 2 ) ((U_i,y_1)-(L_i,y_2))\times ((X_i, Y_i)-(L_i,y_2))=\\(U_i-L_i,y_1-y_2)\times (X_i-L_i, Y_i-y_2) ((Ui,y1)−(Li,y2))×((Xi,Yi)−(Li,y2))=(Ui−Li,y1−y2)×(Xi−Li,Yi−y2)成右手系。由于隔板是从左到右给出的,所以某个玩具一定对于这些隔板依次是右右……左左……,从而可以二分出位置。代码如下:
#include <iostream>
#include <cstring>
#define x first
#define y second
using namespace std;
using PLL = pair<long, long>;
const int N = 5010;
int n, m;
PLL a[N], b[N];
int res[N];
PLL operator-(PLL a, PLL b) {
return {
a.x - b.x, a.y - b.y};
}
long cross(PLL a, PLL b) {
return a.x * b.y - a.y * b.x;
}
int find(long x, long y) {
int l = 0, r = n;
while (l < r) {
int mid = l + r >> 1;
if (cross(a[mid] - b[mid], PLL{
x, y} - b[mid]) > 0) r = mid;
else l = mid + 1;
}
return l;
}
int main() {
bool is_first = true;
while (scanf("%d", &n), n) {
long x1, y1, x2, y2;
scanf("%d%ld%ld%ld%ld", &m, &x1, &y1, &x2, &y2);
for (int i = 0; i < n; i++) {
long u, l;
scanf("%ld%ld", &u, &l);
a[i] = {
u, y1}, b[i] = {
l, y2};
}
a[n] = {
x2, y1}, b[n] = {
x2, y2};
if (is_first) is_first = false;
else puts("");
memset(res, 0, sizeof res);
while (m--) {
long x, y;
scanf("%ld%ld", &x, &y);
res[find(x, y)]++;
}
for (int i = 0; i <= n; i++)
printf("%d: %d\n", i, res[i]);
}
}
对于每组数据,时间复杂度 O ( n + m log n ) O(n+m\log n) O(n+mlogn),空间 O ( n ) O(n) O(n)。
边栏推荐
- Solve the error string value: '\xf0\x9f\x98\xad',... 'for column' commentcontent 'at row 1
- SQL加解密注入详解
- The importance of supporting horizontal expansion of time series database
- webassembly 01基本资料
- AXI协议(5):AXI协议的burst机制
- 补位,稍后补上
- 快恢复二极管工作原理及使用
- Molecular skeleton transition tool -delinker introduction
- 创建MySQL数据库的两种方式
- Autocomplete prevents the form from automatically filling in
猜你喜欢

AXI协议(5):AXI协议的burst机制

Is this my vs not connected to the database

Axi protocol (5): burst mechanism of Axi protocol

Face database collection summary

Array sort 2

There was an unexpected error (type=method not allowed, status=405)

The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly 100 times the improvement of analysis performance

QT compilation error sorting and remote module Download

UE4 keyboard control switch light

Icml2022 | imitation learning by evaluating the professional knowledge of the presenter
随机推荐
webassembly 01基本资料
2、 Internationally renowned project HelloWorld
[enterprise micro procedure]
[wp][gwctf 2019] boring lottery
时代潮流-云原生数据库的崛起
C language - pointer one touch ※
UE4 switching of control rights of multiple roles
Can serial port can 232 can 485 serial port to CANbus bus gateway module can232/485mb converter cancom
NFT的几种发行方式你都了解过吗?不同的发行方式有什么优缺点?
Good at C (summer vacation daily question 6)
Yapi installation
Kubernetes 进阶训练营 调度器
UE4 displays text when it is close to the object, and disappears when it is far away
Axi protocol (4): signals on the Axi channel
What are the well-known to-do apps at home and abroad
有ggjj看看这个问题没,是否缓存导致跨域问题?
2022 Henan Mengxin League game (3): Henan University a - corn cannon
2022杭电多校第二场 A.Static Query on Tree(树剖)
Torch slice maintenance
STM32 development | ad7606 parallel multi-channel data acquisition