답안 #1087710

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1087710 2024-09-13T06:30:22 Z Seungni 치료 계획 (JOI20_treatment) C++17
39 / 100
361 ms 359252 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
const ll inf = 0x3f3f3f3f3f3f3f3fLL;

ll N, M, sz;
pair<ll, pair<pii, ll>> arr[100005];
vector<pii> graph[100005];
ll seg[200505 * 2];

void update(int pos, ll v) {
    pos += sz;
    seg[pos] = min(seg[pos], v);
    for (; pos > 0; pos >>= 1) seg[pos >> 1] = min(seg[pos], seg[pos ^ 1]);
    return;
}

ll query(int l, int r) {
    r++;
    ll ret = inf;
    for (l += sz, r += sz; l < r; l >>= 1, r >>= 1) {
        if (l & 1) ret = min(ret, seg[l++]);
        if (r & 1) ret = min(ret, seg[--r]);
    }
    return ret;
}

void Dijkstra(int S, vector<ll> &dist) {
    priority_queue<pii, vector<pii>, greater<>> pq;
    dist[S] = 0;
    pq.push({0, S});
    
    while (pq.size()) {
        pii t = pq.top();
        pq.pop();
        ll now = t.second, v = t.first;
        if (dist[now] < v) continue;
        
        for (auto &[next, cost]: graph[now]) {
            ll ncost = v + cost;
            if (dist[next] > ncost) {
                dist[next] = ncost;
                pq.push({ncost, next});
            }
        }
    }
}

void subtask_1() {
    vector<ll> v;
    v.push_back(0);
    v.push_back(1), v.push_back(N);
    for (int i = 0; i < M; i++) {
        v.push_back(arr[i].second.first.first);
        v.push_back(arr[i].second.first.second);
    }
    
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    
    sz = v.size();
    
    sort(arr, arr + M, [&](pair<ll, pair<pii, ll>> a, pair<ll, pair<pii, ll>> b) {
        return a.second.first.second < b.second.first.second;
    });
    for (int i = 0; i < M; i++) {
        ll L = arr[i].second.first.first, R = arr[i].second.first.second;
        L = lower_bound(v.begin(), v.end(), L) - v.begin();
        R = lower_bound(v.begin(), v.end(), R) - v.begin();
        arr[i].second.first = {L, R};
    }
    
    vector<ll> dp(sz + 5, inf);
    memset(seg, 0x3f, sizeof(seg));
    dp[0] = 0;
    update(0, dp[0]);
    
    for (int i = 0; i < M; i++) {
        ll l = arr[i].second.first.first, r = arr[i].second.first.second, c = arr[i].second.second;
        ll idx = lower_bound(v.begin(), v.end(), v[l] - 1) - v.begin();
        dp[r] = min(dp[r], query(idx, r) + c);
        update(r, dp[r]);
    }
    
    int idx = lower_bound(v.begin(), v.end(), N) - v.begin();
    if (dp[idx] == inf) cout << -1;
    else cout << dp[idx];
    return;
}

void subtask_2() {
    ll ans = inf;
    
    sort(arr, arr + M);
    
    for (int i = 0; i < (1 << M); i++) {
        vector<int> v;
        ll cost = 0;
        for (int j = 0; j < M; j++) {
            if (i & (1 << j)) v.push_back(j), cost += arr[j].second.second;
        }
        
        vector<pair<pii, ll>> now;
        now.push_back({{1, N}, 0});
        
        for (int j: v) {
            vector<pair<pii, ll>> nxt;
            ll T = arr[j].first, L = arr[j].second.first.first, R = arr[j].second.first.second;
            for (auto &[range, t]: now) {
                ll l = range.first, r = range.second;
                l -= T - t, l = max(l, 1LL);
                r += T - t, r = min(r, N);
                if (l < L) nxt.push_back({{l, min(r, L - 1)}, T});
                if (r > R) nxt.push_back({{max(l, R + 1), r}, T});
            }
            swap(nxt, now);
        }
        
        if (now.empty()) ans = min(ans, cost);
    }
    
    if (ans == inf) cout << -1;
    else cout << ans;
    
    return;
}

void subtask_3() {
    sort(arr, arr + M);
    for (int i = 0; i < M; i++) {
        ll t1 = arr[i].first, l1 = arr[i].second.first.first, r1 = arr[i].second.first.second, c1 = arr[i].second.second;
        for (int j = 0; j < M; j++) {
            ll t2 = arr[j].first, l2 = arr[j].second.first.first, r2 = arr[j].second.first.second, c2 = arr[j].second.second;
            ll diff = abs(t2 - t1);
            if (r1 - diff + 1 >= l2) graph[i].push_back({j, c2});
        }
    }
    
    int S = M;
    for (int i = 0; i < M; i++) {
        if (arr[i].second.first.first == 1) graph[S].push_back({i, arr[i].second.second});
    }
    
    vector<ll> dist(M + 5, inf);
    Dijkstra(S, dist);
    
    ll ans = inf;
    
    for (int i = 0; i < M; i++) {
        if (arr[i].second.first.second == N) ans = min(ans, dist[i]);
    }
    
    if (ans == inf) cout << -1;
    else cout << ans;
    
    return;
}

void solve() {
    cin >> N >> M;
    
    int sub_task1 = 1, sub_task2 = (M <= 16), sub_task3 = (M <= 5000);
    
    for (int i = 0; i < M; i++) {
        ll T, L, R, C;
        cin >> T >> L >> R >> C;
        if (T != 1) sub_task1 = 0;
        arr[i] = {T, {{L, R}, C}};
    }
    
    if (sub_task1) {
        subtask_1();
        return;
    }
    
    if (sub_task2) {
        subtask_2();
        return;
    }
    
    if (sub_task3) {
        subtask_3();
        return;
    }
    
    return;
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) solve();
    return 0;
}

Compilation message

treatment.cpp: In function 'void subtask_3()':
treatment.cpp:135:67: warning: unused variable 'r2' [-Wunused-variable]
  135 |             ll t2 = arr[j].first, l2 = arr[j].second.first.first, r2 = arr[j].second.first.second, c2 = arr[j].second.second;
      |                                                                   ^~
treatment.cpp:133:31: warning: unused variable 'l1' [-Wunused-variable]
  133 |         ll t1 = arr[i].first, l1 = arr[i].second.first.first, r1 = arr[i].second.first.second, c1 = arr[i].second.second;
      |                               ^~
treatment.cpp:133:96: warning: unused variable 'c1' [-Wunused-variable]
  133 |         ll t1 = arr[i].first, l1 = arr[i].second.first.first, r1 = arr[i].second.first.second, c1 = arr[i].second.second;
      |                                                                                                ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 15304 KB Output is correct
2 Correct 60 ms 15300 KB Output is correct
3 Correct 79 ms 12992 KB Output is correct
4 Correct 54 ms 13068 KB Output is correct
5 Correct 44 ms 13776 KB Output is correct
6 Correct 40 ms 13700 KB Output is correct
7 Correct 50 ms 13752 KB Output is correct
8 Correct 41 ms 13744 KB Output is correct
9 Correct 41 ms 13696 KB Output is correct
10 Correct 43 ms 13772 KB Output is correct
11 Correct 79 ms 15036 KB Output is correct
12 Correct 102 ms 15048 KB Output is correct
13 Correct 70 ms 15288 KB Output is correct
14 Correct 70 ms 15304 KB Output is correct
15 Correct 65 ms 15304 KB Output is correct
16 Correct 66 ms 15336 KB Output is correct
17 Correct 61 ms 14536 KB Output is correct
18 Correct 74 ms 14280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 22 ms 4572 KB Output is correct
3 Correct 22 ms 4444 KB Output is correct
4 Correct 40 ms 4444 KB Output is correct
5 Correct 52 ms 4440 KB Output is correct
6 Correct 22 ms 4444 KB Output is correct
7 Correct 37 ms 4440 KB Output is correct
8 Correct 51 ms 4444 KB Output is correct
9 Correct 28 ms 4444 KB Output is correct
10 Correct 44 ms 4700 KB Output is correct
11 Correct 51 ms 4444 KB Output is correct
12 Correct 39 ms 4444 KB Output is correct
13 Correct 24 ms 4440 KB Output is correct
14 Correct 22 ms 4576 KB Output is correct
15 Correct 30 ms 4444 KB Output is correct
16 Correct 33 ms 4552 KB Output is correct
17 Correct 41 ms 4444 KB Output is correct
18 Correct 23 ms 4444 KB Output is correct
19 Correct 39 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 22 ms 4572 KB Output is correct
3 Correct 22 ms 4444 KB Output is correct
4 Correct 40 ms 4444 KB Output is correct
5 Correct 52 ms 4440 KB Output is correct
6 Correct 22 ms 4444 KB Output is correct
7 Correct 37 ms 4440 KB Output is correct
8 Correct 51 ms 4444 KB Output is correct
9 Correct 28 ms 4444 KB Output is correct
10 Correct 44 ms 4700 KB Output is correct
11 Correct 51 ms 4444 KB Output is correct
12 Correct 39 ms 4444 KB Output is correct
13 Correct 24 ms 4440 KB Output is correct
14 Correct 22 ms 4576 KB Output is correct
15 Correct 30 ms 4444 KB Output is correct
16 Correct 33 ms 4552 KB Output is correct
17 Correct 41 ms 4444 KB Output is correct
18 Correct 23 ms 4444 KB Output is correct
19 Correct 39 ms 4444 KB Output is correct
20 Correct 239 ms 271696 KB Output is correct
21 Correct 251 ms 271700 KB Output is correct
22 Correct 32 ms 9812 KB Output is correct
23 Correct 31 ms 9808 KB Output is correct
24 Correct 177 ms 178188 KB Output is correct
25 Correct 130 ms 123868 KB Output is correct
26 Correct 124 ms 117588 KB Output is correct
27 Correct 157 ms 145492 KB Output is correct
28 Correct 174 ms 176148 KB Output is correct
29 Correct 123 ms 122196 KB Output is correct
30 Correct 128 ms 137300 KB Output is correct
31 Correct 137 ms 148872 KB Output is correct
32 Correct 220 ms 230484 KB Output is correct
33 Correct 338 ms 359252 KB Output is correct
34 Correct 221 ms 230944 KB Output is correct
35 Correct 218 ms 230480 KB Output is correct
36 Correct 361 ms 359248 KB Output is correct
37 Correct 228 ms 230736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 15304 KB Output is correct
2 Correct 60 ms 15300 KB Output is correct
3 Correct 79 ms 12992 KB Output is correct
4 Correct 54 ms 13068 KB Output is correct
5 Correct 44 ms 13776 KB Output is correct
6 Correct 40 ms 13700 KB Output is correct
7 Correct 50 ms 13752 KB Output is correct
8 Correct 41 ms 13744 KB Output is correct
9 Correct 41 ms 13696 KB Output is correct
10 Correct 43 ms 13772 KB Output is correct
11 Correct 79 ms 15036 KB Output is correct
12 Correct 102 ms 15048 KB Output is correct
13 Correct 70 ms 15288 KB Output is correct
14 Correct 70 ms 15304 KB Output is correct
15 Correct 65 ms 15304 KB Output is correct
16 Correct 66 ms 15336 KB Output is correct
17 Correct 61 ms 14536 KB Output is correct
18 Correct 74 ms 14280 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 22 ms 4572 KB Output is correct
21 Correct 22 ms 4444 KB Output is correct
22 Correct 40 ms 4444 KB Output is correct
23 Correct 52 ms 4440 KB Output is correct
24 Correct 22 ms 4444 KB Output is correct
25 Correct 37 ms 4440 KB Output is correct
26 Correct 51 ms 4444 KB Output is correct
27 Correct 28 ms 4444 KB Output is correct
28 Correct 44 ms 4700 KB Output is correct
29 Correct 51 ms 4444 KB Output is correct
30 Correct 39 ms 4444 KB Output is correct
31 Correct 24 ms 4440 KB Output is correct
32 Correct 22 ms 4576 KB Output is correct
33 Correct 30 ms 4444 KB Output is correct
34 Correct 33 ms 4552 KB Output is correct
35 Correct 41 ms 4444 KB Output is correct
36 Correct 23 ms 4444 KB Output is correct
37 Correct 39 ms 4444 KB Output is correct
38 Correct 239 ms 271696 KB Output is correct
39 Correct 251 ms 271700 KB Output is correct
40 Correct 32 ms 9812 KB Output is correct
41 Correct 31 ms 9808 KB Output is correct
42 Correct 177 ms 178188 KB Output is correct
43 Correct 130 ms 123868 KB Output is correct
44 Correct 124 ms 117588 KB Output is correct
45 Correct 157 ms 145492 KB Output is correct
46 Correct 174 ms 176148 KB Output is correct
47 Correct 123 ms 122196 KB Output is correct
48 Correct 128 ms 137300 KB Output is correct
49 Correct 137 ms 148872 KB Output is correct
50 Correct 220 ms 230484 KB Output is correct
51 Correct 338 ms 359252 KB Output is correct
52 Correct 221 ms 230944 KB Output is correct
53 Correct 218 ms 230480 KB Output is correct
54 Correct 361 ms 359248 KB Output is correct
55 Correct 228 ms 230736 KB Output is correct
56 Incorrect 22 ms 9304 KB Output isn't correct
57 Halted 0 ms 0 KB -