This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define rall(s) s.rbegin(), s.rend()
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define s second
#define f first
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int N = 1e6, mod = 1e9 + 7;
const int inf = 1e9;
void solve() {
int n;
cin >> n;
int a[n + 1][2];
for (int i = 1; i <= n; i++) cin >> a[i][0];
for (int i = 1; i <= n; i++) cin >> a[i][1];
for (int i = 1; i <= n; i++) {
if (a[i][1] < a[i][0]) swap(a[i][1], a[i][0]);
}
int dp[n + 1][1001], cnt[n + 1][1001];
memset(dp, 0, sizeof(dp));
memset(cnt, 0, sizeof(cnt));
cnt[1][0] = 1;
for (int i = 1; i < n; i++) {
for (int x = 0; x <= 1000; x++) {
int v = a[i][0];
if (v < x) {
int val = (cnt[i][x] * 1ll * (x - v)) % mod;
dp[i + 1][x] = (dp[i + 1][x] + (dp[i][x] + val) % mod) % mod;
cnt[i + 1][x] = (cnt[i + 1][x] + cnt[i][x]) % mod;
}else {
cnt[i + 1][v] = (cnt[i + 1][v] + cnt[i][x]) % mod;
dp[i + 1][v] = (dp[i + 1][v] + dp[i][x]) % mod;
}
v = a[i][1];
if (v < x) {
int val = (cnt[i][x] * 1ll * (x - v)) % mod;
dp[i + 1][x] = (dp[i + 1][x] + (dp[i][x] + val) % mod) % mod;
cnt[i + 1][x] = (cnt[i + 1][x] + cnt[i][x]) % mod;
}else {
cnt[i + 1][v] = (cnt[i + 1][v] + cnt[i][x]) % mod;
dp[i + 1][v] = (dp[i + 1][v] + dp[i][x]) % mod;
}
}
}
int pd[n + 1][1001], ct[n + 1][1001];
memset(pd, 0, sizeof(pd));
memset(ct, 0, sizeof(ct));
ct[n][0] = 1;
for (int i = n; i > 1; i--) {
for (int x = 0; x <= 1000; x++) {
int v = a[i][0];
if (v < x) {
int val = (ct[i][x] * 1ll * (x - v)) % mod;
pd[i - 1][x] = (pd[i - 1][x] + (pd[i][x] + val) % mod) % mod;
ct[i - 1][x] = (ct[i - 1][x] + ct[i][x]) % mod;
}else {
ct[i - 1][v] = (ct[i - 1][v] + ct[i][x]) % mod;
pd[i - 1][v] = (pd[i - 1][v] + pd[i][x]) % mod;
}
v = a[i][1];
if (v < x) {
int val = (ct[i][x] * 1ll * (x - v)) % mod;
pd[i - 1][x] = (pd[i - 1][x] + (pd[i][x] + val) % mod) % mod;
ct[i - 1][x] = (ct[i - 1][x] + ct[i][x]) % mod;
}else {
ct[i - 1][v] = (ct[i - 1][v] + ct[i][x]) % mod;
pd[i - 1][v] = (pd[i - 1][v] + pd[i][x]) % mod;
}
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
int v = a[i][0];
for (int x = 0; x <= v; x++) {
ans = (ans + dp[i][x]) % mod;
if (x < v) ans = (ans + pd[i][x]) % mod;
}
v = a[i][1];
for (int x = 0; x <= v; x++) {
ans = (ans + dp[i][x]) % mod;
if (x < v) ans = (ans + pd[i][x]) % mod;
}
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |