Monday 23 November 2015

A. Permutations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot.
One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation.
Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.
Input
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the quantity of the numbers in the both given permutations.
Next line contains n space-separated integers — the first permutation. Each number between 1 to n will appear in the permutation exactly once.
Next line describe the second permutation in the same format.
Output
Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.
Sample test(s)
Input
3
3 2 1
1 2 3
Output
2
Input
5
1 2 3 4 5
1 5 2 3 4
Output
1
Input
5
1 5 2 3 4
1 2 3 4 5
Output
3
Note
In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3.
In the second sample, he removes number 5 and inserts it after 1.
In the third sample, the sequence of changes are like this:
  • 1 5 2 3 4
  • 1 4 5 2 3
  • 1 3 4 5 2
  • 1 2 3 4 5
So he needs three moves.
 
 
Solution 
 
If we replace the array the 1st array with the corresponding position in 2nd array then the question simply reduces to sorting the first array.
Suppose now we postpone the placement of every last element till we find its appropriate place to put. Therefore  let i be the largest index for which 1 to i is sorted so the answer is n-i.
Let us take an example and justify 
suppose we have p1= 1 2 5 4 3
                           p2= 4 2 1 3 5
the new array acccording to position is   
3 2 1 5 4 
so the longest increasing sequence is 3
Now suppose we have 2 1 54 in our hands and then we place each elements in their respective postion to sort the array.
 
code
#include<bits/stdc++.h>
using namespace std;
int a[200010];
int b[200010];
int h[200010];
int arr[200010];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
         cin>>a[i];
    }
    for(int i=0;i<n;i++)
    {
         cin>>b[i];
         h[b[i]]=i;
    }
    for(int i=0;i<n;i++)
    {
         arr[i]=h[a[i]];
    }
    int mx=-1;
    int cnt=1;
    for(int i=1;i<n;i++)
    {
          if(arr[i]>arr[i-1])
          cnt++;
          else
          break;
    }
    cout<<n-cnt<<endl;
    return 0;
}

No comments:

Post a Comment