A country X consists of N cities numbered 0 through . The map of this country can be represented as a cycle — for each valid i, city i has exactly two neighboring cities and .
The cities in the country are broadly categorized into different types. For each valid i, the type of city i is denoted by .
A person called Suarez wants to travel between some pairs of the cities. You are given Q queries. In each query, Suarez wants to travel from city number Y to a city of type Z. Also, he wants to travel only in a given direction along the cycle.
The direction is represented by a letter — L or R. If it's L, Suarez may only move from city i to city for each valid i. If it's R, he may only move from city i to city .
You need to find the minimum number of steps Suarez needs to take to reach a city of type Z if he starts moving from city Y in the given direction (he can take zero steps). In one step, Suarez can move from his current city to a neighboring city.
If Suarez can never reach a city of type Z for a given query, print 1 instead.
Constraints
The first line of the input contains two space-separated integers N and Q. The next line contains N space-separated integers, where the i-th of these integers represents .
Each of the following Q lines describes a query in the format , where Y is an integer denoting the number of the starting city, Z is an integer denoting the type of a target city and D is a letter ('L' or 'R') denoting the direction along the cycle.
Output format
For each query, print a single line containing one integer — the answer to the query.
The cities in the country are broadly categorized into different types. For each valid i, the type of city i is denoted by .
A person called Suarez wants to travel between some pairs of the cities. You are given Q queries. In each query, Suarez wants to travel from city number Y to a city of type Z. Also, he wants to travel only in a given direction along the cycle.
The direction is represented by a letter — L or R. If it's L, Suarez may only move from city i to city for each valid i. If it's R, he may only move from city i to city .
You need to find the minimum number of steps Suarez needs to take to reach a city of type Z if he starts moving from city Y in the given direction (he can take zero steps). In one step, Suarez can move from his current city to a neighboring city.
If Suarez can never reach a city of type Z for a given query, print 1 instead.
Constraints
The first line of the input contains two space-separated integers N and Q. The next line contains N space-separated integers, where the i-th of these integers represents .
Each of the following Q lines describes a query in the format , where Y is an integer denoting the number of the starting city, Z is an integer denoting the type of a target city and D is a letter ('L' or 'R') denoting the direction along the cycle.
Output format
For each query, print a single line containing one integer — the answer to the query.
Explanation
Lời giải:
For the first query, Suarez is already standing in a city of the required type, hence 0 steps taken.
For the second query, Suarez will take the path 1 -> 0 -> 2, hence 2 steps taken.
For the third query, Suarez will take the path 2 -> 0, hence 1 step taken.
For the fourth query, there is no city of type 5, so the answer is 1.
For the second query, Suarez will take the path 1 -> 0 -> 2, hence 2 steps taken.
For the third query, Suarez will take the path 2 -> 0, hence 1 step taken.
For the fourth query, there is no city of type 5, so the answer is 1.
#include<bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n,q; cin>>n>>q; vector<int> v[200002]; int arr[n]; for(int i=0;i<n;i++){ cin>>arr[i]; v[arr[i]].push_back(i); } while(q--){ int y,z; char d; cin>>y>>z>>d; int ans=INT_MAX; if(v[z].size()==0){ cout<<"-1\n"; } else{ if(d=='L'){ for(int i=0;i<v[z].size();i++) if(v[z][i]<=y) ans=min(ans,abs(v[z][i]-y)); else ans=min(ans,abs(n-abs(v[z][i]-y))); } else{ for(int i=0;i<v[z].size();i++) if(v[z][i]>=y) ans=min(ans,abs(v[z][i]-y)); else ans=min(ans,abs(n-abs(v[z][i]-y))); } cout<<ans<<endl; } } return 0; }
Không có nhận xét nào:
Đăng nhận xét