cogs 896. 圈奶牛
生活随笔
收集整理的這篇文章主要介紹了
cogs 896. 圈奶牛
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
★★☆?? 輸入文件:fc.in?? 輸出文件:fc.out???簡單對比
時間限制:1 s?? 內(nèi)存限制:128 MB
描述
農(nóng)夫約翰想要建造一個圍欄用來圍住他的奶牛,可是他資金匱乏。他建造的圍欄必須包括他的奶牛喜歡吃草的所有地點。對于給出的這些地點的坐標(biāo),計算最短的能夠圍住這些點的圍欄的長度。
PROGRAM NAME: fc
INPUT FORMAT(file fc.in)
輸入數(shù)據(jù)的第一行包括一個整數(shù)?N。N(0 <= N <= 10,000)表示農(nóng)夫約翰想要圍住的放牧點的數(shù)目。接下來?N?行,每行由兩個實數(shù)組成,Xi?和?Yi,對應(yīng)平面上的放牧點坐標(biāo)(-1,000,000 <= Xi,Yi <= 1,000,000)。數(shù)字用小數(shù)表示。
OUTPUT FORMAT(file fc.out)
輸出必須包括一個實數(shù),表示必須的圍欄的長度。答案保留兩位小數(shù)。
SAMPLE INPUT (file fc.in)
4
4 8
4 12
5 9.3
7 8
SAMPLE OUTPUT (file fc.out)
12.00
題解:裸凸包求長度,上模板。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<vector> 9 using namespace std; 10 const int eps=1e-7; 11 int N,top; 12 double ans; 13 struct P{ 14 double x,y; 15 friend P operator-(P a,P b){ 16 P t; t.x=a.x-b.x; t.y=a.y-b.y; 17 return t; 18 } 19 friend double operator*(P a,P b){ 20 return ((a.x*b.y)-(b.x*a.y)); 21 } 22 friend double dis(P a,P b){ 23 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 24 } 25 }p[20000],stac[20000]; 26 inline bool operator<(P a,P b){ 27 double t=(a-p[1])*(b-p[1]); 28 if(fabs(t)<=eps) return dis(a,p[1])<dis(b,p[1]); 29 return t>eps; 30 } 31 inline void graham(){ 32 int tmp=1; 33 for(int i=2;i<=N;i++){ 34 if(p[i].y<p[tmp].y||(fabs(p[i].y-p[tmp].y)<=eps&&p[i].x<p[tmp].x)) tmp=i; 35 } 36 swap(p[1],p[tmp]); 37 sort(p+2,p+N+1); 38 stac[++top]=p[1]; stac[++top]=p[2]; 39 for(int i=3;i<=N;i++){ 40 while((stac[top]-stac[top-1])*(p[i]-stac[top-1])<=eps) top--; 41 stac[++top]=p[i]; 42 } 43 stac[top+1]=p[1]; 44 for(int i=1;i<=top;i++){ 45 ans+=dis(stac[i],stac[i+1]); 46 } 47 printf("%.2lf",ans); 48 } 49 int main(){ 50 freopen("fc.in","r",stdin); 51 freopen("fc.out","w",stdout); 52 scanf("%d",&N); 53 for(int i=1;i<=N;i++) scanf("%lf%lf",&p[i].x,&p[i].y); 54 graham(); 55 return 0; 56 }?
轉(zhuǎn)載于:https://www.cnblogs.com/CXCXCXC/p/5244262.html
總結(jié)
以上是生活随笔為你收集整理的cogs 896. 圈奶牛的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HBase源码分析之HRegion上co
- 下一篇: Apple Pay及其背后的安全技术