Submission #1067117

#TimeUsernameProblemLanguageResultExecution timeMemory
10671170npataFlooding Wall (BOI24_wall)C++17
56 / 100
361 ms237756 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define vec vector const int MOD = 1e9+7; int MX = 1005; void add(int& a, int val) { a += val; a %= MOD; } void mul(int& a, int val) { a *= val; a %= MOD; } int32_t main() { int N; cin >> N; vec<int> a(N), b(N); bool flag = true; for(int i = 0; i<N; i++) { cin >> a[i]; flag &= a[i] <=2; }; for(int i = 0; i<N; i++) { cin >> b[i]; flag &= b[i] <=2; }; if(flag) MX = 4; set<int> vals; for(int i = 0; i<N; i++) { vals.insert(a[i]); vals.insert(b[i]); } int id = 0; vec<int> id_to_val(MX); map<int, int> val_to_id; for(int val : vals) { val_to_id[val] = id; id_to_val[id++] = val; } for(int i = 0; i<N; i++) { a[i] = val_to_id[a[i]]; b[i] = val_to_id[b[i]]; } vec<vec<int>> mxcnt_left(N+2, vec<int>(MX)); vec<vec<int>> mxcnt_right(N+2, vec<int>(MX)); mxcnt_left[0][0] = 1; for(int i = 1; i<=N; i++) { for(int j = 0; j<MX; j++) { add(mxcnt_left[i][max(a[i-1], j)], mxcnt_left[i-1][j]); add(mxcnt_left[i][max(b[i-1], j)], mxcnt_left[i-1][j]); } } mxcnt_right[N+1][0] = 1; for(int i = N; i>=1; i--) { for(int j = 0; j<MX; j++) { add(mxcnt_right[i][max(a[i-1], j)], mxcnt_right[i+1][j]); add(mxcnt_right[i][max(b[i-1], j)], mxcnt_right[i+1][j]); } } vec<vec<int>> mnmx_cnt(N+2, vec<int>(MX)); for(int i = 1; i<=N; i++) { int rightsum = 0; for(int j = MX-1; j>=0; j--) { add(rightsum, mxcnt_right[i+1][j]); add(mnmx_cnt[i][j], (mxcnt_left[i-1][j]*rightsum)%MOD); } int leftsum = 0; for(int j = MX-1; j>=0; j--) { add(mnmx_cnt[i][j], (mxcnt_right[i+1][j]*leftsum)%MOD); add(leftsum, mxcnt_left[i-1][j]); } } int ans = 0; for(int i = 1; i<=N; i++) { for(int j = a[i-1]; j<MX; j++) { add(ans, mnmx_cnt[i][j]*(id_to_val[j]-id_to_val[a[i-1]])); } for(int j = b[i-1]; j<MX; j++) { add(ans, mnmx_cnt[i][j]*(id_to_val[j]-id_to_val[b[i-1]])); } } cout << ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...