【codejam2015 Round1A】C Logging

來源:互聯網
上載者:User

標籤:codejam   計算幾何   

Problem

A certain forest consists of N trees, each of which is inhabited by a squirrel.

The boundary of the forest is the convex polygon of smallest area which contains every tree, as if a giant rubber band had been stretched around the outside of the forest.

Formally, every tree is a single point in two-dimensional space with unique coordinates (Xi, Yi), and the boundary is the convex hull of those points.

Some trees are on the boundary of the forest, which means they are on an edge or a corner of the polygon. The squirrels wonder how close their trees are to being on the boundary of the forest.

One at a time, each squirrel climbs down from its tree, examines the forest, and determines the minimum number of trees that would need to be cut down for its own tree to be on the boundary. It then writes that number down on a log.

Determine the list of numbers written on the log.

Input

The first line of the input gives the number of test cases, T. T test cases follow; each consists of a single line with an integer N, the number of trees, followed by N lines with two space-separated integers Xi and Yi, the coordinates of each tree. No two trees will have the same coordinates.

Output

For each test case, output one line containing “Case #x:”, followed by N lines with one integer each, where line i contains the number of trees that the squirrel living in tree i would need to cut down.

Limits

-106 ≤ Xi, Yi ≤ 106.
Small dataset

1 ≤ T ≤ 100.
1 ≤ N ≤ 15.
Large dataset

1 ≤ T ≤ 14.
1 ≤ N ≤ 3000.
Sample

Input

Output

2
5
0 0
10 0
10 10
0 10
5 5
9
0 0
5 0
10 0
0 5
5 5
10 5
0 10
5 10
10 10

Case #1:
0
0
0
0
1
Case #2:
0
0
0
0
3
0
0
0
0
In the first sample case, there are four trees forming a square, and a fifth tree inside the square. Since the first four trees are already on the boundary, the squirrels for those trees each write down 0. Since one tree needs to be cut down for the fifth tree to be on the boundary, the fifth squirrel writes down 1.

簡述一下題目:
平面上有一些點,問刪除掉那些點才能使得某一個點成為凸包邊界上的點。

一個點成為凸包上的點的充要條件是:

過該點的某一條直線的一側沒有點。

那麼我們枚舉每一個點,然後以這個點為原點建系,並將其他點極角排序。

然後只要用一條直線按照極角序轉一圈,計算出一側最少的點即可。

如何極角排序呢?
①需要用到C++中的函數atan2(y,x):

簡單來說就是從第三象限開始,逆時針遞增

②還有一種方法是看象限,然後用叉積算。

int cmp(const point&a,const point&b){    if(a==b)return 0;    int s1=(a.y<=0 && a.x<=0 || a.y<0);    int s2=(b.y<=0 && b.x<=0 || b.y<0);    if(s1!=s2)return s1>s2;    return a*b>0;}

代碼:O(n2logn)

#include <iostream>#include <cmath>#include <cstdio>#include <cstring>#include <cstdlib>#define eps 1e-9#define Pi acos(-1)using namespace std;int x[3005],y[3005],T,N;double a[6005];int main(){    cin>>T;    for (int t=1;t<=T;t++)    {        printf("Case #%d:\n",t);        scanf("%d",&N);        for (int i=1;i<=N;i++)            scanf("%d%d",&x[i],&y[i]);        for (int i=1;i<=N;i++)        {            int n=0;            for (int j=1;j<=N;j++)                if (i!=j)                    a[++n]=atan2(y[j]-y[i],x[j]-x[i]);            sort(a+1,a+1+n);            for (int j=1;j<=n;j++)                a[j+n]=a[j]+2*Pi;            int j=1,ans=n;            for (int k=1;k<=n;k++)            {                j=max(k,j);                while (a[j+1]<a[k]+Pi-eps)                    j++;                ans=min(ans,j-k);            }            cout<<ans<<endl;        }    }    return 0;}

【codejam2015 Round1A】C Logging

聯繫我們

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