POJ1328 qsort

至于很多朋友说这道题快排为什么不行,请看qcmp函数,返回1与-1

// Rader.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <algorithm>
#include <math.h>
#include <iostream>
using namespace std;
typedef struct island{
    double l;
    double r;
}Node;
int   qcmp(   const   void   *arg1,   const   void   *arg2   )  
{  
    return   (((Node*)arg1)->l >  ((Node*)arg2)->l)?1:-1;
}
int   cmp(   Node   arg1,   Node   arg2   )  
{  
    return   arg1.l<  arg2.l;
}
int main(int argc, char* argv[])
{
    freopen("d:/t.txt","r",stdin);
    int count;
    int r;
    int c = 0;
    while(cin>>count>>r&&(count||r)){
        bool err = false;
        Node n[1000];
        c++;
        for(int i=0;i<count;i++){
            double x,y;
            cin>>x>>y;
            y=y<0?-y:y;
            if(y>r){
                err = true;
            }
            double d = sqrt(r*r-y*y);
            n[i].l = x-d;
            n[i].r = x+d;
        }
        if(err)
        {
            printf("Case %d: -1\n",c);
            continue;
        }
        qsort(n,count,sizeof(n[0]),qcmp);
        //sort(n,n+count,cmp);
        for(int m = 0;m<count;m++)
            printf("%f\n",n[m].l);
        int ans = 1;
        double left = n[0].r;

        for(int j=1;j<count;j++){
            if(n[j].r<=left){
                left = n[j].r;
            }else{
                if(n[j].l>left){ans++;left = n[j].r;}
            }
        }
        printf("Case %d: %d\n",c,ans);
    }
    return 0;
}

你可能感兴趣的:(sort)