Problem p03946 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
using ull = unsigned long long;
using ll = long long;
using lli = long long int;
using ld = long double;
using pa = pair<int, int>;
using ppa = pair<pa, int>;
#define SORT(v, n) sort(v, v + n);
#define ALL(v) v.begin(), v.end()
#define VSORT(v) sort(ALL(v))
#define GRESORT(v) sort(ALL(v), greater<ll>())
#define REVERSE(v) reverse(ALL(v))
#define overlap(v) v.erase(unique(ALL(v)), v.end())
#define debug(x) cout << #x << ": " << x << endl
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (b - 1); i >= a; i--)
#define rrep(i, n) RFOR(i, 0, n)
#define INF INT_MAX
#define SUP INT_MIN
#define mins(a, b) a = min(a, b)
#define maxs(a, b) a = max(a, b)

// 4近傍
// int dy[]={0, 1, 0, -1};
// int dx[]={1, 0, -1, 0};

// 8近傍
// int dy[]={0,0,1,-1,1,1,-1,-1};
// int dx[]={1,-1,0,0,1,-1,1,-1};

// const ll MOD = 1000000007;

ll pow_mod(ll a, ll b, ll mod) {
  ll tmp = 1;
  a %= mod;
  if (b > 0) {
    tmp = pow_mod(a, b / 2, mod);
    if (b % 2)
      tmp = (((tmp * tmp) % mod) * a) % mod;
    else
      tmp = (tmp * tmp) % mod;
  }
  return tmp;
}

void solve() {
  int n, t;
  cin >> n >> t;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll ma = 0;
  int cnt = 0;
  for (int i = 0; i < n - 1; i++) {
    ll tmp = *max_element(a.begin() + i + 1, a.end()) - a[i];
    if (ma < tmp) {
      ma = tmp;
      cnt = 1;
    } else if (ma == tmp) {
      cnt++;
    }
    // debug(tmp);
  }
  cout << cnt << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  solve();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ull = unsigned long long;
using ll = long long;
using lli = long long int;
using ld = long double;
using pa = pair<int, int>;
using ppa = pair<pa, int>;
#define SORT(v, n) sort(v, v + n);
#define ALL(v) v.begin(), v.end()
#define VSORT(v) sort(ALL(v))
#define GRESORT(v) sort(ALL(v), greater<ll>())
#define REVERSE(v) reverse(ALL(v))
#define overlap(v) v.erase(unique(ALL(v)), v.end())
#define debug(x) cout << #x << ": " << x << endl
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (b - 1); i >= a; i--)
#define rrep(i, n) RFOR(i, 0, n)
#define INF INT_MAX
#define SUP INT_MIN
#define mins(a, b) a = min(a, b)
#define maxs(a, b) a = max(a, b)

// 4近傍
// int dy[]={0, 1, 0, -1};
// int dx[]={1, 0, -1, 0};

// 8近傍
// int dy[]={0,0,1,-1,1,1,-1,-1};
// int dx[]={1,-1,0,0,1,-1,1,-1};

// const ll MOD = 1000000007;

ll pow_mod(ll a, ll b, ll mod) {
  ll tmp = 1;
  a %= mod;
  if (b > 0) {
    tmp = pow_mod(a, b / 2, mod);
    if (b % 2)
      tmp = (((tmp * tmp) % mod) * a) % mod;
    else
      tmp = (tmp * tmp) % mod;
  }
  return tmp;
}

void solve() {
  int n, t;
  cin >> n >> t;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll ma = a[n - 1];
  vector<ll> sell(n);
  for (int i = n - 1; i >= 0; i--) {
    sell[i] = ma - a[i];
    ma = max(ma, a[i]);
  }
  GRESORT(sell);
  int cnt = 1;
  for (int i = 1; i < n; i++) {
    if (sell[0] != sell[i])
      break;
    cnt++;
  }
  cout << cnt << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  solve();

  return 0;
}