• 【数据结构】事件模拟


    https://codeforces.com/contest/1239/problem/C

    人为规定事件的优先级

    永续事件用个触发器触发

    
    int n;
    ll p;
    ll ans[100005];
    
    struct Event {
        ll Time;
        int Type, Id;
        bool operator<(const Event &event)const {
            if (Time != event.Time)
                return Time > event.Time;
            if (Type != event.Type)
                return Type > event.Type;
            return Id > event.Id;
        }
    };
    
    set<int> Queue;
    set<int> Want;
    priority_queue<Event>PQ;
    
    void solve() {
        scanf("%d%lld", &n, &p);
        for (int i = 1; i <= n; ++i) {
            ll ti;
            scanf("%lld", &ti);
            PQ.push({ti, 1, i});
        }
        ll curTime = 0;
        ll QueueTime = 0;
        while (!PQ.empty()) {
            // 时间流逝
            if (curTime < PQ.top().Time) {
                curTime = PQ.top().Time;
                QueueTime = max(QueueTime, curTime);
                continue;
            }
            Event e = PQ.top();
    //        printf("  time=%lld type=%d id=%d ", e.Time, e.Type, e.Id);
            PQ.pop();
            if (e.Type == 0) {
                // 出队
                Queue.erase(e.Id);
                if (Queue.empty())
                    QueueTime = curTime;
                ans[e.Id] = e.Time;
            } else 
                // 进入want
                Want.insert(e.Id);
            // 下一事件已经到达
            if (!PQ.empty() && curTime >= PQ.top().Time)
                continue;
            // 下一事件尚未到达,处理永续事件
            if (Want.size() && (Queue.empty() || *Want.begin() < *Queue.begin())) {
                e.Id = *Want.begin();
                Want.erase(e.Id);
                Queue.insert(e.Id);
                e.Type = 0;
                QueueTime = max(QueueTime, curTime);
                QueueTime += p;
                e.Time = QueueTime;
                PQ.push(e);
           }
        }
        for (int i = 1; i <= n; ++i)
            printf("%lld%c", ans[i], " 
    "[i == n]);
    }
    
  • 相关阅读:
    hdu 1301 Jungle Roads
    hdu 1233 还是畅通工程
    12.3日周二学习笔记
    12.2周一学习记录
    12.1周日学习记录
    浅表副本
    DL项目代码目录结构管理
    因子分析
    relu非线性映射函数,能拟合任意函数吗
    神经网络拟合连续函数
  • 原文地址:https://www.cnblogs.com/purinliang/p/14503390.html
Copyright © 2020-2023  润新知