题解 CF1253B

题解 CF1253B

这个题是一个模拟题

只需要注意几点:

1. 同一天同一个人只能进入一次

2. 同一天同一个人只能出去一次

3. 一天中一个人没进来就不能出去

然后我们用 vis 数组维护每个人进入的日期,用 out 数组维护每个人出去的日期

如果一个人是今天进来的,又进来一次,不合法

如果一个人是今天进来的,今天出去的,又出去一次,不合法

如果一个人今天没进来,今天出去的,不合法

ps: vis 数组和 out 数组都要开 a 的值域大小,不然会 RE ,同时注意是负数的时候,下标为其相反数

// #Tyrue#
#include<map>
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
inline int read(){
    int 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*10+ch-'0',ch=getchar();
    return x*f;
}
const int N=1e6+10;
int T,n,date=1,tot,cnt;
int a[N],vis[N],p[N],out[N];
int main(){
    n=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
    }
    for(int i=1;i<=n;i++){
        if(a[i]>0){
        	if(vis[a[i]]==date){
				puts("-1");
            	return 0;
            }
            vis[a[i]]=date;
            cnt++;
        }else{
            if(vis[-a[i]]==date && out[-a[i]]!=date){
                cnt--;
                out[-a[i]]=date;
            }else{
                puts("-1");
                return 0;
            }
        }
        if(!cnt){
            p[++tot]=i;
            date++;
        }
    }
    if(cnt){
        puts("-1");
        return 0;
    }
    printf("%d\n",tot);
    for(int i=1;i<=tot;i++){
        printf("%d ",p[i]-p[i-1]);
    }
    return 0;
}
posted @ 2022-11-30 09:01  Tyrue  阅读(9)  评论(0编辑  收藏  举报