zoukankan      html  css  js  c++  java
  • codeforce 886C Petya and Catacombs (map,思路)

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

    先把题给贴了。

    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=1;
        mp.clear();
        mp[0]++;
        for(i=1;i<=n;i++)
        {
            cin>>k;
            if(mp[k]!=0)
            {
                mp[k]--;
                mp[i]++;
            }
            else 
            {
                mp[i]++;
                ans++;
            }
        }
        cout<<ans;
     } 
  • 相关阅读:
    Java 8 对 List<List<String>> 排序
    获取自然月的起始日期和终止日期
    Java 中如何计算两个字符串时间之间的时间差?(单位为分钟)
    Java StringJoiner
    回文数
    Java 中的值传递和引用传递问题
    substring
    集合的四种遍历方式
    Java 中的静态嵌套类和非静态嵌套类
    使用 Java 查找字符串中出现次数最多的字符以及出现的次数?
  • 原文地址:https://www.cnblogs.com/wsblm/p/7840626.html
Copyright © 2011-2022 走看看