CF270B.Multithreading

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.

Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.

Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a__i-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.

Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:

  1. thread x is not updated (it has no new messages);
  2. the list order 1, 2, ..., n changes to _a_1, _a_2, ..., a__n.

Emuskald 沉迷于 Codeforces,不断刷新主页,以免错过“最近动态”列表中的任何变化。他喜欢阅读讨论帖,而每个帖子由多条消息组成。

“最近动态”显示一个包含 $ n $ 个不同帖子的列表,这些帖子按各自最新一条消息的发布时间降序排列。当某个帖子中发布了一条新消息时,该帖子便会跳至列表顶端。不同帖子中的消息绝不会在同一时刻发布。

Emuskald 刚刚读完所有已打开的帖子,接着刷新主页,以获取更多消息来满足他的瘾。他注意到列表中没有出现任何新帖子,且刷新后列表中第 $ i $ 个位置上的帖子,在刷新前位于第 $ a_i $ 个位置。他不想浪费时间阅读旧消息,因此只想打开那些包含新消息的帖子。

请帮助 Emuskald 找出必定含有新消息的帖子数量。我们称帖子 $ x $ 必定含有新消息,当且仅当不存在任何一种帖子更新(即发消息)序列,使得以下两个条件同时成立:

  1. 帖子 $ x $ 未被更新(即它没有新消息);
  2. 列表顺序由 $ 1, 2, \dots, n $ 变为 $ a_1, a_2, \dots, a_n $。

输入格式

The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list of n space-separated integers _a_1, _a_2, ..., a__n where a__i (1 ≤ a__i ≤ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a__i are distinct.

输入的第一行包含一个整数 nn,表示线程的数量(1 ≤ n ≤ 1051 ≤ n ≤ 10^5)。第二行包含 nn 个用空格分隔的整数 a1,a2,…,ana_1, a_2, \dots, a_n,其中 aia_i(1 ≤ ai ≤ n1 ≤ a_i ≤ n)表示第 ii 个线程在新列表中的原始位置。保证所有 aia_i 互不相同。

输出格式

Output a single integer — the number of threads that surely contain a new message.

输出一个整数——必定包含新消息的线程数量。

输入输出样例

  • 输入#1

    5
    5 2 1 3 4

    输出#1

    2
  • 输入#2

    3
    1 2 3

    输出#2

    0
  • 输入#3

    4
    4 3 2 1

    输出#3

    3

说明/提示

In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.

In the second test case, there may be no new messages at all, since the thread order hasn't changed.

In the third test case, only thread 1 can contain no new messages.

在第一个测试用例中,线程 2 和 5 被置于线程 1 之前,因此这些线程必须包含新消息。若仅有线程 2 和 5 包含新消息,则线程 1、3 和 4 可能不包含任何新消息。

在第二个测试用例中,可能完全没有任何新消息,因为线程顺序未发生改变。

在第三个测试用例中,仅线程 1 可能不包含新消息。

输入解题思路,AI测评打分。不知道怎么写?

首页