priority_queue做法求助
查看原帖
priority_queue做法求助
152652
AndyChen2005121楼主2020/10/6 16:25
#include <iostream>
#include <queue>
using namespace std;
int n,m;
struct node {
    int x, t, w;
    node(){}
    node(int xx, int tt, int ww){
        x = xx;
        t = tt;
        w = ww;
    }
};
bool operator < (const node& a, const node& b){
    return a.w > b.w;
}
priority_queue<node> pq;
int main(){
    int ans = 0;
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        node tmp;
        cin >> tmp.x >> tmp.t;
        tmp.w = tmp.x + tmp.t;
        pq.push(tmp);
    }
    node last(0, 0, 0);
    
    while(!pq.empty()){
        node cur = pq.top();
        pq.pop();
        if(m-(abs(cur.x-last.x)+cur.t) < 0){
            break;
        }
        m -= (abs(cur.x-last.x)+cur.t);
        last.x = cur.x;
        last.t = cur.t;
        last.w = cur.w;
        ans++;
    }
    cout << ans << endl;
    return 0;
}
2020/10/6 16:25
加载中...