QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#334689 | #4939. Red Black Ball | lam | WA | 1ms | 5204kb | C++14 | 4.7kb | 2024-02-22 11:41:35 | 2024-02-22 11:41:37 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=51;
const int mod = 998244353;
int dp[N][N][N][N];
int cnt[N][N], cnt2[4 * N];
int LIM = 2 * N;
int kdp[5 * N][4 * N], it_kdp = 0;
inline void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
void calc(vector <int> a, int L, int xL, int R, int xR)
{
int n=a.size();
vector <int> p; p.push_back(L);
for (int i:a) p.push_back(i); p.push_back(R);
memset(cnt, 0, sizeof(cnt));
memset(cnt2, 0, sizeof(cnt2));
if (xL == xR) {
if (xL == 0) cnt[n][0] = 1;
else cnt[0][n] = 1;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
if (cnt[i][j] != 0)
add(cnt2[i - j + LIM], cnt[i][j]);
return;
}
for (int i = 0; i <= n + 1; i++)
for (int j = 0; j <= n + 1; j++)
for (int x = 0; x <= n + 1; x++)
for (int y = 0; y <= n + 1; y++)
dp[i][j][x][y] = 0;
dp[0][0][0][n+1] = 1;
for (int len = 0; len < n; len++) {
for (int i = 0; i <= n; i++) {
int j = len - i;
for (int lr = 0; lr <= n + 1; lr++)
for (int lb = 0; lb <= n + 1; lb++)
if (dp[i][j][lr][lb] != 0) {
int x = dp[i][j][lr][lb];
// cout << i << ' ' << j << ' ' << lr << ' ' << lb << " = " << x << '\n';
if (i + 1 <= lr) add(dp[i+1][j][lr][lb],x*(lr - i)%mod);
if (j + 1 <= n + 1 - lb) add(dp[i][j+1][lr][lb], x*(n + 1 - lb - j)%mod);
for (int new_pos = lr + 1; new_pos < lb; new_pos++) {
if (p[new_pos]-p[lr] <= p[lb]-p[new_pos])
add(dp[i+1][j][new_pos][lb],x);
else add(dp[i][j+1][lr][new_pos],x);
}
}
}
}
for (int i = 0; i <= n; i++) {
int j = n - i;
for (int lr = 0; lr <= n + 1; lr++)
for (int lb = 0; lb <= n + 1; lb++)
if (dp[i][j][lr][lb]) {
// cout << i << ' ' << lr << " & " << j << ' ' << lb << " === " << dp[i][j][lr][lb] << '\n';
add(cnt[i][j], dp[i][j][lr][lb]);
}
}
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
if (cnt[i][j] != 0) {
int x;
if (xL == 0) x = (i - j + LIM);
else x = (j - i + LIM);
add(cnt2[x],cnt[i][j]);
}
}
void upd_cnt() {
it_kdp++;
for (int sum = 0; sum < 4 * N; sum++)
if (kdp[it_kdp-1][sum] != 0)
for (int sum2 = 0; sum2 < 4 * N; sum2++)
if (cnt2[sum2] != 0)
{
int sum3 = sum + sum2 - LIM;
add(kdp[it_kdp][sum3],kdp[it_kdp-1][sum] * cnt2[sum2] % mod);
}
}
const string vkt = "BLACK";
void solve() {
int n, m;
cin >> m >> n;
vector <int> a(n);
vector <pair<int,int>> b(m + 2);
for (int i = 1; i <= m; i++) {
cin >> b[i].first;
string s; cin >> s;
b[i].second = (s == vkt);
}
b[0]={-1e18,0};
b[m+1]={1e18,1};
sort(b.begin(), b.end());
for (int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
it_kdp = 0;
kdp[it_kdp][LIM] = 1;
for (int i = 1; i < b.size(); i++) {
int L = b[i-1].first; int xL = b[i-1].second;
int R = b[i].first; int xR = b[i].second;
vector <int> tmp;
for (int j = 0; j < n; j++)
if (a[j] > L && a[j] < R) tmp.push_back(a[j]);
calc(tmp, L, xL, R, xR);
upd_cnt();
}
memset(cnt,0,sizeof(cnt));
memset(cnt2,0,sizeof(cnt2));
int sum = 0;
for (int i = 1; i <= m; i++) {
if (b[i].second == 0) sum++;
else sum--;
}
cnt2[sum + LIM] = 1;
upd_cnt();
int ans = 0;
for (int sum = 0; sum < 4 * N; sum++)
if (sum > LIM) add(ans, kdp[it_kdp][sum]);
cout << ans << '\n';
}
signed main()
{
ios_base::sync_with_stdio(false);
// cin.tie(nullptr); cout.tie(nullptr);
solve();
return 0;
// int L, R, xL, xR;
// cout << "L = "; cin >> L;
// cout << "xL = "; cin >> xL;
// cout << "R = "; cin >> R;
// cout << "xR = "; cin >> xR;
// int n; cout << "n = "; cin >> n;
// vector <int> a(n);
// for (int i = 0; i < n; i++) cin >> a[i];
// it_kdp = 0;
// kdp[it_kdp][LIM] = 1;
// calc(a, L, xL, R, xR);
// upd_cnt();
// int ans = 0;
// for (int sum = 0; sum < 4 * N; sum++)
// if (sum > LIM) add(ans, kdp[it_kdp][sum]);
// cout << ans << '\n';
}
/**
2 3
1 RED
10 BLACK
2 4 7
**/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3924kb
input:
2 3 1 BLACK 10 RED 2 5 7
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3724kb
input:
2 3 1 RED 10 BLACK 2 4 7
output:
6
result:
ok single line: '6'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3956kb
input:
2 3 1 RED 10 BLACK 7 4 2
output:
6
result:
ok single line: '6'
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 5204kb
input:
20 46 238846592 BLACK 199923217 RED 526626128 BLACK 62308338 RED 523811748 RED 59432 BLACK 273113193 BLACK 730729301 BLACK 973259012 RED 225318015 BLACK 611574923 RED 234880345 RED 485448383 BLACK 405607946 BLACK 747693124 RED 794086621 BLACK 91585417 BLACK 466451303 RED 244184598 RED 334788273 RED ...
output:
739852650
result:
wrong answer 1st lines differ - expected: '850819974', found: '739852650'