CF 1140A Detective Book

版权声明:没什么好说的,版权声明呢 https://blog.csdn.net/weixin_43741224/article/details/88819639

Description

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 (ai≥iai≥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 (1≤n≤1041≤n≤104) — the number of pages in the book.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (i≤ai≤ni≤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

题目分析

    水题一道,就是翻译费了一会儿功夫(因为第一次翻译出错,导致一直不知道样例是怎么计算的,英语很重要呀),主要内容如下:

    每天,Ivan都会读他之前没有读过的第一页,然后一页一页地继续读下一页,直到他所读到的所有秘密都被解释清楚为止(如果任何一页Ivan都读过,Ivan就会停下来 )。

    也就是说每天读的所以书中秘密所在页数最大的页数必须读完,那么我们在输入的时候记录就可以了,如果当前需要读到的最大页数m = max(m,a[i]) ,a[i] 代表当前页数秘密所在的页数,如果m == i ,那么就说明这一天所读的书的全部秘密已经读完,我们只需要将m = -1 ,代表新的一天中需要读到的最大页数(为负值就是不可能,这是为了方便取最大页数而取的特殊值,没什么特殊意义),每次m重置的时候将天数sum++即可,最后输出sum就是所求答案.

代码区

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int Max = 5e5 + 5;

int page[Max];

int main()
{
	int n;
	while (scanf("%d", &n) != EOF)
	{
		int m = -1;		                        //记录这一天要读到的最远位置
		int sum = 0;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", page + i);
			m = max(m, page[i]);	                //记录读到的最远距离
			if(m == i)				//如果当前的位置为目前需要读到的最远页数,那么这一天的读书完毕
			{
				sum++;
				m = -1;				//表示这一天的读书结束,为下一次做准备
			}
		}
		cout << sum << endl;


	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43741224/article/details/88819639
今日推荐