제출 #1299968

#제출 시각아이디문제언어결과실행 시간메모리
1299968azik21Stove (JOI18_stove)C++20
100 / 100
58 ms12836 KiB
#include<iostream>
// #include<bits/stdc++.h>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#define pb push_back
#define int long long 
#define all(v) (v).begin() , (v).end()
using namespace std;
const int inf = 1e17 , N = 5e3 + 4;
signed main(){
    ios_base::sync_with_stdio(0) , cin.tie(0);
    int n , k ;
    cin >> n >> k ;
    int t[n+2];
    vector<int>us(n + 2 , 0);
    set<int>q;
    for(int i= 1; i<= n ; i++){
        cin >> t[i];
        q.insert(i);
    }
    t[0] = -inf , t[n+1] = inf;
    multiset<pair<int,int>>st;
    for(int i= 1; i < n ; i++){
            st.insert({t[i+1] - t[i] , i });
    }
    int len = n , cnt =0 ;
    while(len > k && st.size() > 0 ){
        int cost = st.begin()->first , i = st.begin()->second;
        st.erase({cost , i });
        // cout << i<< ' ' << cost << '\n';
        if(us[i] && us[i+1]){
            cnt+=cost-1;
            len--;
        }
        else if(us[i] || us[i+1]){
            cnt+=cost;
            len--;
        }
        else {
            cnt+=cost+1;
            len--;
        }
        us[i] = us[i+1] = 1 ;
        // k--;
    }
    for(int i= 1; i<= n; i++){
        if(!us[i])cnt++;
    }
    cout << cnt ;

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...