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


#include <bits/stdc++.h>

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

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

int main() {

  int N, M, best = 0;
  long int K;

  cin >> N >> M >> K;

  vector<int> A(N), B(M);

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

  vector<long long> vA, vB;
  vA.push_back(0);
  vB.push_back(0);
  for (int n = 0; n < N; n++) {
    if (vA[n] + A[n] <= K) {
      vA.push_back(vA[n] + A[n]);
    } else {
      break;
    }
  }
  for (int m = 0; m < M; m++) {
    if (vB[m] + B[m] <= K) {
      vB.push_back(vB[m] + B[m]);
    } else {
      break;
    }
  }

  int nstart = vA.size() - 1;
  for (int m = 0; m < vB.size(); m++) {
    for (int n = nstart; n >= 0; n--) {
      if (vA[n] + vB[m] <= K) {
        if (best < (n + m)) {
          best = (n + m);
        }
        nstart = n;
        break;
      }
    }
  }

#if (0)
  int posA = 0, posB = 0, sum1 = 0, sum2 = 0;
  long int et = 0;
  bool flagA = false, flagB = false;

  while (1) {

    if (posA == N) {
      flagA = true;
    }
    if (posB == M) {
      flagB = true;
    }

    if (flagA && flagB) {
      break;
    } else if (flagA) {
      et = et + B[posB];
      sum1++;
      posB++;
    } else if (flagB) {
      et = et + A[posA];
      sum1++;
      posA++;
    } else if (A[posA] <= B[posB]) {
      et = et + A[posA];
      sum1++;
      posA++;
    } else {
      et = et + B[posB];
      sum1++;
      posB++;
    }

    if (et > K) {
      sum1--;
      break;
    } else if (sum1 == (M + N)) {
      break;
    }
  }

  et = 0;
  posA = 0;
  posB = 0;
  flagA = false;
  flagB = false;

  while (1) {

    if (posA == N) {
      flagA = true;
    }
    if (posB == M) {
      flagB = true;
    }

    if (flagA && flagB) {
      break;
    } else if (flagA) {
      et = et + B[posB];
      sum2++;
      posB++;
    } else if (flagB) {
      et = et + A[posA];
      sum2++;
      posA++;
    } else if (A[posA] < B[posB]) {
      et = et + A[posA];
      sum2++;
      posA++;
    } else {
      et = et + B[posB];
      sum2++;
      posB++;
    }

    if (et > K) {
      sum2--;
      break;
    } else if (sum2 == (M + N)) {
      break;
    }
  }

  cout << ((sum1 > sum2) ? sum1 : sum2);
#endif

  cout << best;

  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>

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

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

int main() {

  int N, M, best = 0;
  long int K;

  cin >> N >> M >> K;

  vector<int> A(N), B(M);

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

  vector<long long> vA, vB;
  vA.push_back(0);
  vB.push_back(0);
  for (int n = 0; n < N; n++) {
    if (vA[n] + A[n] <= K) {
      vA.push_back(vA[n] + A[n]);
    } else {
      break;
    }
  }
  for (int m = 0; m < M; m++) {
    if (vB[m] + B[m] <= K) {
      vB.push_back(vB[m] + B[m]);
    } else {
      break;
    }
  }

  for (int m = 0; m < vB.size(); m++) {
    for (int n = vA.size() - 1; n >= 0; n--) {
      if (vA[n] + vB[m] <= K) {
        if (best < (n + m)) {
          best = (n + m);
        }
        break;
      }
    }
  }

#if (0)
  int posA = 0, posB = 0, sum1 = 0, sum2 = 0;
  long int et = 0;
  bool flagA = false, flagB = false;

  while (1) {

    if (posA == N) {
      flagA = true;
    }
    if (posB == M) {
      flagB = true;
    }

    if (flagA && flagB) {
      break;
    } else if (flagA) {
      et = et + B[posB];
      sum1++;
      posB++;
    } else if (flagB) {
      et = et + A[posA];
      sum1++;
      posA++;
    } else if (A[posA] <= B[posB]) {
      et = et + A[posA];
      sum1++;
      posA++;
    } else {
      et = et + B[posB];
      sum1++;
      posB++;
    }

    if (et > K) {
      sum1--;
      break;
    } else if (sum1 == (M + N)) {
      break;
    }
  }

  et = 0;
  posA = 0;
  posB = 0;
  flagA = false;
  flagB = false;

  while (1) {

    if (posA == N) {
      flagA = true;
    }
    if (posB == M) {
      flagB = true;
    }

    if (flagA && flagB) {
      break;
    } else if (flagA) {
      et = et + B[posB];
      sum2++;
      posB++;
    } else if (flagB) {
      et = et + A[posA];
      sum2++;
      posA++;
    } else if (A[posA] < B[posB]) {
      et = et + A[posA];
      sum2++;
      posA++;
    } else {
      et = et + B[posB];
      sum2++;
      posB++;
    }

    if (et > K) {
      sum2--;
      break;
    } else if (sum2 == (M + N)) {
      break;
    }
  }

  cout << ((sum1 > sum2) ? sum1 : sum2);
#endif

  cout << best;

  return 0;
}