样例已过求调
查看原帖
样例已过求调
1235819
_std_xzh楼主2024/12/7 15:50
#include<bits/stdc++.h>
using namespace std;
int n , x , a[1000005] , ans[1000005] , cnt , sum;
bool f[1000005];
struct node{
    int x , id;
    bool operator<(const node &a)const{
        if(x != a.x)return x > a.x;
        return id < a.id;
    }
};
priority_queue<node>pq;
int main(){
    memset(ans , 0x3f , sizeof(ans));
    cin >> n >> x;
    for(int i = 1;i <= n;i++)cin >> a[i];
    for(int i = 1;i <= x;i++){
        pq.push(node{a[i] , i});
        f[i] = true;
    }
    for(int i = 1;i <= x;i++)ans[i] = a[i] - pq.top().x;
    cnt++;
    int nr = x;
    while(nr < n){
        for(int i = nr - x + 1;i <= nr;i++)f[i] = false;
        int l = nr;
        nr = min(n , pq.top().id + x);
        if(nr == l)break;
        int nl = nr - x + 1;
        for(int i = nl;i <= nr;i++){
            f[i] = true;
            pq.push(node{a[i] , i});
        }
        while(!pq.empty() && !f[pq.top().id])pq.pop();
        for(int i = nl;i <= nr;i++)ans[i] = min(ans[i] , a[i] - pq.top().x);
        cnt++;
    }
    for(int i = 1;i <= n;i++){
        sum += ans[i];
    }
    cout << sum << '\n' << cnt;
    return 0;
}
2024/12/7 15:50
加载中...