Problem p00440 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

int arr[100000 + 5] = {0};
int arr_2[100000 + 5];

int main() {
  int n, k;
  while (cin >> n >> k) {
    if (n == 0 && k == 0)
      break;
    int num;
    int count = 0;
    bool flag_2 = false;
    for (int i = 0; i < k; i++) {
      cin >> num;
      if (num == 0)
        flag_2 = true;
      else {
        // count++;
        arr[num]++;
        arr_2[i] = num;
      }
    }
    if (!flag_2) {
      int sum = 0;
      int max = 0;
      for (int i = 1; i <= n; i++) {

        while ((i <= n) && arr[i]) {
          sum++;
          i++;
        }
        if (sum > max)
          max = sum;
        sum = 0;
      }
      cout << max << endl;
    } else {
      int blank[100000 + 5];
      int seg[100000 + 5];
      int sum;
      int sum_2;
      int max = 0;
      int cnt = 0;
      int cnt_2 = 0;
      int start, end;
      sort(arr_2, arr_2 + k);
      for (int i = 0; i < k; i++) {
        if (i == 0 && arr_2[i] != 1)
          blank[cnt++] = 1;
        else if (i == 0)
          blank[cnt++] = 0;
        else {
          int temp = arr_2[i] - arr_2[i - 1];
          if (temp == 2)
            blank[cnt++] = 1;
          else if (temp > 2)
            blank[cnt++] = 2;
        }
      }
      // int temp=n-arr_2[k-1];
      // if(temp>=1) blank[cnt++]=1;
      for (int i = 1; i < k; i++) {
        sum = 1;
        while ((i < k) && (arr_2[i] - arr_2[i - 1] == 1)) {
          sum++;
          i++;
        }
        seg[cnt_2++] = sum;
      }
      for (int i = 0; i < cnt_2; i++) {
        if (i == 0)
          max = seg[i] + 1;
        else {
          if (blank[i] == 1) {
            sum_2 = seg[i] + 1 + seg[i - 1];
            if (sum_2 > max)
              max = sum_2;
          } else if (blank[i] == 2) {
            sum_2 = seg[i] + 1;
            if (sum_2 > max)
              max = sum_2;
          }
        }
      }
      cout << max << endl;
    }
  }
  // while(1);
  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

int arr[100000 + 5] = {0};

int main() {
  int n, k;
  while (cin >> n >> k) {
    if (n == 0 && k == 0)
      break;
    int num;
    int count = 0;
    bool flag_2 = false;
    for (int i = 0; i < k; i++) {
      cin >> num;
      if (num == 0)
        flag_2 = true;
      else {
        // count++;
        arr[num]++;
      }
    }
    if (!flag_2) {
      int sum = 0;
      int max = 0;
      for (int i = 1; i <= n; i++) {

        while ((i <= n) && arr[i]) {
          sum++;
          i++;
        }
        if (sum > max)
          max = sum;
        sum = 0;
      }
      cout << max << endl;
    } else {
      int sum;
      int max = 0;
      for (int i = 1; i <= n; i++) {
        if (!arr[i]) {
          arr[i] = 1;
          sum = 0;
          for (int j = 1; j <= n; j++) {
            while ((j <= n) && arr[j]) {
              sum++;
              j++;
            }
            if (max < sum) {
              max = sum;
            }
            sum = 0;
          }
          arr[i] = 0;
        }
      }
      cout << max << endl;
    }
  }
  // while(1);
  return 0;
}