Submission #959184

#TimeUsernameProblemLanguageResultExecution timeMemory
959184AbitoRailway Trip (JOI17_railway_trip)C++17
5 / 100
22 ms604 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...