Codeforces 890 C. Petya and Catacombs (贪心)
来源:互联网 发布:网络女主播直播洗澡 编辑:程序博客网 时间:2024/06/11 00:25
Description
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·10^5) — 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
20 0
Examples output
2
题意
每到达一个房间,有两种标号的记法:
- 记录上一次到达该房间的时间
- 随机一个小于当前时间的数字
问最少有几个房间。
思路
显然房间数目越少要求我们经过同一个房间的次数尽可能的多,于是对于每一个编号,我们假设它记录的是上一次到达该房间的时间,判断假设是否成立,若成立,更新该房间的最后抵达时间,否则房间数加一。
AC 代码
#include<bits/stdc++.h>#define IO ios::sync_with_stdio(false);\ cin.tie(0);\ cout.tie(0);typedef long long LL;using namespace std;const int maxn = 2e6+10;int last[maxn];int a[maxn];int num[maxn];int n;void solve(){ int ans = 1; for(int i=1; i<=n; i++) { int now = a[i]; if(last[num[now]]==a[i]) { last[num[now]]=i; num[i] = num[now]; } else { num[i]=++ans; last[num[i]]=i; } } cout<<ans<<endl;}int main(){ IO; cin>>n; for(int i=1; i<=n; i++) cin>>a[i]; solve(); return 0;}
- Codeforces 890 C. Petya and Catacombs (贪心)
- CodeForces 890C Petya and Catacombs
- Codeforces Round #445 C. Petya and Catacombs 贪心
- [Codeforces 66C] Petya and File System (模拟)
- codeforces 111C Petya and Spiders
- 【codeforces 111C】 Petya and Spiders
- CodeForces - 111C Petya and Spiders
- codeforces B. Petya and Exam (模拟)
- Petya and Staircases CodeForces
- Codeforces Round #319 (Div. 2) ——C. Vasya and Petya's Game(数学题)
- Codeforces #319(Div.2) C. Vasya and Petya's Game(数学题)
- codeforces 111C Petya and Spiders 状态压缩
- Codeforces 111C Petya and Spiders 题解&代码
- Codeforces 66C Petya and File System(STL--map)
- 【CodeForces】A. Petya and Strings
- Codeforces 362E Petya and Pipes (费用流)
- Codeforces 112A-Petya and Strings(实现)
- Codeforces 112B-Petya and Square(实现)
- MarkDown
- C#模拟操作记事本
- uva 10080 Gopher II【匈牙利算法】
- 留给人类的时间不多了?现在不学机器学习更待何时!
- android 小知识
- Codeforces 890 C. Petya and Catacombs (贪心)
- python︱matplotlib使用(读入、显示、写出、opencv混用、格式转换...)
- CEF 2987 WIN10 编译历程
- 趣味机器学习入门小项目(附教程与数据)
- java FreeMarker制作word
- Android使用三方库实现日期选择器
- dpkg: error processing package XXX (--configure) 解决方法 (ubuntu右上角红色警告)
- linux 使用 rz 和 sz 命令
- winform窗体抖动效果