Emuskald is addicted to Codeforces, and keeps refreshing the main pagenot to miss any changes in the "recent actions" list. He likes toread 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 thethread. When a new message is posted in a thread that thread jumps on the topof the list. No two messages of different threads are ever posted at the sametime.
Emuskald has just finished reading all his opened threads and refreshesthe main page for some more messages to feed his addiction. He notices that nonew threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time readingold 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 a1, a2, ..., an.
Input
The first line of input contains an integer n, the number of threads (1 ≤ n ≤ 105). Thenext line contains a list of n space-separated integers a1, a2, ..., an where ai (1 ≤ ai ≤ n) is theold position of the i-th threadin the new list. It is guaranteed that all of the ai are distinct.
Output
Output a single integer — thenumber of threads that surely contain a new message.
Sample test(s)
input
5
5 2 1 3 4
output
2
input
3
1 2 3
output
0
input
4
4 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 nonew messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since thethread order hasn't changed.
In the third test case, only thread 1 can contain no new messages.
思路:
判断最后一对相邻逆序数
程序:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;
#define L(u) (u<<1)
#define R(u) (u<<1|1)
#define lowbit(x) (x&-x)
#define rep(i,x,y) for (i=x;i<=y;i++)
#define ll __int64
#define max(x,y) ((x>y)?(x),(y))
#define min(x,y) ((x<y)?(x),(y))
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define slld(x) scanf("%lld",&x)
const int N = 100005;
struct node
{
int x, y;
};
int a[N];
int b[N], p;
int main()
{
int n, pos, poss;
sd(n);
int i, flag = true;;
rep(i, 1, n)
{
sd(a[i]);
}
pos = 0;
rep(i, 1, n - 1)
{
if (a[i] > a[i + 1])
pos = i;
}
printf("%d\n", pos);
return 0;
}