This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
#define int long long
#define ll long long
typedef unsigned long long ull;
using namespace std;
const int N=105;
int a[N],n,q,k,dis[N][N];
bool vis[N][N];
void dijkstra(int s){
for (int i=0;i<N;i++) for (int j=0;j<N;j++) dis[i][j]=INT_MAX,vis[i][j]=0;
dis[s][a[s]]=0;
priority_queue<vector<int>> pq;
pq.push({0,s,a[s]});
while (!pq.empty()){
int x=pq.top()[1],y=pq.top()[2];
pq.pop();
if (vis[x][y]) continue;
vis[x][y]=1;
if (y>1){
if (dis[x][y-1]>dis[x][y]){
dis[x][y-1]=dis[x][y];
pq.push({-dis[x][y-1],x,y-1});
}
}
for (int i=x+1;i<=n;i++){
if (a[i]<y) continue;
if (dis[i][a[i]]>dis[x][y]+1){
dis[i][a[i]]=dis[x][y]+1;
pq.push({-dis[i][a[i]],i,a[i]});
}break;
}
for (int i=x-1;i>0;i--){
if (a[i]<y) continue;
if (dis[i][a[i]]>dis[x][y]+1){
dis[i][a[i]]=dis[x][y]+1;
pq.push({-dis[i][a[i]],i,a[i]});
}break;
}
}return;
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>k>>q;
for (int i=1;i<=n;i++) cin>>a[i];
while (q--){
int x,y,ans=INT_MAX;
cin>>x>>y;
dijkstra(x);
for (int j=1;j<=k;j++) ans=min(ans,dis[y][j]-1);
cout<<ans<<endl;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |