CodeForces 890C Petya and Catacombs
来源:互联网 发布:阿里云客服一天多少钱 编辑:程序博客网 时间:2024/06/10 17:51
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 minutei, he makes a note in his logbook with numberti:
- 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 minutei.
Initially, Petya was in one of the rooms at minute 0, he didn't write down numbert0.
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?
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 integerst1, t2, ..., tn (0 ≤ ti < i) — notes in the logbook.
In the only line print a single integer — the minimum possible number of rooms in Paris catacombs.
20 0
2
50 1 0 1 3
3
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秒时他站在第一个山洞。记录的根据是: 1.如果他来过这个山洞, 那么他记录的数字就是上一次他来到这个山洞的时间数 2.如果他没走过这个山洞就随便记录一个比之前写过的值小的数字。现在又有一个探险家来到这个山洞并且拿到了这个本本, 请问这个探险家最少能知道一共有几个山洞。
解题思路:
根据题目所给题意, 我们既然要求最小山洞数, 就要贪心的认为只要是符合条件一的数字我们都认为是去到了去过的山洞, 特判+贪心 一遍过。
举个例子:
0 1 0 1 3(本号)
1 -> 1 -> 1 -> 2 -> 3 -> 2 (洞号)
0 1 2 3 4 5 (时间)
代码:
#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
using namespace std;
/*
*ios::sync_with_stdio(false);
*/
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x7fffffff;
const int mod = 1000000;
const int Max = (int) 2e5 + 9;
struct node {
int num;
bool vis;
}arr[Max];
int main() {
int n, ans = 1;
scanf("%d", &n);
// initialization
memset(arr, 0, sizeof(arr));
for (int i = 1; i <= n; ++i) {
scanf("%d", &arr[i].num);
}
arr[0].num = 0;
for (int i = 1; i <= n; ++i) {
// come again
if ((arr[i].num == i - 1) && !arr[i - 1].vis) {
arr[i - 1].vis = 1;
arr[i].num = i;
continue ;
} else if (arr[arr[i].num].vis == 0) {
arr[arr[i].num].vis = 1;
arr[i].num = i;
continue ;
// not come
} else {
ans++;
}
}
printf("%d\n", ans);
return 0;
}
- CodeForces 890C Petya and Catacombs
- Codeforces 890 C. Petya and Catacombs (贪心)
- Codeforces Round #445 C. Petya and Catacombs 贪心
- codeforces 111C Petya and Spiders
- 【codeforces 111C】 Petya and Spiders
- CodeForces - 111C Petya and Spiders
- Petya and Staircases CodeForces
- codeforces 111C Petya and Spiders 状态压缩
- Codeforces 111C Petya and Spiders 题解&代码
- [Codeforces 66C] Petya and File System (模拟)
- Codeforces 66C Petya and File System(STL--map)
- 【CodeForces】A. Petya and Strings
- Codeforces 66E - Petya and Post
- Codeforces 362B Petya and Staircases
- CodeForces 111B - Petya and Divisors
- CodeForces 111B Petya and Divisors
- CodeForces 362B Petya and Staircases
- 【CodeForces】[66A]Petya and Java
- 链栈 尹成
- linux mint如何安装输入法(以google输入法为例)
- hive中的外表EXTERNAL TABLE
- CCF-训练50题-NO.26-在霍格沃茨找零钱
- Primitive Topology
- CodeForces 890C Petya and Catacombs
- 易错2(闰年)
- CSAPP课程笔记1
- [2017.11.12]Set&HashSet&LinkedHashSet&TreeSet&模拟用户
- Impala中的invalidate metadata和refresh
- 浏览器工作原理详解
- 英文论文-城市云脑,基于互联网云脑的智慧城市新架构
- Ubuntu上systemtap安装
- 使用ThreadPoolExecutor,当提交线程超过maximumPoolSize 会阻塞主线程吗?