Watering Grass UVA - 10382
喷水装置的圆心和半径确定,就能确定左端和右端。开始时pos=0,选取左端小于pos,右端最大的更新pos。
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <iomanip> #include <cstring> #include <map> #include <queue> #include <set> #include <cassert> #include <stack> #include <bitset> #define mkp make_pair using namespace std; const double EPS=1e-8; typedef long long lon; const lon SZ=330,INF=0x7FFFFFFF,mod=1000000007; struct nd{ double ll,rr; nd(double a,double b):ll(a),rr(b){} bool operator<(const nd& rbs)const { return ll<rbs.ll; } }; void init(int n,double wid,vector<nd> &vct) { for(int i=0;i<n;++i) { double pi,ri; cin>>pi>>ri; if(ri>0.5*wid) { double delta=sqrt(ri*ri-0.25*wid*wid); vct.push_back(nd(pi-delta,pi+delta)); } } sort(vct.begin(),vct.end()); } int work(double len,vector<nd> &vct) { double pos=0; int res=0; for(int i=0;;) { double rm=0; for(;i<vct.size()&&(vct[i].ll<pos||fabs(vct[i].ll-pos)<EPS);++i) {//注意限制i的条件写在前 rm=max(rm,vct[i].rr); } if(fabs(rm)<EPS)return -1; pos=rm; ++res; if(pos>len||fabs(pos-len)<EPS)break; if(i>=vct.size())return -1; } //cout<<res<<endl; return res; } int main() { std::ios::sync_with_stdio(0); //freopen("d:\\1.txt","r",stdin); lon casenum; //cin>>casenum; //cout<<casenum<<endl; //for(lon time=1;time<=casenum;++time) int n,len,wid; for(int time=1;cin>>n>>len>>wid;++time) { vector<nd> vct; init(n,wid,vct); cout<<work(len,vct)<<endl; } return 0; }
重复。