이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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]);
if (a[i][1] > 1000) return;
}
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... |