问个关于stl的语法问题
  • 板块学术版
  • 楼主JimmyFlower
  • 当前回复8
  • 已保存回复9
  • 发布时间2024/10/15 22:18
  • 上次更新2024/10/16 10:52:37
查看原帖
问个关于stl的语法问题
124676
JimmyFlower楼主2024/10/15 22:18
#include <iostream>
#include <queue>
#include <vector>
#include <set>
using namespace std;
typedef pair<int, int> PII;
int n, t, m, p;

queue<PII> waits; //first : 长度, second :释放时间
set<PII> runs; // first : 起始地址, second :长度
priority_queue<PII, vector<PII>, greater<PII>> endts; // (first : 释放时间, second: 起始时间)
int tm, cnt;

bool give(int t, int m, int p){
    for (auto it = runs.begin(); it != runs.end(); it ++){
        auto jt = it;
        jt ++;
        if (jt != runs.end()){
            if (m <= jt->first - (it->first + it->second - 1) - 1){
                int start = it->first + it->second;
                runs.insert({start, m});
                endts.push({t + p, start});
                return true;
            }
        }
    }
    return false;
}

void finish(int t){
    while (endts.size() && endts.top().first <= t){
        int f = endts.top().first;
        while (endts.size() && endts.top().first == f){
            auto top = endts.top();
            endts.pop();
            auto it = runs.lower_bound({top.second, 0});
            runs.erase(it);
        }

        tm = f;

        while (waits.size()){
            auto front = waits.front();
            if (give(f, front.first, front.second)){
                waits.pop();
            }
            else break;
        }
    }
}
int main(){
    cin >> n;
    runs.insert({-1, 1}), runs.insert({n, 1});
    while (cin >> t >> m >> p, t || m || p){
        finish(t);
        if (!give(t, m, p)){
            waits.push({m, p});
            cnt ++;
        }
    }
    finish(2e9);

    cout << tm << endl << cnt << endl;
    return 0;
}

为什么give函数那里要用it->first,而finish函数那里就可以用it.first
2024/10/15 22:18
加载中...