#include <bits/stdc++.h>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define pii pair<int,int>
#define pb push_back
#define ins insert
#define ff first
#define ss second
#define opa(x) cout << #x << " = " << x << ", ";
#define op(x) cout << #x << " = " << x << endl;
#define ops(x) cout << x;
#define entr cout << endl;
#define spac cout << ' ';
#define all(x) (x).begin(), (x).end()
#define STL(x) cout << #x << " : "; for(auto &qwe:x) cout << qwe << ' '; cout << endl;
#define arr(x, n) cout << #x << " : "; loop(qwe,0,n) cout << x[qwe] << ' '; cout << endl;
#define deb1 cout << "deb1" << endl;
#define deb2 cout << "deb2" << endl;
#define deb3 cout << "deb3" << endl;
#define deb4 cout << "deb4" << endl;
#define deb5 cout << "deb5" << endl;
using namespace std;
typedef long long ll;
const int mxn = 4194304+10;
ll dp[mxn];
int a_in[100005], b_in[100005];
vector<ll> a, b;
vector<ll> mul_a, mul_b;
const ll INF = 4e18;
vector<pair<int, ll>> E[mxn];
inline int hsh(int i, int j){ return (i << 11) + j; }
inline int get_i(int x){ return x >> 11; }
inline int get_j(int x){ return x & 2047; }
void solve(){
int n, m; cin >> n >> m;
loop(i,0,n){
cin >> a_in[i];
}
loop(j,0,m){
cin >> b_in[j];
}
a.pb(a_in[0]);
int now_mul = 1;
loop(i,1,n-1){
if(a_in[i] >= a_in[i-1] && a_in[i] >= a_in[i+1]){
a_in[i] = a_in[i-1];
now_mul++;
}
else{
a.pb(a_in[i]);
mul_b.pb(now_mul);
now_mul = 1;
}
}
a.pb(a_in[n-1]);
mul_b.pb(now_mul);
n = a.size();
now_mul = 1;
b.pb(b_in[0]);
loop(i,1,m-1){
if(b_in[i] >= b_in[i-1] && b_in[i] >= b_in[i+1]){
b_in[i] = b_in[i-1];
now_mul++;
}
else{
b.pb(b_in[i]);
mul_a.pb(now_mul);
now_mul = 1;
}
}
b.pb(b_in[m-1]);
mul_a.pb(now_mul);
m = b.size();
loop(i,0,n){
loop(j,0,m){
dp[hsh(i, j)] = INF;
}
}
loop(i,0,n){
loop(j,0,m-1){
E[hsh(i, j)].pb({hsh(i, j+1), a[i] * mul_a[j]});
}
}
loop(j,0,m){
loop(i,0,n-1){
E[hsh(i, j)].pb({hsh(i+1, j), b[j] * mul_b[i]});
}
}
dp[0] = 0;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
pq.push({0, 0});
while(!pq.empty()){
ll dis = pq.top().ff;
int now = pq.top().ss;
// opa(dis) op(now);
pq.pop();
if(dis != dp[now]) continue;
for(auto &i:E[now]){
ll nxt_dis = dis + i.ss;
int nxt = i.ff;
if(dp[nxt] > nxt_dis){
dp[nxt] = nxt_dis;
pq.push({nxt_dis, nxt});
}
}
}
cout << dp[hsh(n-1, m-1)] << '\n';
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
// freopen("test_input.txt", "r", stdin);
int t = 1;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
100952 KB |
Output is correct |
2 |
Correct |
21 ms |
100956 KB |
Output is correct |
3 |
Correct |
20 ms |
100952 KB |
Output is correct |
4 |
Correct |
26 ms |
101976 KB |
Output is correct |
5 |
Correct |
22 ms |
105304 KB |
Output is correct |
6 |
Correct |
21 ms |
103144 KB |
Output is correct |
7 |
Correct |
22 ms |
103140 KB |
Output is correct |
8 |
Correct |
20 ms |
100952 KB |
Output is correct |
9 |
Correct |
156 ms |
125908 KB |
Output is correct |
10 |
Correct |
153 ms |
125032 KB |
Output is correct |
11 |
Correct |
134 ms |
125732 KB |
Output is correct |
12 |
Correct |
135 ms |
125460 KB |
Output is correct |
13 |
Correct |
391 ms |
168016 KB |
Output is correct |
14 |
Correct |
130 ms |
125204 KB |
Output is correct |
15 |
Correct |
124 ms |
124640 KB |
Output is correct |
16 |
Correct |
401 ms |
168020 KB |
Output is correct |
17 |
Correct |
22 ms |
100952 KB |
Output is correct |
18 |
Correct |
21 ms |
100952 KB |
Output is correct |
19 |
Correct |
21 ms |
100956 KB |
Output is correct |
20 |
Correct |
23 ms |
100952 KB |
Output is correct |
21 |
Correct |
24 ms |
100952 KB |
Output is correct |
22 |
Correct |
21 ms |
100952 KB |
Output is correct |
23 |
Correct |
21 ms |
100952 KB |
Output is correct |
24 |
Correct |
21 ms |
100952 KB |
Output is correct |
25 |
Correct |
21 ms |
100952 KB |
Output is correct |
26 |
Correct |
21 ms |
100952 KB |
Output is correct |
27 |
Correct |
21 ms |
100952 KB |
Output is correct |
28 |
Correct |
21 ms |
100952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
100952 KB |
Output is correct |
2 |
Correct |
22 ms |
101036 KB |
Output is correct |
3 |
Correct |
114 ms |
120440 KB |
Output is correct |
4 |
Runtime error |
131 ms |
270536 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
100952 KB |
Output is correct |
2 |
Correct |
21 ms |
100956 KB |
Output is correct |
3 |
Correct |
20 ms |
100952 KB |
Output is correct |
4 |
Correct |
26 ms |
101976 KB |
Output is correct |
5 |
Correct |
22 ms |
105304 KB |
Output is correct |
6 |
Correct |
21 ms |
103144 KB |
Output is correct |
7 |
Correct |
22 ms |
103140 KB |
Output is correct |
8 |
Correct |
20 ms |
100952 KB |
Output is correct |
9 |
Correct |
156 ms |
125908 KB |
Output is correct |
10 |
Correct |
153 ms |
125032 KB |
Output is correct |
11 |
Correct |
134 ms |
125732 KB |
Output is correct |
12 |
Correct |
135 ms |
125460 KB |
Output is correct |
13 |
Correct |
391 ms |
168016 KB |
Output is correct |
14 |
Correct |
130 ms |
125204 KB |
Output is correct |
15 |
Correct |
124 ms |
124640 KB |
Output is correct |
16 |
Correct |
401 ms |
168020 KB |
Output is correct |
17 |
Correct |
22 ms |
100952 KB |
Output is correct |
18 |
Correct |
21 ms |
100952 KB |
Output is correct |
19 |
Correct |
21 ms |
100956 KB |
Output is correct |
20 |
Correct |
23 ms |
100952 KB |
Output is correct |
21 |
Correct |
24 ms |
100952 KB |
Output is correct |
22 |
Correct |
21 ms |
100952 KB |
Output is correct |
23 |
Correct |
21 ms |
100952 KB |
Output is correct |
24 |
Correct |
21 ms |
100952 KB |
Output is correct |
25 |
Correct |
21 ms |
100952 KB |
Output is correct |
26 |
Correct |
21 ms |
100952 KB |
Output is correct |
27 |
Correct |
21 ms |
100952 KB |
Output is correct |
28 |
Correct |
21 ms |
100952 KB |
Output is correct |
29 |
Correct |
21 ms |
100952 KB |
Output is correct |
30 |
Correct |
22 ms |
101036 KB |
Output is correct |
31 |
Correct |
114 ms |
120440 KB |
Output is correct |
32 |
Runtime error |
131 ms |
270536 KB |
Execution killed with signal 11 |
33 |
Halted |
0 ms |
0 KB |
- |