Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
这个怎么会TLE呢?5000^2的复杂度阿按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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator