Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- // #include <iostream>
- // #include <cstdio>
- // #include <cstdlib>
- // #include <algorithm>
- // #include <cmath>
- // #include <vector>
- // #include <set>
- // #include <map>
- // #include <queue>
- // #include <stack>
- // #include <ctime>
- // #include <cassert>
- // #include <complex>
- // #include <string>
- // #include <cstring>
- // #include <bitset>
- using namespace std;
- // #pragma GCC optimize("Ofast,no-stack-protector")
- // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- // #pragma GCC optimize("unroll-loops")
- #define ll long long int
- #define vi vector< int >
- #define vll vector< ll >
- #define sc scanf
- #define pf printf
- #define cspf(i) pf("Case %d: ", i)
- #define spc pf(" ")
- #define line pf("\n")
- #define ff first
- #define ss second
- #define mp make_pair
- #define pb push_back
- #define ppb pop_back
- #define tp(v,j) get<j>(v)
- #define Log(b,x) (log(x)/log(b))
- #define FOR(i,x,y) for(int i = int(x); i < int(y); i++)
- #define ROF(i,x,y) for(int i = int(x)-1; i >= int(y); i--)
- #define clr(arr,x) memset(arr, x, sizeof arr)
- #define vout(v,sz) for(int w=0;w<sz;w++){if(w) spc; cout<<v[w];}
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define unq(v) sort(all(v)),(v).resize(unique(all(v))-v.begin())
- #define fastIO ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
- #define sc1(x) sc("%d",&x)
- #define sc2(x,y) sc("%d %d", &x, &y)
- #define sc3(x,y,z) sc("%d %d %d", &x, &y, &z)
- #define scl1(x) sc("%lld",&x)
- #define scl2(x,y) sc("%lld %lld", &x, &y)
- #define scf1(x) sc("%lf",&x);
- #define scf2(x,y) sc("%lf %lf", &x, &y)
- #define pf1(x) pf("%d",x)
- #define pf2(x,y) pf("%d %d", x, y)
- #define pfl1(x) pf("%lld\n",x)
- #define pfl2(x,y) pf("%lld %lld", x, y)
- #define MOD (ll)(1e9)
- #define MaxN 1000000
- #define inf 0x3f3f3f3f
- #define PI acos(-1.0) // 3.1415926535897932
- #define eps 1e-6
- #ifdef ERFANUL007
- #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
- template < typename Arg1 >
- void __f(const char* name, Arg1&& arg1){
- cout << name << " = " << arg1 << std::endl;
- }
- template < typename Arg1, typename... Args>
- void __f(const char* names, Arg1&& arg1, Args&&... args){
- const char* comma = strchr(names, ',');
- cout.write(names, comma - names) << " = " << arg1 <<" | ";
- __f(comma+1, args...);
- }
- #else
- #define debug(...)
- #endif
- template <class T> inline T bigMod(T p,T e,T M){T ret=1; for(;e>0;e>>=1){ if(e&1) ret=(ret*p)%M; p=(p*p)%M;} return (T)ret;}
- template <class T> inline T modInverse(T a,T M){return bigMod(a,M-2,M);}
- template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
- template <class T> inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/gcd(a,b))*b;}
- int dx[] = { 1,-1, 0, 0}; //graph moves
- int dy[] = { 0, 0, 1,-1}; //graph moves
- struct PT{
- ll x, y;
- PT(){}
- PT(ll x, ll y) : x(x), y(y) {}
- void scan(){ scanf("%lld %lld",&x,&y);}
- void prnt(){ printf("%lld %lld\n",x,y);}
- inline bool operator == (const PT &p) const {
- return (x == p.x && y == p.y);
- }
- inline bool operator < (const PT &p) const {
- return ((x < p.x) || (x == p.x && y < p.y));
- }
- };
- ll SQ(ll x){ return x*x;}
- /* return the euclidean distance of two point */
- ll Dis(PT a,PT b){ return SQ(a.x-b.x) + SQ(a.y-b.y);}
- double absDis(PT a,PT b){ return sqrt(Dis(a,b));}
- /*convert degree to radian*/
- double toRadian(double x){ return (x*(PI/180.0));}
- /*convert radian to degree*/
- double toDegree(double x){ return (x*(180.0/PI));}
- /*convert a coordinate p from cartesian to polar
- r = sqrt(x*x + y*y)
- theta = atan(y/x) [in radian]*/
- PT toPolar(PT p){
- return PT(sqrt(SQ(p.x)+SQ(p.y)), atan(p.y/p.x));
- }
- /*convert a coordinate p from polar to cartesian
- x = r * cos(theta) [in radian]
- y = r * sin(theta) [in radian]*/
- PT toCartesian(PT a){
- return PT(a.x * cos(a.y), a.x * sin(a.y));
- }
- /* divide line ab into m:n and return midpoint */
- PT divideLine(PT a, PT b, double m, double n){
- return PT((a.x * m + b.x * n)/(m+n), (a.y * m + b.y * n)/(m+n));
- }
- /* convert two points to vector line */
- PT vect(PT a,PT b){ return PT(a.x-b.x, a.y-b.y);}
- /* two vector lines Dot product */
- ll Dot(PT a,PT b){ return a.x*b.x + a.y*b.y;}
- /* two vector lines Cross product */
- ll Cross(PT a,PT b){ return a.x*b.y - b.x*a.y;}
- /* line ab to point c
- 0 => if ab and c collinear
- + => clockwise
- - => anticlockwise */
- int orientation(PT a, PT b, PT c)
- {
- ll val = (b.y-a.y)*(c.x-b.x)-(b.x-a.x)*(c.y-b.y);
- if(val == 0) return 0;
- return val > 0 ? 1 : -1;
- }
- /* area of Triangle abc
- positive -> anticlockwise
- negative -> clockwise
- zero -> collinear */
- ll TriArea(PT a, PT b, PT c){ return Cross(vect(b, a), vect(c, a));}
- /*line ab and line cd is parallel if ab X cd = 0 */
- bool ifParallel(PT a,PT b,PT c,PT d){ return Cross(vect(a,b),vect(c,d)) == 0;}
- /*line ab and line cd is perpendicular if ab . cd = 0 */
- bool ifPerpendicular(PT a,PT b,PT c,PT d){ return Dot(vect(a,b),vect(c,d)) == 0;}
- /* segment ab and segment cd intersect or not */
- bool isSegIntersect(PT a,PT b,PT c,PT d){
- if(a.x > b.x) swap(a, b);
- if(a.y > b.y) swap(a, b);
- if(orientation(a, b, c) == 0) return c.x >= a.x && c.x <= b.x && c.y >= a.y && c.y <= b.y;
- if(orientation(a, b, d) == 0) return d.x >= a.x && d.x <= b.x && d.y >= a.y && d.y <= b.y;
- return (orientation(a, b, c) != orientation(a, b, d)
- && orientation(c, d, a) != orientation(c, d, b));
- }
- /* init first coordinate of array to f = arr[0] */
- PT _f;
- /*for clockwise sorting*/
- bool CC(PT &a,PT &b){ return (_f.x-a.x)*(_f.y-b.y)<(_f.x-b.x)*(_f.y-a.y);}
- /*for anti-clockwise sorting*/
- bool CCW(PT &a,PT &b){ return (_f.x-a.x)*(_f.y-b.y)>(_f.x-b.x)*(_f.y-a.y);}
- //--------------------------//
- struct Data
- {
- ll pos;
- bool sesh;
- int inx;
- Data(ll pos, bool sesh, int inx) : pos(pos), sesh(sesh), inx(inx) {}
- };
- struct cmp {
- bool operator() (Data &a, Data &b) const {
- if(a.pos == b.pos) return a.sesh > b.sesh;
- return a.pos > b.pos;
- }
- };
- PT guard[10001];
- bool vis[10001];
- ll k, s, d;
- int n;
- bool possible(ll y){
- priority_queue< Data, vector< Data >, cmp > pq;
- ll needs = 0, hyp = d * d;
- for(int i=0; i<n; i++){
- PT temp = PT(guard[i].x, k-y);
- ll h = Dis(guard[i], temp);
- if(h > hyp) return false;
- ll bhumi = sqrt(hyp-h);
- pq.push(Data(temp.x - bhumi, false,i));
- pq.push(Data(temp.x + bhumi, true, i));
- }
- clr(vis, false);
- stack < Data> st;
- while(!pq.empty()){
- if(pq.top().sesh && vis[pq.top().inx]) pq.pop();
- else if(!pq.top().sesh){
- st.push(pq.top());
- pq.pop();
- }
- else{
- needs++;
- while(!st.empty()){
- vis[st.top().inx] = true;
- st.pop();
- }
- }
- }
- return needs <= s;
- }
- ll solve(){
- ll lo = 1, hi = d, mid, ans = -1;
- while(lo <= hi){
- mid = (lo + hi)/2LL;
- if(possible(mid)){
- ans = mid;
- lo = mid+1;
- }
- else hi = mid-1;
- }
- return ans;
- }
- int main()
- {
- #ifdef ERFANUL007
- clock_t tStart = clock();
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- int t, ca=0; sc1(t);
- while(t--){
- scl1(k); sc1(n); scl2(s, d);
- k += MOD;
- for(int i=0; i<n; i++){
- ll x, y; scl2(x, y);
- guard[i] = PT(x+MOD, y+MOD);
- // guard[i].prnt();
- }
- // line;
- cspf(++ca);
- ll ans = solve();
- if(ans == -1) pf("impossible\n");
- else pfl1(ans);
- }
- #ifdef ERFANUL007
- fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement