mr_dot_convict

10827-Maximum_sum_on_a_torus.UVa.mr.convict

Jun 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. using namespace std;
  10. #pragma GCC      optimize ("Ofast")
  11. #pragma GCC      optimize ("unroll-loops")
  12. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  13.  
  14. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  15. #define PREC     cout.precision (10); cout << fixed
  16. #define bg(x)    " [ " << #x << " : " << (x) << " ]"
  17. #define x        first
  18. #define y        second
  19. using ll = long long;
  20.  
  21. #define debug(args...) { \
  22.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  23.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  24. }
  25. void err(istream_iterator<string> it) { it->empty();
  26.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  27. }
  28. template<typename T, typename... Args>
  29. void err(istream_iterator<string> it, T a, Args... args) {
  30.     cerr << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  31.     err(++it, args...);
  32. }
  33.  
  34. const int N = 75*3 + 10;
  35. int mat[N][N];
  36. int n, m;
  37.  
  38. void solve() {
  39.  
  40.    for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j)
  41.    {
  42.       if (j > 0) mat[i][j] += mat[i][j-1];
  43.    }
  44.  
  45.    int mx_sum = INT_MIN;
  46.    for (int l = 0; l < m/2; ++l) for (int r = l; r < l + m/2; ++r)
  47.    {
  48.       for (int j = 0; j < n/2; ++j)
  49.          for (int i = j, cur_sum = 0; i < j + n/2; ++i)
  50.       {
  51.          cur_sum += mat[i][r];
  52.          if (l > 0) cur_sum -= mat[i][l-1];
  53.          mx_sum = max(mx_sum, cur_sum);
  54.          if (cur_sum < 0) cur_sum = 0;
  55.       }
  56.    }
  57.    cout << mx_sum << '\n';
  58. }
  59.  
  60. void read() {
  61.    cin >> n; m = n;
  62.    for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j)
  63.       cin >> mat[i][j];
  64.  
  65.    n *= 2, m *= 2;
  66.  
  67.    for (int i = 0; i < n/2; ++i) for (int j = m/2; j < m; ++j)
  68.       mat[i][j] = mat[i][j-m/2];
  69.    for (int i = n/2; i < n; ++i) for (int j = 0; j < m; ++j)
  70.       mat[i][j] = mat[i-n/2][j];
  71. }
  72.  
  73. signed main() {
  74.    IOS; PREC;
  75.    int tc; cin >> tc;
  76.    while (tc--) {
  77.       read();
  78.       solve();
  79.    }
  80.    return EXIT_SUCCESS;
  81. }
Add Comment
Please, Sign In to add comment