You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
862 B
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<int> p(n);
for (auto &x:p) cin >> x;
vector<pair<int, int>> canswap(m);
for (auto &x:canswap) cin >> x.first >> x.second;
set<pair<int, int>> S(canswap.begin(), canswap.end());
int targetval = *p.rbegin();
for (int i = n - 1; i; i--)
if (S.count(make_pair(p[i - 1], p[i]))) {
swap(p[i - 1], p[i]);
for (int j = i + 1; j < n; j++)
if (S.count(make_pair(p[j - 1], p[j])))
swap(p[j - 1], p[j]);
else break;
}
cout << distance(p.rbegin(), find(p.rbegin(), p.rend(), targetval));
return 0;
}