用的斜率判断左右,没有用二分加快速度,用c++提交刚刚过= =。
/*
数据:
输入:
第一行6个数分别是:
纸板数 玩具数 x1 y1 x2 y2
紧接n行纸板ui li //后期坐标为(ui,y1) (li,y2)
紧接m行玩具x y
n个纸板可以将一个区域划分成n+1个区域,将左边界右边界考虑进来,就是n+2条边。
讨论各个区域各有多少个玩具落入。
最后的输出就是
区域号:区域内存在的玩具个数。
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
输出:
0: 2
1: 1
2: 1
3: 1
4: 0
5: 1
0: 2
1: 2
2: 2
3: 2
4: 2
*/
#include"stdafx.h"
#include<iostream>
#include<math.h>
#include<algorithm>
#include<memory>
#include<string.h>
using namespace std;
struct line {
int ui;
int li;
}line[5005];
struct toy {
int toyx;
int toyy;
int belong;
}toy[5005];
int belonging[5005];
//1:右,0:左。
int ans(int x1, int y1, int x2, int y2, int x, int y)
{
/*if (x1==x2&&y1==y2)
{
if (x > x1)
return 1;
else return 0;
}
else*/
{
if ((x1 - x2) / (y1 - y2) > 0)//斜率
{
if ((x1 - x) *(y2 - y) > (x2 - x) * (y1 - y))
return 1;
else
return 0;
}
else
{
if ((x1 - x) *(y2 - y) > (x2 - x) * (y1 - y))
return 0;
else
return 1;
}
}
}
int main()
{
ios::sync_with_stdio(false);
int n, m, x1, y1, x2, y2, xmax, ymax, i, j;//(x1,y1)左上角。
while (cin >> n && n != 0)
{
cin >> m >> x1 >> y1 >> x2 >> y2;
//输入纸板ui,li。一共n+2条线。
line[0].ui = x1;
line[0].li = x1;
line[n + 1].ui = x2;
line[n + 1].li = x2;
for (i = 1; i <= n; ++i)
{
cin >> line[i].ui >> line[i].li;
}
//输入玩具。
for (i = 1; i <= m; ++i)
{
cin >> toy[i].toyx >> toy[i].toyy;
}
//根据点遍历所有线。
int now = 0, compared;
for (i = 1; i <= m; ++i)//点
{
for (j = 0; j <= n + 1; ++j)//线
{
compared = ans(line[j].ui, y1, line[j].li, y2, toy[i].toyx, toy[i].toyy);
if (now != compared)
{
//toy[i].belong = j - 1;//j-1区域。
++belonging[j - 1];
break;
}
}
}
for (i = 0; i <= n; ++i)
{
cout << i << ": " << belonging[i] << endl;
belonging[i] = 0;
}
cout << endl;
}
return 0;
}