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

这个怎么会TLE呢?5000^2的复杂度阿

Posted by Eagle_eo at 2008-07-10 15:34:39 on Problem 1925
按building DP的,有点像最长上升子序列

#include <iostream>
#include <math.h>
using namespace std;

#define N 5001
#define INF 0x7fffffff
int n;
int x[N], y[N];
int currx[N], curry[N];
int c[N];

double dis(int x1, int y1, int x2, int y2)
{
       return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

void work()
{
     int i,j,min;
     scanf ("%d",&n);
     for (i=0; i<n; i++)
     {
         scanf ("%d%d",&x[i],&y[i]);
         c[i] = INF;
     }
     memset(currx, -1, sizeof(currx));
     memset(curry, -1, sizeof(curry));
     c[0] = 0;
     currx[0] = x[0], curry[0] = y[0];
     int tx,ty;
     for (i=1; i<n; i++)
     {
         min = INF; tx = 0; ty = 0;
         for (j=i-1; j>=0; j--)
         {
             if (currx[j] == -1) continue;
             if (currx[j] > x[i]) continue;
             double d = dis(currx[j], curry[j], x[i], y[i]);
             //printf ("dis %.0lf\n",d);
             if (d <= y[i])
             {
                int t = c[j]+1;
                if (t<min){
                   min = t;
                   tx = x[i]+(x[i]-currx[j]);
                   ty = curry[j];
                }
                //printf ("tx %d  ty %d\n",tx,ty);
             }
         }
         //printf ("min %d\n",min);
         if (c[i]>min){
            c[i] = min;
            currx[i] = tx;
            curry[i] = ty;
            //printf ("currx %d curry %d\n",currx[i],curry[i]);
         }
     }
     //for (i=0; i<n; i++)
         //printf ("%d\n",c[i]);
	 if (c[n-1]!=INF)
		printf ("%d\n",c[n-1]);
	else
		printf ("-1\n");
}

int main()
{
    int cas;
    scanf ("%d",&cas);
    while (cas--)
    {
          work();
    }
    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