Submission #393556

# Submission time Handle Problem Language Result Execution time Memory
393556 2021-04-24T02:50:12 Z aaronhma A Huge Tower (CEOI10_tower) C++17
100 / 100
129 ms 5068 KB
// O(n log n + n)
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vll;

#define all(x) begin(x), end(x)
#define FORE(i, a, b) for (ll i = a; i < b; i++)

const int MOD = 1e9 + 9; // 10^9 + 9 for "CEOI 2010 - A Huge Tower"

// INPUT
template <class T>
void read(T &x) { cin >> x; }

template <class T>
void read(vector<T> &a)
{
  FORE(i, 0, (int)a.size())
  read(a[i]);
}

template <class Arg, class... Args>
void read(Arg &first, Args &...rest)
{
  read(first);
  read(rest...);
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // ans: We'll set the answer to 1.
  // j: Current index that we can build start from arr[0] (j = 0 is a dummy value)
  ll n, d, ans = 1, j = 0;
  read(n, d);

  vll arr(n);
  read(arr);

  // Sort the blocks in increasing order
  sort(all(arr));

  // Let i = starting index of the first block possible.
  FORE(i, 0, n) {
    // Find the maximum index possible that we can build starting from arr[i] as the base
    while (j < n && arr[j] - arr[i] <= d)
      j++;

    // j - i: Largest tower we can build when arr[i] is the base
    ans *= j - i;

    // Make the answer within range
    ans %= MOD;
  }

  cout << ans << "\n";

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 592 KB Output is correct
2 Correct 12 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 2252 KB Output is correct
2 Correct 50 ms 2252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 5068 KB Output is correct
2 Correct 129 ms 5068 KB Output is correct