zoukankan      html  css  js  c++  java
  • A. Detective Book

    A. Detective Book

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Ivan recently bought a detective book. The book is so interesting that each page of this book introduces some sort of a mystery, which will be explained later. The ii-th page contains some mystery that will be explained on page aiai (aiiai≥i).

    Ivan wants to read the whole book. Each day, he reads the first page he didn't read earlier, and continues to read the following pages one by one, until all the mysteries he read about are explained and clear to him (Ivan stops if there does not exist any page ii such that Ivan already has read it, but hasn't read page aiai). After that, he closes the book and continues to read it on the following day from the next page.

    How many days will it take to read the whole book?

    Input

    The first line contains single integer nn (1n1041≤n≤104) — the number of pages in the book.

    The second line contains nn integers a1,a2,,ana1,a2,…,an (iaini≤ai≤n), where aiai is the number of page which contains the explanation of the mystery on page ii.

    Output

    Print one integer — the number of days it will take to read the whole book.

    Example

    input

    9
    1 3 3 6 7 6 8 8 9
    

    output

    4

    Note

    Explanation of the example test:

    During the first day Ivan will read only the first page. During the second day Ivan will read pages number 2 and 3. During the third day — pages 4-8. During the fourth (and the last) day Ivan will read remaining page number 9.

    题意:lvan看一本页数为n页的书,求出他要多少天看完(如果当前的所有谜团没有解决的话,lvan就会一直看下去)。

    题解:找出当前所看到的最大页数,如果最大页数正好等于当前看到的页数,那就天数就加1。

    #include<iostream>
    #include<cstdio>
    #include<memory.h>
    
    using namespace std;
    
    int main()
    {
        int n,x,maxx=-1,ans=0;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            if(maxx<x)        //找出当前看到的最大页数
                maxx=x;
            if(maxx==i)        //判断最大页数是不是当前页数
                ans++;
        }
        cout<<ans<<endl;
        return 0;
    }


  • 相关阅读:
    P3952 [NOIP2017 提高组] 时间复杂度
    1905. 统计子岛屿
    1102 Invert a Binary Tree (25 分)
    P1077 [NOIP2012 普及组] 摆花
    P3915 树的分解
    P1045 [NOIP2003 普及组] 麦森数
    P4961 小埋与扫雷
    P1123 取数游戏
    P1460 [USACO2.1]健康的荷斯坦奶牛 Healthy Holsteins
    CF1059B Forgery
  • 原文地址:https://www.cnblogs.com/buhuiflydepig/p/10639172.html
Copyright © 2011-2022 走看看