Advertisement
erfanul007

LOJ 1285

Jun 27th, 2021
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.17 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:\n", 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 ",x)
  62. #define pfl2(x,y)       pf("%lld %lld", x, y)
  63.  
  64. #define MOD             (int)(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.     int id;
  97.     PT(){}
  98.     PT(ll x, ll y) : x(x), y(y), id(0) {}
  99.     PT(ll x, ll y, int id) : x(x), y(y), id(id) {}
  100.     void scan(){ scanf("%lld %lld",&x,&y);}
  101.     void prnt(){ printf("%lld %lld\n",x, y);}
  102.     inline bool operator == (const PT &p) const {
  103.         return (x == p.x && y == p.y);
  104.     }
  105.     inline bool operator < (const PT &p) const {
  106.         return ((x < p.x) || (x == p.x && y < p.y));
  107.     }
  108. };
  109.  
  110. ll SQ(ll x){ return x*x;}
  111. /* return the euclidean distance of two point */
  112. ll Dis(PT a,PT b){ return SQ(a.x-b.x) + SQ(a.y-b.y);}
  113.  
  114. double absDis(PT a,PT b){ return sqrt(Dis(a,b));}
  115.  
  116. /*convert degree to radian*/
  117. double toRadian(double x){ return (x*(PI/180.0));}
  118. /*convert radian to degree*/
  119. double toDegree(double x){ return (x*(180.0/PI));}
  120.  
  121. /*convert a coordinate p from cartesian to polar
  122. r = sqrt(x*x + y*y)
  123. theta = atan(y/x) [in radian]*/
  124. PT toPolar(PT p){
  125.     return PT(sqrt(SQ(p.x)+SQ(p.y)), atan(p.y/p.x));
  126. }
  127. /*convert a coordinate p from polar to cartesian
  128. x = r * cos(theta) [in radian]
  129. y = r * sin(theta) [in radian]*/
  130. PT toCartesian(PT a){
  131.     return PT(a.x * cos(a.y), a.x * sin(a.y));
  132. }
  133.  
  134. /* divide line ab into m:n and return midpoint */
  135. PT divideLine(PT a, PT b, double m, double n){
  136.     return PT((a.x * m + b.x * n)/(m+n), (a.y * m + b.y * n)/(m+n));
  137. }
  138.  
  139. /* convert two points to vector line */
  140. PT vect(PT a,PT b){ return PT(a.x-b.x, a.y-b.y);}
  141. /* two vector lines Dot product */
  142. ll Dot(PT a,PT b){ return a.x*b.x + a.y*b.y;}
  143. /* two vector lines Cross product */
  144. ll Cross(PT a,PT b){ return a.x*b.y - b.x*a.y;}
  145.  
  146. /* line ab to point c
  147. 0 => if ab and c collinear
  148. + => clockwise
  149. - => anticlockwise */
  150. int orientation(PT a, PT b, PT c)
  151. {
  152.     ll val = (b.y-a.y)*(c.x-b.x)-(b.x-a.x)*(c.y-b.y);
  153.     if(val == 0) return 0;
  154.     return val > 0 ? 1 : -1;
  155. }
  156.  
  157. /* area of Triangle abc
  158. positive -> anticlockwise
  159. negative -> clockwise
  160. zero -> collinear */
  161. ll TriArea(PT a, PT b, PT c){ return Cross(vect(b, a), vect(c, a));}
  162.  
  163. /*line ab and line cd is parallel if ab X cd = 0 */
  164. bool ifParallel(PT a,PT b,PT c,PT d){ return Cross(vect(a,b),vect(c,d)) == 0;}
  165.  
  166. /*line ab and line cd is perpendicular if ab . cd = 0 */
  167. bool ifPerpendicular(PT a,PT b,PT c,PT d){ return Dot(vect(a,b),vect(c,d)) == 0;}
  168.  
  169. /* segment ab and segment cd intersect or not */
  170. bool isSegIntersect(PT a,PT b,PT c,PT d){
  171.     if(a.x > b.x) swap(a, b);
  172.     if(a.y > b.y) swap(a, b);
  173.     if(orientation(a, b, c) == 0) return c.x >= a.x && c.x <= b.x && c.y >= a.y && c.y <= b.y;
  174.     if(orientation(a, b, d) == 0) return d.x >= a.x && d.x <= b.x && d.y >= a.y && d.y <= b.y;
  175.     return (orientation(a, b, c) != orientation(a, b, d)
  176.         && orientation(c, d, a) != orientation(c, d, b));
  177. }
  178.  
  179. /* init first coordinate of array to f = arr[0] */
  180. PT _f;
  181. /*for clockwise sorting*/
  182. bool CC(PT &a,PT &b){ return (_f.x-a.x)*(_f.y-b.y)<(_f.x-b.x)*(_f.y-a.y);}
  183. /*for anti-clockwise sorting*/
  184. bool CCW(PT &a,PT &b){ return (_f.x-a.x)*(_f.y-b.y)>(_f.x-b.x)*(_f.y-a.y);}
  185. /* init first coordinate of array to _f = lowest left
  186. swap lowest left with 0th and sort start from 1st
  187. compare _fab, how it turns, if collinear sort by distance */
  188. /* for clockwise sorting */
  189. bool ClockCmp(PT &a,PT &b){
  190.     ll val = orientation(_f, a, b);
  191.     if(val == 0) return Dis(_f, a) < Dis(_f, b);
  192.     return val > 0;
  193. }
  194. /* for anti-clockwise sorting */
  195. bool AClockCmp(PT &a,PT &b){
  196.     ll val = orientation(_f, a, b);
  197.     if(val == 0) return Dis(_f, a) < Dis(_f, b);
  198.     return val < 0;
  199. }
  200. //--------------------------//
  201.  
  202. /* receive a list of points and return their concave hull */
  203. bool possible; // mark if possible or not
  204. vector< PT > GrahamScan(vector< PT >& v){
  205.     int n = v.size();
  206.     if(n < 3) return v;
  207.     _f = v[0];
  208.     int pivot = 0;
  209.     for(int i=1; i<v.size(); i++){
  210.         if(v[i].y < _f.y) _f = v[i], pivot = i;
  211.         else if(v[i].y == _f.y && v[i].x < _f.x) _f = v[i], pivot = i;
  212.     }
  213.     swap(v[0], v[pivot]);
  214.     sort(v.begin()+1, v.end(), AClockCmp);
  215.     vector< PT > hull;
  216.     hull.pb(v[0]); hull.pb(v[1]);
  217.     for(int i=2; i<n; i++){
  218.         if(orientation(hull[hull.size()-2], hull.back(), v[i])) possible = true;
  219.         hull.pb(v[i]);
  220.     }
  221.     pivot = n-1;
  222.     for(int i=n-2; i>=0; i--){
  223.         if(orientation(hull[0], hull[pivot], hull[i])) break;
  224.         pivot = i;
  225.     }
  226.     reverse(hull.begin()+pivot, hull.end());
  227.     return hull;
  228. }
  229.  
  230. int main()
  231. {
  232.     #ifdef ERFANUL007
  233.         clock_t tStart = clock();
  234.         freopen("input.txt", "r", stdin);
  235.         freopen("output.txt", "w", stdout);
  236.     #endif
  237.  
  238.     int t, ca=0; sc1(t);
  239.  
  240.     while(t--){
  241.         int n; sc1(n);
  242.         vector< PT > v(n);
  243.         for(int i=0; i<n; i++){
  244.             ll x, y; scl2(x, y);
  245.             v[i] = PT(x, y, i);
  246.         }
  247.         possible = false;
  248.         vector< PT > ConcaveHull = GrahamScan(v);
  249.         cspf(++ca);
  250.         if(!possible){
  251.             pf("Impossible\n");
  252.             continue;
  253.         }
  254.         for(int i=0; i<n; i++){
  255.             if(i) spc;
  256.             pf1(ConcaveHull[i].id);
  257.         }
  258.         line;
  259.     }
  260.  
  261.  
  262.     #ifdef ERFANUL007
  263.         fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
  264.     #endif
  265.  
  266.     return 0;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement