Diamond Dealer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 606 Accepted Submission(s):
175
Problem Description
Mr. Chou is the atworld diamond dealer. It is
important that he knows the value of his (twodimensional) diamonds in order to
be a succesful businessman. Mr. Chou is tired of calculating the values by hand
and you have to write a program that makes the calculation for him.
Figure
2: Example diamond
The value of a diamond is determined by smoothness of
its surface. This
depends on the amount of faces on the surface, more faces
means a smoother surface. If there are dents (marked red in gure 2) in the
surface of the diamond, the value of the diamond decreases. Counting the number
of dents in the surface (a) and the number of faces on the surface that are not
in dents (b), the value of the diamond is determined by the following formula: v
= -a * p + b * q. When v is negative, the diamond has no value (ie. zero
value).
Input
The first line of input consists of the integer number
n, the number of test cases;
Then, for each test case:
One line
containing:
The cost for a dent in the surface of a diamond (0 <= p <=
100);
The value of a face in the surface of a diamond (0 <= q <= 100);
The number of vertices (3 <= n <= 30) used to describe the shape of
the diamond.
n lines containing one pair of integers (-1000 <=xi,yi <=
1000) describing the surface of the diamond (x0,y0) - (x1,y1) -.....-(xn-1,
yn-1) - (x0 ,y0) in clockwise order.
No combination of three vertices within
one diamond will be linearly aligned.
Output
For each test case, the output contains one line with
one number: the value of the diamond.
Sample Input
1
10 5 7
0 10
8 4
10 -7
6 -9
-5 -4
-5 7
-2 6
Sample Output
15
题意:找到凸包的边数n2条, 求出凹的地方有cnt个, 价值ans = -p * cnt + (n2-cnt) * q;然后判断一下ans > 0 ? ans : 0;
思路:凸包+简单的搜索
搜索方法:记录凸包每个顶点在原图中的编号id,用flag[stack[i].id]=1标记其为凸包中的点,将所有的点遍历满足flag[i]==1&&flag[i+1]==0的则cnt++
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<math.h> 5 #include<algorithm> 6 using namespace std; 7 const int N=40; 8 struct point 9 { 10 double x,y; 11 double angel; 12 int id; 13 } p[N],stack[N]; 14 int top,n; 15 16 double dis(point a,point b)//求距离 17 { 18 return sqrt ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 19 } 20 21 bool mult(point p1,point p2,point p0)//叉乘 22 { 23 return (p1.x-p0.x)*(p2.y-p0.y) >= (p2.x-p0.x)*(p1.y-p0.y); 24 } 25 26 bool cmp(point a,point b) 27 { 28 if(a.angel == b.angel) 29 { 30 if (a.x == b.x) 31 return a.y > b.y; 32 return a.x > b.x; 33 } 34 return a.angel < b.angel; 35 } 36 37 void graham() 38 { 39 //p为点集,n为点的个数,stack为凸包点集,top为凸包个数 40 int i,k=0; 41 point temp; 42 for(i=0; i<n; i++) 43 if(p[i].y<p[k].y||((p[i].y==p[k].y)&&(p[i].x<p[k].x))) 44 k=i; 45 temp=p[0]; 46 p[0]=p[k]; 47 p[k]=temp; 48 for(i=1; i<n; i++) 49 p[i].angel=atan2(p[i].y-p[0].y,p[i].x-p[0].x); 50 sort(p+1,p+n,cmp); 51 stack[0]=p[0]; 52 stack[1]=p[1]; 53 stack[2]=p[2]; 54 top=3; 55 for(i=3; i<n; i++) 56 { 57 while(top > 2 && mult(stack[top-2],stack[top-1],p[i])<=0) 58 top--; 59 stack[top++]=p[i]; 60 } 61 } 62 int main() 63 { 64 int i,j,t,pp,q,cnt,ans; 65 int flag[500]; 66 scanf("%d",&t); 67 while(t--) 68 { 69 cnt=0; 70 scanf("%d%d%d",&pp,&q,&n); 71 for(i=0; i<n; i++) 72 { 73 scanf("%lf%lf",&p[i].x,&p[i].y); 74 p[i].id=i; 75 } 76 graham(); 77 memset(flag,0,sizeof(flag)); 78 for(i=0; i<top; i++) 79 { 80 flag[stack[i].id]=1; 81 } 82 flag[n]=flag[0]; 83 for(i=0; i<n; i++) 84 { 85 if(flag[i]==1&&flag[i+1]==0) 86 cnt++; 87 } 88 ans=q*(top-cnt)-pp*cnt; 89 if(ans<=0) 90 printf("0\n"); 91 else 92 printf("%d\n",ans); 93 } 94 return 0; 95 }
hdu2907