poj 2187 Beauty Contest(凸包),poj2187

來源:互聯網
上載者:User

poj 2187 Beauty Contest(凸包),poj2187

連結:poj 2187

題意:已知平面的一些點的座標,求最遠兩點的距離的平方值。

分析:這題題意很簡單,但如果直接枚舉肯定會逾時,不過可以轉化為求凸包,

因為最遠兩點肯定為它們的凸包的兩個頂點,這樣枚舉凸包上的點求距離,時間就大大減少了

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct stu{    int x,y;}p[50010],s[50010];int m,top;int chaji(struct stu p1,struct stu p2,struct stu p3)              //叉積{    return (p1.x-p2.x)*(p3.y-p2.y)-(p3.x-p2.x)*(p1.y-p2.y);}double dis(struct stu p1,struct stu p2)                        //兩點的距離的平方{    return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);}int cmp(struct stu p1,struct stu p2){    int k;    k=chaji(p1,p[0],p2);    if(k>0||(k==0&&dis(p1,p[0])<dis(p2,p[0])))        return 1;    return 0;}void graham(){    int i,k=0;    struct stu t;    for(i=1;i<m;i++)        if(p[i].y<p[k].y||(p[i].y==p[k].y&&p[i].x<p[k].x))   //先找到最左下的點作為凸包的第一個頂點            k=i;                                                      t=p[k];    p[k]=p[0];    p[0]=t;    sort(p+1,p+m,cmp);               //將除凸包第一個頂點外,按任意兩點與第一個頂點的叉積正負排序    s[0]=p[0];    s[1]=p[1];    top=1;    for(i=2;i<m;i++){        while(top>=1&&chaji(s[top-1],s[top],p[i])>=0)            top--;        top++;        s[top]=p[i];    }}int main(){    int n,i,j,k;    while(scanf("%d",&m)!=EOF){        if(m==0)            break;        for(i=0;i<m;i++)            scanf("%d%d",&p[i].x,&p[i].y);        graham();                              //用Graham演算法求凸包上的點        k=0;        for(i=0;i<=top;i++)                  //枚舉凸包上的點,求最遠兩點的距離的平方            for(j=0;j<i;j++){                n=dis(s[i],s[j]);                if(n>k)                    k=n;            }        printf("%d\n",k);    }    return 0;}



Beauty Contest poj2187凸包的題WA 幫忙看下錯在哪解救

這是AC代碼,你看下錯在哪,就知道了。還有你應該對凸包掌握的還不是很牢固,不然這樣的題應該秒殺的!
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define N 50005
using namespace std;
struct point{
int x,y;
}pt[N];
int convex[N];
int n;
int xmult(point a,point b,point c){
return ((b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y));
}
int dist(point a,point b){
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
bool cmp(point a,point b)
{
if(xmult(pt[0],a,b)>0||(xmult(pt[0],a,b)==0&&dist(a,pt[0])<dist(b,pt[0])))return true;
return false;
}
bool left(int a,int b,int c){
if(xmult(pt[a],pt[b],pt[c])>0)return true;
return false;
}
int max(int a,int b){
return a>b?a:b;
}
int main()
{
//freopen("in.txt","r",stdin);
int i,j,k,ans,top;
while(scanf("%d",&n)!=EOF){
k=top=ans=0;
for(i=0;i<n;i++){
scanf("%d %d",&pt[i].x,&pt[i].y);
if(pt[i].y<pt[k].y)
k=i;
else if(pt[i].y==pt[k].y&&pt[i].x<pt[k].x)
k=i;
}
if(k){
swap(pt[k].x,pt[0].x);
swap(pt[k].y,pt[0].y);
}
sort(pt+1,pt+n,cmp);
convex[top++]=0;
convex[top++]=1;
convex[top++]=2;
for(i=3;i<n;){
if(top<2||left(convex[top-2],convex[top-1],i))
convex[top++]=i++;
else top--;
}
for(i=0;i<top;i++){
for(j=i+1;j<top;j++)
ans=max(ans,dist(pt[convex[i]],pt[convex[j]]));
}
printf("%d\n",ans);
}
return 0;
}...餘下全文>>
 
ACM解題報告

一個POJ題目分類
主流演算法:
1.搜尋 //回溯
2.DP(動態規劃)
3.貪心
4.圖論 //Dijkstra、最小產生樹、網路流
5.數論 //解模線性方程
6.計算幾何 //凸殼、同等安置矩形的並的面積與周長
7.組合數學 //Polya 定理
8.類比
9.資料結構 //並查集、堆
10.博弈論
1、 排序
1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376,
2377, 2380, 1318, 1877,1928, 1971, 1974, 1990, 2001, 2002, 2092, 2379,
1002(需要字元處理,排序用快排即可) 1007(穩定的排序) 2159(題意較難懂)
2231 2371(簡單排序) 2388(順序統計演算法) 2418(二叉排序樹)
2、 搜尋、回溯、遍曆
1022,1111,1118,1129,1190,1562,1564,1573,1655,2184 ,2225,2243,2312,2362,2378,2386,1010,1011,1018,1020,1054,1062,1256,1321,1363,1501,1650,1659,1664,1753,2078,2083,2303,2310,2329
簡單:1128, 1166, 1176, 1231, 1256, 1270, 1321, 1543, 1606, 1664,
1731, 1742, 1745, 1847,1915, 1950, 2038, 2157, 2182, 2183, 2381, 2386, 2426
不易:1024, 1054, 1117, 1167, 1708, 1746, 1775, 1878, 1903, 1966, 2046,
2197, 2349
推薦:1011, 1190, 1191, 1416, 1579, 1632, 1639, 1659, 1680, 1683, 1691,
1709, 1714, 1753,1771, 1826, 1855, 1856, 1890, 1924, 1935, 1948, 1979, 1980, 2170,2288, 2331, 2339,2340,1979(和迷宮類似),1980(對剪枝要求較高)
3、 曆法
1008 2080 (這種題要小心)
4、 枚舉
1012,1046, 1387, 1411, 2245, 2326, 2363, 2381,1054(剪枝要求較高),1650 (小數的精度問題)
5、 資料結構的典型演算法
容易:1182, 1656, 2021, 2023, 2051, 2153, 2227, 2236, 2247, 2352,
2395
不易:1145, 1177, 1195, 1227, 1661, 1834
推薦:1330, 1338, 1451, 1470, 1634, 1689, 1693, 1703, 1724, 1988, 2004,
2010, 2119, 2274
1125(弗洛伊德演算法) ,2421(圖的最小產生樹)
6、 動態規劃
1037 A decorative fence、
1050 To the Max、
1088 滑雪、
1125 Stockbroker Grapevine、
1141 Brackets Sequence、
1159 Palindrom......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.