답안 #784824

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
784824 2023-07-16T14:58:14 Z eltu0815 Sightseeing in Kyoto (JOI22_kyoto) C++14
0 / 100
1 ms 340 KB
#include <bits/stdc++.h>
#define MAX 500005
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define inf (1987654321)
 
using namespace std;    
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
 
int n, m, q;
int a[100005], b[100005];
ll ans1[100005], ans2[100005];
int prea[100005], preb[100005];
int posta[100005], postb[100005];
deque<pii> graph;
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    cin >> n >> m;
    for(int i = 1; i <= n; ++i) cin >> a[i];
    for(int i = 1; i <= m; ++i) cin >> b[i];
    
    prea[1] = 1, preb[1] = 1;
    for(int i = 2; i <= n; ++i) prea[i] = a[prea[i - 1]] >= a[i] ? i : prea[i - 1];
    for(int i = 2; i <= m; ++i) preb[i] = b[preb[i - 1]] >= b[i] ? i : preb[i - 1];
    
    posta[n] = n, postb[m] = m;
    for(int i = n - 1; i >= 1; --i) posta[i] = a[posta[i + 1]] > a[i] ? i : posta[i + 1];
    for(int i = m - 1; i >= 1; --i) postb[i] = b[postb[i + 1]] > b[i] ? i : postb[i + 1];
    
    auto solve1 = [&](auto&& self, int r, int c) -> void {
        if(r == 0 || c == 0) return;
        int i = prea[r], j = preb[c];
        graph.push_front({i, j});
        self(self, i - 1, j - 1);
    };
    
    auto solve2 = [&](auto&& self, int r, int c) -> void {
        if(r == n + 1 || c == m + 1) return;
        int i = posta[r], j = postb[c];
        graph.push_back({i, j});
        self(self, i + 1, j + 1);
    };
    
    int i = prea[n], j = preb[m];
    graph.push_back({i, j});
    solve1(solve1, i - 1, j - 1);
    graph.push_front({1, 1});
    int sz = graph.size();
    
    ll ans = 0;
    for(int i = 1; i < (int)graph.size(); ++i) {
        ll r = graph[i].first - graph[i - 1].first, c = graph[i].second - graph[i - 1].second;
        ll cost = min(ans1[i - 1] + 1ll * a[graph[i - 1].first] * c + 1ll * b[graph[i].second] * r,
                      ans1[i - 1] + 1ll * b[graph[i - 1].second] * r + 1ll * a[graph[i].first] * c);
        r = graph[i].first - graph[0].first, c = graph[i].second - graph[0].second;
        cost = min(cost, min(1ll * a[graph[0].first] * c + 1ll * b[graph[i].second] * r,
                             1ll * b[graph[0].second] * r + 1ll * a[graph[i].first] * c));
        ans1[i] = cost;
    }
    
    graph.clear(); graph.push_back({i, j});
    solve2(solve2, i + 1, j + 1);
    graph.push_back({n, m});
    for(int i = 1; i < (int)graph.size(); ++i) {
        ll r = graph[i].first - graph[i - 1].first, c = graph[i].second - graph[i - 1].second;
        ll cost = min(ans2[i - 1] + 1ll * a[graph[i - 1].first] * c + 1ll * b[graph[i].second] * r,
                      ans2[i - 1] + 1ll * b[graph[i - 1].second] * r + 1ll * a[graph[i].first] * c);
        r = graph[i].first - graph[0].first, c = graph[i].second - graph[0].second;
        cost = min(cost, min(1ll * a[graph[0].first] * c + 1ll * b[graph[i].second] * r,
                             1ll * b[graph[0].second] * r + 1ll * a[graph[i].first] * c));
        ans2[i] = cost;
    }
    cout << ans1[sz - 1] + ans2[graph.size() - 1];
    return 0;
}

Compilation message

kyoto.cpp: In function 'int main()':
kyoto.cpp:56:8: warning: unused variable 'ans' [-Wunused-variable]
   56 |     ll ans = 0;
      |        ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Incorrect 1 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 324 KB Output is correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Incorrect 1 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -