#include <bits/stdc++.h>
/*
20p: just Dijkstra
45p: keep left/right for each level (impossible to go past them)
Cartesian tree-like
Square root?
*/
int main(){
std::ios::sync_with_stdio(0);
std::cin.tie(0);
std::vector<int> levels;
int n,k,q;
std::cin>>n>>k>>q;
std::vector<std::vector<int>> edges(n);
std::vector<std::vector<int>> bylevel(k);
std::set<int> nums;
for(int i=0;i<n;i++){
int x;
std::cin>>x;
x--;
levels.push_back(x);
bylevel[x].push_back(i);
}
for(int i=k;i--;){
for(int j=0;j<bylevel[i].size();j++){
nums.insert(bylevel[i][j]);
}
for(int j=0;j<bylevel[i].size();j++){
if(bylevel[i][j]!=0)edges[bylevel[i][j]].push_back(*--nums.lower_bound(bylevel[i][j]));
if(bylevel[i][j]!=n-1)edges[bylevel[i][j]].push_back(*nums.upper_bound(bylevel[i][j]));
//std::cout<<edges[bylevel[i][j]][0]<<'\n';
}
}
for(int i=0;i<q;i++){
int a,b;
std::cin>>a>>b;
a--,b--;
std::vector<int> dists1(n,1e9),dists2(n,1e9);
std::priority_queue<std::pair<int,int>> dijkstra;
dijkstra.push({-0,a});
while(!dijkstra.empty()){
std::pair<int,int> cur=dijkstra.top();
dijkstra.pop();
if(dists1[cur.second]<=-cur.first)continue;
dists1[cur.second]=-cur.first;
for(int i:edges[cur.second]){
dijkstra.push({cur.first-1,i});
}
}
dijkstra.push({-0,b});
while(!dijkstra.empty()){
std::pair<int,int> cur=dijkstra.top();
dijkstra.pop();
if(dists2[cur.second]<=-cur.first)continue;
dists2[cur.second]=-cur.first;
for(int i:edges[cur.second]){
dijkstra.push({cur.first-1,i});
}
}
int mndst=1e9;
for(int j=0;j<n;j++){
//std::cout<<dists1[j]+dists2[j]<<'\n';
mndst=std::min(mndst,dists1[j]+dists2[j]);
}
std::cout<<mndst-1<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
87 ms |
12544 KB |
Output is correct |
3 |
Correct |
74 ms |
12736 KB |
Output is correct |
4 |
Correct |
68 ms |
12688 KB |
Output is correct |
5 |
Correct |
71 ms |
12584 KB |
Output is correct |
6 |
Correct |
80 ms |
12900 KB |
Output is correct |
7 |
Correct |
82 ms |
16700 KB |
Output is correct |
8 |
Correct |
388 ms |
14824 KB |
Output is correct |
9 |
Correct |
243 ms |
15848 KB |
Output is correct |
10 |
Correct |
256 ms |
13452 KB |
Output is correct |
11 |
Correct |
298 ms |
15916 KB |
Output is correct |
12 |
Correct |
271 ms |
14264 KB |
Output is correct |
13 |
Correct |
290 ms |
13656 KB |
Output is correct |
14 |
Correct |
270 ms |
15916 KB |
Output is correct |
15 |
Correct |
265 ms |
14328 KB |
Output is correct |
16 |
Correct |
241 ms |
13616 KB |
Output is correct |
17 |
Correct |
59 ms |
15420 KB |
Output is correct |
18 |
Correct |
51 ms |
15252 KB |
Output is correct |
19 |
Correct |
58 ms |
17720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2040 ms |
12760 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2044 ms |
17016 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |