QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#232933 | #6410. Classical DP Problem | adelina_15 | WA | 0ms | 3416kb | C++14 | 2.5kb | 2023-10-31 01:35:21 | 2023-10-31 01:35:22 |
Judging History
answer
#include <iostream>
#include <vector>
using namespace std;
int n;
vector<int> a;
#define Mod 998244353
int dp[5005][5005];
long long Fact(int k)
{
long long f = 1;
for(int i = 2; i <= k; i++)
{
f *= i*1LL;
f %= Mod;
}
return f;
}
bool OK(int i, int j)
{
if(a[i] >= j)
return true;
return false;
}
int KMin()
{
int i = 1, j = 1, rez = 0;
while(OK(i, j))
{
i++;
j++;
rez++;
}
return rez;
}
int Linii(int k)
{
int x;
if(k == n)
x = 0;
else
x = a[k+1];
dp[0][x] = 1;
for(int i = 0; i <= k; i++)
for(int j = x; j >= 0; j--)
{
if(j > 0)
{
dp[i+1][j-1] += dp[i][j]*j;
dp[i+1][j-1]%=Mod;
}
dp[i+1][j] += dp[i][j]*(a[i+1]-j);
dp[i+1][j] %= Mod;
//cout << dp[i][j] << " ";
}
return dp[k][0];
}
void SchimbaA(vector<int> h)
{
vector<int> b;
b.resize(n+1);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= h[i]; j++)
b[j]++;
}
a = b;
}
void RotireMat()
{
vector<int> hCol;
hCol.resize(n+1);
for(int i = 1; i <= n; i++)
{
int r = 0;
for(int j = 1; j <= n; j++)
if(a[j] >= a[i])
r++;
hCol[i] = r;
}
SchimbaA(hCol);
}
int Col()
{
RotireMat();
int k = KMin();
int x;
if(k == n)
x = 0;
else
x = a[k+1];
for(int i = 0; i <= k; i++)
for(int j = x; j >= 0; j--)
dp[i][j] = 0;
dp[0][x] = 1;
for(int i = 0; i <= k; i++)
for(int j = x; j >= 0; j--)
{
if(j > 0)
{
dp[i+1][j-1] += dp[i][j]*j;
dp[i+1][j-1]%=Mod;
}
dp[i+1][j] += dp[i][j]*(a[i+1]-j);
dp[i+1][j] %= Mod;
//cout << dp[i][j] << " ";
}
return dp[k][0];
}
int main()
{
cin >> n;
a.resize(n+1);
for(int i = 1; i <= n; i++)
cin >> a[i];
// cout << k << " ";
for(int i = 1; i <= n/2; i++)
swap(a[i], a[n-i+1]);
int k = KMin();
cout << k << " ";
//cout << a[1] << " " << a[2] << " " << a[3] << "\n";
//cout << Linii(k) << " " << Col() << "\n";
cout << Linii(k)+Col()%Mod-Fact(k);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3396kb
input:
3 1 2 3
output:
2 6
result:
ok 2 number(s): "2 6"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3416kb
input:
1 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3396kb
input:
2 1 1
output:
1 4
result:
wrong answer 2nd numbers differ - expected: '2', found: '4'