1771E - Hossam and a Letter - CodeForces Solution


brute force dp implementation two pointers *2500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>

#define all(x) (x).begin(), (x).end()

void yn(bool b)

{ cout << (b ? "YES" : "NO") << '\n';}

using ll = long long;

using ld = long double;

using VI = vector<int>;

using VVI = vector<VI>;

using PII = pair<int,int>;

using VPII = vector<PII>;

using VVPII = vector<VPII>;

using Graph = vector<vector<int>>;

#define pb push_back

#define eb emplace_back

#define ff first

#define ss second

const int Inf = 0x3f3f3f3f;

const int Mod = 1e9 + 7;

const int N = 405 + 10;

bool testcases = 0;



char c;

int a[N][N];

int ri[N][N];

int n, m;



void solve_testcase() {

  cin >> n >> m;



  for (int i = 0; i <= n + 2; ++i)

    for (int j = 0; j <= m + 2; ++j)

      ri[i][j] = 0;



  for (int i = 1; i <= n; ++i) {

    for (int j = 1; j <= m; ++j) {

      cin >> c;

      if (c == '.')

        a[i][j] = 0;

      if (c == 'm')

        a[i][j] = 1;

      if (c == '#')

        a[i][j] = 2;

    }

  }

  for (int i = 1; i <= n; ++i) {

    for (int j = m; j > 0; --j) {

      ri[i][j] = ri[i][j+1] + a[i][j];

    }

  }

  int ans = 0;

  for (int col1 = 1; col1 <= m; ++col1) {

    for (int col2 = col1 + 2; col2 <= m; ++col2) {



      int l = 1, s = 0;



      int last0 = -1, last1 = -1;



      for (int r = 1; r <= n; ++r) {

        if (a[r][col1] + a[r][col2] > 1) {

          l = r + 1;

          last0 = last1 = -1;

          s = 0;

          continue;

        }

        s += a[r][col1] + a[r][col2];



        while (s > 1) {

          s -= a[l][col1] + a[l][col2]; ++l;

        }



        if (r - l >= 2) {

          if (s <= 1 && last0 > l)

            ans = max(ans, (r - l + 1) * 2 + col2 - col1 - 1);//, cout << (r - l + 1) * 2 + col2 - col1 << " : " << l << ' ' << r << ' ' << col1 << ' ' << col2 << '\n';

          if (s == 0 && last1 > l)

            ans = max(ans, (r - l + 1) * 2 + col2 - col1 - 1);//, cout << (r - l + 1) * 2 + col2 - col1 << " : " << l << ' ' << r << ' ' << col1 << ' ' << col2 << '\n';

        }



        if (ri[r][col1 + 1] - ri[r][col2] == 0)

          last0 = r;

        if (ri[r][col1 + 1] - ri[r][col2] == 1)

          last1 = r;

      }

    }

  }

  cout << ans << '\n';

}



int main() {

  ios_base::sync_with_stdio(false);

  cin.tie(nullptr);

  cout.tie(nullptr);

  int t;

  if (testcases)

    cin >> t;

  else t = 1;

  while (t--)

    solve_testcase();

}








Comments

Submit
0 Comments
More Questions

1661B - Getting Zero
1661A - Array Balancing
1649B - Game of Ball Passing
572A - Arrays
1455A - Strange Functions
1566B - MIN-MEX Cut
678C - Joty and Chocolate
1352E - Special Elements
1520E - Arranging The Sheep
1157E - Minimum Array
1661D - Progressions Covering
262A - Roma and Lucky Numbers
1634B - Fortune Telling
1358A - Park Lighting
253C - Text Editor
365B - The Fibonacci Segment
75A - Life Without Zeros
1519A - Red and Blue Beans
466A - Cheap Travel
659E - New Reform
1385B - Restore the Permutation by Merger
706A - Beru-taxi
686A - Free Ice Cream
1358D - The Best Vacation
1620B - Triangles on a Rectangle
999C - Alphabetic Removals
1634C - OKEA
1368C - Even Picture
1505F - Math
1473A - Replacing Elements