Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
For each index, there are only two possible outcomes: leave alone, or flip it into . So a subset is just a binary choice at every position.
The arrays being non-descending only cares about neighboring positions. Once positions are valid, position only needs to be compatible with position .
Let state mean index is not swapped, and state mean it is swapped. Then store how many valid prefixes end with each state.
A transition from previous state to current state is allowed exactly when both chosen values do not decrease: and .
Initialize both states at with count because both subsets and are distinct even if . Then do the 4 transitions per adjacent pair and sum the two final states.
We need count subsets of indices. Choosing a subset is the same thing as choosing, for every index , whether to swap or not.
So define two possible states for each position:
After all choices, the first chosen values must form a non-descending array, and the second chosen values must also form a non-descending array.
The Key Local Fact
Non-descending order is checked between neighbors. That means if the prefix up to is already valid, then adding position only needs one check against position .
For a state at index , let:
and
A previous state at index can transition into current state at index iff:
and
Both arrays have to behave. One failing inequality kills the transition. Brutal, but fair.
DP Definition
Let be the number of valid choices for the prefix , where index is in state .
Base case:
Why both? Because choosing no swap and choosing swap are different subsets. Even if and the final arrays look identical, the subsets are still different. The sample with makes this very clear.
Transition:
over only the states where the transition is valid.
Since there are only previous states and current states, each position takes constant time.
Answer
After processing all positions, the answer is:
modulo .
Complexity
For each adjacent pair, we try transitions. So the runtime is:
per test case, with memory if we keep only the previous DP row.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
const ll MOD = 998244353;
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n), b(n);
for (int &x : a) cin >> x;
for (int &x : b) cin >> x;
auto firstVal = [&](int i, int swapped) {
return swapped ? b[i] : a[i];
};
auto secondVal = [&](int i, int swapped) {
return swapped ? a[i] : b[i];
};
array<ll, 2> dp = {1, 1};
for (int i = 1; i < n; i++) {
array<ll, 2> ndp = {0, 0};
for (int prev = 0; prev < 2; prev++) {
for (int cur = 0; cur < 2; cur++) {
if (firstVal(i - 1, prev) <= firstVal(i, cur) &&
secondVal(i - 1, prev) <= secondVal(i, cur)) {
ndp[cur] = (ndp[cur] + dp[prev]) % MOD;
}
}
}
dp = ndp;
}
cout << (dp[0] + dp[1]) % MOD << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
const ll MOD = 998244353;
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n), b(n);
for (int &x : a) cin >> x;
for (int &x : b) cin >> x;
auto firstVal = [&](int i, int swapped) {
return swapped ? b[i] : a[i];
};
auto secondVal = [&](int i, int swapped) {
return swapped ? a[i] : b[i];
};
array<ll, 2> dp = {1, 1};
for (int i = 1; i < n; i++) {
array<ll, 2> ndp = {0, 0};
for (int prev = 0; prev < 2; prev++) {
for (int cur = 0; cur < 2; cur++) {
if (firstVal(i - 1, prev) <= firstVal(i, cur) &&
secondVal(i - 1, prev) <= secondVal(i, cur)) {
ndp[cur] = (ndp[cur] + dp[prev]) % MOD;
}
}
}
dp = ndp;
}
cout << (dp[0] + dp[1]) % MOD << '\n';
}
return 0;
}