hdu 1392 模板凸包

來源:互聯網
上載者:User

這真的是太囧了,我差點就這樣了,明明就是一道模板題,可怎麼改都錯,交了十幾二十次還是WA……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................…………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………............................................................................................................................................……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………一天早上就過去了……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………吃完飯當然還是不甘心,突然在用別人的代碼上看到,當只有兩點的時候,有要算一邊就好了,這時我就這樣了,太囧了…………結果就這樣過了

煩,不解釋,不寫了

#include<iostream>#include<vector>#include<algorithm>#include<stack>#include<cmath>using namespace std;class cor{public:int x, y;};cor start;bool drection(cor& p0,cor& p1,cor& p2){int x1 = p1.x-p0.x;int y1 = p1.y-p0.y;int x2 = p2.x-p0.x;int y2 = p2.y-p0.y;if(x2*y1-x1*y2 > 0)return true;return false;}bool cmp(cor& lhs,cor& rhs){return drection(start,rhs,lhs);}double calc(cor p1,cor p2){double a = pow((double)(p1.x-p2.x),2);double b = pow((double)(p1.y-p2.y),2);return sqrt(a+b);}int main(){int ntree;while(cin >>ntree && ntree){vector<cor> trees;stack<cor> ch;cin >>start.x >>start.y;for(int i = 1;i < ntree;i++){cor p;cin >>p.x >>p.y;if(p.y < start.y){trees.push_back(start);start = p;}else if(p.y == start.y && p.x <start.x){trees.push_back(start);start = p;}else    trees.push_back(p);}if(ntree == 1){printf("0.00\n");continue;}if(ntree == 2){printf("%.2lf\n",calc(start,trees[0]));continue;}sort(trees.begin(),trees.end(),cmp);ch.push(start);ch.push(trees[0]);ch.push(trees[1]);for(vector<cor>::size_type i = 2;i < trees.size();i++){cor p0,p1,p2;p1 = ch.top();ch.pop(); p0 = ch.top(); ch.push(p1);//取棧頂下一個元素p2 = trees[i];while(drection(p0,p1,p2)){ch.pop();p1 = ch.top();ch.pop(); p0 = ch.top(); ch.push(p1);//取棧頂下一個元素}ch.push(p2);}double len = 0.0;cor p1,p2; ch.push(start);p1 = ch.top(); p2 = p1;stack<cor>::size_type k = ch.size();for(stack<cor>::size_type i = 1;i < k;i++){ch.pop();p2 = ch.top();len += calc(p1,p2);p1 = p2;}printf("%.2lf\n",len);}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.