QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#232879#6410. Classical DP Problemana_valeriaWA 0ms3720kbC++141.9kb2023-10-31 00:09:072023-10-31 00:09:08

Judging History

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

  • [2023-10-31 00:09:08]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3720kb
  • [2023-10-31 00:09:07]
  • 提交

answer

#include <iostream>
#include <algorithm>
#define MAX 5000
#define MOD 998244353
using namespace std;
///ifstream cin ("c.in");
///ofstream cout ("c.out");
int a[MAX + 10], aux[MAX + 10], dp[MAX + 10][MAX + 10], n;
int findK()
{
    int k = 0;
    for (int i = 1; i <= n + 1 && k == 0; i++)
        if (a[i] < i)
            k = i - 1;
    return k;
}
int findX(int k)
{
    return a[k + 1];
}
int kFact(int k)
{
    int p = 1;
    for (int i = 1; i <= k; i++)
        p = 1LL * p * i % MOD;
    return p;
}
int DP(int k)
{
    int x = findX(k);
    for (int i = 0; i <= k; i++)
        for (int j = 0; j <= x; j++)
            dp[i][j] = 0;
    dp[0][x] = 1;
    for (int i = 0; i < k; i++)
        for (int j = x; j >= 0; j--)
        {
            dp[i + 1][j] = (dp[i + 1][j] + 1LL * (a[i + 1] - j) * dp[i][j] % MOD) % MOD;
            if (j > 0)
                dp[i + 1][j - 1] = (dp[i + 1][j - 1] + 1LL * j * dp[i][j] % MOD) % MOD;
        }
    return dp[k][0];
}
void flip()
{
    int length = 1, dim = 0, last, first;
    for (int i = 2; i <= n; i++)
        if (a[i - 1] == a[i])
            length++;
        else
        {
            last = i - 1;
            first = last - length + 1;
            dim = dim + length;
            for (int j = first; j <= last; j++)
                aux[j] = dim;
            length = 1;
        }
    last = n;
    first = last - length + 1;
    dim = dim + length;
    for (int j = first; j <= last; j++)
        aux[j] = dim;
    n = a[1];
    for (int i = n; i >= 1; i--)
        a[n - i + 1] = aux[i];
    /*for (int j = 1; j <= n; j++)
        cout << a[j] << ' ';*/
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    reverse(a + 1, a + n + 1);
    int k = findK();
    cout << k << ' ';
    int ans = DP(k);
    flip();
    ans = (ans + DP(k) - kFact(k) + MOD) % MOD;
    cout << ans;
    return 0;
}

详细

Test #1:

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

input:

3
1 2 3

output:

2 6

result:

ok 2 number(s): "2 6"

Test #2:

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

input:

1
1

output:

1 1

result:

ok 2 number(s): "1 1"

Test #3:

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

input:

2
1 1

output:

1 1

result:

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