【集训笔记】计算几何【HDOJ2036【HDOJ1086【HDOJ1115【HDOJ1147【HDOJ1392 【ZOJ2976

改革春风吹满地



Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 16889    Accepted Submission(s): 8636





Problem Description

“ 改革春风吹满地,

不会AC没关系;

实在不行回老家,

还有一亩三分地。

谢谢!(乐队奏乐)”



话说部分学生心态极好,每天就知道游戏,这次考试如此简单的题目,也是云里雾里,而且,还竟然来这么几句打油诗。

好呀,老师的责任就是帮你解决问题,既然想种田,那就分你一块。

这块田位于浙江省温州市苍南县灵溪镇林家铺子村,多边形形状的一块地,原本是linle 的,现在就准备送给你了。不过,任何事情都没有那么简单,你必须首先告诉我这块地到底有多少面积,如果回答正确才能真正得到这块地。

发愁了吧?就是要让你知道,种地也是需要AC知识的!以后还是好好练吧...

 



Input

输入数据包含多个测试实例,每个测试实例占一行,每行的开始是一个整数n(3<=n<=100),它表示多边形的边数(当然也是顶点数),然后是按照逆时针顺序给出的n个顶点的坐标(x1, y1, x2, y2... xn, yn),为了简化问题,这里的所有坐标都用整数表示。

输入数据中所有的整数都在32位整数范围内,n=0表示数据的结束,不做处理。

 



Output

对于每个测试实例,请输出对应的多边形面积,结果精确到小数点后一位小数。

每个实例的输出占一行。

 



Sample Input

3 0 0 1 0 0 1

4 1 0 0 1 -1 0 0 -1

0

 



Sample Output

0.5

2.0

 
HDOJ2036

有关叉积相关:http://www.cnblogs.com/wushuaiyi/p/3458659.html

 

 1 #include <stdio.h>

 2 

 3 struct node{

 4     double x,y;

 5 }a[110],p1,p2;

 6 

 7 int main(){

 8     int i,j,t;

 9     double ans;

10     while(scanf("%d",&t)!=EOF){

11         if(t == 0)

12             break;

13         ans = 0.00000;

14         for(i=0;i<t;i++)

15             scanf("%lf%lf",&a[i].x,&a[i].y);

16         for(i=0;i<t-1;i++){

17             ans += ((a[i].x * a[i+1].y) - (a[i+1].x * a[i].y))/2;

18         }

19         ans+=((a[t-1].x * a[0].y) - (a[0].x * a[t-1].y))/2;

20         printf("%.1lf\n",ans);

21     }

22     return 0;

23 }
View Code
Lifting the Stone



Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4898    Accepted Submission(s): 2038





Problem Description

There are many secret openings in the floor which are covered by a big heavy stone. When the stone is lifted up, a special mechanism detects this and activates poisoned arrows that are shot near the opening. The only possibility is to lift the stone very slowly and carefully. The ACM team must connect a rope to the stone and then lift it using a pulley. Moreover, the stone must be lifted all at once; no side can rise before another. So it is very important to find the centre of gravity and connect the rope exactly to that point. The stone has a polygonal shape and its height is the same throughout the whole polygonal area. Your task is to find the centre of gravity for the given polygon. 

 



Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer N (3 <= N <= 1000000) indicating the number of points that form the polygon. This is followed by N lines, each containing two integers Xi and Yi (|Xi|, |Yi| <= 20000). These numbers are the coordinates of the i-th point. When we connect the points in the given order, we get a polygon. You may assume that the edges never touch each other (except the neighboring ones) and that they never cross. The area of the polygon is never zero, i.e. it cannot collapse into a single line. 

 



Output

Print exactly one line for each test case. The line should contain exactly two numbers separated by one space. These numbers are the coordinates of the centre of gravity. Round the coordinates to the nearest number with exactly two digits after the decimal point (0.005 rounds up to 0.01). Note that the centre of gravity may be outside the polygon, if its shape is not convex. If there is such a case in the input data, print the centre anyway. 

 



