时间:2016-03-23 18:17:43 星期三
题目编号:[2016-03-23][POJ][3069][Saruman’s Army]
题目大意:在数轴上n个点放半径为r的圆,要求所有点被覆盖,问至少需要多少元
分析:用两个for,第一个for找到圆左边一般能覆盖的点,第二个for求圆后面一般能覆盖的点,这些区域的点放一个圆
#include <algorithm>#include <cstdio>using namespace std;int a[1000 + 10];int main(){ int r,n; while(~scanf("%d%d",&r,&n) && (~n||~r)){ for(int i = 0 ; i < n ;++i){ scanf("%d",a+i); } int prepos,ans = 0; sort(a,a+n); for(int i = 0; i < n ;){ prepos = a[i]; while(i < n && a[i] - prepos <= r) ++i; ++ans; prepos = a[i - 1]; while(i < n && a[i] - prepos <= r) ++i; } printf("%d\n",ans); } return 0;}