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

#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

int trans_str(std::string str) {
  int l = str.length();
  if (l == 1) {
    return (int)(str[0]) - '0';
  }

  int sum = 0;
  int d;
  if (str[0] == '-') {
    for (int i = 0; i < l - 1; i++) {
      d = (int)(str[l - 1 - i]) - '0';
      sum += d * pow(10, i);
    }
    return -sum;
  }

  for (int i = 0; i < l; i++) {
    d = (int)(str[l - 1 - i]) - '0';
    sum += d * pow(10, i);
  }
  return sum;
}

bool check_sample(std::vector<int> v) {
  int size = v.size() - 1;
  for (int i = 2; i < size; i++) {
    if (i % 2 == 0) {
      if (v[i] <= v[i - 1]) {
        return false;
      }
    } else {
      if (v[i] >= v[i - 1]) {
        return false;
      }
    }
  }
  return true;
}

int main(void) {
  int N;
  cin >> N;
  while (N != 0) {
    std::vector<int> data(N + 2, 0);
    std::vector<int> xinds;
    std::string str;
    bool ex = false;
    bool seq = false;
    bool mode = false;
    data[0] = 1000000003;
    if (N % 2 == 0) {
      data[N + 1] = -data[0];
    } else {
      data[N + 1] = data[0];
    }
    for (int i = 0; i < N; i++) {
      cin >> str;
      if (str[0] == 'x') {
        if (mode) {
          seq = true;
        }
        ex = true;
        mode = true;
        xinds.push_back(i + 1);
      } else {
        data[i + 1] = trans_str(str);
        mode = false;
      }
    }

    if (!ex) {
      if (check_sample(data)) {
        cout << "ambiguous" << endl;
      } else {
        cout << "none" << endl;
      }
    } else {
      if (seq) {
        cout << "none" << endl;
      } else {
        int smin = -1000000003;
        int smax = 1000000003;
        int ind;
        for (std::vector<int>::iterator itr = xinds.begin(); itr != xinds.end();
             ++itr) {
          ind = *itr;
          if (ind % 2 == 0) {
            if (smin < data[ind - 1] + 1) {
              smin = data[ind - 1] + 1;
            }
            if (smin < data[ind + 1] + 1) {
              smin = data[ind + 1] + 1;
            }
          } else {
            if (smax > data[ind - 1]) {
              smax = data[ind - 1];
            }
            if (smax > data[ind + 1]) {
              smax = data[ind + 1];
            }
          }
        }

        int ret = smin;
        int count = smax - smin;
        for (std::vector<int>::iterator itr = xinds.begin(); itr != xinds.end();
             ++itr) {
          data[*itr] = ret;
        }

        if (check_sample(data)) {
          if (count != 1) {
            if (count < 0) {
              cout << "none" << endl;
            } else {
              cout << "ambiguous" << endl;
            }
          } else {
            cout << ret << endl;
          }
        } else {
          cout << "none" << endl;
        }
      }
    }
    cin >> N;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;

int trans_str(std::string str) {
  int l = str.length();
  if (l == 1) {
    if (str[0] == 'x') {
      return 0;
    } else {
      return (int)(str[0]) - '0';
    }
  }

  int sum = 0;
  int d;
  for (int i = 0; i < l; i++) {
    d = (int)(str[l - 1 - i]) - '0';
    sum += d * pow(10, i);
  }
  return sum;
}

bool check_sample(std::vector<int> v) {
  int size = v.size() - 1;
  for (int i = 2; i < size; i++) {
    if (i % 2 == 0) {
      if (v[i] < v[i - 1]) {
        return false;
      }
    } else {
      if (v[i] > v[i - 1]) {
        return false;
      }
    }
  }
  return true;
}

int main(void) {
  int N;
  cin >> N;
  while (N != 0) {
    char c;
    std::vector<int> data(N + 2, 0);
    std::vector<int> xinds;
    std::string str;
    bool ex = false;
    bool seq = false;
    bool mode = false;
    for (int i = 0; i < N; i++) {
      cin >> str;
      data[i + 1] = trans_str(str);
      if (data[i + 1] == 0) {
        if (mode) {
          seq = true;
        }
        mode = true;
        ex = true;
        xinds.push_back(i + 1);
      } else {
        mode = false;
      }
    }

    if (!ex) {
      if (check_sample(data)) {
        cout << "ambiguous" << endl;
      } else {
        cout << "none" << endl;
      }
    } else {
      if (seq) {
        cout << "none" << endl;
      } else {
        int ret = 0;
        int count = 0;
        int smin = 0;
        int smax = 0;
        int ind;
        for (std::vector<int>::iterator itr = xinds.begin(); itr != xinds.end();
             ++itr) {
          ind = *itr;
          if (ind % 2 == 0) {
            if (smin == 0) {
              smin = data[ind - 1] + 1;
            }
            if (smin < data[ind - 1] + 1) {
              smin = data[ind - 1] + 1;
            }
            if (ind != N) {
              if (smin < data[ind + 1] + 1) {
                smin = data[ind + 1] + 1;
              }
            }
          } else {
            if (ind == 1) {
              smax = data[ind + 1];
            } else {
              if (smax == 0) {
                smax = data[ind - 1];
              }
            }
            if (ind != 1 && smax > data[ind - 1]) {
              smax = data[ind - 1];
            }
            if (ind != N) {
              if (smax > data[ind + 1]) {
                smax = data[ind + 1];
              }
            }
          }
        }
        if (smin == 0) {
          for (int i = 1; i < smax && count < 2; i++) {
            for (std::vector<int>::iterator itr = xinds.begin();
                 itr != xinds.end(); ++itr) {
              data[*itr] = i;
            }
            if (check_sample(data)) {
              ret = i;
              count++;
            }
          }
        } else if (smax == 0) {
          for (int i = smin; count < 2; i++) {
            for (std::vector<int>::iterator itr = xinds.begin();
                 itr != xinds.end(); ++itr) {
              data[*itr] = i;
            }
            if (check_sample(data)) {
              ret = i;
              count++;
            }
          }
        } else {
          for (int i = smin; i < smax && count < 2; i++) {
            for (std::vector<int>::iterator itr = xinds.begin();
                 itr != xinds.end(); ++itr) {
              data[*itr] = i;
            }
            if (check_sample(data)) {
              ret = i;
              count++;
            }
          }
        }

        if (count == 0) {
          cout << "none" << endl;
        } else if (count == 1) {
          cout << ret << endl;
        } else {
          cout << "ambiguous" << endl;
        }
      }
    }
    cin >> N;
  }

  return 0;
}