Sample Input

2

4

5 0

0 5

-5 0

0 -5

4

1 1

11 1

11 11

1 11

 



Sample Output

0.00 0.00

6.00 6.00
HDOJ1115

求任意多边形的重心(from:http://www.cnblogs.com/bo-tao/archive/2011/08/16/2141395.html

已知一多边形没有边相交,质量分布均匀。顺序给出多边形的顶点坐标,求其重心。

分析:

求多边形重心的题目大致有这么几种:

1,质量集中在顶点上。n个顶点坐标为(xi,yi),质量为mi,则重心
  X = ∑( xi×mi ) / ∑mi
  Y = ∑( yi×mi ) / ∑mi
  特殊地,若每个点的质量相同,则
  X = ∑xi / n
  Y = ∑yi / n

2,质量分布均匀。这个题就是这一类型,算法和上面的不同。
  特殊地,质量均匀的三角形重心:
  X = ( x0 + x1 + x2 ) / 3
  Y = ( y0 + y1 + y2 ) / 3

3,质量分布不均匀。只能用积分来算,不会……

求任意多边形的重心

已知一多边形没有边相交,质量分布均匀。顺序给出多边形的顶点坐标,求其重心。

分析:

求多边形重心的题目大致有这么几种:

1,质量集中在顶点上。n个顶点坐标为(xi,yi),质量为mi,则重心
  X = ∑( xi×mi ) / ∑mi
  Y = ∑( yi×mi ) / ∑mi
  特殊地,若每个点的质量相同,则
  X = ∑xi / n
  Y = ∑yi / n

2,质量分布均匀。这个题就是这一类型,算法和上面的不同。
  特殊地,质量均匀的三角形重心:
  X = ( x0 + x1 + x2 ) / 3
  Y = ( y0 + y1 + y2 ) / 3

3,质量分布不均匀。只能用积分来算,不会……

2.7.2 猜想n边形的重心

猜想由n个点(x1,y1), (x2,y2), ……, (xn,yn)

构成的多边形的重心的坐标是:( ( x1+x2...+xn )/n,( y1+y2+...+yn )/n );

v上面公式失效的原因是面积代表的重量并不均匀分布在各个顶点上(如果重量均匀分布在各个顶点上,则上面公式成立)
v可以先求出各个三角形的重心和面积,然后对它们按照权重相加;
 
一张图说明问题!
【集训笔记】计算几何【HDOJ2036【HDOJ1086【HDOJ1115【HDOJ1147【HDOJ1392 【ZOJ2976
 
 1 #include<stdio.h>

 2 #include<stdlib.h>

 3 int main(){

 4     double x0,y0,x1,y1,x2,y2;

 5     int n,m;

 6     while(scanf("%d",&n)!=EOF){

 7         for(int i=1; i<=n; i++){

 8            double sx,sy,s;

 9            sx = sy = s =0.00000;

10            scanf("%d",&m);

11            scanf("%lf%lf%lf%lf",&x0,&y0,&x1,&y1);

12            for(int j=2;j<m; j++){

13                  scanf("%lf%lf",&x2,&y2);

14                  double area=0.50000*((x1-x0)*(y2-y0)-(x2-x0)*(y1-y0));

15                  sx+=area*(x0+x1+x2);//wait to divilded 3

16                  sy+=area*(y0+y1+y2);

17                  s+=area;

18                  x1=x2;

19                  y1=y2;

20            }

21            printf("%.2lf %.2lf\n",sx/(3*s),sy/(3*s));

22         }

23     }

24     return 0;

25 }
View Code
You can Solve a Geometry Problem too



Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6551    Accepted Submission(s): 3159





Problem Description

Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now attending an exam, not a contest :)

Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.



Note:

You can assume that two segments would not intersect at more than one point. 

 



Input

Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending. 

A test case starting with 0 terminates the input and this test case is not to be processed.

 



Output

For each case, print the number of intersections, and one line one case.

 



Sample Input

2

0.00 0.00 1.00 1.00

0.00 1.00 1.00 0.00

3

0.00 0.00 1.00 1.00

0.00 1.00 1.00 0.000

0.00 0.00 1.00 0.00

0

 



Sample Output

1

3

 
HDOJ1086
 1 #include<iostream>

 2 #include<cstring>

 3 #include<cstdlib>

 4 #include<cmath>

 5 

 6 using namespace std;

 7 

 8 const int maxv = 105;

 9 struct point {          //

10     double x;           // x 坐标

11     double y;           // y 坐标

12 } first[maxv], final[maxv];

13 

14 double intersect(point p1, point q1, point p2){

15     point tmp1, tmp2;

16     tmp1.x = q1.x - p1.x, tmp1.y = q1.y - p1.y;     // tmp1 = q1-p1   是向量

17     tmp2.x = p2.x - p1.x, tmp2.y = p2.y - p1.y;     // tmp2 = p2-p1   是向量

18     return tmp1.x * tmp2.y - tmp2.x * tmp1.y;       // ans = (q1-p1) x (p2-p1) (向量叉积)

19 }

20 

21 int main(){

22     int n, i, j;

23     double ans1, ans2, ans3, ans4;

24     int cnt;                //相交次数

25     while(cin >> n && n) {

26         cnt = 0;

27         memset(first, 0, sizeof(first));

28         memset(final, 0, sizeof(final));

29         for(i = 0; i < n; ++i) {

30             cin >> first[i].x >> first[i].y >> final[i].x >> final[i].y;

31         }

32         for(i = 0; i < n; ++i) {

33             for(j = i+1; j < n; ++j) {

34                 ans1 = intersect(first[i], final[i], first[j]);

35                 ans2 = intersect(first[i], final[i], final[j]);

36                 double t1 = ans1 * ans2;

37                 ans3 = intersect(first[j], final[j], first[i]);

38                 ans4 = intersect(first[j], final[j], final[i]);

39                 double t2 = ans3 * ans4;

40                 if(t1 <= 0 && t2 <= 0)

41                     cnt++;

42             }

43         }

44         cout << cnt << endl;

45     }

46     return 0;

47 }

HDOJ1147

Pick-up sticks

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1858    Accepted Submission(s): 701


Problem Description
Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.
【集训笔记】计算几何【HDOJ2036【HDOJ1086【HDOJ1115【HDOJ1147【HDOJ1392 【ZOJ2976
 

Input
Input consists of a number of cases. The data for each case start with 1 ≤ n ≤ 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.
 

Output
For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.
The picture to the right below illustrates the first case from input.
 

Sample Input
5 1 1 4 2 2 3 3 1 1 -2.0 8 4 1 4 8 2 3 3 6 -2.0 3 0 0 1 1 1 0 2 1 2 0 3 1 0
 

Sample Output
Top sticks: 2, 4, 5. Top sticks: 1, 2, 3.
相同的题目,1000MS过了POJ,却在HDOJ上卡了2000MS = =
 
 1 #include<iostream>

 2 #include<cstring>

 3 #include<cstdlib>

 4 #include<cmath>

 5 #include<stdio.h>

 6 

 7 using namespace std;

 8 

 9 const int maxv =  100001;

10 struct point {          //

11     double x;           // x 坐标

12     double y;           // y 坐标

13 } first[maxv], final[maxv];

14 

15 double intersect(point p1, point q1, point p2){

16     point tmp1, tmp2;

17     tmp1.x = q1.x - p1.x, tmp1.y = q1.y - p1.y;     // tmp1 = q1-p1   是向量

18     tmp2.x = p2.x - p1.x, tmp2.y = p2.y - p1.y;     // tmp2 = p2-p1   是向量

19     return tmp1.x * tmp2.y - tmp2.x * tmp1.y;       // ans = (q1-p1) x (p2-p1) (向量叉积)

20 }

21 

