U
U
user_of_toster2021-06-14 06:07:44
Algorithms
user_of_toster, 2021-06-14 06:07:44

Where is the error in reasoning?

Problem https://codeforces.com/contest/602/problem/B

I rephrased the problem like this:

Find the length of the longest segment containing at most two different numbers.

Implemented like this:
We remember the last two numbers and how many times they came across. Iterate over each value in the array. If the value is equal to one of the last two values, then increment the number of hits. If not, we add a new one with a hit frequency of 1. At the end of each cycle, we rewrite the answer for the last two numbers.

Better read the code:

using namespace std;

bool isValidIndex(int index) {
  return index >= 0;
}

int main(){
  // data input
  int n; cin >> n;
  vector<int> dataPoints(n);
  for (int i{}; i < n; i++) cin >> dataPoints[i];
  
  // pair consists if <number, how many times met>
  vector<pair<int, int>> lastTwoNumbers{};
  
  // now, iterate over all data points
  // if data point equal to one of <last two numbers>
  // then increate <how many times met> of this number
  // otherwise push new number in <lastTwoNumbers>
  // in the end, refresh best answer;
  int answer = 0;
  
  for (int i{}; i < n; i++) {
    int number = dataPoints[i];
    bool foundInLastTwoNumbers = false;
    for (int j{}; j < 2; j++) {
      int index = lastTwoNumbers.size() - 1 - j;
      if (isValidIndex(index) and number == lastTwoNumbers[index].first) {
        lastTwoNumbers[index].second++;
        foundInLastTwoNumbers = true;
        break;
      }
    }
    
    if (!foundInLastTwoNumbers) {
      lastTwoNumbers.push_back(make_pair(number, 1));
    }
    
    // calculating answer
    int currentAnswer = 0;
    for (int j{}; j < 2; j++) {
      int index = lastTwoNumbers.size() - 1 - j;
      if (isValidIndex(index)) currentAnswer += lastTwoNumbers[index].second;
    }
    
    answer = max(answer, currentAnswer);
  }
  
  cout << answer;
}


Unfortunately, some of the tests fail - my answer comes out less than the correct answer. What is the error in reasoning/implementation?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wataru, 2021-06-14
@user_of_toster

You count only the segments at the very end, but they can be in the middle.
Instead of the sum of the numbers at the end, you need to somehow select the maximum during the main loop.
But by the way, your solution does not work like this, for example, on this test:
0 0 0 0 1 1 1 1 0 0 2 2
When you process zeros and ones, you will see 6 zeros and 4 ones. Then you add a new number 2 and calculate that it is 2. But you can't take all the twos and zeros together! There are still units in between. Moreover, in your array of last numbers there will be {{0, 6}, {1, 4}, {2, 2}} - that is, you will try to take twos and ones together.
Need tips on the right solution?

V
Vitaly Kachan, 2021-06-14
@MANAB

Find the length of the longest segment containing at most two different numbers.
in your interpretation, the case [0, 1, 0, 1, 0, 0, 1] is omitted, although according to the original formulation, the entire segment should fit.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question