UVALive - 4356 Fire-Control System

來源:互聯網
上載者:User

題意:在平面上有n個點,求一個圓心在原點的扇型,至少有k個點在裡面,要求扇型面積盡量小

思路:首先先枚舉半徑,然後找到半徑小於等於枚舉值的點,其中要求點要連續,按正弦值的從小到大排序,因為我們要包括從大到小的連續尋找,所以我們開一個兩倍的儲存數組,然後從我們找到的符合要求的點中,求出最小的扇型面積,注意當大於n的時候需要加一個圓的面積了,還有就是精度的問題,看了別人的才注意過來

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>using namespace std;const int MAXN = 5010;const double eps = 1e-6;const double pi = acos(-1.0);int dcmp(double x){    if (fabs(x) < eps)        return 0;    else return x < 0 ? -1 : 1;}struct node{    int x,y;    double ang;    double len;    node(int x = 0,int y = 0):x(x),y(y){        ang = atan2(y,x);        len = sqrt(x*x+y*y);    }    bool operator<(const node &t)const {        return dcmp(ang-t.ang) < 0;    }}p[2*MAXN];int n,k,a[2*MAXN];int main(){    int cas = 1;    while (scanf("%d%d",&n,&k) != EOF && n+k){        for (int i = 0; i < n; i++){            int x,y;            scanf("%d%d",&x,&y);            p[i] = node(x,y);        }        sort(p,p+n);        for (int i = n; i < 2*n; i++)            p[i] = p[i-n];        double ans = 1e10;        for (int i = 0; i < n; i++){            double r = p[i].len;            int m = 0;            for (int j = 0; j < 2*n; j++)                if (dcmp(p[j].len-r) <= 0)                    a[m++] = j;            for (int j = 0; j < m-k+1; j++){                if (a[j+k-1]-a[j] >= n || a[j] >= n)                    break;                double angle = p[a[j+k-1]].ang - p[a[j]].ang;                if (a[j+k-1] >= n)                    angle += 2*pi;                ans = min(ans,r*r*angle/2);            }        }        printf("Case #%d: %.2f\n",cas++,ans);    }    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.