1. 程式人生 > >[kuangbin帶你飛]專題十三 基礎計算幾何A

[kuangbin帶你飛]專題十三 基礎計算幾何A

A - TOYS


Calculate the number of toys that land in each bin of a partitioned toy box. 
Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for John to find his favorite toys. 

John's parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box. 
 
For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box. Input The input file contains one or more problems. The first line of a problem consists of six integers, n m x1 y1 x2 y2. The number of cardboard partitions is n (0 < n <= 5000) and the number of toys is m (0 < m <= 5000). The coordinates of the upper-left corner and the lower-right corner of the box are (x1,y1) and (x2,y2), respectively. The following n lines contain two integers per line, Ui Li, indicating that the ends of the i-th cardboard partition is at the coordinates (Ui,y1) and (Li,y2). You may assume that the cardboard partitions do not intersect each other and that they are specified in sorted order from left to right. The next m lines contain two integers per line, Xj Yj specifying where the j-th toy has landed in the box. The order of the toy locations is random. You may assume that no toy will land exactly on a cardboard partition or outside the boundary of the box. The input is terminated by a line consisting of a single 0. Output The output for each problem will be one line for each separate bin in the toy box. For each bin, print its bin number, followed by a colon and one space, followed by the number of toys thrown into that bin. Bins are numbered from 0 (the leftmost bin) to n (the rightmost bin). Separate the output of different problems by a single blank line. Sample Input
5 6 0 10 60 0
3 1
4 3
6 8
10 10
15 30
1 5
2 1
2 8
5 5
40 10
7 9
4 10 0 10 100 0
20 20
40 40
60 60
80 80
 5 10
15 10
25 10
35 10
45 10
55 10
65 10
75 10
85 10
95 10
0
Sample Output
0: 2
1: 1
2: 1
3: 1
4: 0
5: 1

0: 2
1: 2
2: 2
3: 2
4: 2
Hint As the example illustrates, toys that fall on the boundary of the box are "in" the box. 程式碼:
#include<cstdio>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long  ll;
const int maxn=1e5+10;
struct Node{
	int x,y;
};
struct Line{
	Node a,b;
}line[maxn]; 
int n,m;
int pos[maxn];
Node lu,rl,tmp;
int chaji(Node p0,Node p1,Node p2)
{
	return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
void check(Node d)
{
	for(int i=0;i<n;i++)
	{
		if(chaji(line[i].b,d,line[i].a)>0) continue;
		else {
			++pos[i];
			return;
		}
	}
	++pos[n];
	return;
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0) break;
		scanf("%d%d%d%d%d",&m,&lu.x,&lu.y,&rl.x,&rl.y); 
		memset(pos,0,sizeof(pos));
		for(int i=0;i<n;i++)
		{
		    cin>>line[i].a.x>>line[i].b.x;
		    line[i].a.y=lu.y;
		    line[i].b.y=rl.y;
		} 
		for(int i=0;i<m;i++)
		{
			cin>>tmp.x>>tmp.y;
			check(tmp);
		}
		for(int i=0;i<=n;i++)
		{
			cout<<i<<": "<<pos[i]<<endl;
		}
		cout<<endl;
	}
	return 0; 
} 



相關推薦

[kuangbin]專題十三 基礎計算幾何A

A - TOYS Calculate the number of toys that land in each bin of a partitioned toy box.  Mom and dad have a problem - their child John never puts his toy

