1778B - The Forbidden Permutation - CodeForces Solution


greedy math

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;

#define ll long long int 
#define nline "\n"
#define Mod 1000000007
#define vi vector<ll>
#define vii vector<pair<ll,ll>>
#define umap unordered_map
#define uset unordered_set
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define rep1(i,a,n) for(ll i=a;i<n;i++)
#define rep2(i,a,n) for(ll i=a;i<=n;++i)
#define rep3(i,n,a) for(ll i=n;i>=a;--i)
#define P pair<ll,ll>
#define ff first
#define ss second



void solve(){
	ll n,m,d;
	cin>>n>>m>>d;
	vector<ll>pos(n+1);
	for(ll i=1;i<=n;i++){
		ll x;
		cin>>x;
		pos[x]=i;

	}
	vector<ll>v2(m+1);
	for(ll i=0;i<m;++i){
		cin>>v2[i];
	}
	ll ans=1e8;
	for(ll i=0;i<m-1;++i){
		if(pos[v2[i]]>pos[v2[i+1]]){
			ans=0;
			break;
		}
		else if(pos[v2[i]]+d<pos[v2[i+1]]){
			ans=0;
			break;
		}
		else{
			ll move=d+1-(pos[v2[i+1]]-pos[v2[i]]);
			if(move<=(pos[v2[i]]-1+n-pos[v2[i+1]])){
				ans=min(ans,move);
			}
			ans=min(ans,pos[v2[i+1]]-pos[v2[i]]);
		}
	}
	cout<<ans<<nline;

}
        


int main(){
	ll t;
	cin>>t;
	while(t--){
		solve();
}

}


Comments

Submit
0 Comments
More Questions

513A - Game
1711E - XOR Triangle
688A - Opponents
20C - Dijkstra
1627D - Not Adding
893B - Beautiful Divisors
864B - Polycarp and Letters
1088A - Ehab and another construction problem
1177B - Digits Sequence (Hard Edition)
1155B - Game with Telephone Numbers
1284A - New Year and Naming
863B - Kayaking
1395B - Boboniu Plays Chess
1475D - Cleaning the Phone
617B - Chocolate
1051B - Relatively Prime Pairs
95B - Lucky Numbers
1692D - The Clock
1553D - Backspace
1670D - Very Suspicious
1141B - Maximal Continuous Rest
1341A - Nastya and Rice
1133A - Middle of the Contest
385A - Bear and Raspberry
1311B - WeirdSort
1713F - Lost Array
236B - Easy Number Challenge
275A - Lights Out
147A - Punctuation
253A - Boys and Girls