답안 #544498

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544498 2022-04-02T07:03:00 Z model_code Sightseeing in Kyoto (JOI22_kyoto) C++17
100 / 100
414 ms 17504 KB
#include <array>
#include <climits>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;
const ll INF = LLONG_MAX / 4;
void chmin(ll& a, ll b){ if(a > b) a = b; }

template<class T, class F> struct erasable_heap{
    priority_queue<T, vector<T>, F> q, e;
    erasable_heap(F f, vector<T> v): q(f, v), e(f, v){}
    void push(T a){ q.push(a); }
    void erase(T a){
        e.push(a);
        while(q.size() && q.top() == e.top()){
            q.pop();
            e.pop();
        }
    }
    void pop(){ erase(top()); }
    T top() const { return q.top(); }
    size_t size() const { return q.size() - e.size(); }
};
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    ll H, W;
    cin >> H >> W;
    vector<ll> A(H), B(W);
    for(ll& a : A) cin >> a;
    for(ll& b : B) cin >> b;
    vector<ll> nextA(H - 1), nextB(W - 1);
    for(ll i = 0; i < H - 1; i++) nextA[i] = i + 1;
    for(ll i = 0; i < W - 1; i++) nextB[i] = i + 1;
    auto comp = [&](const auto& a, const auto& b){
        if(a[0] * b[1] != a[1] * b[0]) return a[0] * b[1] < a[1] * b[0];
        if(a[1] != b[1]) return a[1] < b[1];
        return a[2] < b[2];
    };
    erasable_heap qA(comp, vector<array<ll, 3>>{}), qB = qA;
    // {差分の分子, 差分の分母, 始点の idx}
    for(ll i = 0; i < H - 1; i++) qA.push({A[i + 1] - A[i], 1, i});
    for(ll i = 0; i < W - 1; i++) qB.push({B[i + 1] - B[i], 1, i});
    qA.push({(ll)-1.1e12, 1, -1});
    qB.push({(ll)-1.1e12, 1, -1});
    ll ans = 0;
    while(qA.size() > 1 || qB.size() > 1){
        if(comp(qB.top(), qA.top())){
            auto [p, q, i] = qA.top();
            qA.pop();
            const int j = nextA[i];
            if(j == A.size() - 1){
                while(q--){
                    ans += B.back();
                    A.pop_back();
                }
            }
            else{
                const int k = nextA[j];
                qA.erase({A[k] - A[j], k - j, j});
                nextA[i] = k;
                qA.push({A[k] - A[i], k - i, i});
            }
        }
        else{
            auto [p, q, i] = qB.top();
            qB.pop();
            const int j = nextB[i];
            if(j == B.size() - 1){
                while(q--){
                    ans += A.back();
                    B.pop_back();
                }
            }
            else{
                const int k = nextB[j];
                qB.erase({B[k] - B[j], k - j, j});
                nextB[i] = k;
                qB.push({B[k] - B[i], k - i, i});
            }
        }
    }
    cout << ans << endl;
}

Compilation message

kyoto.cpp: In function 'int main()':
kyoto.cpp:54:18: warning: comparison of integer expressions of different signedness: 'const int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |             if(j == A.size() - 1){
      |                ~~^~~~~~~~~~~~~~~
kyoto.cpp:71:18: warning: comparison of integer expressions of different signedness: 'const int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |             if(j == B.size() - 1){
      |                ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 484 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 460 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 2 ms 356 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 312 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 320 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 115 ms 6652 KB Output is correct
5 Correct 63 ms 6336 KB Output is correct
6 Correct 38 ms 3028 KB Output is correct
7 Correct 379 ms 16048 KB Output is correct
8 Correct 402 ms 16088 KB Output is correct
9 Correct 401 ms 14900 KB Output is correct
10 Correct 391 ms 15580 KB Output is correct
11 Correct 221 ms 16120 KB Output is correct
12 Correct 414 ms 17504 KB Output is correct
13 Correct 376 ms 16036 KB Output is correct
14 Correct 208 ms 16104 KB Output is correct
15 Correct 58 ms 11184 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 304 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 2 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 484 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 460 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 2 ms 356 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 312 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 320 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 320 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 115 ms 6652 KB Output is correct
33 Correct 63 ms 6336 KB Output is correct
34 Correct 38 ms 3028 KB Output is correct
35 Correct 379 ms 16048 KB Output is correct
36 Correct 402 ms 16088 KB Output is correct
37 Correct 401 ms 14900 KB Output is correct
38 Correct 391 ms 15580 KB Output is correct
39 Correct 221 ms 16120 KB Output is correct
40 Correct 414 ms 17504 KB Output is correct
41 Correct 376 ms 16036 KB Output is correct
42 Correct 208 ms 16104 KB Output is correct
43 Correct 58 ms 11184 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 304 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 2 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 212 KB Output is correct
52 Correct 0 ms 212 KB Output is correct
53 Correct 0 ms 212 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 125 ms 9160 KB Output is correct
56 Correct 2 ms 340 KB Output is correct
57 Correct 8 ms 1108 KB Output is correct
58 Correct 27 ms 2240 KB Output is correct
59 Correct 350 ms 16940 KB Output is correct
60 Correct 401 ms 15708 KB Output is correct
61 Correct 355 ms 15980 KB Output is correct
62 Correct 379 ms 16936 KB Output is correct
63 Correct 84 ms 11700 KB Output is correct
64 Correct 382 ms 17448 KB Output is correct
65 Correct 384 ms 17280 KB Output is correct
66 Correct 202 ms 13992 KB Output is correct
67 Correct 81 ms 12484 KB Output is correct