codeforces 889A
1 second
256 megabytes
standard input
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?
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.
In the only line print a single integer — the minimum possible number of rooms in Paris catacombs.
2
0 0
2
5
0 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.
题意:
地下墓穴有很多房间和房间之间的双向通道,探险家在里面探索房间。如果到了之前来过的房间,探险家会在记录一个数字t,t是他上次到这个房间记录的时间,如果这个房间他没来过他就会标记一个当前时间i小的t。通过这些他记录下来的时间t,让你求出最少房间的可能。
思路:
乍一看很复杂,但是题目线索给的很清晰了,一个房间记录的t有两种可能:1.上次来过t是上一次到达这个房间的时间。2.没来过这个房间,t是小于当前时间的一个数字,想让房间最少,当然是尽量选择第一种可能啊。
如果每次都是访问之前到达过的房间的话,是不会有相同的值出现的,一开始会在1个房间,此时时间为0s,如第一个样例: 0 0 ,第一个0代表0秒在的房间,设为房间1 ,第二个0,就不能代表0s在的房间了,因为0s是在1房间,但1房间上一次访问是1s时,1房间记录的应该是1。矛盾了,所以只能是第二种可能,未到过的房间,0是小于当前时间i的随机数。
实现代码:
#include<bits/stdc++.h>
using namespace std; int main()
{
int n,ans,i,a[],b[];
scanf("%d",&n);
ans = ;
memset(b,,sizeof(b));
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=n;i++){
if(!b[a[i]])
b[a[i]]=;
//cout<<a[i]<<endl;
else ans++;
}
cout<<ans<<endl;
}
codeforces 889A的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- debian jessie 网络设置
从stable更换到testing后,更新系统apt-get dist-upgrade,然后是等待, 然后不耐烦了不等了,关机! 第二天早上开机apt-get update,找不到源! 用ifconf ...
- WPF Good UI
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.co ...
- 20155232《网络对抗》Exp5 MSF基础应用
20155232<网络对抗>Exp5 MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode. exploit:就是利用可能存在的漏洞对目标进行攻击 ...
- 20155239吕宇轩 Exp1 PC平台逆向破解(5)M
20155239 网络对抗 Exp1 PC平台逆向破解(5)M 实验内容 (1).掌握NOP, JNE, JE, JMP, CMP汇编指令的机器码(1分) (2)掌握反汇编与十六进制编程器 (1分) ...
- CISCN 应用环境相关指令备忘录
1 - 关于Python环境的 使用Anaconda2管理Python环境 1.1 - 安装 官网下载安装包下载. 1.2 - 创建Python环境 localhost:template mac$ c ...
- 【SP1811】LCS - Longest Common Substring
[SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...
- JAVAWEB 项目注册登录模块问题总结
tomcat: 假如tomcat服务器启动出现错误,那就可能是servlet或代码的原因 tomcat服务器出现不能访问页面的情况,可以在eclipse tomcat服务器设置里设置为共享服务器模式 ...
- jvm系列(九):Java GC 分析
Java GC就是JVM记录仪,书画了JVM各个分区的表演. 什么是 Java GC Java GC(Garbage Collection,垃圾收集,垃圾回收)机制,是Java与C++/C的主要区别之 ...
- R绘图 第五篇:绘制散点图(ggplot2)
ggplot2包中绘制点图的函数有两个:geom_point和 geom_dotplot,当使用geom_dotplot绘图时,point的形状是dot,不能改变点的形状,因此,geom_dotplo ...
- effective c++ 笔记 (26-29)
//---------------------------15/04/09---------------------------- //#26 尽可能延后变量定义式的出现时间 { /* 1:只要 ...