mr_dot_convict

873-E-Awards-for-contestants-codeforces-mr.convict

May 5th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.85 KB | None | 0 0
  1. // Hack it and have it ;) //
  2. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  3.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  4.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  5.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  6.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  7. When I wrote this, only God and I understood what I was doing
  8.  ** * * * * * * * Now, only God knows!* * * * * * */
  9. #include      <bits/stdc++.h> /*{{{*/
  10. #include      <ext/pb_ds/assoc_container.hpp>
  11. #include      <ext/pb_ds/tree_policy.hpp>
  12. using namespace std;
  13. using namespace __gnu_pbds;
  14.  
  15. #ifdef CONVICTION
  16. #pragma GCC       optimize ("Ofast")
  17. #pragma GCC       optimize ("unroll-loops")
  18. #pragma GCC       target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  19. #endif
  20.  
  21. #define IOS       ios_base::sync_with_stdio(false); cin.tie (nullptr)
  22. #define PREC      cout.precision (10); cout << fixed
  23. #define X         first
  24. #define Y         second
  25. #define sz(x)     (int)x.size()
  26. #define fr(i,x,y) for (int i = (int)x; i <= (int)y; ++i)
  27. #define rv(i,x,y) for (int i = (int)x; i >= (int)y; --i)
  28. #define bcnt(x)   __builtin_popcount(x)
  29. #define bcntll(x) __builtin_popcountll(x)
  30. #define bg(x)     " [ " << #x << " : " << (x) << " ] "
  31. #define un(x)     sort(x.begin(), x.end()), \
  32.   x.erase(unique(x.begin(), x.end()), x.end())
  33. using   ll  =     long long;
  34. using   ull =     unsigned long long;
  35. using   ff  =     long double;
  36. using   pii =     pair<int,int>;
  37. using   pil =     pair<int,ll>;
  38. using   pll =     pair<ll,ll>;
  39. using   vi  =     vector <int>;
  40. using   vl  =     vector<ll>;
  41. using   vvi =     vector <vi>;
  42. using   vvl =     vector <vl>;
  43. using   vp  =     vector <pii>;
  44. using   vvp =     vector <vp>;
  45. typedef tree
  46. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  47. ordered_set;
  48.  
  49. struct chash {
  50.   int operator () (pii x) const { return x.X*31 + x.Y; }
  51. };
  52. gp_hash_table <pii, int, chash> gmp;
  53.  
  54. #if __cplusplus > 201103L
  55. seed_seq seq{
  56.   (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  57.     (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  58.     (uint64_t) __builtin_ia32_rdtsc(),
  59.     (uint64_t) (uintptr_t) make_unique<char>().get()
  60. };
  61. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  62. #else
  63. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  64. mt19937 rng(seed);
  65. #endif
  66.  
  67. #ifdef CONVICTION
  68. void __print(int x) {cerr << x;}
  69. void __print(long x) {cerr << x;}
  70. void __print(long long x) {cerr << x;}
  71. void __print(unsigned x) {cerr << x;}
  72. void __print(unsigned long x) {cerr << x;}
  73. void __print(unsigned long long x) {cerr << x;}
  74. void __print(float x) {cerr << x;}
  75. void __print(double x) {cerr << x;}
  76. void __print(long double x) {cerr << x;}
  77. void __print(char x) {cerr << '\'' << x << '\'';}
  78. void __print(const char *x) {cerr << '\"' << x << '\"';}
  79. void __print(const string &x) {cerr << '\"' << x << '\"';}
  80. void __print(bool x) {cerr << (x ? "true" : "false");}
  81.  
  82. template<typename T, typename V>
  83. void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  84. template<typename T>
  85. void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  86. void _print() {cerr << "]" << endl;}
  87. template <typename T, typename... V>
  88. void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  89. #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
  90. #else
  91. #define debug(x...)
  92. #endif
  93. //         __                                           __
  94. //        (**)                                         (**)
  95. //        IIII                                         IIII
  96. //        ####                                         ####
  97. //        HHHH     Madness comes, and madness goes     HHHH
  98. //        HHHH    An insane place, with insane moves   HHHH
  99. //        ####   Battles without, for battles within   ####
  100. //     ___IIII___        Where evil lives,          ___IIII___
  101. //  .-`_._"**"_._`-.      and evil rules         .-`_._"**"_._`-.
  102. // |/``  .`\/`.  ``\|    Breaking them up,      |/``  .`\/`.  ``\|
  103. // `     }    {     '  just breaking them in    `     }    {     '
  104. //       ) () (  Quickest way out, quickest way wins  ) () (
  105. //       ( :: )      Never disclose, never betray     ( :: )
  106. //       | :: |   Cease to speak or cease to breath   | :: |
  107. //       | )( |        And when you kill a man,       | )( |
  108. //       | || |          you're a murderer            | || |
  109. //       | || |             Kill many                 | || |
  110. //       | || |        and you're a conqueror         | || |
  111. //       | || |        Kill them all ... Ooh..        | || |
  112. //       | || |           Oh you're a God!            | || |
  113. //       ( () )                       -Megadeth       ( () )
  114. //        \  /                                         \  /
  115. //         \/                                           \/
  116. /*}}}*/
  117. /*****************************************************************************/
  118. //Don’t practice until you get it right. Practice until you can’t get it wrong
  119.  
  120. #define T(x) (1 << (x))
  121. template <typename F, typename T = int>
  122. class sparse_table {
  123.   public:
  124.     static const int D = 21;
  125.     int n;
  126.     vector <vector <T>> dp, who;
  127.     vector <int> LOG;
  128.  
  129.     sparse_table (const vector <T> &v) {
  130.       n = (int)v.size();
  131.       dp.assign(D + 1, vector <T> (n));
  132.       who.assign(D + 1, vector <T> (n));
  133.       LOG.assign(n + 1, 0);
  134.  
  135.       for (int i = 0; i < n; ++i)
  136.         dp[0][i] = v[i];
  137.       iota(who[0].begin(), who[0].end(), 0);
  138.  
  139.       for (int k = 1; k <= D; ++k)
  140.         for (int i = 0; i < n; ++i)
  141.           if (i + T(k) - 1 < n) {
  142.             T lt = dp[k - 1][i], rt = dp[k - 1][i + T(k - 1)];
  143.             if (F()(lt, rt) == lt) { // max
  144.               who[k][i] = who[k - 1][i];
  145.               dp[k][i] = lt;
  146.             }
  147.             else {
  148.               who[k][i] = who[k - 1][i + T(k - 1)];
  149.               dp[k][i] = rt;
  150.             }
  151.           }
  152.  
  153.       for (int i = 2; i <= n; ++i)
  154.         LOG[i] = LOG[i / 2] + 1;
  155.     }
  156.  
  157.     pair <T, int> get (int l, int r) {
  158.       int k = LOG[r - l + 1];
  159.       T lt = dp[k][l], rt = dp[k][r - T(k) + 1];
  160.       return (F ()(lt, rt) == lt ? make_pair(who[k][l], lt) : make_pair(who[k][r - T(k) + 1], rt)); // max
  161.     }
  162.  
  163.     pair <int, int> get_range (int x) {
  164.       int L = x, R = x;
  165.       for (int k = D; k >= 0; --k)
  166.         if (R + T(k) - 1 < n && dp[k][R] >= dp[0][x])
  167.           R += T(k);
  168.       --R;
  169.       for (int k = D; k >= 0; --k)
  170.         if (L - T(k) + 1 >= 0 && dp[k][L - T(k) + 1] >= dp[0][x])
  171.           L -= T(k);
  172.       ++L;
  173.       return make_pair(L, R);
  174.     }
  175. };
  176.  
  177. template <typename T>
  178. class binop {
  179.   public:
  180.     T operator () (const T &x, const T &y) {
  181.       return max(x, y);
  182.     }
  183. };
  184. #undef T
  185.  
  186. void solve() {
  187.   int n;
  188.   cin >> n;
  189.   vector <pair <int, int>> ar(n);
  190.   {
  191.     for (int i = 0; i < n; ++i) {
  192.       cin >> ar[i].first;
  193.       ar[i].second = i;
  194.     }
  195.     sort(ar.begin(), ar.end(), greater <pair <int, int>> ());
  196.   }
  197.  
  198.   vector <int> df(n);
  199.   for (int i = 0; i < n; ++i)
  200.     df[i] = ar[i].first - (i + 1 < n ? ar[i + 1].first : 0);
  201.  
  202.   sparse_table <binop <int>, int> ST(df);
  203.  
  204.   int FX = INT_MIN, SX = INT_MIN, RX = INT_MIN, ii = -1, jj = -1, kk = -1;
  205.   vector <pair <int, int>> b(n);
  206.   {
  207.     for (int i = 0; i < n; ++i) {
  208.       for (int j = i + 1; j < n; ++j) {
  209.         int li = i + 1, lj = j - i;
  210.         if (li > 2 * lj || lj > 2 * li)
  211.           continue;
  212.         // chose some k, lk = k - j, lk <= 2 * min(lj, li), 2 * lk >= max(li, lj)
  213.         int leftk = j + max(1, (max(li, lj) + (max(li, lj) & 1)) / 2);
  214.         int rightk = j + min(2 * min(li, lj), n - 1 - j);
  215.         if (leftk > rightk) continue;
  216.         /*
  217.            for (int k = leftk; k <= rightk; ++k) {
  218.            int fx = dp[i][0], sx = dp[j][0], rx = dp[k][0];
  219.            if (FX < fx || (FX == fx && SX < sx) || (FX == fx && SX == sx && RX < rx))
  220.            FX = fx, SX = sx, RX = rx, ii = i, jj = j, kk = k;
  221.            }
  222.            */
  223.         pair <int, int> gt = ST.get(leftk, rightk);
  224.         int k = gt.first;
  225.         int fx = df[i], sx = df[j], rx = df[k];
  226.         if (FX < fx || (FX == fx && SX < sx) || (FX == fx && SX == sx && RX < rx))
  227.           FX = fx, SX = sx, RX = rx, ii = i, jj = j, kk = k;
  228.       }
  229.     }
  230.   }
  231.   //   debug(FX, SX, RX, ii, jj, kk);
  232.   for (int i = 0; i < n; ++i) {
  233.     b[i].first = ar[i].second;
  234.     if (i <= ii) b[i].second = 1;
  235.     else if (i <= jj) b[i].second = 2;
  236.     else if (i <= kk) b[i].second = 3;
  237.     else b[i].second = -1;
  238.   }
  239.   sort(b.begin(), b.end());
  240.   for (int i = 0; i < n; ++i)
  241.     cout << b[i].second << ' ';
  242.   cout << '\n';
  243. }
  244.  
  245.  
  246. signed main() {
  247.   IOS; PREC;
  248.   int tc = 1;
  249.   // cin >> tc;
  250.   for (int Tt = 1; Tt <= tc; ++Tt) {
  251.     // cout << "Case #" << Tt << ": ";
  252.     solve();
  253.   }
  254.   return EXIT_SUCCESS;
  255. }
Add Comment
Please, Sign In to add comment