#include <bitset>
#include <cmath>
#include <functional>
#include <algorithm>
#include <numeric>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <cstring>
#include <climits>
#define pb push_back
#define MOD 1000000007
#define NMAX 200001
#define nl '\n'
#define INF 1000000007
#define pii1 pair<int, pair<int,int>> (1,(1,2));
#define pii pair<int,int>
#define tpl tuple<int,int,int,int,int>
using namespace std;
ifstream fin("data.in");
ofstream fout("data.out");
/*
====================DEMONSTRATION======================
n=2
1....#####..##
....##########
####..........
##########....
##########....
1....####....#
=========================END===========================
*/
int n;
vector<int>v;
vector<vector<int>>G(NMAX);
int start;
bool vis[NMAX];
int maxi[NMAX];
signed main() {
cin>>n;
v.resize(n+1);
map<int,int>mp;
for(int i=1;i<=n;++i) {
cin >> v[i];
if (v[i] == n)
start = i;
mp[v[i]]=i;
}
for(int i=1;i<n;++i)
{
int x,y;
cin>>x>>y;
G[x].pb(y);
G[y].pb(x);
}
if(start<=(n/2)) {
for (int i = 1; i < start; ++i)
vis[i] = 1;
}
else
{
for(int i=start;i<=n;++i)
vis[i]=1;
}
int i=start;
int ans=0;
while(!(v[i-1]>v[i] && v[i]<v[i+1]) && i>0)
{
int aux=v[i];
vis[mp[aux]]=1;
while(vis[mp[aux]])
aux--;
// cout<<i<<" "<<aux<<" "<<abs(i-mp[aux])<<nl;
ans+=abs(i-mp[aux]);
i=mp[aux];
}
cout<<ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |