답안 #668835

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
668835 2022-12-05T04:08:44 Z vuavisao Lasers (NOI19_lasers) C++14
100 / 100
716 ms 66352 KB
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
using namespace std;

template<typename Lhs, typename Rhs> inline void Max_self(Lhs &a, Rhs b) { a = (a > b ? a : b); }
template<typename Lhs, typename Rhs> inline void Min_self(Lhs &a, Rhs b) { a = (a < b ? a : b); }

const int N = 5e5 + 10;

int LEN, n;
vector<vector<int>> door;

namespace sub1_2 {
    bool check() {
        for(int i = 0; i < n; ++ i) if(door[i].size() != 1) return false;
        return true;
    }

    void solve() {
        int best = 0;
        for(int i = 0; i < n; ++ i) {
            Max_self(best, door[i][0]);
        }
        int res = 2 * max(0, best - (LEN / 2));
        if(res > 0) res -= (LEN & 1);
        cout << res;
    }
}

namespace sub3_4 {
    bool check() {
        bool check_sub = false;
        if(LEN <= 1000000 && n == 2) check_sub = true;
        int cnt_door = 0;
        for(int i = 0; i < n; ++ i) cnt_door += door[i].size();
        if(LEN <= 1000 && cnt_door <= 1000) check_sub = true;
        return check_sub;
    }

    vector<vector<int>> pred;

    int sum_to(int l, int r, int idx) {
        if(l > r) return 0;
        if(l == 0) return pred[idx][r];
        return pred[idx][r] - pred[idx][l - 1];
    }

    bool calc_line(int idx) {
        for(int i = 0; i < n; ++ i) {
            const auto& psy = pred[i];
            int first = lower_bound(psy.begin(), psy.end(), idx) - psy.begin();
            int len_last = sum_to(first, psy.size() - 1, i);
            if(LEN - len_last + 1 <= idx) return false;
        }
        return true;
    }

    void solve() {
        for(int i = 0; i < n; ++ i) {
            auto psy = door[i];
            vector<int> tmp(psy.size());
            for(int j = 0; j < psy.size(); ++ j) {
                if(j == 0) tmp[j] = psy[j];
                else tmp[j] = tmp[j - 1] + psy[j];
            }
            pred.push_back(tmp);
        }
        int res = 0;
        for(int i = 1; i <= LEN; ++ i) res += calc_line(i);
        cout << LEN - res;
    }
}

namespace sub5 {
    bool check() {
        return LEN <= 1000000;
    }

    struct state {
        int v = 0, w = 0, last = 0, idx = 0;
        state() {};
        state(int _v, int _w, int _last, int _idx) { v = _v; w = _w; last = _last; idx = _idx; };

        bool operator < (const state& other) const {
            return this -> w < other.w;
        }
    };

    vector<int> pred;

    void solve() {
        vector<state> buffer = {};
        multiset<int, greater<int>> mst = {};
        for(int i = 0; i < n; ++ i) {
            auto psy = door[i];
            vector<int> tmp(psy.size());
            for(int j = 0; j < psy.size(); ++ j) {
                if(j == 0) tmp[j] = psy[j];
                else tmp[j] = tmp[j - 1] + psy[j];
                buffer.push_back(state(j, tmp[j], (j == 0 ? 0 : tmp[j - 1]), i));
            }
            mst.insert(tmp.back());
            pred.push_back(tmp.back());
        }
        sort(buffer.begin(), buffer.end());
        int res = 0;
        for(int i = 1, j = 0; i <= LEN; ++ i) {
            while(j < buffer.size() && buffer[j].w < i) {
                auto psy = buffer[j];
                auto tmp = mst.find(pred[psy.idx] - psy.last);
                if(tmp != mst.end()) mst.erase(tmp);
                else assert(false);
                mst.insert(pred[psy.idx] - psy.w);
                ++ j;
            }
            int first_right = LEN - *mst.begin() + 1;
            if(first_right > i) res++;
        }
        cout << LEN - res;
    }
}

namespace sub6 {
     struct state {
        int v = 0, w = 0, last = 0, idx = 0;
        state() {};
        state(int _v, int _w, int _last, int _idx) { v = _v; w = _w; last = _last; idx = _idx; };

        bool operator < (const state& other) const {
            return this -> w < other.w;
        }
    };

    vector<int> pred;

    void solve() {
        vector<state> buffer = {};
        multiset<int, greater<int>> mst = {};
        for(int i = 0; i < n; ++ i) {
            auto psy = door[i];
            vector<int> tmp(psy.size());
            for(int j = 0; j < psy.size(); ++ j) {
                if(j == 0) tmp[j] = psy[j];
                else tmp[j] = tmp[j - 1] + psy[j];
                buffer.push_back(state(j, tmp[j], (j == 0 ? 0 : tmp[j - 1]), i));
            }
            mst.insert(tmp.back());
            pred.push_back(tmp.back());
        }
        sort(buffer.begin(), buffer.end());
        int res = 0;
        int first_right = LEN - *mst.begin() + 1;
        res += min(first_right - 1, buffer[0].w);
        for(int i = 0, j = 0; i < buffer.size(); i = j) {
            while(j < buffer.size() && buffer[j].w == buffer[i].w) {
                auto psy = buffer[j];
                auto tmp = mst.find(pred[psy.idx] - psy.last);
                if(tmp != mst.end()) mst.erase(tmp);
                else assert(false);
                mst.insert(pred[psy.idx] - psy.w);
                ++ j;
            }
            int last = (j == buffer.size() ? LEN : buffer[j].w);
            int first_right = LEN - *mst.begin() + 1;
            res += max(0, min(last, first_right - 1) - buffer[i].w);
        }
        cout << LEN - res;
    }
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if (fopen("NOI19_lasers.inp", "r")) {
        freopen("NOI19_lasers.inp", "r", stdin);
        freopen("NOI19_lasers.out", "w", stdout);
    }
    cin >> LEN >> n;
    for(int i = 1; i <= n; ++ i) {
        int cnt; cin >> cnt;
        vector<int> tmp(cnt);
        for(int j = 0; j < cnt; ++ j) cin >> tmp[j];
        door.push_back(tmp);
    }
//    if(sub1_2::check()) {
//        sub1_2::solve();
//        return 0;
//    }
//    if(sub3_4::check()) {
//        sub3_4::solve();
//        return 0;
//    }
//    if(sub5::check()) {
//        sub5::solve();
//        return 0;
//    }
    sub6::solve();
//    assert(false);
    return 0;
}

