【集合】CFdiv.2#600B Silly Mistake
题目链接:https://codeforces.com/contest/1253/problem/B
B. Silly Mistake time
limit per test1 second
memory limit per test256
megabytes inputstandard input outputstandard output 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.Examples
inputCopy
6 1 7 -7 3 -1 -3
outputCopy
1 6
inputCopy
8
1 -1 12 -1 -2 3 -3
outputCopy
2 2 6
inputCopy
6
2 5 -5 5 -5 -2
outputCopy
-1
inputCopy
3
-8 1 1
outputCopy
-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] and [1,2,−1,−2,3,−3] (d=2 and c=[2,6]). The only
other valid solution would be to split the array into [1,−1],
[1,2,−1,−2] and [3,−3] (d=3 and 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.
第一次用到unordered_set,纪念一下
一个set“s
”判断该数是否进入,一个set"v
"判断该数是否进过
由于unordered_set
的性质,每次count(x)
时间复杂度为O(1),可以很快地在集合中进行查找(用set
也可以,快logn
倍),刚开始是用vis[N]来判断是否进过,加了memset
然后TLE,困得不行就去睡了(从不畏惧掉分),另外给出一种数组模拟集合的做法(感谢楠神支持)
unordered_set版:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;unordered_set<int> v, s;int n, x;
vector<int> ans;
int main()
{cin >> n;int f = 0;int cnt = 0, cot = 0;for (int i = 1; i <= n; i++){cin >> x;if (!s.size()){cnt++;v.clear();if (cot){ans.push_back(cot);cot = 0;}}if (x > 0 && !v.count(x) && !s.count(x))v.insert(x), s.insert(x);else if (x < 0 && s.count(-x))s.erase(-x);elsef = 1;cot++;}if (cot)ans.push_back(cot);if (s.size() || f){puts("-1");return 0;}else{cout << cnt << endl;for (int i = 0; i < cnt; i++){cout << ans[i] << " ";}cout << endl;}//system("pasue"); //记得注释掉,会REreturn 0;
}
恪楠大佬的数组模拟集合
#include <bits/stdc++.h>
using namespace std;const int MAXN = 1e6 + 50;int vis1[MAXN], vis2[MAXN];
// is1是否入队。vis2是否有过一次。
vector<int> ans, jl;int main()
{int n;int cnt = 0, size = 0, flag = 1;scanf("%d", &n);for (int i = 1; i <= n; i++){int t;scanf("%d", &t);if (t > 0){if (vis2[t]){flag = 0;}jl.push_back(t);vis1[t] = 1;vis2[t] = 1;size++;cnt++;}else{t = -t;if (!vis1[t]){ //cout << i << " " << vis1[t] << " ";flag = 0;}else{cnt--;size++;vis1[t] = 0;if (!cnt){ans.push_back(size);for (int j = 0; j < jl.size(); j++)vis2[jl[j]] = 0;size = 0;}}}}if (cnt == 0 && size)ans.push_back(size);if (cnt)flag = 0;if (flag){printf("%d\n", ans.size());for (int i = 0; i < ans.size(); i++){printf("%d ", ans[i]);}}elseprintf("-1");return 0;
}
【集合】CFdiv.2#600B Silly Mistake相关推荐
- Deep Learning Trends @ ICLR 2016:深度学习趋势@ICLR2016(译)
Preface 这是一篇译文,原文作者是Tomasz Malisiewicz大神,这是他在博客Tombone's Computer Vision Blog的文章,一发出来就引起这个圈子的广泛关注. ...
- 分享成为高效程序员的7个重要习惯
作者:Phil Chu 作为软件工程师,你希望从工作中获得的是:稳定的薪水.参与好项目的机会.好工作的跳板或只是和其他程序师成为好基友.这里的"高效",我指的是按时完符合要求的项目 ...
- 要记住的Facepalm:我在未先测试SDK的情况下对其进行了改进。
by Rahul Chowdhury 通过拉胡尔·乔杜里 要记住的Facepalm:我在未先测试SDK的情况下对其进行了改进. (A Facepalm to Remember: I bumped up ...
- 在CSS中使用not:first-child选择器
Introduction: 介绍: Well, selectors are a very common term to deal with while we are developing a webs ...
- What's New in C# 6.0(转)
原文地址:http://www.codeproject.com/Tips/1023426/Whats-New-in-Csharp 本来想翻译一下贴出来,但是好像很多语言组织起来比较困难,读书少不会表达 ...
- 英语3500词(五)Who is Your Favorite Athlete (2022.1.17)
athlete 1 n. 运动员,擅长运动的人 athletic 1 adj. 体育运动的,健壮的 用法搭配 an athletic man 健壮的男子 sports meeting = athlet ...
- 《高效的项目和团队》
Productive Projects and Teams是一本好书. 许多其中许多关于管理和沟通的精辟言论让我大有相见很晚之感.其实不仅是软件的开发项目,任何项目,甚至任何行业的管理,都首先是对人的 ...
- 阅读_分享成为高效程序员的7个重要习惯
Tags:优化编程,把工作当真,理解代码,理解需求,高效程序员 作者:Phil Chu 作为软件工程师,你希望从工作中获得的是:稳定的薪水.参与好项目的机会.好工作的跳板或只是和其他程序师成为好基友. ...
- Data Visualization – Banking Case Study Example (Part 1-6)
python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...
- python3 搜索附近蓝牙
本来想用java写,奈何java太繁琐,python 功能包真丰富啊 依赖包 pip install -i https://mirrors.aliyun.com/pypi/simple/ pyblue ...
最新文章
- 通过WebService调用SQLXML(SQL Server 2005) [ZT]
- loj #6053 简单的函数 min_25筛
- TCP报文段首部格式详解
- 根据端口不同来切换站点_KVM切换器是什么,看懂这一篇就够
- c语言计算机二级考试要点,全国计算机二级考试c语言考试要点
- HEVC/H265 HM10.0 分析(三)TAppDecTop.cpp
- oracle正在启动或关闭中的解决方法
- (26)IMPCAT软件bit文件下载流程(FPGA不积跬步101)
- python 输出文字_Python中输出ASCII大文字、艺术字、字符字小技巧
- 数字水印--给我的文件充当保护神
- 专业学习经验交流会成功举行
- 机械行业想转行IT互联网行业,大家有什么好的建议?
- 正斜杠 “/” 与反斜杠 “\”辨析
- 强推:raw图片处理软件DxO PhotoLab
- 通过fork来剖析Linux内核的内存管理和进程管理(下)
- 第一章 初识Java总结
- 西数USB硬盘 WD10JMVW-11AJGS 4数据恢复步骤
- 计算机最优配置,2019年度电脑配置最优选择,这些CPU、显卡、SSD不容错过!
- 股豆网:2019全国高校名单公布共计2956所 江苏省数量最多
- Android-分享