UVA 11627 Slalom(二分)
二分,判断的时候,一个点一个点的考虑肯定是不行啦,考虑的单位是一个区间,
每次左端点尽量向左边移动,右端点尽量向右,得到下次可以达到的范围,检查一下和下一个区间有没有交集。
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5+5, maxns = 1e6+5; double x[maxn],y[maxn]; double W,vh; int N; int S[maxns]; bool ok(int s) { double lm = x[0], rm = x[0]+W; for(int i = 1;i < N; i++){ double t = (y[i]-y[i-1])/s; lm -= t*vh; rm += t*vh; if(rm < x[i] || lm > x[i]+W) return false; lm = max(lm,x[i]); rm = min(rm,x[i]+W); } return true; } int main() { //freopen("in.txt","r",stdin); int T; scanf("%d",&T); while(T--){ scanf("%lf%lf%d",&W,&vh,&N); for(int i = 0; i < N; i++) scanf("%lf%lf",x+i,y+i); int ns; scanf("%d",&ns); for(int i = 0; i < ns; i++) scanf("%d",S+i); sort(S,S+ns); if(!ok(S[0])) { puts("IMPOSSIBLE") ;continue;} int l = 0, r = ns-1, m; for(;S[l]<S[r]; ok(S[m])?l=m:r=m-1 ) m = (l+r+1)>>1; printf("%d\n",S[l]); } return 0; }