/// Code by vuavisao

Compilation message

lasers.cpp: In function 'void sub3_4::solve()':
lasers.cpp:64:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |             for(int j = 0; j < psy.size(); ++ j) {
      |                            ~~^~~~~~~~~~~~
lasers.cpp: In function 'void sub5::solve()':
lasers.cpp:99:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |             for(int j = 0; j < psy.size(); ++ j) {
      |                            ~~^~~~~~~~~~~~
lasers.cpp:110:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<sub5::state>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |             while(j < buffer.size() && buffer[j].w < i) {
      |                   ~~^~~~~~~~~~~~~~~
lasers.cpp: In function 'void sub6::solve()':
lasers.cpp:144:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |             for(int j = 0; j < psy.size(); ++ j) {
      |                            ~~^~~~~~~~~~~~
lasers.cpp:156:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<sub6::state>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  156 |         for(int i = 0, j = 0; i < buffer.size(); i = j) {
      |                               ~~^~~~~~~~~~~~~~~
lasers.cpp:157:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<sub6::state>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  157 |             while(j < buffer.size() && buffer[j].w == buffer[i].w) {
      |                   ~~^~~~~~~~~~~~~~~
lasers.cpp:165:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<sub6::state>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |             int last = (j == buffer.size() ? LEN : buffer[j].w);
      |                         ~~^~~~~~~~~~~~~~~~
lasers.cpp: In function 'int32_t main()':
lasers.cpp:178:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  178 |         freopen("NOI19_lasers.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lasers.cpp:179:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  179 |         freopen("NOI19_lasers.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 324 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 324 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 397 ms 61052 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 452 KB Output is correct
17 Correct 661 ms 61988 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 12648 KB Output is correct
2 Correct 12 ms 3572 KB Output is correct
3 Correct 18 ms 3584 KB Output is correct
4 Correct 62 ms 12380 KB Output is correct
5 Correct 34 ms 7376 KB Output is correct
6 Correct 74 ms 15524 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 95 ms 13620 KB Output is correct
9 Correct 30 ms 6856 KB Output is correct
10 Correct 68 ms 12716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 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 0 ms 212 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 452 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 12648 KB Output is correct
2 Correct 12 ms 3572 KB Output is correct
3 Correct 18 ms 3584 KB Output is correct
4 Correct 62 ms 12380 KB Output is correct
5 Correct 34 ms 7376 KB Output is correct
6 Correct 74 ms 15524 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 95 ms 13620 KB Output is correct
9 Correct 30 ms 6856 KB Output is correct
10 Correct 68 ms 12716 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 452 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 320 KB Output is correct
21 Correct 716 ms 61904 KB Output is correct
22 Correct 47 ms 3656 KB Output is correct
23 Correct 22 ms 3268 KB Output is correct
24 Correct 106 ms 12520 KB Output is correct
25 Correct 655 ms 62740 KB Output is correct
26 Correct 98 ms 10980 KB Output is correct
27 Correct 39 ms 3528 KB Output is correct
28 Correct 665 ms 62612 KB Output is correct
29 Correct 664 ms 62804 KB Output is correct
30 Correct 82 ms 11596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 324 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 397 ms 61052 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 452 KB Output is correct
17 Correct 661 ms 61988 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 316 KB Output is correct
21 Correct 66 ms 12648 KB Output is correct
22 Correct 12 ms 3572 KB Output is correct
23 Correct 18 ms 3584 KB Output is correct
24 Correct 62 ms 12380 KB Output is correct
25 Correct 34 ms 7376 KB Output is correct
26 Correct 74 ms 15524 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 95 ms 13620 KB Output is correct
29 Correct 30 ms 6856 KB Output is correct
30 Correct 68 ms 12716 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 452 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 320 KB Output is correct
41 Correct 716 ms 61904 KB Output is correct
42 Correct 47 ms 3656 KB Output is correct
43 Correct 22 ms 3268 KB Output is correct
44 Correct 106 ms 12520 KB Output is correct
45 Correct 655 ms 62740 KB Output is correct
46 Correct 98 ms 10980 KB Output is correct
47 Correct 39 ms 3528 KB Output is correct
48 Correct 665 ms 62612 KB Output is correct
49 Correct 664 ms 62804 KB Output is correct
50 Correct 82 ms 11596 KB Output is correct
51 Correct 28 ms 3848 KB Output is correct
52 Correct 685 ms 65772 KB Output is correct
53 Correct 674 ms 65760 KB Output is correct
54 Correct 75 ms 6708 KB Output is correct
55 Correct 171 ms 13676 KB Output is correct
56 Correct 78 ms 12664 KB Output is correct
57 Correct 112 ms 11908 KB Output is correct
58 Correct 682 ms 66352 KB Output is correct
59 Correct 82 ms 6860 KB Output is correct
60 Correct 117 ms 12084 KB Output is correct