于文武 發表於 2025-10-23 21:21:00

比赛题解 总结

<h1 id="1hnoi2003-操作系统">1. 操作系统</h1>
<h2 id="思路">思路</h2>
<p>此题是一道大模拟,主要<strong>根据任务优先级来计算最后执行此任务的时间</strong>,此时我们可以进行分类讨论:</p>
<ul>
<li>当此任务的到达时间晚于等于上一个未执行完任务的结束时间,上一个任务就一定能运行完,因此直接输出结束时间</li>
<li>当此任务的到达时间早于上一个未执行完任务的结束时间,上一个任务就只能在 CPU 中运行一段时间,因此只能更新执行时间<br>
因为执行任务要看其优先级,因此我们用优先队列来存所有进入过 CPU 但还未运行完的任务,然后根据其优先级排序。</li>
</ul>
<h3 id="注意">注意</h3>
<p>我们需要用个变量 lati 来存总时间,因此本文中的上一个 "上一个未执行完任务的结束时间" 是指 lati + 上个任务的执行时间</p>
<h2 id="ac代码">AC代码</h2>
<details>
<summary>点开有惊喜</summary>
<pre><code>#include&lt;bits/stdc++.h&gt;
#define ll long long
using namespace std;
struct node{
        ll id,be,ti,yx;
        bool operator &lt; (const node &amp;a)const{
                if(yx==a.yx) return be&gt;a.be;
                return yx&lt;a.yx;
        }
}a;
ll lati;
priority_queue&lt;node&gt; q;
int main(){
        while(scanf("%lld%lld%lld%lld",&amp;a.id,&amp;a.be,&amp;a.ti,&amp;a.yx)!=EOF){
                while(q.size()&amp;&amp;q.top().ti+lati&lt;=a.be){
                        node b=q.top();
                        q.pop();
                        cout&lt;&lt;b.id&lt;&lt;" "&lt;&lt;lati+b.ti&lt;&lt;"\n";
                        lati+=b.ti;
                }
                if(q.size()){
                        node b=q.top();
                        q.pop();
                        b.ti-=a.be-lati;
                        q.push(b);
                }
                q.push(a);
                lati=a.be;
        }
        while(q.size()){
                node b=q.top();
                q.pop();
                lati+=b.ti;
                cout&lt;&lt;b.id&lt;&lt;" "&lt;&lt;lati&lt;&lt;"\n";
        }
        return 0;
}
</code></pre>
</details><br><br>
来源:https://www.cnblogs.com/chenxuan11/p/19161666
頁: [1]
查看完整版本: 比赛题解 总结