Advertisement
erfanul007

LOJ 1383

Jun 27th, 2021
965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.00 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // #include <iostream>
  3. // #include <cstdio>
  4. // #include <cstdlib>
  5. // #include <algorithm>
  6. // #include <cmath>
  7. // #include <vector>
  8. // #include <set>
  9. // #include <map>
  10. // #include <queue>
  11. // #include <stack>
  12. // #include <ctime>
  13. // #include <cassert>
  14. // #include <complex>
  15. // #include <string>
  16. // #include <cstring>
  17. // #include <bitset>
  18. using namespace std;
  19.  
  20. // #pragma GCC optimize("Ofast,no-stack-protector")
  21. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  22. // #pragma GCC optimize("unroll-loops")
  23.  
  24. #define ll              long long int
  25. #define vi              vector< int >
  26. #define vll             vector< ll >
  27.  
  28. #define sc              scanf
  29. #define pf              printf
  30. #define cspf(i)         pf("Case %d: ", i)
  31. #define spc             pf(" ")
  32. #define line            pf("\n")
  33.  
  34. #define ff              first
  35. #define ss              second
  36. #define mp              make_pair
  37. #define pb              push_back
  38. #define ppb             pop_back
  39. #define tp(v,j)         get<j>(v)
  40. #define Log(b,x)        (log(x)/log(b))
  41.  
  42. #define FOR(i,x,y)      for(int i = int(x); i < int(y); i++)
  43. #define ROF(i,x,y)      for(int i = int(x)-1; i >= int(y); i--)
  44. #define clr(arr,x)      memset(arr, x, sizeof arr)
  45. #define vout(v,sz)      for(int w=0;w<sz;w++){if(w) spc; cout<<v[w];}
  46. #define all(v)          v.begin(), v.end()
  47. #define rall(v)         v.rbegin(), v.rend()
  48. #define unq(v)          sort(all(v)),(v).resize(unique(all(v))-v.begin())
  49. #define fastIO          ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
  50.  
  51. #define sc1(x)          sc("%d",&x)
  52. #define sc2(x,y)        sc("%d %d", &x, &y)
  53. #define sc3(x,y,z)      sc("%d %d %d", &x, &y, &z)
  54. #define scl1(x)         sc("%lld",&x)
  55. #define scl2(x,y)       sc("%lld %lld", &x, &y)
  56. #define scf1(x)         sc("%lf",&x);
  57. #define scf2(x,y)       sc("%lf %lf", &x, &y)
  58.  
  59. #define pf1(x)          pf("%d",x)
  60. #define pf2(x,y)        pf("%d %d", x, y)
  61. #define pfl1(x)         pf("%lld\n",x)
  62. #define pfl2(x,y)       pf("%lld %lld", x, y)
  63.  
  64. #define MOD             (ll)(1e9)
  65. #define MaxN            1000000
  66. #define inf             0x3f3f3f3f
  67. #define PI              acos(-1.0)  // 3.1415926535897932
  68. #define eps             1e-6
  69.  
  70. #ifdef ERFANUL007
  71.     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
  72.     template < typename Arg1 >
  73.     void __f(const char* name, Arg1&& arg1){
  74.         cout << name << " = " << arg1 << std::endl;
  75.     }
  76.     template < typename Arg1, typename... Args>
  77.     void __f(const char* names, Arg1&& arg1, Args&&... args){
  78.         const char* comma = strchr(names, ',');
  79.         cout.write(names, comma - names) << " = " << arg1 <<" | ";
  80.         __f(comma+1, args...);
  81.     }
  82. #else
  83.     #define debug(...)
  84. #endif
  85.  
  86. 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;}
  87. template <class T> inline T modInverse(T a,T M){return bigMod(a,M-2,M);}
  88. template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
  89. template <class T> inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/gcd(a,b))*b;}
  90.  
  91. int dx[] = { 1,-1, 0, 0};                //graph moves
  92. int dy[] = { 0, 0, 1,-1};               //graph moves
  93.  
  94. struct PT{
  95.     ll x, y;
  96.     PT(){}
  97.     PT(ll x, ll y) : x(x), y(y) {}
  98.     void scan(){ scanf("%lld %lld",&x,&y);}
  99.     void prnt(){ printf("%lld %lld\n",x,y);}
  100.     inline bool operator == (const PT &p) const {
  101.         return (x == p.x && y == p.y);
  102.     }
  103.     inline bool operator < (const PT &p) const {
  104.         return ((x < p.x) || (x == p.x && y < p.y));
  105.     }
  106. };
  107.  
  108. ll SQ(ll x){ return x*x;}
  109. /* return the euclidean distance of two point */
  110. ll Dis(PT a,PT b){ return SQ(a.x-b.x) + SQ(a.y-b.y);}
  111.  
  112. double absDis(PT a,PT b){ return sqrt(Dis(a,b));}
  113.  
  114. /*convert degree to radian*/
  115. double toRadian(double x){ return (x*(PI/180.0));}
  116. /*convert radian to degree*/
  117. double toDegree(double x){ return (x*(180.0/PI));}
  118.  
  119. /*convert a coordinate p from cartesian to polar
  120. r = sqrt(x*x + y*y)
  121. theta = atan(y/x) [in radian]*/
  122. PT toPolar(PT p){
  123.     return PT(sqrt(SQ(p.x)+SQ(p.y)), atan(p.y/p.x));
  124. }
  125. /*convert a coordinate p from polar to cartesian
  126. x = r * cos(theta) [in radian]
  127. y = r * sin(theta) [in radian]*/
  128. PT toCartesian(PT a){
  129.     return PT(a.x * cos(a.y), a.x * sin(a.y));
  130. }
  131.  
  132. /* divide line ab into m:n and return midpoint */
  133. PT divideLine(PT a, PT b, double m, double n){
  134.     return PT((a.x * m + b.x * n)/(m+n), (a.y * m + b.y * n)/(m+n));
  135. }
  136.  
  137. /* convert two points to vector line */
  138. PT vect(PT a,PT b){ return PT(a.x-b.x, a.y-b.y);}
  139. /* two vector lines Dot product */
  140. ll Dot(PT a,PT b){ return a.x*b.x + a.y*b.y;}
  141. /* two vector lines Cross product */
  142. ll Cross(PT a,PT b){ return a.x*b.y - b.x*a.y;}
  143.  
  144. /* line ab to point c
  145. 0 => if ab and c collinear
  146. + => clockwise
  147. - => anticlockwise */
  148. int orientation(PT a, PT b, PT c)
  149. {
  150.     ll val = (b.y-a.y)*(c.x-b.x)-(b.x-a.x)*(c.y-b.y);
  151.     if(val == 0) return 0;
  152.     return val > 0 ? 1 : -1;
  153. }
  154.  
  155. /* area of Triangle abc
  156. positive -> anticlockwise
  157. negative -> clockwise
  158. zero -> collinear */
  159. ll TriArea(PT a, PT b, PT c){ return Cross(vect(b, a), vect(c, a));}
  160.  
  161. /*line ab and line cd is parallel if ab X cd = 0 */
  162. bool ifParallel(PT a,PT b,PT c,PT d){ return Cross(vect(a,b),vect(c,d)) == 0;}
  163.  
  164. /*line ab and line cd is perpendicular if ab . cd = 0 */
  165. bool ifPerpendicular(PT a,PT b,PT c,PT d){ return Dot(vect(a,b),vect(c,d)) == 0;}
  166.  
  167. /* segment ab and segment cd intersect or not */
  168. bool isSegIntersect(PT a,PT b,PT c,PT d){
  169.     if(a.x > b.x) swap(a, b);
  170.     if(a.y > b.y) swap(a, b);
  171.     if(orientation(a, b, c) == 0) return c.x >= a.x && c.x <= b.x && c.y >= a.y && c.y <= b.y;
  172.     if(orientation(a, b, d) == 0) return d.x >= a.x && d.x <= b.x && d.y >= a.y && d.y <= b.y;
  173.     return (orientation(a, b, c) != orientation(a, b, d)
  174.         && orientation(c, d, a) != orientation(c, d, b));
  175. }
  176.  
  177. /* init first coordinate of array to f = arr[0] */
  178. PT _f;
  179. /*for clockwise sorting*/
  180. bool CC(PT &a,PT &b){ return (_f.x-a.x)*(_f.y-b.y)<(_f.x-b.x)*(_f.y-a.y);}
  181. /*for anti-clockwise sorting*/
  182. bool CCW(PT &a,PT &b){ return (_f.x-a.x)*(_f.y-b.y)>(_f.x-b.x)*(_f.y-a.y);}
  183. //--------------------------//
  184.  
  185. struct Data
  186. {
  187.     ll pos;
  188.     bool sesh;
  189.     int inx;
  190.     Data(ll pos, bool sesh, int inx) : pos(pos), sesh(sesh), inx(inx) {}
  191. };
  192.  
  193. struct cmp {
  194.     bool operator() (Data &a, Data &b) const {
  195.         if(a.pos == b.pos) return a.sesh > b.sesh;
  196.         return a.pos > b.pos;
  197.     }
  198. };
  199.  
  200. PT guard[10001];
  201. bool vis[10001];
  202. ll k, s, d;
  203. int n;
  204.  
  205. bool possible(ll y){
  206.     priority_queue< Data, vector< Data >, cmp > pq;
  207.     ll needs = 0, hyp = d * d;
  208.     for(int i=0; i<n; i++){
  209.         PT temp = PT(guard[i].x, k-y);
  210.         ll h = Dis(guard[i], temp);
  211.         if(h > hyp) return false;
  212.         ll bhumi = sqrt(hyp-h);
  213.         pq.push(Data(temp.x - bhumi, false,i));
  214.         pq.push(Data(temp.x + bhumi, true, i));
  215.     }
  216.     clr(vis, false);
  217.     stack < Data> st;
  218.     while(!pq.empty()){
  219.         if(pq.top().sesh && vis[pq.top().inx]) pq.pop();
  220.         else if(!pq.top().sesh){
  221.             st.push(pq.top());
  222.             pq.pop();
  223.         }
  224.         else{
  225.             needs++;
  226.             while(!st.empty()){
  227.                 vis[st.top().inx] = true;
  228.                 st.pop();
  229.             }
  230.         }
  231.     }
  232.  
  233.     return needs <= s;
  234. }
  235.  
  236. ll solve(){
  237.     ll lo = 1, hi = d, mid, ans = -1;
  238.     while(lo <= hi){
  239.         mid = (lo + hi)/2LL;
  240.         if(possible(mid)){
  241.             ans = mid;
  242.             lo = mid+1;
  243.         }
  244.         else hi = mid-1;
  245.     }
  246.     return ans;
  247. }
  248.  
  249. int main()
  250. {
  251.     #ifdef ERFANUL007
  252.         clock_t tStart = clock();
  253.         freopen("input.txt", "r", stdin);
  254.         freopen("output.txt", "w", stdout);
  255.     #endif
  256.  
  257.     int t, ca=0; sc1(t);
  258.    
  259.     while(t--){
  260.         scl1(k); sc1(n); scl2(s, d);
  261.         k += MOD;
  262.    
  263.         for(int i=0; i<n; i++){
  264.             ll x, y; scl2(x, y);
  265.             guard[i] = PT(x+MOD, y+MOD);
  266.             // guard[i].prnt();
  267.         }
  268.         // line;
  269.         cspf(++ca);
  270.         ll ans = solve();
  271.         if(ans == -1) pf("impossible\n");
  272.         else pfl1(ans);
  273.     }
  274.  
  275.    
  276.  
  277.     #ifdef ERFANUL007
  278.         fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
  279.     #endif
  280.  
  281.     return 0;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement