一开始想起了训练指南第一章一开始的流星那道题(例题20),扫描法
不过再一想,其实就是以村庄为圆心,D为半径作圆,可选区间就是高速路在圆内的部分。
稍微注意一下浮点转整数时的取舍,就变成了很纯粹的区间选点问题。
Run Time: 0.028s
#define UVa "8-11.1615.cpp" //1615 - Highway
char fileIn[30] = UVa, fileOut[30] = UVa;
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
//Global Variables. Reset upon Each Case!
long long L, D, N;
/
struct Village {
long long x, y;
long long l, r;
Village(){};
Village(int a, int b):x(a),y(b){
long long D2 = D*D, y2 = y*y;
long long t = sqrt(D2-y2);
l = max((long long)0, (long long)ceil(x-t));
r = min((long long)L, (long long)floor(x+t));
}
bool operator < (const Village& v) const {
return r < v.r;
}
};
int main() {
while(scanf("%lld%lld%lld", &L, &D, &N) != EOF) {
vector<Village> v;
long long a, b;
for(int i = 0; i < N; i ++) {
scanf("%lld%lld", &a, &b);
v.push_back(Village(a,b));
}
sort(v.begin(), v.end());
long long prev = v[0].r, ans = 1;
for(int i = 0; i < N; i ++) {
if(v[i].l > prev) {
ans ++;
prev = v[i].r;
}
}
printf("%lld\n", ans);
}
return 0;
}