传送门
题意:n个人,每天只能进出办公室一次;现在给一个数组,问能分成几天,例如: 8 (1 -1 1 2 -1 -2 3 -3) 可以分成:(1 -1) , (1 2 -1 -2) , (3 -3) 3天,或者 (1 -1) , (1 2 -1 -2 3 -3) 2天 分析:直接模拟 map 大法好 ,这里是用map模拟和标记,普通数组标记貌似会T #include <set> #include <map> #include <cmath> #include <stack> #include <queue> #include <string> #include <vector> #include<cstring> #include <stdio.h> #include <iostream> #include <algorithm> #define INF 0x3f3f3f3f using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn=1e5+5; int t,n; map<int,int>mp,m; int a[maxn],b[maxn],tot; int main() { cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; int ans=0,cnt=0; bool flag=false; for(int i=1;i<=n;i++) { if(mp.count(a[i])==0&&a[i]>0) mp[a[i]]++,cnt++; else if(a[i]<0&&mp.count(-a[i])==1&&m.count(-a[i])==0) { mp.erase(-a[i]); m[-a[i]]=1; if(mp.empty()){ b[tot++]=cnt*2,cnt=0,ans++; m.clear(); } } else flag=true; } while (!mp.empty()){ flag=true; mp.clear(); } if(flag){printf("-1");return 0;} printf("%d\n",ans); for(int i=0;i<tot;i++) printf("%d ",b[i]); return 0; }