QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#272842#6410. Classical DP Problemucup-team859#WA 0ms3864kbC++143.0kb2023-12-02 19:37:262023-12-02 19:37:27

Judging History

你现在查看的是最新测评结果

  • [2023-12-02 19:37:27]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3864kb
  • [2023-12-02 19:37:26]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using namespace chrono;

using ll = long long;
using ull = unsigned long long;

string to_string(const string &s) {
  return '"' + s + '"';
}

string to_string(bool b) {
  return b ? "true" : "false";
}

template <typename A, typename B>
string to_string(const pair<A, B> &p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename T>
string to_string(const T &v) {
  string s = "{";
  bool first = true;
  for (const auto &it : v) {
    if (!first)
      s += ", ";
    else
      first = false;
    s += to_string(it);
  }
  return s += "}";
}

void debug_out() {
  cerr << endl;
}

template <typename T, typename... Args>
void debug_out(const T &first, const Args&... rest) {
  cerr << to_string(first) << " ";
  debug_out(rest...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

auto startTime = high_resolution_clock::now();
int get_time() {
  auto stopTime = chrono::high_resolution_clock::now();
  auto duration = duration_cast<milliseconds>(stopTime - startTime);
  return duration.count(); // in ms
}

const int MOD = 998244353;

void solve() {
  int n;
  cin >> n;

  vector<int> h(n + 2), l(n + 2);
  int m = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> l[i];
    m = max(m, l[i]);
    ++h[1];
    --h[l[i] + 1];
  }

  for (int i = 1; i <= m; ++i) {
    h[i] += h[i - 1];
  }

  for (int i = 1; i <= m; ++i) {
    if (h[i] < 0)
      h[i] = 0;
  }

  debug(h);
  int k = 0;
  reverse(l.begin() + 1, l.begin() + n + 1);
  for (int i = 1; i <= n; ++i) {
    if (l[i] < k + 1)
      break;

    ++k;
  }

  debug(k);

  cout << k << " ";
  auto get_dp = [&](int h) {
    vector<vector<int>> dp(k + 1, vector<int>(h + 1, 0));
    dp[0][0] = 1;
    for (int i = 1; i <= k; ++i) {
      for (int j = 0; j <= h; ++j) {
        dp[i][j] = (1LL * dp[i - 1][j] * (l[i] - h + j)) % MOD;
        if (j > 0)
          dp[i][j] = (dp[i][j] + 1LL * dp[i - 1][j - 1] * (h - j + 1)) % MOD;
      }
    }

    return dp[k][h];
  };

  if (l[k] == k) {
    int hm = 0;

    if (k < n) {
      hm = l[k + 1];
    }

    debug(hm);
    int ans = get_dp(hm);
    swap(l, h);
    swap(n, m);
    
    if (k < n) {
      hm = l[k + 1];
    }
    debug(ans);
    ans += get_dp(hm);
    debug(ans);

    int p = 1;
    for (int i = 1; i <= k; ++i)
      p = 1LL * p * i % MOD;
    debug(p);

    ans -= p;
    if (ans < 0)
      ans += MOD;

    cout << ans << '\n';
  } else {
    if (k == n) {
      int ans = 1;
      for (int i = 1; i <= n; ++i)
        ans = 1LL * ans * l[i] % MOD;

      cout << ans << '\n';
      return;
    }

    int h = l[k + 1];
    debug(h);
    cout << get_dp(h) << '\n';
  }
}

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

  int t = 1;
//  cin >> t;
  while (t--)
    solve();

  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3864kb

input:

3
1 2 3

output:

2 6

result:

ok 2 number(s): "2 6"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3640kb

input:

1
1

output:

1 1

result:

ok 2 number(s): "1 1"

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3628kb

input:

2
1 1

output:

1 1

result:

wrong answer 2nd numbers differ - expected: '2', found: '1'