| ||||||||||
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 |
关于POJ 1328怎么WA?看了网上其他人的源码都差不多的思路。帮着看看吧。谢谢#include<stdio.h> #include<math.h> #include<stdlib.h> struct point{ int x; int y; }array[1000]; struct dis{ double left; double right; }ran[1000]; //按x坐标排序 void quicksort(int begin, int end){ if(begin < end){ int lab = rand()%(end-begin) + begin; struct point temp, loc; int i = begin - 1; temp = array[lab]; array[lab] = array[end-1]; array[end-1] = temp; for(int j = begin; j < end; ++ j){ if(array[j].x < temp.x){ ++ i; loc = array[i]; array[i] = array[j]; array[j] = loc; } } ++ i; temp = array[end-1]; array[end-1] = array[i]; array[i] = temp; quicksort(begin, i-1); quicksort(i+1, end); } } //贪心,从左到右画圈 int greedy(int isl, int rad){ if(rad <= 0) return -1; for(int i = 0; i < isl; ++ i) if(abs(array[i].y) > rad) return -1; double mid = 0; double sqr = pow(rad, 2); for(int i = 0; i < isl; ++ i){ mid = sqrt(sqr - pow(array[i].y, 2)); ran[i].left = array[i].x - mid; ran[i].right = array[i].x + mid; } int count = 1; int j; //若从左到右,基准点i的右边缘小于左边缘就往右遍历,否则计数变量加1,基准点为变为j for(int i = 0; i < isl; ){ for(j = i+1; j < isl; ++ j) if(ran[i].right < ran[j].left){ ++ count; i = j; break; } if(j == isl) break; } return count; } int main(){ int isl = 0, rad = 0; scanf("%d%d", &isl, &rad); int temp = 0; int count = 0; while(isl || rad){ ++ count; for(int i = 0; i < isl; ++ i) scanf("%d%d", &array[i].x, &array[i].y); quicksort(0, isl); temp = greedy(isl, rad); printf("Case%d:%d\n", count, temp); scanf("%d%d", &isl, &rad); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator