C. Petya and Catacombs
time 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?

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停止,进入下一个循环

代码:

 #include <bits/stdc++.h>
using namespace std;
int ans[],vis[];
int main() {
memset(vis,,sizeof(vis));
int n,num=;
cin>>n;
for(int i=;i<=n;++i) {
scanf("%d",&ans[i]);
}
for(int i=n;i>=;i--) {
if(vis[i]) continue;
num++;vis[i]=;
int j=i;
while(ans[j]!=||vis[ans[j]]==) {
vis[ans[j]]=;
j=ans[j];
}
}
cout<<num<<endl;
return ;
}

最新文章

  1. kafka Failed to send messages after 3 tries 问题解决
  2. mac上eclipse上配置hadoop
  3. Fedora 17 修改GRUB启动菜单顺序
  4. android studio 偶记
  5. struts2 Action 接收参数的三种方法
  6. TCP四个计数器
  7. 使用 libdvm.so 内部函数dvm* 加载 dex
  8. surging 微服务框架使用系列之surging介绍
  9. C++(2):错误:undefined reference to `__gxx_personality_v0&#39;
  10. 涨姿势:Mysql 性能优化完全手册
  11. 实验七:Xen环境下cirrOS的安装配置
  12. 使用Consul 实现 MagicOnion(GRpc) 服务注册和发现
  13. 如何用fiddler + 手机设置无线代理 下载只有 手机才能访问的资源。
  14. linux df查看硬盘使用量 du查看文件所占大小
  15. springcloud 入门 1 (浅谈版本关系)
  16. jQuery UI dialog插件出错信息:$(this).dialog is not a function
  17. YUI JS压缩Ant脚本
  18. bzoj 4927: 第一题
  19. SQLHappy微软数据库连接查询操作,对数据的处理和查询
  20. linux下修改了tomcat端口之后无法访问

热门文章

  1. JDBC连接池-C池3P0连接
  2. A Simple Game
  3. Android 开发笔记___基本适配器的使用__BaseAdapter
  4. jQuery选择器(添加节点及删除节点及克隆及替换及包装)第九节
  5. nohup和&amp;后台运行,查看占用端口进程
  6. 最近跟mysql的自动shutdown干上了。。。
  7. c语言学习笔记 —— 数组
  8. J2EE--常见面试题总结 -- 一
  9. DateTime格式
  10. python进阶---Python中的socket编程