mr_dot_convict

792C-Divide-by-Three-Codeforces-mr.convict

Sep 7th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 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. #pragma GCC   optimize ("Ofast")
  15. #pragma GCC   optimize ("unroll-loops")
  16. #pragma GCC   target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  17.  
  18. #define IOS   ios_base::sync_with_stdio(false); cin.tie (nullptr)
  19. #define PREC  cout.precision (10); cout << fixed
  20. #define x     first
  21. #define y     second
  22. #define bg(x) " [ " << #x << " : " << (x) << " ] "
  23. #define un(x) sort(x.begin(), x.end()), \
  24.    x.erase(unique(x.begin(), x.end()), x.end())
  25. using   ll  = long long;
  26. using   ull = unsigned long long;
  27. using   ff  = long double;
  28. using   pii = pair<int,int>;
  29. using   pil = pair<int,ll>;
  30. typedef tree
  31. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  32. ordered_set;
  33.  
  34. struct chash {
  35.    int operator()(pii x) const { return x.x*31 + x.y; }
  36. };
  37. gp_hash_table <pii, int, chash> mp;
  38.  
  39. #if __cplusplus > 201103L
  40. seed_seq seq{
  41.    (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  42.       (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  43.       (uint64_t) __builtin_ia32_rdtsc(),
  44.       (uint64_t) (uintptr_t) make_unique<char>().get()
  45. };
  46. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  47. #else
  48. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  49. mt19937 rng(seed);
  50. #endif
  51.  
  52. #define debug(args...) { \
  53.    /* WARNING : do NOT compile this debug func calls with following flags: // \
  54.     * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  55.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  56.    stringstream _ss(_s); \
  57.    istream_iterator<string> _it(_ss); err(_it, args); \
  58. }
  59. void err(istream_iterator<string> it) {
  60.    it->empty(); cerr << " (Line : " << __LINE__ << ")" << '\n';
  61. }
  62. template<typename T, typename... Args>
  63. void err(istream_iterator<string> it, T a, Args... args) {
  64.    cerr << fixed << setprecision(15)
  65.       << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  66.    err(++it, args...);
  67. }
  68. /*****************************************************************************/
  69.  
  70. const int N = (int)1e5 + 10, infi = (int)1e9;
  71. int dp[N][2][3]; // max length
  72. string s;
  73. int n;
  74. bool take = true;
  75.  
  76. void solve() {
  77.    n = (int)s.size();
  78.    for (int sum = 0; sum < 3; ++sum) {
  79.       dp[0][!take][sum] = (sum == 0 ? 0 : -infi);
  80.       dp[0][take][sum] = (sum == (s[0] - '0') % 3 ? 1 : -infi);
  81.    }
  82.  
  83.    for (int i = 1; i < n; ++i) {
  84.       for (int sum = 0; sum < 3; ++sum) {
  85.          int from = (sum - (s[i] - '0') + 9) % 3;
  86.          dp[i][take][sum] = max(dp[i - 1][take][from], dp[i - 1][!take][from]) + 1;
  87.          dp[i][!take][sum] = max(dp[i - 1][!take][sum], dp[i - 1][take][sum]);
  88.  
  89.          if (s[i] - '0' == 0 && dp[i][take][sum] == 1) {// This is what you can miss and mess up
  90.             dp[i][take][sum] = -infi;
  91.          }
  92.       }
  93.    }
  94.  
  95.    int exist = max(dp[n - 1][take][0], dp[n - 1][!take][0]);
  96.    if (!exist) {
  97.       for (char ch : s) {
  98.          if (ch == '0') {
  99.             cout << 0 << '\n';
  100.             return;
  101.          }
  102.       }
  103.       cout << -1 << '\n';
  104.    }
  105.    else {
  106.       vector <int> digit;
  107.       int cur_sum = 0;
  108.       int i = n - 1;
  109.       while (i >= 0) {
  110.          if (dp[i][take][cur_sum] > dp[i][!take][cur_sum]) {
  111.             digit.push_back(s[i] - '0');
  112.             cur_sum = (cur_sum - (s[i] - '0') + 9) % 3;
  113.          }
  114.          i -= 1;
  115.       }
  116.       reverse(digit.begin(), digit.end());
  117.       for (int el : digit) cout << el;
  118.       cout << '\n';
  119.    }
  120. }
  121.  
  122. signed main() {
  123.    IOS; PREC;
  124.    cin >> s;
  125.    solve();
  126.    return EXIT_SUCCESS;
  127. }
Add Comment
Please, Sign In to add comment