#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main(){
int n, k, q;
cin >> n >> k >> q;
vector<int> level(n);
for(int i = 0; i < n; i++){
cin >> level[i];
level[i] = k - level[i];
}
vector<pair<int,int> > queries;
for(int i = 0; i < q; i++){
int a, b;
cin >> a >> b;
a--; b--;
queries.push_back({a,b});
}
int next[2][n][k]; // next stop of level <= j, you are <= j+1 (taking trains of j+1)
int idx[n][k+1];
for(int j = 0; j <= k; j++){
int cur = 0;
for(int i = 0; i < n; i++){
if(level[i] <= j){
idx[i][j] = cur;
cur++;
}
}
}
for(int j = 0; j < k; j++){
int prev = 0;
for(int i = 0; i < n; i++){
if(level[i] > j+1) continue;
if(level[i] <= j){
prev = i;
}
next[0][i][j] = prev;
}
prev = n-1;
for(int i = n-1; i >= 0; i--){
if(level[i] > j+1) continue;
if(level[i] <= j){
prev = i;
}
next[1][i][j] = prev;
}
}
for(int zz = 0; zz < queries.size(); zz++){
vector<pair<int,int> > stuff[2]; // loc, dist
stuff[0].push_back({queries[zz].first, 0});
stuff[1].push_back({queries[zz].second, 0});
int ans = 1000000000;
for(int j = k-1; j >= 0; j--){
// go to level j
for(int p = 0; p < 2; p++){
vector<pair<int,int> > newstuff;
for(pair<int,int> x : stuff[p]){
// go left from x, go right from x;
int loc = x.first;
int dist = x.second;
for(int dir = 0; dir < 2; dir++){
int newloc = next[dir][loc][j];
int newdist = dist + abs(idx[ loc ][j+1] - idx[ newloc ][j+1]);
newstuff.push_back({newloc, newdist});
}
}
int a = 0;
while(a + 1 < newstuff.size()){
if(newstuff[a].first == newstuff[a+1].first){
newstuff[a].second = min(newstuff[a].second, newstuff[a+1].second);
newstuff.erase(newstuff.begin() + a + 1);
}
a++;
}
stuff[p] = newstuff;
}
// find ans
for(pair<int,int> x0 : stuff[0]){
for(pair<int,int> x1 : stuff[1]){
ans = min(ans, x0.second + x1.second + abs(idx[x0.first][j] - idx[x1.first][j]));
}
}
}
cout << ans - 1 << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
7 ms |
488 KB |
Output is correct |
3 |
Correct |
7 ms |
556 KB |
Output is correct |
4 |
Correct |
8 ms |
588 KB |
Output is correct |
5 |
Correct |
4 ms |
636 KB |
Output is correct |
6 |
Execution timed out |
2057 ms |
4432 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
172 ms |
12504 KB |
Output is correct |
2 |
Correct |
48 ms |
12988 KB |
Output is correct |
3 |
Correct |
83 ms |
24760 KB |
Output is correct |
4 |
Correct |
241 ms |
60044 KB |
Output is correct |
5 |
Correct |
313 ms |
118840 KB |
Output is correct |
6 |
Execution timed out |
2085 ms |
525312 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1208 ms |
525312 KB |
Memory limit exceeded 525312 {'time-wall': '1.264', 'max-rss': '19492', 'csw-forced': '25', 'cg-mem': '525312', 'time': '1.208', 'csw-voluntary': '2'} 524288 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
108 ms |
525312 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |