标签:des style blog http io ar color os sp
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4104 | Accepted: 2433 |
Description
Input
Output
Sample Input
4 10 0 10 100 0 20 20 80 80 60 60 40 40 5 10 15 10 95 10 25 10 65 10 75 10 35 10 45 10 55 10 85 10 5 6 0 10 60 0 4 3 15 30 3 1 6 8 10 10 2 1 2 8 1 5 5 5 40 10 7 9 0
Sample Output
Box 2: 5 Box 1: 4 2: 1
题意:
告诉一个矩形,在告诉n条直线的信息,这n条直线把这个矩形划分成n+1个区域,接着告诉m个点的坐标,
升序输出包含非零的 i 个点的区域有多少个。
输入是无序的,需要自己排序
分析:
暴搜一遍,用叉积判断点是在线段的左侧还是右侧。
还有一种做法是枚举线段,然后对点排序以后,二分点,这种二分的做法比较快。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <algorithm> 7 #define LL __int64 8 const int maxn = 1e3 + 10; 9 const double eps = 1e-8; 10 using namespace std; 11 struct node 12 { 13 double x, y; 14 }p[maxn]; 15 struct line 16 { 17 double u, l; 18 }li[maxn]; 19 bool cmp(line a, line b) 20 { 21 return a.u < b.u; 22 } 23 double cross(node a, node b, node c) 24 { 25 return ((b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y)); 26 } 27 28 int main() 29 { 30 int n, m, i, j, f[maxn], d[maxn]; 31 double x1, x2, y1, y2; 32 while(~scanf("%d", &n)&&n) 33 { 34 scanf("%d", &m); 35 memset(f, 0, sizeof(f)); 36 memset(d, 0, sizeof(d)); 37 scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); 38 for(i = 0; i < n; i++) 39 { 40 scanf("%lf%lf", &li[i].u, &li[i].l); 41 } 42 li[i].u = x2; li[i].l = x2; 43 n ++; 44 sort(li, li+n, cmp); 45 for(i = 0; i < m; i++) 46 { 47 scanf("%lf%lf", &p[i].x, &p[i].y); 48 } 49 int cnt = 0; 50 for(i = 0; i < n; i++) 51 { 52 node t1, t2; 53 t1.x = li[i].u; t1.y = y1; 54 t2.x = li[i].l; t2.y = y2; 55 int sum = 0; 56 for(j = 0; j < m; j++) 57 { 58 if(cross(t1, t2, p[j])<=0) 59 sum ++; 60 } 61 if(i == 0) d[i] = sum; 62 else d[i] = sum-cnt; 63 f[d[i]] ++; 64 cnt = sum; 65 } 66 cout<<"Box"<<endl; 67 for(i = 1; i <= m; i++) 68 { 69 if(f[i]) 70 printf("%d: %d\n", i, f[i]); 71 } 72 } 73 return 0; 74 }
POJ 2398 Toy Storage (叉积判断点和线段的关系)
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/bfshm/p/4123235.html