Loading [MathJax]/jax/output/HTML-CSS/jax.js

题解:P11289 【MX-S6-T1】「KDOI-11」打印

P11289 题解

题面

原题传送门

思路

题目浅显易懂,模拟题。

首先按下发命令的 t 给它排序,接下来考虑一个个的分配给打印机。

由于每次会选择等待时间最短的打印机,所以可以用两个优先队列来维护,一个优先队列 q 用来维护当前需要等待的打印机要等多久以及其编号,另一个 num 用来维护不用等待的打印机的编号。

每一次先把已经打印完的打印机出队,并让这些打印机的编号插入 num,接下来开始判断当前这个文件该用哪个打印机来打印,有以下两种情况。

  • num 是空的,那么所有的打印机都需要等待,那就是等一等啦,就用 q 最前面的那个打印机。
  • num 是非空的,那就直接用 num 中编号最小的。

于是开始模拟,就过了。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#define ll long long
#define pll pair<ll,ll>
using namespace std;
const int MN=2e5+5;
ll n,m;
priority_queue<pll,vector<pll>,greater<pll> > q;
priority_queue<ll,vector<ll>,greater<ll> > num;
vector<ll> ans[MN];
struct point{ll s,t,id;}p[MN];
void write(ll n){if(n<0){putchar('-');write(-n);return;}if(n>9)write(n/10);putchar(n%10+'0');}
ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
bool cmp(point a, point b){return a.t<b.t;}
int main(){
    // freopen("print4.in","r",stdin);
    // freopen("1.out","w",stdout);
    n=read();m=read();
    for(int i=1; i<=n; i++) p[i].s=read(),p[i].t=read(),p[i].id=i;
    sort(p+1,p+1+n,cmp);
    for(int i=1; i<=m; i++) num.push(i);
    for(int i=1; i<=n; i++){
        while(!q.empty()&&q.top().first<=p[i].t) num.push(q.top().second),q.pop();//出队
        if(num.empty()){
            pll tmp=q.top();q.pop();
            ans[tmp.second].push_back(p[i].id);
            q.push({tmp.first+p[i].s,tmp.second});
        }
        else{
            ans[num.top()].push_back(p[i].id);
            q.push({p[i].s+p[i].t,num.top()});num.pop();
        }
    }
    for(int i=1; i<=m; i++){
        write(ans[i].size());putchar(' ');
        sort(ans[i].begin(),ans[i].end());
        for(int j=0; j<(int)ans[i].size(); j++) write(ans[i][j]),putchar(' ');
        putchar('\n');
    }
    return 0;
}
posted @   naroto2022  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示
花开如火,也如寂寞。