#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
344 KB |
Output is correct |
2 |
Correct |
17 ms |
348 KB |
Output is correct |
3 |
Correct |
18 ms |
348 KB |
Output is correct |
4 |
Correct |
18 ms |
560 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
22 ms |
560 KB |
Output is correct |
7 |
Correct |
20 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
476 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |