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


#include <bits/stdc++.h>
#include <ctime>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using ll = long long;
using P = pair<int, int>;

void calc_primefactorization(long long in, vector<vector<long long>> &out) {

  long long N = in, Ntmp;
  long long p, e, p_init = 2;
  vector<long long> out_tmp(2);

  if (in == 1) {
    out_tmp[0] = 1;
    out_tmp[1] = 1;
  } else {
    while (1) {

      Ntmp = N;

      p = N;
      e = 1;

      for (long long i = p_init; i * i <= N; i++) {

        if (Ntmp % i == 0) {
          e = 0;
          while (1) {
            if (Ntmp % i != 0) {
              break;
            } else {
              Ntmp = Ntmp / i;
              e++;
            }
          }
          p = i;
          break;
        }
      }

      out_tmp[0] = p;
      out_tmp[1] = e;
      out.push_back(out_tmp);

      for (int i = 0; i < e; i++) {
        N = N / p;
      }

      if (N == 1) {
        break;
      }
    }
  }
}

int bitmask(int n) {

  int a = 1;

  return (a << (n - 1));
}

long long s_buy(long long &money, int price) {

  long long stock = 0;

  stock = money / price;

  if (price * stock > money) {
    stock--;
  }

  money = money - stock * price;

  /*
  while(1){

          if( money - price < 0 ){
                  break;
          }

          money = money - price;
          stock++;

  }
  */

  return stock;
}

int main() {

  int N;

  cin >> N;

  vector<int> A(N), Signal(N - 1);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  long long money = 1000;
  long long stock = 0;

  for (int i = 0; i < N - 1; i++) {
    if (A[i] < A[i + 1]) {
      Signal[i] = 1;
    } else if (A[i] > A[i + 1]) {
      Signal[i] = -1;
    }
  }

  for (int i = 0; i < N - 1; i++) {
    if (Signal[i] == 1) { // buy
      money = money + A[i] * stock;
      stock = s_buy(money, A[i]);
    } else if (Signal[i] == -1) { // sell
      money = money + A[i] * stock;
      stock = 0;
    } else { // stay
    }
  }

  money = money + A[N - 1] * stock;

  cout << money;

  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
#include <ctime>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using ll = long long;
using P = pair<int, int>;

void calc_primefactorization(long long in, vector<vector<long long>> &out) {

  long long N = in, Ntmp;
  long long p, e, p_init = 2;
  vector<long long> out_tmp(2);

  if (in == 1) {
    out_tmp[0] = 1;
    out_tmp[1] = 1;
  } else {
    while (1) {

      Ntmp = N;

      p = N;
      e = 1;

      for (long long i = p_init; i * i <= N; i++) {

        if (Ntmp % i == 0) {
          e = 0;
          while (1) {
            if (Ntmp % i != 0) {
              break;
            } else {
              Ntmp = Ntmp / i;
              e++;
            }
          }
          p = i;
          break;
        }
      }

      out_tmp[0] = p;
      out_tmp[1] = e;
      out.push_back(out_tmp);

      for (int i = 0; i < e; i++) {
        N = N / p;
      }

      if (N == 1) {
        break;
      }
    }
  }
}

int bitmask(int n) {

  int a = 1;

  return (a << (n - 1));
}

long long s_buy(long long &money, int price) {

  long long stock = 0;

  while (1) {

    if (money - price < 0) {
      break;
    }

    money = money - price;
    stock++;
  }

  return stock;
}

int main() {

  int N;

  cin >> N;

  vector<int> A(N), Signal(N - 1);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  long long money = 1000;
  long long stock = 0;

  for (int i = 0; i < N - 1; i++) {
    if (A[i] < A[i + 1]) {
      Signal[i] = 1;
    } else if (A[i] > A[i + 1]) {
      Signal[i] = -1;
    }
  }

  for (int i = 0; i < N - 1; i++) {
    if (Signal[i] == 1) { // buy
      money = money + A[i] * stock;
      stock = s_buy(money, A[i]);
    } else if (Signal[i] == -1) { // sell
      money = money + A[i] * stock;
      stock = 0;
    } else { // stay
    }
  }

  money = money + A[N - 1] * stock;

  cout << money;

  return 0;
}