• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 87  文章 - 279  trackbacks - 0
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            潛心看書研究!

            常用鏈接

            留言簿(19)

            隨筆分類(81)

            文章分類(89)

            相冊(cè)

            ACM OJ

            My friends

            搜索

            •  

            積分與排名

            • 積分 - 216452
            • 排名 - 117

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            用差積做極角排序,減少浮點(diǎn)誤差

            #include? < iostream >
            #include?
            < algorithm >
            using ? namespace ?std;

            typedef?
            int ?XYType;

            struct ?POINT? {
            ????XYType?x,?y;
            }
            ?ps;

            XYType?cross(POINT?p1,?POINT?p2,?POINT?p0)?
            {
            ????
            // 求矢量[p0,p1],[p0,p2]的差積;
            ???? return ?(p1.x? - ?p0.x)? * ?(p2.y? - ?p0.y)? - ?(p2.x? - ?p0.x)? * ?(p1.y? - ?p0.y);
            }


            int ?cmp(POINT?p1,?POINT?p2)? {
            ????
            // 以ps為中心的極角排序
            ????XYType?ax,?ay,?bx,?by;
            ????ax?
            = ?p1.x? - ?ps.x;?ay? = ?p1.y? - ?ps.y;
            ????bx?
            = ?p2.x? - ?ps.x;?by? = ?p2.y? - ?ps.y;
            ????
            if ?(ay? * ?by? <= ? 0 )? return ?by? > ?ay? || ?(by? == ?ay? && ?bx? > ?ax);
            ????XYType?ret?
            = ?cross(p1,?p2,?ps);
            ????
            if ?(ret? > ? 0 )? return ? 1 ;
            ????
            if ?(ret? == ? 0 )? return ?bx? > ?ax;
            ????
            if ?(ret? < ? 0 )? return ? 0 ;
            ????
            return ? 1 ;
            }
            ;

            int ?main()? {
            ????freopen(
            " test.txt " ,? " r " ,?stdin);
            ????POINT?arrP[
            100 ];
            ????
            int ?n? = ? 0 ,?i,?beg;
            ????
            int ?stack[ 100 ],?top;????
            ????
            while ?(scanf( " %d%d " ,? & arrP[n].x,? & arrP[n].y)? != ?EOF)?n ++ ;
            ????
            for ?(i = 1 ;?i < n;?i ++ )? {
            ????????
            if ?(arrP[i].y? < ?arrP[ 0 ].y? || ?(arrP[i].y? == ?arrP[ 0 ].y? && ?arrP[i].x? < ?arrP[ 0 ].x))?
            ????????????swap(arrP[
            0 ],?arrP[i]);
            ????}

            ????ps.x?
            = ?arrP[ 0 ].x;?ps.y? = ?arrP[ 0 ].y;
            ????sort(arrP
            + 1 ,?arrP + n,?cmp);
            ????
            for ?(i = 0 ;?i <= 2 ;?i ++ )?stack[i]? = ?i;
            ????top?
            = ? 2 ;
            ????
            for ?(i = 3 ;?i < n;?i ++ )? {
            ????????
            while ?(cross(arrP[stack[top]],?arrP[i],?arrP[stack[top - 1 ]])? < ? 0 )? {
            ????????????top
            -- ;
            ????????}

            ????????stack[
            ++ top]? = ?i;
            ????}

            ????
            for ?(i = 0 ;?i <= top;?i ++ )? {
            ????????
            if ?(arrP[stack[i]].x? == ? 0 ? && ?arrP[stack[i]].y? == ? 0 )? {
            ????????????beg?
            = ?i;
            ????????????
            break ;
            ????????}

            ????????
            // printf("(%d,%d)\n",?arrP[stack[i]].x,?arrP[stack[i]].y);
            ????}

            ????
            for ?(i = beg;?i <= top + beg;?i ++ )? {
            ????????
            int ?k? = ?i? > ?top? ? ?i? - ?top? - ? 1 ?:?i;
            ????????printf(
            " (%d,%d)\n " ,?arrP[stack[k]].x,?arrP[stack[k]].y);
            ????}

            ????
            return ? 0 ;
            }
            posted on 2007-03-25 02:43 閱讀(1168) 評(píng)論(1)  編輯 收藏 引用 所屬分類: 數(shù)據(jù)結(jié)構(gòu)與算法

            FeedBack:
            # re: 凸包... 2007-06-30 10:26 姜雨生
            #include<fstream>
            #include<cstdlib>
            using namespace std;
            ifstream fin ("bag.in");
            ofstream fout ("bag.out");
            struct xys
            {
            int x;
            int y;
            };
            int N;//數(shù)目
            xys xy[101];//坐標(biāo)系
            int top;//堆棧頂
            int stk[101];//堆棧
            void swap(xys *a,xys *b)
            {
            xys tmp = *a;
            *a = *b;
            *b =tmp;
            }
            int multi(xys a,xys b,xys c)
            {
            return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);//求叉積
            }
            bool comp(xys p1,xys p2)
            {
            int t;
            t=multi(p1,p2,xy[0]);
            if ((t>=0)&&((p1.x-xy[0].x)+(p1.y-xy[0].y)<(p2.x-xy[0].x)+(p2.y-xy[0].y)))
            return true;//叉積正確
            return false;
            }
            void sort(int p,int r)
            {
            int i,j;
            xys x;
            if (r-p+1<=5)
            {
            for (j=p+1;j<=r;j++)
            {
            i=j;
            while(i>1&&comp(xy[i],xy[i-1]))
            {
            swap(&xy[i],&xy[i-1]);//交換元素
            i--;
            }
            }
            }
            else
            {
            x=xy[p+rand()%(r-p+1)];//隨即選區(qū)一個(gè)支點(diǎn)
            i=p,j=r;
            do
            {
            while (comp(xy[i],x))i++;
            while (comp(x,xy[j]))j--;
            if (i<j)swap(&xy[i],&xy[j]);
            }//一次規(guī)劃
            while (i<j);
            sort(p,j);//前半部
            sort(p+1,r);//后半部
            }
            }
            void init()
            {
            int i;
            fin>>N;
            for(i=0;i<N;i++){
            fin>>xy[i].x>>xy[i].y;
            if (xy[i].y<=xy[0].y&&xy[i].x<xy[0].y) swap(xy[0],xy[i]);//交換
            }
            sort(1,N-1);
            }
            void graham()
            {
            int i;
            for(i=1;i<=3;i++) stk[i]=i-1;
            top=3;
            for(i=3;i<N;i++)
            {
            while(multi(xy[i],xy[stk[top]],xy[stk[top-1]])>=0) top--;//所有未向左傳的點(diǎn)去掉
            top++;
            stk[top]=i;//入棧
            }
            for (i=1;i<=top;i++)
            fout<<xy[stk[i]].x<<" "<<xy[stk[i]].y<<endl;
            }
            int main (void)
            {
            init();
            graham();//掃描出凸包,打印
            return 0;
            }  回復(fù)  更多評(píng)論
              
            亚洲国产成人久久综合一| 99久久国产宗和精品1上映| 久久精品成人免费国产片小草| 久久成人18免费网站| 久久久久久久久波多野高潮| 国产精品禁18久久久夂久| 九九久久精品无码专区| 国产aⅴ激情无码久久| 精品久久久久中文字幕一区| 性欧美大战久久久久久久久| 一级做a爱片久久毛片| 精品久久久中文字幕人妻| 国产精品成人久久久久三级午夜电影| 99精品国产综合久久久久五月天| 国产精品欧美亚洲韩国日本久久 | 久久久无码人妻精品无码| 国产精品美女久久久久AV福利| 99蜜桃臀久久久欧美精品网站| 精品久久久久久无码人妻热| 久久久精品人妻一区二区三区蜜桃| 久久久人妻精品无码一区| 91精品国产91久久| 777午夜精品久久av蜜臀| 伊人久久大香线蕉精品不卡| 国产精品亚洲综合专区片高清久久久| 国内精品久久久久久久97牛牛 | 日韩欧美亚洲综合久久影院d3| 国产69精品久久久久9999APGF| 久久综合精品国产一区二区三区| 国产69精品久久久久99| 伊人色综合久久天天| 国产精品美女久久久| 久久久久久国产精品无码超碰| 久久丫忘忧草产品| 最新久久免费视频| 久久天天躁狠狠躁夜夜躁2014| 午夜精品久久影院蜜桃| 亚洲中文字幕伊人久久无码| 性高朝久久久久久久久久| 久久伊人五月丁香狠狠色| 久久婷婷是五月综合色狠狠|