题目链接:http://poj.org/problem?id=2318
题面:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17413 | Accepted: 8300 |
Description
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
Output
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
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 5e3 + ;
int n, m, x1, x2, y1, y2;
int num[maxn]; struct node {
int x, y;
bool operator < (const node &b) const {
return x < b.x;
}
} P[maxn], L[maxn], nw, nxt; //P储存所访问的点(其实也可以不用数组的),L储存直线(对于直线此处的x为上方的x,y为下方的y); double dot(node a, node b) {
return (a.x * b.y - a.y * b.x);
} bool check(node p, node pp) {
nw.x = p.x - pp.y, nw.y = p.y - y2, nxt.x = pp.x - pp.y, nxt.y = y1 - y2;
if( dot(nw, nxt) < ) { //为负则说明当前访问的点在该直线的左端;
return true;
} else
return false;
} void Throw(node p) {
for(int i = ; i < n; i++) {
if(check(p, L[i])) {
num[i]++;
return;
}
}
num[n]++;
return;
} int main() {
//freopen("in.txt", "r", stdin);
while(~scanf("%d", &n) && n) {
scanf("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2);
memset(num, , sizeof(num));
for(int i = ; i < n; i++) {
scanf("%d%d", &L[i].x, &L[i].y);
}
sort(L, L + n);
for(int i = ; i < m; i++) {
scanf("%d%d", &P[i].x, &P[i].y);
Throw(P[i]);
}
for(int i = ; i <= n; i++) {
printf("%d: %d\n", i, num[i]);
}
printf("\n");
}
return ;
}
18年10月6日更新:
前面的代码是以前看题解写的,今天再做一次,发现就是求一个叉积的事。
思路:首先将所有直线用结构体存起来,按照u排序,若u相同则按照l排序;这样就使得线段是有序的,第1条线段左边是0号区域,第2条左边是1号……第n条左边是n-1号,右边是n号。将每个点依次与这些直线求叉积,若该点在第i条直线的顺时针方向(也就是叉积为正数),那么点必落在第i-1号区域;若与所有直线的叉积都为负数则落在第n号区域。
代码实现如下:
#include <set>
#include <map>
#include <deque>
#include <ctime>
#include <stack>
#include <cmath>
#include <queue>
#include <string>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define name2str(name)(#name)
#define bug printf("**********\n");
#define IO ios::sync_with_stdio(false);
#define debug(x) cout<<#x<<"=["<<x<<"]"<<endl;
#define FIN freopen("/home/dillonh/CLionProjects/in.txt","r",stdin); const double eps = 1e-;
const int maxn = + ;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n, m, x1, yy, x2, y2, x, y;
int cnt[maxn]; struct Line {
int l, r;
bool operator < (const Line& x) const {
return l == x.l ? r < x.r : l < x.l;
}
}L[maxn]; int cross(int x1, int y1, int x2, int y2, int x3, int y3) {
return (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);
} int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
int vis = ;
while(~scanf("%d", &n) && n) {
if(vis) printf("\n");
vis = ;
scanf("%d%d%d%d%d", &m, &x1, &yy, &x2, &y2);
for(int i = ; i <= n; i++) {
scanf("%d%d", &L[i].l, &L[i].r);
}
sort(L + , L + n + );
memset(cnt, , sizeof(cnt));
for(int i = ; i <= m; i++) {
scanf("%d%d", &x, &y);
int flag = ;
for(int j = ; j <= n; j++) {
if(cross(L[j].r, y2, L[j].l, yy, x, y) > ) {
flag = ;
cnt[j-]++;
break;
}
}
if(!flag) cnt[n]++;
}
for(int i = ; i <= n; i++) {
printf("%d: %d\n", i, cnt[i]);
}
}
return ;
}