【CodeForces 1253B --- Silly Mistake】

Description

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

The security system logs entrances and departures. The entrance of the i-th employee is denoted by the integer i, while the departure of the i-th employee is denoted by the integer −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] is a valid day (1 enters, 7 enters, 7 leaves, 3 enters, 1 leaves, 3 leaves).
  • [2,−2,3,−3] is also a valid day.
  • [2,5,−5,5,−5,−2] is not a valid day, because 5 entered the office twice during the same day.
  • [−4,4] is not a valid day, because 4 left the office without being in it.
  • [4] is not a valid day, because 4 entered the office and didn’t leave it before the end of the day.

There are n events a1,a2,…,an, 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 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 and 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].

Help the administrator to partition the given array 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≤105).

The second line contains n integers a1,a2,…,an (−106≤ai≤106 and ai≠0).

Output

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

On the first line print the number d of days (1≤d≤n).
On the second line, print d integers c1,c2,…,cd (1≤ci≤n and c1+c2+…+cd=n), where ci is the number of events in the 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.

Sample Input

6
1 7 -7 3 -1 -3

Sample Output

1
6

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
using ll = long long;
const int inf = 0x3f3f3f3f;
const ll INT = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e6+5;
bool in[MAXN];
int last[MAXN];int main()
{SIS;int n,x,cnt=0,y=0;bool flag=false;vector<int> v;cin >> n;for(int i=0;i<n;i++){cin >> x;if(x>0){if(in[x]) flag=true;if(y<last[x]) flag=true;cnt++;in[x]=true;}else{if(!in[-x]) flag=true;cnt--;in[-x]=false;last[-x]=i;}if(cnt==0) v.emplace_back(i+1),y=i;} if(cnt) flag=true;if(flag) cout << -1 << endl;else{int len=v.size();cout << len << endl << v[0];for(int i=1;i<len;i++) cout << " " << v[i]-v[i-1];cout << endl;}return 0;
}

【CodeForces 1253B --- Silly Mistake】相关推荐

  1. 【CodeForces 332B --- Maximum Absurdity】递推

    [CodeForces 332B --- Maximum Absurdity]递推 题目来源:点击进入[CodeForces 332B - Maximum Absurdity] Description ...

  2. 【CodeForces 1255D --- Feeding Chicken】

    [CodeForces 1255D --- Feeding Chicken] Description Long is a huge fan of CFC (Codeforces Fried Chick ...

  3. 【CodeForces 1255B --- Fridge Lockers】

    [CodeForces 1255B --- Fridge Lockers] Description Hanh lives in a shared apartment. There are n peop ...

  4. 【CodeForces 1257C --- Dominated Subarray】

    [CodeForces 1257C --- Dominated Subarray] Description Let's call an array t dominated by value v in ...

  5. 【CodeForces 1253C --- Sweets Eating】DP

    [CodeForces 1253C --- Sweets Eating]DP Description Tsumugi brought n delicious sweets to the Light M ...

  6. 【Codeforces #167 Div1 Div2】Solutions

    [A. Dima and Friends] http://www.codeforces.com/contest/272/problem/A 题目大意:n+1个人出手指头,加起来再做类似约瑟夫的出圈,问 ...

  7. 【Codeforces Gym - 101635C Macarons 】【矩阵快速幂+状压】【dfs时间换空间】

    [链接] http://codeforces.com/gym/101635/attachments [题意] 求用1*1,1*2的方格填n*m的矩阵的方法数 [知识点] 状压dfs+矩阵快速幂 [分析 ...

  8. 【Codeforces Round #438 C】 Qualification Rounds

    [链接]h在这里写链接 [题意] 给你n个问题,每个人都知道一些问题. 然后让你选择一些问题,使得每个人知道的问题的数量,不超过这些问题的数量的一半. [题解] 想法题. 只要有两个问题. 这两个问题 ...

  9. 【Codeforces Global Round 23】B. Rebellion

    Codeforces Global Round 23中B. Rebellion Codeforces比赛记录 文章目录 题目链接: 一.B. Rebellion 题目意思: 上思路: 总结 B. Re ...

  10. 【Codeforces 321E / BZOJ 5311】【DP凸优化】【单调队列】贞鱼

    目录 题意: 输入格式 输出格式 思路: DP凸优化的部分 单调队列转移的部分 坑点 代码 题意: 有n条超级大佬贞鱼站成一行,现在你需要使用恰好k辆车把它们全都运走.要求每辆车上的贞鱼在序列中都是连 ...

最新文章

  1. 用XMing + Putty 凿出让Linux 图形界面在Windows裸奔的隧道
  2. FactoryMethod工厂方法模式升级成AbstractFactory抽象工厂模式
  3. java 基本类型内存_java基本数据类型、内存分析、装包拆包
  4. malloc原理和内存碎片
  5. java使用POI工具类导出excel
  6. Spring源码-applicationcontext.xml解析过程
  7. 【深入理解JVM】ClassLoader类加载机制
  8. oracle必须声明标识符函数,引用变量时需要必须声明标识符
  9. 放大镜_小学科学实验器材使用规范之放大镜
  10. 口罩人脸检测与分类开源代码汇总
  11. 查看数据库开了inodb_Mysql中查看表的类型InnoDB
  12. linux 锐捷客户端登录密码,Linux使用经验_使用锐捷客户端登录校园网
  13. 一些Winodws mobile相关资料的整理
  14. 无线点菜服务器英文怎么拼,无线点菜系统(客户端和服务器端交互)
  15. 计算机学风建设新闻稿,第十一届学风建设月动员大会新闻稿_
  16. 图像存储的地理坐标位置信息
  17. 单、双精度浮点型转换为十六进制方法
  18. STM32的SWD调试方式
  19. matlab之列主元素高斯消去法
  20. Jupyter股票分析

热门文章

  1. 【报错记录】解决Shell脚本报ambiguous redirect
  2. word文档插入尾注,并且设置尾注在参考文献这一章
  3. 随机过程 Class 3 条件期望
  4. linux V4L2子系统——v4l2架构(1)之整体架构
  5. Labview_QMH模板解析
  6. cpuz测试分数天梯图_2019年CPU单核跑分天梯图V1.22版(190712)
  7. C语言如何区别素数和合数,怎么快速记住100以内的素数和合数
  8. java 银行卡归属地查询_银行卡归属地查询示例代码
  9. 400企业智能服务器,全球领先的企业级服务器、存储、融合系统及解决方案-H3C与HPE...
  10. c语言 tls单向认证 验证证书,使用wireshark观察SSL/TLS握手过程--双向认证/单向认证...