#include<bits/stdc++.h>
using namespace std;
//#define int long long
const int TAILLE_MAX=100*1000+5,INFINI=1000*1000*1000;
int nbVal,typeMaxi,nbReq;
int val[TAILLE_MAX];
vector<int> posVal[TAILLE_MAX];
set<int> plusGrand;
set<int>::iterator it;
int vois[TAILLE_MAX][2];
int dist[TAILLE_MAX][2];
void calcDist(int deb,int tab) {
for (int i=1;i<=nbVal;i++) {
dist[i][tab]=INFINI;
}
deque<int> enCours;
enCours.push_back(deb);
int longueur,pos,distCour=0;
while (!enCours.empty()) {
longueur=enCours.size();
for (int i=0;i<longueur;i++) {
pos=enCours.front();
enCours.pop_front();
if (dist[pos][tab]==INFINI) {
dist[pos][tab]=distCour;
for (int j=0;j<2;j++) {
if (vois[pos][j]!=0) {
enCours.push_back(vois[pos][j]);
}
}
}
}
distCour++;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>nbVal>>typeMaxi>>nbReq;
for (int i=1;i<=nbVal;i++) {
cin>>val[i];
posVal[val[i]].push_back(i);
}
plusGrand.insert(-INFINI);
plusGrand.insert(INFINI);
for (int i=typeMaxi;i>=1;i--) {
for (int j:posVal[i]) {
plusGrand.insert(j);
}
for (int j:posVal[i]) {
it=plusGrand.lower_bound(j);
it++;
if (abs((*it))!=INFINI) {
vois[j][1]=(*it);
}
it--;
it--;
if (abs((*it))!=INFINI) {
vois[j][0]=(*it);
}
}
}
int deb,fin,rep;
for (int ireq=0;ireq<nbReq;ireq++) {
cin>>deb>>fin;
calcDist(deb,0);
calcDist(fin,1);
rep=INFINI;
for (int i=1;i<=nbVal;i++) {
rep=min(rep,dist[i][0]+dist[i][1]);
}
cout<<rep-1<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
35 ms |
10076 KB |
Output is correct |
3 |
Correct |
37 ms |
10328 KB |
Output is correct |
4 |
Correct |
42 ms |
10068 KB |
Output is correct |
5 |
Correct |
47 ms |
10432 KB |
Output is correct |
6 |
Correct |
49 ms |
10324 KB |
Output is correct |
7 |
Correct |
54 ms |
11860 KB |
Output is correct |
8 |
Correct |
89 ms |
9948 KB |
Output is correct |
9 |
Correct |
63 ms |
10832 KB |
Output is correct |
10 |
Correct |
65 ms |
10456 KB |
Output is correct |
11 |
Correct |
67 ms |
11092 KB |
Output is correct |
12 |
Correct |
69 ms |
10836 KB |
Output is correct |
13 |
Correct |
73 ms |
10832 KB |
Output is correct |
14 |
Correct |
72 ms |
11088 KB |
Output is correct |
15 |
Correct |
74 ms |
11152 KB |
Output is correct |
16 |
Correct |
68 ms |
10832 KB |
Output is correct |
17 |
Correct |
33 ms |
10312 KB |
Output is correct |
18 |
Correct |
33 ms |
10324 KB |
Output is correct |
19 |
Correct |
36 ms |
12632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2064 ms |
10324 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2048 ms |
12116 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |