• POJ 1328 Radar Installation


    小小的一道贪心算法题,

    由于读数据的时候未读完数据居然卡了两个小时。

    不过这次代码写的自我感觉不错,结构也清楚,这种小题现在对我来讲毫无压力啊。。。。

    View Code
     1 #include<iostream>
    2 #include<stdlib.h>
    3 #include<math.h>
    4
    5 using namespace std;
    6
    7 struct ln
    8 {
    9 double x1;
    10 double x2;
    11 }section[1010];
    12
    13 int cmp(const void *a,const void *b)
    14 {
    15 struct ln* c=(ln *)a;
    16 struct ln* d=(ln *)b;
    17 return c->x1>d->x1?1:-1;
    18 //return (*(ln*)a)->x1>(*(ln*)b)->x1?1:-1;//注意解引用操作符。。。
    19 }
    20
    21 int main()
    22 {
    23 int n,d;
    24 int cases=0;
    25 while(cin>>n>>d&&!(n==0&&d==0))
    26 {
    27 cases++;
    28 bool flag=false;
    29 int solution=1;
    30 for(int i=0;i<n;i++)
    31 {
    32 int x,y;
    33 cin>>x>>y;
    34 if(y>d||y<(-1)*d||d<0)
    35 {
    36 flag=true;
    37 solution=-1;
    38 // break; //就是这条万恶的语句,导致runtime error。。。。
    39 }
    40 section[i].x1=(double)x-sqrt((double)(d*d-y*y));
    41 section[i].x2=(double)x+sqrt((double)(d*d-y*y));
    42 }
    43 if(!flag)
    44 {
    45 qsort(section,n,sizeof(section[0]),cmp);
    46 for(int i=0;i<n-1;i++)
    47 {
    48 if(section[i].x2>=section[i+1].x1)
    49 {
    50 if(section[i+1].x2>section[i].x2)
    51 section[i+1].x2=section[i].x2;
    52
    53 }
    54 else solution++;
    55 }
    56 }
    57 cout<<"Case "<<cases<<": "<<solution<<endl;
    58 }
    59 return 0;
    60 }

  • 相关阅读:
    mybatis中_parameter使用和常用sql
    ibatis中井号跟美元符号区别(#.$)
    mybatis动态sql中的trim标签的使用
    c语言捕捉异常
    lua lua解读
    lua luaconf解读
    android堆栈调试--详细
    cocos2d-x安卓应用启动调用过程简析
    ndk-stack使用方法
    cocos2dx3.2移植android
  • 原文地址:https://www.cnblogs.com/YipWingTim/p/2209604.html
Copyright © 2020-2023  润新知