首页 > ACM题库 > HDU-杭电 > hdu 2469 Fire-Control System-计算几何-[解题报告]C++
2014
01-26

hdu 2469 Fire-Control System-计算几何-[解题报告]C++

Fire-Control System

问题描述 :

A new mighty weapon has just been developed, which is so powerful that it can attack a sector of indefinite size, as long as the center of the circle containing the sector is the location of the weapon. We are interested in developing a fire-control system that calculates firing-solutions automatically.
The following example gives an example of a firing solution:
Figure 1

Here the firing region is the sector "ABC" that covers six points: A, B, C, D, E, H. You may further assume that the weapon is always located at point (0, 0), no targets will be on the point (0, 0) and the coordinates of the targets will be distinct.
A firing solution is called effective if and only if it covers a minimum of K points
out of N given points (targets) on the two-dimensional Cartesian plane. Furthermore,since the cost of a particular fire solution is in direct proportion to the size of the area it covers, a firing could be quite costly; thus we are only interested in the optimal firing solution with the minimum cost.

输入:

There are multiple test cases in the input file.
Each test case starts with two non-negative integers, N and K
(1 ≤ N ≤ 5000 , K ≤ N ), followed by N lines each containing two integers, X, and Y, describing the distinct location of one target. It is guaranteed that the absolute value of any integer does not exceed 1000.
Two successive test cases are separated by a blank line. A case with N = 0 and K = 0 indicates the end of the input file, and should not be processed by your program.

输出:

There are multiple test cases in the input file.
Each test case starts with two non-negative integers, N and K
(1 ≤ N ≤ 5000 , K ≤ N ), followed by N lines each containing two integers, X, and Y, describing the distinct location of one target. It is guaranteed that the absolute value of any integer does not exceed 1000.
Two successive test cases are separated by a blank line. A case with N = 0 and K = 0 indicates the end of the input file, and should not be processed by your program.

样例输入:

3 1
0 1
1 0
-5 -6
3 2
0 2
2 0
-5 -6
0 0

样例输出:

Case #1: 0.00
Case #2: 3.14

#include <iostream>
#include <stdio.h>
#include <queue>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
#define N 100100
#define PI (2*asin(1.0))

struct node
{
    int x,y;
    double r;//表示半径
    double du;//用来表示角度
}g[N];

int n,k;
double g1[N];

int cmp(node t,node t1)
{
    return t.du<t1.du;
}

double que[2*N];

double mabs(double x)
{
    if(x<0) return -x;
    return x;
}

int main()
{
    //freopen("//home//ismdeep//xianchang1//in","r",stdin);
    int tt=1;
    double pi=PI;
    while(scanf("%d%d",&n,&k)&&(n+k))
    {

        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&g[i].x,&g[i].y);
            g[i].r=sqrt((double)g[i].x*g[i].x+g[i].y*g[i].y);
            g1[i]=g[i].r;

            double tmp;

            if(mabs(g[i].x-0)<1e-8)
            {
                if(g[i].y>0) tmp=90.0;
                else tmp=270.0;
            }
            else
            {
                tmp=((double)g[i].y/(double)g[i].x); //将这个点的斜率求出来
                if(mabs(g[i].y-0)<1e-8)
                {
                    if(g[i].x>0) tmp=0.0;
                    else tmp=180.0;
                }
                else
                {
                    tmp=atan(tmp);//求出角度
                    tmp=(180.0/PI)*tmp;
                    if(g[i].y*g[i].x > 0&&g[i].y<0)
                        tmp+=180.0;
                    if(g[i].y*g[i].x<0)
                    {
                        tmp*=-1;
                        if(g[i].x<0)
                            tmp=180.0-tmp;
                        else
                        {
                            tmp=360-tmp;
                        }
                    }

                }
            }
            g[i].du=tmp;
        }
        // 角度求好了
        if(k==0)
        {
            printf("Case #%d: 0.00\n",tt++);
            continue;
        }
        //sort(g1+1,g1+n+1); //半径从小到大来搞一搞啊
        sort(g+1,g+1+n,cmp);

        double mi=1999999999;

        for(int ii=1;ii<=n;ii++)
        {
            double key=g1[ii]; //表示固定的半径
            int flag=0;

            for(int i=1;i<=n;i++)//提前
            {
                if(g[i].r <= key+1e-6)
                {
                    que[flag++]=g[i].du;
                }
            }

            for(int i=0;i<flag;i++)
            {
                que[flag+i] = que[i]+360.0;
            }

            if(flag<k) continue;

            for(int i=0;i<flag;i++)
            {
                double tmp=que[i];
                int tt=i+k-1;
                double tmp1=que[tt];
                tmp=tmp1-tmp;

                tmp=(tmp/(360.0))*PI*key*key;

                mi=min(tmp,mi);
            }
        }

        printf("Case #%d: ",tt++);

        printf("%.2lf\n",mi);
    }
    return 0;
}

解题转自:http://www.cnblogs.com/chenhuan001/p/3349706.html


  1. 5.1处,反了;“上一个操作符的优先级比操作符ch的优先级大,或栈是空的就入栈。”如代码所述,应为“上一个操作符的优先级比操作符ch的优先级小,或栈是空的就入栈。”

  2. 这道题目虽然简单,但是小编做的很到位,应该会给很多人启发吧!对于面试当中不给开辟额外空间的问题不是绝对的,实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟,今天看到小编这篇文章相见恨晚。

  3. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.