Showing posts with label Two Pointer. Show all posts
Showing posts with label Two Pointer. Show all posts

Monday, September 30, 2019

[Codeforces] D - Table with Letters - 2

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : D - Table with Letters - 2
Source            : Codeforces
Category          : Data Structure, Two Pointer
Algorithm         : Two Pointer
Verdict           : Accepted

  1. #include "bits/stdc++.h"  
  2.   
  3. using namespace std;  
  4.   
  5. static const int maxn = 400 + 5;  
  6.   
  7. int row;  
  8. int column;  
  9. int maxa;  
  10. char grid[maxn][maxn];  
  11. int mat[maxn][maxn];  
  12.   
  13. int subRectangleSum(int x1, int y1, int x2, int y2)  
  14. {  
  15.       int sum = mat[x2][y2] - mat[x2][y1 - 1] - mat[x1 - 1][y2] + mat[x1 - 1][y1 - 1];  
  16.       return sum;  
  17. }  
  18.   
  19. void solve()  
  20. {  
  21.       long long ans = 0;  
  22.       for (int col_1 = 1; col_1 <= column; col_1++)  
  23.       {  
  24.             for (int col_2 = col_1 + 1; col_2 <= column; col_2++)  
  25.             {  
  26.                   int preRow = 1;  
  27.                   vector <int> same(27, 0);  
  28.                   for (int nxtRow = 1; nxtRow <= row; nxtRow++)  
  29.                   {  
  30.                         if (grid[nxtRow][col_1] == grid[nxtRow][col_2])  
  31.                         {  
  32.                               int val = grid[nxtRow][col_1] - 'a' + 1;  
  33.                               while (preRow < nxtRow)  
  34.                               {  
  35.                                     int total_a = subRectangleSum(preRow, col_1, nxtRow, col_2);  
  36.                                     if (total_a <= maxa) break;  
  37.                                     if (grid[preRow][col_1] == grid[preRow][col_2])  
  38.                                     {  
  39.                                           int bad = grid[preRow][col_1] - 'a' + 1;  
  40.                                           same[bad]--;  
  41.                                     }  
  42.                                     preRow++;  
  43.                               }  
  44.                               if (preRow < nxtRow) ans += same[val];  
  45.                               same[val]++;  
  46.                         }  
  47.                   }  
  48.             }  
  49.       }  
  50.       cout << ans << '\n';  
  51. }  
  52.   
  53. signed main()  
  54. {  
  55.       ios_base::sync_with_stdio(false);  
  56.       cin.tie(nullptr);  
  57.       cout.tie(nullptr);  
  58.   
  59.       freopen("input.txt", "r", stdin);  
  60.       freopen("output.txt", "w", stdout);  
  61.   
  62.       cin >> row >> column >> maxa;  
  63.       for (int i = 1; i <= row; i++)  
  64.       {  
  65.             for (int j = 1; j <= column; j++)  
  66.             {  
  67.                   cin >> grid[i][j];  
  68.                   mat[i][j] = mat[i][j - 1] + (grid[i][j] == 'a');  
  69.             }  
  70.             for (int j = 1; j <= column; j++)  
  71.             {  
  72.                   mat[i][j] += mat[i - 1][j];  
  73.             }  
  74.       }  
  75.       solve();  
  76. }  

Tuesday, December 11, 2018

[Devskill] DCP-98: How Lazy One can be Part 2

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : DCP-98: How Lazy One can be Part 2
Source            : Devskill
Category          : Data Structure
Algorithm         : Two Pointer
Verdict           : Accepted
#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
      int tc;
      scanf("%d", &tc); 
      for (int tcase=1; tcase<=tc; tcase++)
      {
            int n, s;
            scanf("%d %d", &n, &s);
            vector <int> vec;
            for (int i=0; i<n; i++)
            {
                  int num;
                  scanf("%d", &num);
                  vec.push_back(num);
            }
            int minLen = n+1;
            int sum = 0;
            int low = 0;
            int high = 0;
            while (high <= n)
            {
                  if (sum >= s)
                  {
                        minLen = min(minLen, high-low);
                  }
                  if (sum >= s && low < high)
                  {
                        sum -= vec[low];
                        low++;
                  }
                  else
                  {
                        sum += vec[high];
                        high++;
                  }
             }
             if (minLen == n+1) minLen = -1;
             printf("Case %d: %d\n", tcase, minLen);
             vec.clear();
      }
      return 0;
}