B题居然是卡我时间最长的题卡了快一个小时,真的想复杂了,对于这种模拟题就是得思路清晰,然后理清思路再去写。
题意就是 把一堆数字分开,满足同一个数先出现正数后出现负数,并且只有一次,输出每个划分区间的大小。这道题卡掉很多人因为没有考虑划分的合法性。
AC代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", (n))
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
inline int read()
{
int ret = 0, sgn = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')
sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
ret = ret*10 + ch - '0';
ch = getchar();
}
return ret*sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
}
int qpow(int m, int k, int mod)
{
int res = 1, t = m;
while (k)
{
if (k&1)
res = res * t % mod;
t = t * t % mod;
k >>= 1;
}
return res;
}
int t;
int n,m;
int i,j,k;
map<int,int>pos;
map<int,int>res;
int flag=0;
int cnt=0;
int ch[510000]= {0},p=0;
int main()
{
int n;
scanf("%d",&n);
for(i=1; i<=n; i++)
{
int t;
scanf("%d",&t);
if(t>0)
{
if(pos[t]!=0)
{
flag=1;
}
else
{
if(res[t]!=0)
{
flag=1;
}
pos[t]++;
res[t]++;
cnt++;
}
}
else
{
t=-t;
if(pos[t]==0)
{
flag=1;
}
else
{
pos[t]--;
cnt--;
if(cnt==0)
{
ch[++p]=i;
res.clear();
}
}
}
}
if(flag==1||cnt!=0)
printf("-1\n");
else
{
ch[0]=0;
printf("%d\n",p);
printf("%d",ch[1]-ch[0]);
for(int i=2; i<=p; i++)
printf(" %d",ch[i]-ch[i-1]);
printf("\n");
}
return 0;
}