QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#292425 | #5474. Incredibly Cute Penguin Chicks | DAleksa | WA | 32ms | 331724kb | C++14 | 3.4kb | 2023-12-28 04:45:32 | 2023-12-28 04:45:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int add(int x, int y) {
x += y;
if(x >= mod) x -= mod;
return x;
}
int sub(int x, int y) {
x -= y;
if(x < 0) x += mod;
return x;
}
int mul(int x, int y) {
return (x * 1LL * y) % mod;
}
void sadd(int& x, int y) {
x = add(x, y);
}
void ssub(int& x, int y) {
x = sub(x, y);
}
void smul(int& x, int y) {
x = mul(x, y);
}
struct Fenwick {
int n;
vector<int> fenw;
Fenwick() {
n = 0;
}
Fenwick(int _n) {
n = _n;
fenw.resize(n);
}
void update(int x, int val) {
for(int i = x; i < n; i += (i & -i)) {
sadd(fenw[i], val);
}
}
int get(int r) {
int ans = 0;
for(int i = r; i >= 1; i -= (i & -i)) {
sadd(ans, fenw[i]);
}
return ans;
}
int get(int l, int r) {
return sub(get(r), get(l - 1));
}
};
const int N = 1e6 + 10;
int dp[N][3];
int a[N], b[N], c[N];
Fenwick fw[3][2 * N];
vector<int> vals[3][2 * N];
int shift;
int get(int letter, int index, int x) {
index += shift;
x = upper_bound(vals[letter][index].begin(), vals[letter][index].end(), x) - vals[letter][index].begin();
return fw[letter][index].get(x, vals[letter][index].size());
}
void update(int letter, int index, int x, int val) {
index += shift;
x = lower_bound(vals[letter][index].begin(), vals[letter][index].end(), x) - vals[letter][index].begin();
fw[letter][index].update(x, val);
}
int solve(string s) {
int n = s.size();
shift = n;
s = "#" + s;
a[0] = b[0] = c[0] = 0;
for(int i = 1; i <= n; i++) {
a[i] = a[i - 1] + (s[i] == 'a');
b[i] = b[i - 1] + (s[i] == 'b');
c[i] = c[i - 1] + (s[i] == 'c');
}
for(int i = 0; i <= 2 * n; i++) {
for(int j = 0; j < 3; j++) {
vals[j][i].push_back(-1e9);
}
}
for(int i = 1; i <= n; i++) {
vals[0][b[i] - c[i] + shift].push_back(3 * b[i] - i);
vals[1][c[i] - a[i] + shift].push_back(3 * c[i] - i);
vals[2][a[i] - b[i] + shift].push_back(3 * a[i] - i);
}
for(int i = 0; i <= 2 * n; i++) {
for(int j = 0; j < 3; j++) {
sort(vals[j][i].begin(), vals[j][i].end());
fw[j][i] = Fenwick(vals[j][i].size() + 1);
}
}
for(int i = 1; i <= n; i++) {
sadd(dp[i][0], get(0, b[i] - c[i], 3 * b[i] - i));
sadd(dp[i][1], get(1, c[i] - a[i], 3 * c[i] - i));
sadd(dp[i][2], get(2, a[i] - b[i], 3 * a[i] - i));
if(a[i] == b[i] && c[i] > a[i]) sadd(dp[i][2], 1);
else if(a[i] == c[i] && b[i] > a[i]) sadd(dp[i][1], 1);
else if(b[i] == c[i] && a[i] > b[i]) sadd(dp[i][0], 1);
int tot = add(add(dp[i][0], dp[i][1]), dp[i][2]);
update(0, b[i] - c[i], 3 * b[i] - i, tot);
update(1, c[i] - a[i], 3 * c[i] - i, tot);
update(2, a[i] - b[i], 3 * a[i] - i, tot);
}
return add(add(dp[n][0], dp[n][1]), dp[n][2]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
int n = s.size();
for(int i = 0; i < n; i++) {
if(s[i] == 'I') s[i] = 'a';
else if(s[i] == 'P') s[i] = 'b';
else s[i] = 'c';
}
cout << s << "\n";
cout << solve(s);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 32ms
memory: 331724kb
input:
ICPC
output:
acbc 2
result:
wrong answer 1st lines differ - expected: '2', found: 'acbc'