跳到正文

题解

题解:CF253E Printer

Imerance1018约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年1月27日
收录
2026年7月6日
更新
2026年11月10日
篇幅
约 4 分钟
获赞
2
题目
CF253E

版本

  1. v1当前

Description

传送门

Solution

不妨设 pp 值未知的任务编号为 pospos。

那么随着 pposp_{pos} 的增大,完成任务的时刻 resposres_{pos} 单调不上升。

所以考虑对 pospos 进行二分答案。

对于每个二分出的答案 midmid,使用优先队列模拟打印过程,再将结果 resposres_{pos} 与输入的 TT 比较。

总复杂度 O(nlog⁡2n)O(n \log^2 n)。

code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e4+10;
int n,T,pos,ans,res[N];
struct node
{
	int t,s,p,num;
}a[N],b[N];
bool cmp(node x,node y)
{
	return x.t<y.t;
}
struct Node
{
	int num,val;
	bool operator <(const Node &x)const
	{
		return val<x.val;
	}
};
priority_queue<Node>q;
bool judge(int x)//直接模拟,但要注意细节(我感觉 2200 分就是因为这个模拟难写)
{
	int now=0;
	for(int i=1;i<=n;i++)res[i]=0,b[i]=a[i];
	for(int i=1;i<=n;i++)
	{
		now=b[i].t;
		if(b[i].num==pos)b[i].p=x;
		q.push((Node){i,b[i].p});
		if(i==n)break;
		while(!q.empty())
		{
			int tmp=q.top().num;
			q.pop();
			if(b[tmp].s+now-1<b[i+1].t)
			{
				res[b[tmp].num]=b[tmp].s+now;
				now=b[tmp].s+now;
			}
			else
			{
				b[tmp].s-=(b[i+1].t-now);
				q.push((Node){tmp,b[tmp].p});
				now=b[i+1].t;
				break;	
			}
		}
	
	}
	while(!q.empty())
	{
		int tmp=q.top().num;
		q.pop();
		res[b[tmp].num]=b[tmp].s+now;
		now=b[tmp].s+now;
	}
	return res[pos]>=T;
}
signed main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i].t>>a[i].s>>a[i].p;
		a[i].num=i;
		if(a[i].p==-1)pos=i;
	}
	cin>>T;
	sort(a+1,a+n+1,cmp);//注意 a[i].t 为乱序
	int L=1,R=1e9;
	while(L<=R)
	{
		int mid=(L+R)/2;
		if(judge(mid))ans=mid,L=mid+1;
		else R=mid-1;
	}
	for(int i=1;i<=n;i++)
	{
		if(a[i].p==ans)
		{
			ans--;
			break;
		}
	}//此处是因为 p 各不相同
	cout<<ans<<endl;
	judge(ans);
	for(int i=1;i<=n;i++)cout<<res[i]<<" ";
	return 0;
}
原文