22 int main(){

23     int n, i, j, flag[100001];

24     double ans1, ans2, ans3, ans4;

25     while(cin >> n && n) {

26         memset(flag, -1, sizeof(flag));

27         memset(first, 0, sizeof(first));

28         memset(final, 0, sizeof(final));

29         for(i = 0; i < n; ++i) {

30             cin >> first[i].x >> first[i].y >> final[i].x >> final[i].y;

31         }

32         for(i = 0; i < n; i++) {

33             for(j = i+1; j < n; j++) {

34                 ans1 = intersect(first[i], final[i], first[j]);

35                 ans2 = intersect(first[i], final[i], final[j]);

36                 double t1 = ans1 * ans2;

37                 ans3 = intersect(first[j], final[j], first[i]);

38                 ans4 = intersect(first[j], final[j], final[i]);

39                 double t2 = ans3 * ans4;

40                 if(t1 <= 0 && t2 <= 0){

41                     flag[i] = 0;

42                     break;

43                 }

44             }

45             if(flag[i] == -1)

46                 flag[i] = 1;

47         }

48         printf("Top sticks:");

49         for(i=0;i<n-1;i++){

50             if(flag[i] == 1)

51                 printf(" %d,",i+1);

52         }

53         printf(" %d.\n",n);

54     }

55     return 0;

56 }

Surround the Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6758    Accepted Submission(s): 2567


Problem Description
There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.

【集训笔记】计算几何【HDOJ2036【HDOJ1086【HDOJ1115【HDOJ1147【HDOJ1392 【ZOJ2976


There are no more than 100 trees.
 

 

Input
The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.

Zero at line for number of trees terminates the input for your program.
 

 

Output
The minimal length of the rope. The precision should be 10^-2.
 

 

Sample Input
9 12 7 24 9 30 5 41 9 80 7 50 87 22 9 45 1 50 7 0
 

 

Sample Output
243.06
 
 1 #include<iostream>

 2 #include<stdio.h>

 3 #include<cstring>

 4 #include<cstdlib>

 5 #include<cmath>

 6 #include<algorithm>

 7 using namespace std;

 8 

 9 struct node{

10     int x,y;

11 };

12 node a[102],stack1[102];

13 

14 double dis(node n1,node n2)//求距离{

15     return (double)sqrt( (n1.x-n2.x)*(n1.x-n2.x)*1.0 + (n1.y-n2.y)*(n1.y-n2.y)*1.0 );

16 }

17 double cross(node a,node n1,node n2)// 为正时 "左转"{

18     return (n1.x-a.x)*(n2.y-a.y) - (n1.y-a.y)*(n2.x-a.x);

19 }

20 bool cmp(node n1,node n2)// 叉乘越小的,越靠前{

21     double k = cross(a[0],n1,n2);

22     if( k>0) return true;

23     else if( k==0 && dis(a[0],n1)<dis(a[0],n2))

24         return true;

25     else return false;

26 }

27 void Graham(int n){

28     int i,head;

29     double r=0;

30     for(i=1;i<n;i++)

31         if(a[i].x<a[0].x ||(a[i].x==a[0].x&&a[i].y<a[0].y ) )

32             swap(a[0],a[i]);

33     sort(a+1,a+n,cmp); //排序

34     a[n]=a[0]; //为了对最后一点的检验是否为满足凸包。cross(stack1[head-1],stack1[head],stack[i]);

35     stack1[0]=a[0];

36     stack1[1]=a[1];

37     stack1[2]=a[2];// 放入3个先

38     head=2;

39     for(i=3;i<=n;i++)

40     {

41         while( head>=2 && cross(stack1[head-1],stack1[head],a[i])<=0 )head--; 

42         // == 包含了重点和共线的情况。此题求周长,并没有关系。所以不加==,也是可以的。

43         stack1[++head]=a[i];

44     }

45     for(i=0;i<head;i++) //不是<=.  因为 a[0]在 0 和 head 两个位置都出现了。

46     {

47         r=r+dis(stack1[i],stack1[i+1]);

48     }

49     printf("%.2lf\n",r);

50 }

