답안 #1080334

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1080334 2024-08-29T08:55:22 Z anton Fire (BOI24_fire) C++17
0 / 100
1 ms 600 KB
#include<bits/stdc++.h>

using namespace std;
#define pii pair<int, int>

const int MAX_S = 20;
int N, M;
vector<int> times;

vector<vector<int>> calc_bl(vector<int> optimal_next){
    int n = optimal_next.size();
    vector<vector<int>> bl(MAX_S, vector<int>(n));

    for(int i = 0; i<n; i++){
        bl[0][i] = optimal_next[i];
    }
    for(int step =1; step<MAX_S; step++){
        for(int i = 0; i<n; i++){
            bl[step][i] = bl[step-1][bl[step-1][i]];
        }
    }
    return bl;

}

int get_nth(vector<vector<int>>& bl, int pos, int dist){
    for(int i = MAX_S-1; i>=0; i--){
        if((dist&(1LL<<i))!=0LL){
            pos = bl[i][pos];
        }
    }
    return pos;
}

int cost_to_move(vector<vector<int>>& bl, int target,int pos){
    int cost = 0;
    for(int i = MAX_S-1; i>=0; i--){
        if(bl[i][pos]<target){
            pos = bl[i][pos];
            cost += (1<<i);
        }
    }
    if(bl[0][pos]>=target){
        return cost+1;
    }
    return 1e9;
}
signed main(){
    cin>>N>>M;

    vector<pii> inters;

    for(int i = 0; i<N; i++){
        pii inter;
        cin>>inter.first>>inter.second;
        if(inter.first>inter.second){
            inters.push_back({inter.first, inter.second+M});
        }
        else{
            inters.push_back({inter.first, inter.second});
            inters.push_back({inter.first+M, inter.second+M});
        }
    }

    for(auto inter: inters){
        times.push_back(inter.first);
        times.push_back(inter.second);
    }

    sort(times.begin(), times.end());

    for(pii& e: inters){
        if(e.first<e.second){
            e.first = lower_bound(times.begin(), times.end(), e.first) - times.begin();
            e.second = lower_bound(times.begin(), times.end(), e.second)- times.begin();
        }
    }
    sort(inters.begin(), inters.end());


    vector<pii> ev;
    set<pii> cur_open;

    for(int i = 0; i<inters.size(); i++){
        auto e = inters[i];
        ev.push_back({e.first, i});
    }

    vector<int> opt(times.size());
    sort(ev.begin(), ev.end());
    reverse(ev.begin(), ev.end());
    for(int i = 0; i<times.size(); i++){
        while(ev.size()>0 && ev.back().first==i){
            auto cur_ev = ev.back();
            ev.pop_back();
            cur_open.insert(pii({inters[cur_ev.second].second, cur_ev.second}));
        }
        if(cur_open.size()>0){
            opt[i] = (--cur_open.end())->first;
        }
        while(cur_open.size()>0 && cur_open.begin()->first == i){
            cur_open.erase(cur_open.begin());
        }
    }
    /*for(int i = 0; i<times.size(); i++){
        cout<<times[i]<<" "<<times[opt[i]]<<endl;
    }*/

    auto bl = calc_bl(opt);


    int res =1e9;


    for(int i = 0; i<times.size(); i++){
        res = min(res, cost_to_move(bl, lower_bound(times.begin(), times.end(), times[i]+M)-times.begin(), i));
    }
    if(res>=1e6){
        cout<<-1<<endl;
    }
    else{
        cout<<res<<endl;
    }
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:84:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |     for(int i = 0; i<inters.size(); i++){
      |                    ~^~~~~~~~~~~~~~
Main.cpp:92:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |     for(int i = 0; i<times.size(); i++){
      |                    ~^~~~~~~~~~~~~
Main.cpp:115:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |     for(int i = 0; i<times.size(); i++){
      |                    ~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Incorrect 1 ms 348 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Incorrect 1 ms 348 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Incorrect 1 ms 348 KB Output isn't correct
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 432 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 0 ms 344 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Incorrect 0 ms 348 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Incorrect 1 ms 348 KB Output isn't correct
9 Halted 0 ms 0 KB -