跳到正文

题解

AT_abc443_c 题解

fft_type约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年1月31日
收录
2026年2月1日
更新
2026年11月13日
篇幅
约 3 分钟
获赞
2

版本

  1. v2当前
  2. v1

此题应使用模拟的思路。

首先记录机器当前的开关状态 ss(开启为 11,关闭为 00)以及最近一次被启用的时间 tt。

初始时 s←1s \larr 1(即机器开启)且 t←0t \larr 0。

用优先队列按时间从小到大存储所有青木经过的事件和机器重新开启的事件(在输入时,并不存在机器重新开启的事件)。

具体地,可以使用 pair,第一项记录事件时间,第二项记录事件类型。

然后遍历优先队列,取出时间最靠前的事件,其发生时间定义为 kk。分情况讨论:

1. 当事件类型为青木经过时

  • 如果此时 s=1s=1(即机器开启),那么 s←0s \larr 0(关闭机器);向优先队列中添加一次机器重新开启的事件,时间为 k+100k+100;记录机器开启至今的时长。
  • 如果此时 s=0s=0(即机器已被关闭),则不用处理。

2. 当事件类型为机器重新开启时

  • s←1s \larr 1(开启机器);
  • t←kt \larr k(保存事件发生时间)。

最后统计所有机器开启的时长总和。别忘了时间为 TT 时也要进行统计。

AC Code
C++
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PIR;//PIR.second=0表示关闭,1表示开启 
priority_queue<PIR, vector<PIR>, greater<PIR> > todo; 
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	bool opening = 1;
	
	int N, T;
	int t = 0, tt = 0;
	cin>>N>>T;
	for(int i = 1; i <= N; i++){
		int x;
		cin>>x;
		todo.push({x, 0});
	}
	
	while(!todo.empty()){
		PIR task = todo.top();
		todo.pop();
		
		if(task.first > T) break; 
		
		if(!task.second){
			if(opening){
				//关闭
				opening = 0;
				tt += (task.first - t);//添加时间段 
				
				todo.push({task.first + 100, 1});//100秒后开启 
			}
		}else{
			if(!opening){
				//开启
				opening = 1;
				t = task.first; 
			}
		}
	}
	
	if(opening){
		//别忘了最后一段
		tt += (T - t); 
	}
	
	cout<<tt;
} 
原文
存档评论1 条抓取于