B. Silly Mistake(Codeforces Round #600 (Div. 2))

B. Silly Mistake(Codeforces Round #600 (Div. 2))

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

Description

The Central Company has an office with a sophisticated security system. There are 1 0 6 10^6 employees, numbered from 1 1 to 1 0 6 10^6 .

The security system logs entrances and departures. The entrance of the i i -th employee is denoted by the integer i i , while the departure of the i i -th employee is denoted by the integer i −i .

The company has some strict rules about access to its office:

An employee can enter the office at most once per day.
He obviously can’t leave the office if he didn’t enter it earlier that day.
In the beginning and at the end of every day, the office is empty (employees can’t stay at night). It may also be empty at any moment of the day.
Any array of events satisfying these conditions is called a valid day.

Some examples of valid or invalid days:

  • [ 1 , 7 , 7 , 3 , 1 , 3 ] [1,7,−7,3,−1,−3] is a valid day ( 1 1 enters, 7 7 enters, 7 7 leaves, 3 3 enters, 1 1 leaves, 3 3 leaves).
  • [ 2 , 2 , 3 , 3 ] [2,−2,3,−3] is also a valid day.
  • [ 2 , 5 , 5 , 5 , 5 , 2 ] [2,5,−5,5,−5,−2] is not a valid day, because 5 5 entered the office twice during the same day.
  • [ 4 , 4 ] [−4,4] is not a valid day, because 4 4 left the office without being in it.
  • [ 4 ] [4] is not a valid day, because 4 4 entered the office and didn’t leave it before the end of the day.

There are n n events a 1 , a 2 , , a n a_1,a_2,…,a_n , in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn’t change the order of the events.

You must partition (to cut) the array a a of events into contiguous subarrays, which must represent non-empty valid days (or say that it’s impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day.

For example, if n = 8 n=8 and a = [ 1 , 1 , 1 , 2 , 1 , 2 , 3 , 3 ] a=[1,−1,1,2,−1,−2,3,−3] then he can partition it into two contiguous subarrays which are valid days: a = [ 1 , 1 1 , 2 , 1 , 2 , 3 , 3 ] a=[1,−1 | 1,2,−1,−2,3,−3] .

Help the administrator to partition the given array a a in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts.

Input

The first line contains a single integer n ( 1 n 1 0 5 ) n (1≤n≤10^5) .

The second line contains n n integers a 1 , a 2 , , a n ( 1 0 6 a i 1 0 6 a_1,a_2,…,a_n (−10^6≤a_i≤10^6 and a i 0 ) a_i≠0) .

Output

If there is no valid partition, print 1 −1 . Otherwise, print any valid partition in the following format:

On the first line print the number d d of days (1≤d≤n).
On the second line, print d integers c 1 , c 2 , , c d ( 1 c i n c_1,c_2,…,c_d (1≤c_i≤n and c 1 + c 2 + + c d = n ) c_1+c_2+…+c_d=n) , where c i c_i is the number of events in the i i -th day.
If there are many valid solutions, you can print any of them. You don’t have to minimize nor maximize the number of days.

input

6
1 7 -7 3 -1 -3

output

1
6

input

8
1 -1 1 2 -1 -2 3 -3

output

2
2 6

input

6
2 5 -5 5 -5 -2

output

-1

input

3
-8 1 1

output

-1

Note

In the first example, the whole array is a valid day.

In the second example, one possible valid solution is to split the array into [ 1 , 1 ] [1,−1] and [ 1 , 2 , 1 , 2 , 3 , 3 ] ( d = 2 [1,2,−1,−2,3,−3] (d=2 and c = [ 2 , 6 ] ) c=[2,6]) . The only other valid solution would be to split the array into [ 1 , 1 ] , [ 1 , 2 , 1 , 2 ] [1,−1], [1,2,−1,−2] and [ 3 , 3 ] ( d = 3 [3,−3] (d=3 and c = [ 2 , 4 , 2 ] ) c=[2,4,2]) . Both solutions are accepted.

In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.

题意

有一个公司,每天有员工进出。

公司对进入办公室有一些严格的规定:

  • 员工每天最多只能进入一次办公室。

  • 如果那天他没有进办公室的话,他显然不能离开。

  • 每天开始和结束时,办公室都是空的(员工不能呆在晚上)。它也可能在一天中的任何时候都是空的。

任何满足这些条件的事件数组都称为有效日。

  • [ 1 , 7 , 7 , 3 , 1 , 3 ] [1,7,−7,3,−1,−3] 是有效的一天 ( 1 1 进入, 7 7 进入, 7 7 离开, 3 3 进入, 1 1 离开, 3 3 离开)。
  • [ 2 , 2 , 3 , 3 ] [2,−2,3,−3] 也是有效的一天。
  • [ 2 , 5 , 5 , 5 , 5 , 2 ] [2,5,−5,5,−5,−2] 不是有效的一天,因为 5 5 在同一天进入了两次。
  • [ 4 , 4 ] [−4,4] 也不是有效的一天,因为 4 4 在进入之前离开了。
  • [ 4 ] [4] 不是有效的一天,因为 4 4 进入了办公室但是没有离开。

您必须将事件数组 a a 分割为相邻的子数组,子数组必须表示非空的有效日期(或者说这是不可能的)。每个数组元素应该正好属于一个分区的一个相邻子数组。分区的每个相邻子数组都应该是有效的一天。

题解

边遍历边审查,一个一个来。

代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#define _for(i, a) for(int i = 0; i < (a); ++i)
#define _rep(i, a, b) for(int i = (a); i <= (b); ++i)
using namespace std;

int n;
vector<int> a, ans;
map<int, bool> mp, mmp;

void init() {
	a.clear();	//原始数组
	ans.clear();//答案数组
	mp.clear();	//对拼映射
	mmp.clear();//单个日期内的事件映射
}

bool sol() {
	a.resize(n);
	_for(i, n) cin >> a[i];
	int i = 0;
	while (i < n) {
		if (mmp.count(a[i])) return 0;	//如果当前日期还没消完又出现了该数字,就认定失败
		mmp[a[i]] = 1;	//把该数字归入当前日期,然后开始审查该日期是否合法
		if (mp.count(-a[i])) {	//检查它的相反数是否存在
			if (a[i] < 0) mp.erase(-a[i]);	//该数字合法,把他的相反数“对拼”掉
			else return 0;	//如果它的相反数存在,但是负数比它更早出现,就认定失败
		}
		else mp[a[i]] = 1;	//相反数不存在,则该数字合法

		if (!mp.size() && mmp.size()) {	//检查是否可以截断日期-判断条件是如果所有的数字都对拼掉了,而且日期内有事件
			ans.push_back(mmp.size());
			mmp.clear();
		}
		i++;
	}
	if(!mp.size()) return 1;
	else return 0;	//如果所有的数字都处理完了还没完全对拼掉,则认定失败
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//freopen("in.txt", "r", stdin);

	while (cin >> n) {
		init();
		if (sol() == 0) cout << -1 << "\n";
		else {
			cout << ans.size() << "\n";
			_for(i, ans.size()) cout << ans[i] << (i == ans.size() - 1 ? "\n" : " ");
		}
	}
	return 0;
}
发布了163 篇原创文章 · 获赞 54 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_42856843/article/details/103107111