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


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; i < (int)(n); (i)++)
typedef long long ll;

vector<int> A;
int N, M, Q;
int maxValue = 0;
vector<int> a;
vector<int> b;
vector<int> c;
vector<int> d;

void dfs(int i, const int size, const int range_start, const int range_end) {
  if (i == size) {
    bool flag = true;
    ll temp = 0;
    // for(int i = A.size() - 1; i > 0; i--) {
    //     for(int j = i - 1; j >= 0; j--) {
    //         if(A[j] > A[i]) {
    //             flag = false;
    //         }
    //     }
    // }
    if (flag) {
      rep(i, Q) {
        if ((A[b[i] - 1] - A[a[i] - 1]) == c[i]) {
          temp += d[i];
        }
      }
      if (temp >= maxValue)
        maxValue = temp;
    }
  } else {
    for (int j = range_start; j <= range_end; ++j) {
      A[i] = j;
      bool flag = true;
      for (int i = A.size() - 1; i > 0; i--) {
        for (int j = i - 1; j >= 0; j--) {
          if (A[j] > A[i]) {
            flag = false;
          }
        }
      }
      if (flag)
        dfs(i + 1, size, range_start, range_end);
    }
  }
}

void func_A() {
  bool f = false;
  int K, A, B;
  cin >> K;
  cin >> A >> B;
  for (int i = A; i <= B; i++) {
    if (i % K == 0)
      f = true;
  }
  if (f) {
    cout << "OK";
  } else {
    cout << "NG";
  }
}
void func_B() {
  ll money = 100, X;
  ll i = 0;
  cin >> X;
  while (money < X) {
    i++;
    money = money + money * 0.01;
  }
  cout << i;
}
void func_C() {
  cin >> N >> M >> Q;
  a.resize(Q);
  b.resize(Q);
  c.resize(Q);
  d.resize(Q);
  rep(i, Q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }

  A.resize(N);
  fill(A.begin(), A.end(), 1);
  dfs(0, N, 1, M);
  cout << maxValue;
}
void func_D() {}
int main() {
  func_C();
  return 0;
}

Error: TLE

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; i < (int)(n); (i)++)
typedef long long ll;

vector<int> A;
int N, M, Q;
int maxValue = 0;
vector<int> a;
vector<int> b;
vector<int> c;
vector<int> d;

void dfs(int i, const int size, const int range_start, const int range_end) {
  if (i == size) {
    bool flag = true;
    ll temp = 0;
    for (int i = A.size() - 1; i > 0; i--) {
      for (int j = i - 1; j >= 0; j--) {
        if (A[j] > A[i]) {
          flag = false;
        }
      }
    }
    if (flag) {
      rep(i, Q) {
        if ((A[b[i] - 1] - A[a[i] - 1]) == c[i]) {
          temp += d[i];
        }
      }
      if (temp >= maxValue)
        maxValue = temp;
    }
  } else {
    for (int j = range_start; j <= range_end; ++j) {
      A[i] = j;
      dfs(i + 1, size, range_start, range_end);
    }
  }
}

void func_A() {
  bool f = false;
  int K, A, B;
  cin >> K;
  cin >> A >> B;
  for (int i = A; i <= B; i++) {
    if (i % K == 0)
      f = true;
  }
  if (f) {
    cout << "OK";
  } else {
    cout << "NG";
  }
}
void func_B() {
  ll money = 100, X;
  ll i = 0;
  cin >> X;
  while (money < X) {
    i++;
    money = money + money * 0.01;
  }
  cout << i;
}
void func_C() {
  cin >> N >> M >> Q;
  a.resize(Q);
  b.resize(Q);
  c.resize(Q);
  d.resize(Q);
  rep(i, Q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }

  A.resize(N);
  dfs(0, N, 1, M);
  cout << maxValue;
}
void func_D() {}
int main() {
  func_C();
  return 0;
}