PROBLEM
This problem comes under graph theory. I didn’t get any approach to it using graphs (the editorial said to use DFS but I didn’t understand how to). That’s why I used a different method.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector <pair <int, int>> h(0);
while (n--)
{
vector <int> s(m);
for (int i = 0; i < m; ++i) cin >> s[i];
int prev = 0, size = 0;
for (int i = 0; i < m; ++i)
{
if (prev == 0)
{
size = 1;
prev = s[i];
} else {
if (s[i] == prev) ++size;
else {
h.push_back(make_pair(s[i], size));
size = 1;
}
}
if (i == m-1)
{
h.push_back(make_pair(s[i], size));
size = 1;
}
}
}
int max = -1, bank = 0;
sort(h.begin(), h.end());
for (auto i : h)
{
if (i.second > max)
{
max = i.second;
bank = i.first;
}
}
cout << bank << " " << max << '\n';
}
I go through all values in a line and store their count (i.e., if they are consecutive) in a vector of pairs. I do this for all n lines in a single vector (because I need the maximum number of banks I can hack from all given values). I sort the vector according to the bank type so that I only have to check if the current max value is greater than the second value of a pair and I need to make that the current max.
This approach works fine for a few cases but I fail to understand why I get a WA verdict for the others.
This is the verdict:
Please help me understand what is wrong in my code. Any help is appreciated.