51 int main(){

52     int i,n;

53     while(scanf("%d",&n)>0)

54     {

55         if(n==0)break;

56         for(i=0;i<n;i++)

57             scanf("%d%d",&a[i].x,&a[i].y);//end input

58         if(n==1)//特判 

59         {

60             printf("0.00\n");

61             continue;

62         }

63         if(n==2)//此题的特判

64         {

65             printf("%.2lf\n",dis(a[0],a[1]));

66             continue;

67         }

68         Graham(n);

69     }

70     return 0;

71 }
Light Bulbs

Time Limit: 2 Seconds      Memory Limit: 65536 KB



Wildleopard had fallen in love with his girlfriend for 20 years. He wanted to end the long match for their love and get married this year. He bought a new house for his family and hired a company to decorate his house. Wildleopard and his fiancee were very satisfied with the postmodern design, except the light bulbs. Varieties of light bulbs were used so that the light in the house differed a lot in different places. Now he asks you, one of his best friends, to help him find out the point of maximum illumination.



To simplify the problem, we can assume each bulb is a point light source and we only need to consider the grid points of the flat floor of the house. A grid point is a point whose coordinates are both integers. The length and the width of house can be considered infinite. Illumination means the amount of light that go through in one unit area. The illumination of a point can be calculated by simply adding the illumination from each source. The illumination from a source can be calculated by the following equation:



, where E is the illumination of the point, I is the luminous intensity of the source, R is the distance between the source and the point and i is the angle between the normal of the plane and the light to the point.



Given the position and the luminous intensity of the light bulbs, you are asked to find the maximum illumination on the floor at the grid points.



Input



Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 20) which is the number of test cases. And it will be followed by T consecutive test cases.



The first line of each test case contains one integer n(1 <= n <= 100), indicating the number of bulbs of the lamps in the house. The next n lines will contain 4 integers each, Xi, Yi, Zi, Ii, separated by one space, indicating the coordinates and the luminous intensity of the i-th bulb of the lamp. The absolute values of the coordinates do not exceed 100 and Zi is always positive. Ii is a positive integer less than 32768.



Output



Results should be directed to standard output. The output of each test case should be a real number rounded to 0.01, which is the maximum illumination on the floor at the grid points.



Sample Input



3

1

0 0 1 100

4

1 0 1 100

0 1 1 100

-1 0 1 100

0 -1 1 100

4

1 0 100 10000

0 1 100 10000

-1 0 100 10000

0 -1 100 10000



Sample Output



100.00

147.43

4.00
ZOJ 2976

注意读懂意即可

 1 #include <stdio.h>

 2 #include <math.h>

 3 struct node{

 4     double x,y,z,I;

 5 }a[101];

 6 

 7 double distance(struct node a,int i,int j){

 8     return (a.x-(double)i)*(a.x-(double)i) + (a.y-(double)j)*(a.y-(double)j) + a.z*a.z;

 9 }

10 

11 int main()

12 {

13     int t,n,i,j,k;

14     double ans,Maxs,temp;

15     while(scanf("%d",&t)!=EOF){

16         while(t--){

17             scanf("%d",&n);

18             for(i=1;i<=n;i++){

19                 scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&a[i].z,&a[i].I);

20             }

21             Maxs = 0.0000;

22             for(i=-100;i<=100;i++){

23                 for(j=-100;j<=100;j++){

24                     ans = 0.0000;

25                     for(k=1;k<=n;k++){

26                         temp = distance(a[k],i,j);

27                         ans += a[k].I / temp * a[k].z / sqrt(temp);

28                     }

29                     if(ans > Maxs)

30                         Maxs = ans;

31                 }

32             }

33             printf("%.2lf\n",Maxs);

34         }

35     }

36     return 0;

37 }
View Code

 

你可能感兴趣的:(ZOJ)