QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#272820 | #6410. Classical DP Problem | ucup-team859# | WA | 0ms | 3560kb | C++14 | 2.8kb | 2023-12-02 19:30:52 | 2023-12-02 19:30:52 |
Judging History
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];
int k = 0;
reverse(l.begin() + 1, l.begin() + n + 1);
for (int i = 1; i <= n; ++i) {
if (l[i] < k)
break;
++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];
}
int ans = get_dp(hm);
swap(l, h);
if (k < n) {
hm = l[k + 1];
}
ans += get_dp(hm);
int p = 1;
for (int i = 1; i <= l[k]; ++i)
p = 1LL * p * i % MOD;
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];
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;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3460kb
input:
3 1 2 3
output:
2 6
result:
ok 2 number(s): "2 6"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
1 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3412kb
input:
2 1 1
output:
2 1
result:
wrong answer 1st numbers differ - expected: '1', found: '2'