codeforce 886C Petya and Catacombs (map,思路)
突然发现百度不到这题的单独题解(果然是因为这是水题么),那我就来写一个了~
先把题给贴了。
C. Petya and Catacombstime limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
A very brave explorer Petya once decided to explore Paris catacombs. Since Petya is not really experienced, his exploration is just walking through the catacombs.
Catacombs consist of several rooms and bidirectional passages between some pairs of them. Some passages can connect a room to itself and since the passages are built on different depths they do not intersect each other. Every minute Petya arbitrary chooses a passage from the room he is currently in and then reaches the room on the other end of the passage in exactly one minute. When he enters a room at minute i, he makes a note in his logbook with number ti:
- If Petya has visited this room before, he writes down the minute he was in this room last time;
- Otherwise, Petya writes down an arbitrary non-negative integer strictly less than current minute i.
Initially, Petya was in one of the rooms at minute 0, he didn't write down number t0.
At some point during his wandering Petya got tired, threw out his logbook and went home. Vasya found his logbook and now he is curious: what is the minimum possible number of rooms in Paris catacombs according to Petya's logbook?
InputThe first line contains a single integer n (1 ≤ n ≤ 2·105) — then number of notes in Petya's logbook.
The second line contains n non-negative integers t1, t2, ..., tn (0 ≤ ti < i) — notes in the logbook.
OutputIn the only line print a single integer — the minimum possible number of rooms in Paris catacombs.
Examplesinput2
0 0output2input5
0 1 0 1 3output3NoteIn the first sample, sequence of rooms Petya visited could be, for example 1 → 1 → 2, 1 → 2 → 1 or 1 → 2 → 3. The minimum possible number of rooms is 2.
In the second sample, the sequence could be 1 → 2 → 3 → 1 → 2 → 1.
我来讲讲我的思路吧。
题意就是,有一堆可以互相连通的房间,一个人在0时刻处于0号房间。他在走到下一个房间后会记录下一个数字。数字有两种情况:
1、在第i秒走到一个新的房间(以前没走到过)时,就随便记录一个比i小的数。
2、要是走到的是以前到过的房间,那么就记录下上次走到那个房间的时间。
他给出记录的数字,要求出符合描述的最少房间是多少。
对于这道题,每一个数字都要有两种情况的考虑—是新的房间呢?还是之前走过的呢?。因为要求房间尽量少,所以我们优先考虑情况2.
如果存在满足2条件的房间,那么就看成这个数字代表的是去到这个房间。若找不到这样的房间,就只能是情况1了。
我是用map来表示了是否存在最后逗留时间为某i秒的房间。但其实用一个bool类型的一位数组应该就可以了。
#include<iostream>
#include<map>
using namespace std;
map<int,int> mp;
int main()
{
int i,j,ans,n,k;
cin>>n;
ans=;
mp.clear();
mp[]++;
for(i=;i<=n;i++)
{
cin>>k;
if(mp[k]!=)
{
mp[k]--;
mp[i]++;
}
else
{
mp[i]++;
ans++;
}
}
cout<<ans;
}
codeforce 886C Petya and Catacombs (map,思路)的更多相关文章
- 886C. Petya and Catacombs#墓室探险(set集合)
题目出处:http://codeforces.com/problemset/problem/886/C 题目大意:很多墓穴之间有通道,探险家来回穿梭并记录日志 日志规则:第一次到该墓穴计时间t,0&l ...
- Codeforces 890C - Petya and Catacombs 模拟
C. Petya and Catacombstime limit per test1 secondmemory limit per test256 megabytesinputstandard inp ...
- Codeforces Round #445 C. Petya and Catacombs【思维/题意】
C. Petya and Catacombs time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforce 886 Технокубок 2018 - Отборочный Раунд 3 C. Petya and Catacombs(结论题)
A very brave explorer Petya once decided to explore Paris catacombs. Since Petya is not really exper ...
- CF886C Petya and Catacombs
题目描述 A very brave explorer Petya once decided to explore Paris catacombs. Since Petya is not really ...
- Codeforces Beta Round #94 div 1 D Numbers map+思路
D. Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- 【Codeforces Round #445 (Div. 2) C】 Petya and Catacombs
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 看看时间戳为i的点有哪些. 每次优先用已经访问过的点. 如果不行就新创一个点. 注意新创点的时间戳也是i. [代码] #includ ...
- codeforce B. Petya and Exam
wa一万次难受. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm& ...
- cf519D . A and B and Interesting Substrings 数据结构map
题意: 已知26个小写字母有各自的权值(正,负,或0) 现在给出一个字符串,长度<=1e5 问这个字符串有多少个子串满足: 开头的字母和结尾的字母一样 字符串除了开头和结尾的字母外,其余的字母的 ...
随机推荐
- STL容器之map
[1]map容器 map 是关联容器.容器中的每一个元素都是由一个键值和一个数据值组成的. set 是一个集合它以其元素作为键值(同一个键值只能出现一次),且默认以升序排列. list 是一个顺序容器 ...
- qt5 移植 交叉编译出现错误
类似这样的错误,当时没有完整的记下来,undefined reference to `std::__detail::_List_node_base@GLIBCXX_3.4.10 当时是在编译qt5cl ...
- Linux基础命令---ifconfig
ifconfig ifconfig指令用来配置网络接口参数,同时还可以显示当前内核网络接口的工作状态.如果没有提供参数,则ifconfig将显示当前活动接口的状态.如果给定单个接口参数,则只显示给定接 ...
- ztree使用实例
一.当某节点被选择或被取消选择时获取所有被选择的节点: <link rel="stylesheet" href="./static/libs/ztree/css/m ...
- LNMP的并发配置和资源分配
PHP程序性能如何?程序的并发可以达到多少?程序的瓶颈在哪儿?为了满足业务需求应该购买多少台服务器?负载均衡中php应用服务器需要多少台? LNMP中的N是nginx充当Web Server 内容的分 ...
- 前端框架VUE----学习vue前的准备工作
起步 1.扎实的HTML/CSS/Javascript基本功,这是前置条件. 2.不要用任何的构建项目工具,只用最简单的<script>,把教程里的例子模仿一遍,理解用法.不推荐上来就直接 ...
- linux系统Centos环境下搭建SVN服务器及权限配置
linux系统Centos环境下如何搭建SVN服务器以及svnserve.conf.authz.passwd配置文件详细介绍 至于svn的概念,这里就不做详细阐述了,可以自行百度.简单来讲就是一个 ...
- ltp-fcntl36 偶尔出现fail unexpected data offset 20928 value 94
每次出错的都是和posix相关先把结论说了: fn_ofd_w和fn_ofd_r的SAFE_FCNTL参数F_OFD_SETLKW fn_posix_w和fn_posix_r的SAFE_FCNTL参数 ...
- flask模板,路由,消息提示,异常处理
1.flask的路由与反向路由 from flask import Flask, request, url_for app = Flask(__name__) @app.route('/') def ...
- fjwc2019 D1T2 原样输出(后缀自动机+dp)
#179. 「2019冬令营提高组」原样输出 暴力对每个串建后缀自动机,然后暴力枚举每个自动机的子串.可以拿到部分分. 然鹅我们可以把每个后缀自动机连起来. 我们知道,后缀自动机是用最少的点(空间)表 ...