CF1253B Silly Mistake - 1400

传送门

题目思路

由于“无需最小化或最大化天数”,所以很容易有一个贪心的做法:当办公室内人数变为 \(0\) 时,可以在此处划分为一天。

无解的情况有多种。注意,同一天一个人离开了又来了也算是无解,对应样例 \(3\)

代码中的注释会更详细一点。

AC Code

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,t[N],nw,cnt;
//t[]:桶 
//nw:当天有几个人还没做 
//cnt:当天的事件数
vector<int>ans;
unordered_map<int,int>mp;
//mp[]:当天某个人有没有出现过 
signed main(){
	ios::sync_with_stdio(0),cin.tie(0);
	cin>>n;
	for(int i=1,x;i<=n;i++){
		cin>>x;
		cnt++;
		if(x<0){
			x=-x;
			if(t[x]==0)	return cout<<-1,0;
			t[x]--;nw--;
			if(nw==0){//新的一天 
				mp.clear();
				ans.push_back(cnt);
				cnt=0;
			}
		}
		else{
			if(t[x]>0||mp.count(x))	return cout<<-1,0;
			//mp.count(x)含义:x一天来了两次 
			t[x]++;
			nw++;
			mp[x]=1;
		}
		if(i==n&&cnt!=0)	ans.push_back(cnt);
	}
	if(nw!=0)	return cout<<-1,0;
	cout<<ans.size()<<"\n";
	for(int u:ans)	cout<<u<<" ";
	return 0;
}
posted @ 2026-05-19 17:11  深申  阅读(11)  评论(0)    收藏  举报