Codeforces #165 div2 B Multithreading

来源:互联网 发布:整型变量的数据范围 编辑:程序博客网 时间:2024/05/01 00:22
B. Multithreading
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 thei-th place in the list there is a thread that was at theai-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 toa1, a2, ..., an.
Input

The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). The next line contains a list ofn space-separated integers a1, a2, ...,an whereai (1 ≤ ai ≤ n) is the old position of thei-th thread in the new list. It is guaranteed that all of theai are distinct.

Output

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

Sample test(s)
Input
55 2 1 3 4
Output
2
Input
31 2 3
Output
0
Input
44 3 2 1
Output
3
Note

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.

解题思路:说实话题目我也没有太看明白,大意就是在原先顺序排列的进程中不断有进程刷新至第一位,现在给你原进程排名在新的进程表中的情况,求最少有多少个进程被刷新过。考虑到在最少进程刷新的情况下,第一个被刷新到前面的进程,必然与其后面的一个数构成逆序,那么只要从后向前找到这个数所在的位置,它前面的数也必然是随后刷新的,反之如果没有这样一个进程,就是没有被刷新过。

注:如果1 2 3 4 5刷新为3 1 2 4 5->2 3 1 4 5->1 2 3 4 5虽然刷新了3次但是不符合题目surely确定的,即最少的要求,故输出为0。

#include <iostream>#include<cstring>using namespace std;int main(){    long num[100005],n,i,ans;    while(cin>>n)    {        ans=0;        memset(num,0,sizeof(num));        for(i=1;i<=n;i++)            cin>>num[i];        for(i=n;i>=1;i--)        {            if(num[i-1]>num[i])            {                ans=i-1;                break;            }        }        cout<<ans<<endl;    }    return 0;}


原创粉丝点击