码迷,mamicode.com
首页 > 其他好文 > 详细

贪心Poj1328

时间:2014-06-11 08:00:13      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   http   color   com   

题目:http://poj.org/problem?id=1328 注意 输出 Case :这里是有个空格的。。和之前序列想法差不多 尽可能 向一边贪心。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
struct Node
{
    double l;double r;
}node[11111];
 
double  gao(double r,double x )
{
    return sqrt(r*r-x*x);
}
 
int cmp(const Node &a,const Node &b)
{
    return a.r<b.r;
}
int main()
{
    int n,d;
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    double x,y;int sum=1;
    while(scanf("%d%d",&n,&d),n||d){
        int jiba=0;
        for(int i=0;i<n;i++){
            scanf("%lf%lf",&x,&y);
            if(y>d||d<0||y<0){
                jiba=1;continue;
            }
            double len=gao(d,y);
            double l,r;
            l=x-len;r=x+len;
            node[i].l=l;node[i].r=r;
        }
        sort(node,node+n,cmp);
        double ret[11111];
        int ans=0;
        for(int i=0;i<n;i++){
            int flag=0;
            for(int j=0;j<ans;j++){
                if(node[i].l<=ret[j]&&node[i].r>=ret[j]){
                    flag=1;break;
                }
            }
            if(!flag){
                ret[ans++]=node[i].r;
            }
        }
        printf("Case %d: ",sum++);
        if(jiba)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}

  

贪心Poj1328,布布扣,bubuko.com

贪心Poj1328

标签:class   blog   code   http   color   com   

原文地址:http://www.cnblogs.com/yigexigua/p/3773604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!