Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

来人帮我看看哪里错了

Posted by pooorman at 2009-02-13 14:14:41 on Problem 1328
我把区间按右端点排序,然后从左到右贪心
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;

struct node
{
       double left, right;
};
int N, d, total;
node point[2000];
bool cmp(node a, node b)
{
     if (a.right<b.right) return true;
     else return false;
}
int  Greedy()
{
     int i, j;
     bool f[2000];
     double Xr;
     
     if (d<=0) return -1;
     memset(f, false, sizeof(f));
     
     sort(point, point+N, cmp);
     total=0;
     for (i=0; i<N; i++)
         if (!f[i])
         {
            f[i]=true;
            total++;
            Xr=point[i].right;
            for (j=i+1; j<N; j++)
                if (!f[j])
                {
                   if ((point[j].left<Xr)||(fabs(point[j].left-Xr)<0.001)) f[j]=true;
                }
         }
     
     return total;
}
int main()
{
    int T=0, k;
    double x, y;
    
    while (scanf("%d%d", &N, &d)&&(N!=0||d!=0))
    {
          for (k=0; k<N; k++)
          {
              scanf("%lf%lf", &x, &y);
              if (y>d) total=-1;
              point[k].left=x-sqrt(d*d-y*y);
              point[k].right=x+sqrt(d*d-y*y);
          }
          if (total!=-1) total=Greedy();
          T++;
          printf("Case %d: %d\n", T, total);
    }
    
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator