当前位置:网站首页>[acwing] 2983. Toys
[acwing] 2983. Toys
2022-07-26 05:02:00 【Record algorithm solution】
Title address :
https://www.acwing.com/problem/content/description/2986/
Calculate the toy storage box , Number of toys in each zone . John's parents have a problem ---- Every time John finishes playing with toys, he always throws them everywhere . They prepared a rectangular toy storage box for John , For his toys . But John is very naughty , Throw the toy into the box very casually every time , Make all toys mix together at will , This makes it difficult for John to find his favorite toy . Regarding this , John's parents came up with a countermeasure , Separate the storage box into several partitions with several cardboard , In this way, at least toys thrown into different zones can be separated . The following is a top view example of a storage box .
Your task is , Whenever John throws toys into the storage box , Determine how many toys are in each zone .
Input format :
This question contains several groups of test data .
For each set of data , The first line contains 6 6 6 It's an integer 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, Expressing common ownership n n n Cardboard , m m m A toy , The coordinate of the upper left corner of the storage box is ( x 1 , y 1 ) (x_1,y_1) (x1,y1), The coordinates in the lower right corner are ( x 2 , y 2 ) (x_2,y_2) (x2,y2).
Next n n n That's ok , Each line contains two integers U i , L i U_i,L_i Ui,Li, It means the first one i i i The coordinates of the two ends of the cardboard are ( U i , y 1 ) (U_i,y_1) (Ui,y1) and ( L i , y 2 ) (L_i,y_2) (Li,y2). The data ensures that the cardboard does not intersect , And in order from left to right .
Next m m m That's ok , Each line contains two integers X j , Y j X_j,Y_j Xj,Yj, It means the first one j j j Position coordinates of toys . The order of toys is random . The data guarantees that the toy will not fall on the cardboard , Nor will it fall outside the box .
The input consists of a single 0 0 0 End of line .
Output format :
For each set of data , Output n + 1 n+1 n+1 That's ok .
n n n A cardboard divides the storage box into n + 1 n+1 n+1 Zones , It's numbered... From left to right 0 ∼ n 0∼n 0∼n.
In the order of division number from small to large , Output one line of information per line i : j i:\ j i: j, among i i i Indicates the partition number , j j j Indicates the number of toys in the zone .
Between each group of data , Separate with blank lines .
Data range :
Each test point can contain at most 10 10 10 Group data .
1 ≤ n , m ≤ 5000 1≤n,m≤5000 1≤n,m≤5000
All coordinate value ranges [ − 1 0 5 , 1 0 5 ] [−10^5,10^5] [−105,105].
For a toy ( X i , Y i ) (X_i, Y_i) (Xi,Yi), And some partition ( U i , y 1 ) − ( L i , y 2 ) (U_i,y_1)-(L_i,y_2) (Ui,y1)−(Li,y2), It is on the left side of the partition if and only if ( ( 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) Become a right-handed system . Because the clapboard is given from left to right , So a toy must be right for these partitions in turn …… Left, left ……, Thus, the position can be divided in two . The code is as follows :
#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]);
}
}
For each set of data , Time complexity O ( n + m log n ) O(n+m\log n) O(n+mlogn), Space O ( n ) O(n) O(n).
边栏推荐
- Ansible tutorial
- Whether the SQL that fails to execute MySQL is counted into the slow query?
- C language -- string function, memory function collection and Simulation Implementation
- Character function and string function (I)
- Good at C (summer vacation daily question 6)
- Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!
- Phaser (I): platform jumping collection game
- What points should be paid attention to in the selection of project management system?
- Axi protocol (4): signals on the Axi channel
- 汉字风格迁移篇---通过生成对抗网络学习一对多程式化汉字的转换和生成
猜你喜欢

Tonight! Stonedb is officially open source. Please check this strategy~

【云原生 | 17】容器的四种网络模式

基于R语言的Meta分析【全流程、不确定性分析】方法与Meta机器学习

Wsl2 best practices, eliminate difficult xshell and finalshell

How many holes have you stepped on in BigDecimal?

User defined type details

Molecular skeleton transition tool -delinker introduction

Phaser (I): platform jumping collection game

Excel VBA:按日期汇总计算输出结果(sumif)

Stm32fsmc extended SRAM
随机推荐
BigDecimal 的 4 个坑,你踩过几个?
STM32 development | ad7606 parallel multi-channel data acquisition
Stm32fsmc extended SRAM
注解@Autowired如何自动装配
JVM第五讲:纵横数据如何应对洪峰推送
Sliding window -- leetcode solution
There was an unexpected error (type=Method Not Allowed, status=405).记录报错
Customer service relationship management based on SQL net enterprise messenger enterprise communications
Bsdiff and bspatch incremental updates
常函数const的学习
软考回顾及计划
Unnamed Article 33
vector详解和迭代器失效问题
The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly 100 times the improvement of analysis performance
@Autowired注解的原理
Ggjj, do you have a look at this problem? Does caching cause cross domain problems?
webassembly 01基本资料
Recursive implementation of exponential enumeration
Five simple and practical daily development functions of chrome are explained in detail. Unlock quickly to improve your efficiency!
奥特学园ROS笔记--6