[kuangbin]專題四 最短路練習 A-D

  上了大二,又有新生來了,壓力也來了,不能像大一這麼鬆懈了,所以開始每天刷刷題,想根據專題一個知識點一個知識點過   今天是最短路專題,對於最短路,能想到的方法大概就是dijkstra演算法(求單源最短路不含負環)O(n^2)如果使用堆優化,就是O(mlogn),還有就是floyd演算法(求圖上任意兩點的

[kuangbin]專題十二 基礎DP1

A - Max Sum Plus Plus 求一個數列中取m個不相交子序列所取得的最大值。 dp[i][j]表示取i個子序列且最後一個序列的末尾為j時取得的最大值,所以對於任意一個j只有兩種情況,單獨形成一個新序列的開頭或者加入上一個序列。 #incl

[kuangbin]專題十四 數論基礎——A 題解

Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs some bamboos for his stu

kuangbin專題一 簡單搜索 題解

上下 bst turn str 表示 stream 就是 += find 目錄 [kuangbin帶你飛]專題一 簡單搜索 [kuangbin帶你飛]專題一 簡單搜索 總結:用時2天半終於把這個專題刷完了 對於最基礎的dfs bfs 路徑打印 狀態轉移也有了一點自己些

I - Fire! ~~ [kuangbin]專題一 簡單搜尋

題目描述 喬在迷宮中工作。不幸的是,迷宮的一部分著火了,迷宮的主人沒有制定火災的逃跑計劃。請幫助喬逃離迷宮。根據喬在迷宮中的位置以及迷宮的哪個方塊著火,你必須確定火焰燒到他之前,喬是否可以離開迷宮,如果能離開他能跑多快。 喬和火每分鐘移動一個方格,上、下、左、右,四個方向中的一個。火勢向四個方向

O - Meteor Shower ~~~ [kuangbin]專題一 簡單搜尋

Description - 題目描述 Bessie聽說有場史無前例的流星雨即將來臨;有讖言:隕星將落,徒留灰燼。為保生機,她誓將找尋安全之所(永避星墜之地)。目前她正在平面座標系的原點放牧,打算在群星斷其生路前轉移至安全地點。 此次共有M (1 ≤ M ≤ 50,000)

M - Find a way ~~~ [kuangbin]專題一 簡單搜尋

hsj和lsh最近迷上了pokemon go的遊戲。在雙十一大物期中考試來臨之前,他們想抓一隻稀有土撥鼠來攢攢人品(因為土撥鼠的重新整理地點最近來到了哈工程) 但是由於土撥鼠過於強大,他的雷霆半月斬以及驚天浪濤沙都可以輕鬆的將他們兩擊敗,但是他們兩的合擊必殺技流影電光閃以及天羽屠鼠舞可以將土撥鼠打至

L - 非常可樂 ~~ [kuangbin]專題一 簡單搜尋

大家一定覺的運動以後喝可樂是一件很愜意的事情,但是seeyou卻不這麼認為。因為每次當seeyou買了可樂以後,阿牛就要求和seeyou一起分享這一瓶可樂,而且一定要喝的和seeyou一樣多。但seeyou的手中只有兩個杯子,它們的容量分別是N 毫升和M 毫升 可樂的體積為S (S<101)毫

N - Red and Black ~~ [kuangbin]專題一 簡單搜尋

有一個長方形的房間,覆蓋了正方形的磁磚。每塊磁磚的顏色,要麼是紅色,要麼是黑色。一名男子站在一塊黑色的磁磚上。他可以從一塊磁磚移至相鄰四塊磁磚中的某一塊。但是,他不允許在紅色磁磚上移動,他只允許在黑色磁磚上移動。 編寫一個程式,使得他允許重複上述的移動,判斷他所能到達的黑色磁磚的數量。 輸入

K - Oil Deposits ~~ [kuangbin]專題一 簡單搜尋

GeoSurvComp地質調查公司負責探測地下石油儲藏。 GeoSurvComp現在在一塊矩形區域探測石油,並把這個大區域分成了很多小塊。他們通過專業裝置,來分析每個小塊中是否蘊藏石油。如果這些蘊藏石油的小方格相鄰,那麼他們被認為是同一油藏的一部分。在這塊矩形區域,可能有很多油藏。你的任務是確定有多

F - Prime Path ~~ [kuangbin]專題一 簡單搜尋

給你兩個四位的素數a,b。 a可以改變某一位上的數字變成c,但只有當c也是四位的素數時才能進行這種改變。 請你計算a最少經過多少次上述變換才能變成b。 例如:1033 -> 8179 1033 1733 3733 3739 3779 8779 8179 最少變換了6次。 Input 第

E - Find The Multiple ~ [kuangbin]專題一 簡單搜尋

給定一個正整數n,請編寫一個程式來尋找n的一個非零的倍數m,這個m應當在十進位制表示時每一位上只包含0或者1。你可以假定n不大於200且m不多於100位。 提示:本題採用Special Judge,你無需輸出所有符合條件的m,你只需要輸出任一符合條件的m即可。 Input 輸入包含多組資料,

C - Catch That Cow ~ [kuangbin]專題一 簡單搜尋

  農夫知道一頭牛的位置,想要抓住它。農夫和牛都於數軸上 ,農夫起始位於點 N(0<=N<=100000) ,牛位於點 K(0<=K<=100000) 。農夫有兩種移動方式: 1、從 X移動到 X-1或X+1 ,每次移動花費一分鐘 2、從 X移動到 2*X ,每

A - 棋盤問題 ~ [kuangbin]專題一 簡單搜尋

在一個給定形狀的棋盤(形狀可能是不規則的)上面擺放棋子,棋子沒有區別。要求擺放時任意的兩個棋子不能放在棋盤中的同一行或者同一列,請程式設計求解對於給定形狀和大小的棋盤,擺放k個棋子的所有可行的擺放方案C。 Input 輸入含有多組測試資料。 每組資料的第一行是兩個正整數,n k,用一個空格隔

[kuangbin]專題六 最小生成樹

You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are expected to write a com

[kuangbin]專題一 簡單搜尋

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numb

[kuangbin]專題一 簡單搜尋 題解報告

又重頭開始刷kuangbin,有些題用了和以前不一樣的思路解決。全部題解如下 點選每道題的標題即可跳轉至VJ題目頁面。 A-棋盤問題 棋子不能擺在相同行和相同列,所以我們可以依此列舉每一行,然後標記每一列是否走過,在此基礎上進行DFS即可。 程式碼如下: 1 #include <i

[kuangbin]專題二-搜尋進階-D-Escape

ACM模版 描述 題解 這種問題不難,無非兩點,一:看懂題,二:細心。 矩陣的搜尋一般使用 bfs 比較多,也比較好,這裡的標記要用一個三維陣列,多出來的一維用來標記時間,記住要用 bool 型,習慣性的使用 int 型會爆記憶體的,其他也沒

[kuangbin]專題十六 KMP & 擴充套件KMP & Manacher 題解報告

來刷kuangbin字串了,字串處理在ACM中是很重要的,一般比賽都會都1——2道有關字串處理的題目,而且不會很難的那種,大多數時候都是用到一些KMP的性質或者找規律。 點選標題可跳轉至VJ比賽題目連結。 A - Number Sequence 題意就是讓你去找在串A找串B首次出現的位置,現在串不是字串