突然发现百度不到这题的单独题解(果然是因为这是水题么),那我就来写一个了~

先把题给贴了。

C. Petya and Catacombs
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard 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?

Input

The 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.

Output

In the only line print a single integer — the minimum possible number of rooms in Paris catacombs.

Examples
input
2
0 0
output
2
input
5
0 1 0 1 3
output
3
Note

In 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,思路)的更多相关文章

  1. 886C. Petya and Catacombs#墓室探险(set集合)

    题目出处:http://codeforces.com/problemset/problem/886/C 题目大意:很多墓穴之间有通道,探险家来回穿梭并记录日志 日志规则:第一次到该墓穴计时间t,0&l ...

  2. Codeforces 890C - Petya and Catacombs 模拟

    C. Petya and Catacombstime limit per test1 secondmemory limit per test256 megabytesinputstandard inp ...

  3. 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 ...

  4. 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 ...

  5. CF886C Petya and Catacombs

    题目描述 A very brave explorer Petya once decided to explore Paris catacombs. Since Petya is not really ...

  6. 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 ...

  7. 【Codeforces Round #445 (Div. 2) C】 Petya and Catacombs

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 看看时间戳为i的点有哪些. 每次优先用已经访问过的点. 如果不行就新创一个点. 注意新创点的时间戳也是i. [代码] #includ ...

  8. codeforce B. Petya and Exam

    wa一万次难受. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm& ...

  9. cf519D . A and B and Interesting Substrings 数据结构map

    题意: 已知26个小写字母有各自的权值(正,负,或0) 现在给出一个字符串,长度<=1e5 问这个字符串有多少个子串满足: 开头的字母和结尾的字母一样 字符串除了开头和结尾的字母外,其余的字母的 ...

随机推荐

  1. Collections集合工具类的方法

    addAll & shuffle: 返回类型为boolean类型,执行完操作不接收也行: 其中,静态方法,与对象无关,类名点方法名直接调用: 点点点为可变参数,随便填写几个参数都可以: sor ...

  2. flask 单个表单多个提交按钮

    单个表单多个提交按钮 在某些情况下,可能需要为一个表单添加多个提交按钮.比如在创建文章的表单中添加发布按钮和存草稿的按钮.当用户提交表单时,需要在视图函数中根据按下的按钮来做出不同的处理. 下面例子中 ...

  3. C# 声明隐式类型的局部变量

    在c#中赋值给变量的值必须具有和变量相同的类型.如int值赋给int变量,c#编译器可以迅速判断变量初始化表达式的类型,如果变量类型不符,就会明确告诉你. 提示需要强制转换(例如在char中不允许使用 ...

  4. foreach 语句

    foreach  语句很适合用来枚举   如数组.列表.集合之类的数据结构中的元素.  不必准确知道元素个数.如果基数据不包含任何元素,则foreach循环不执行 foreach(<元素> ...

  5. 转:【专题八】P2P编程

    引言: 前面的介绍专题中有朋友向我留言说介绍下关于P2P相关的内容的,首先本人对于C#网络编程也不是什么大牛,因为能力的关系,也只能把自己的一些学习过程和自己的一些学习过程中的理解和大家分享下的,下面 ...

  6. git从安装到使用

    一.Git简介 Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制 ...

  7. kali linux常用软件配置记录

    首先膜一波,认真细致,简明有效. 感谢原博主的分享,留作参考. https://www.cnblogs.com/youfang/p/5272746.html

  8. shell 多行注释 块注释

    转自 https://www.cnblogs.com/emanlee/p/3749911.html 1 : ' 被注释的多行内容 ' 2 :<<eof 被注释的多行内容 eof 3 :&l ...

  9. Linux笔记 #08# shell编程从零开始到低配学生管理系统

    先熟悉一下基本语法(运行环境是装git的时候一起装的那个windows下的bash): #!/bin/bash # 实现两个函数 # appendToFile()追加一行到文件 # readFile( ...

  10. dubbo spring pom文件报错:提示no declaration can be found for element 'dubbo:service'.

    pom文件报错:The matching wildcard is strict, but no declaration can be found for  element 'dubbo:service ...