好久没写题解了;
Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city.
City consists of n intersections numbered from1 to n. Mike starts walking from his house located at the intersection number1 and goes along some sequence of intersections. Walking from intersection numberi to intersection j requires|i - j| units of energy. The total energy spent by Mike to visit a sequence of intersections p1 = 1, p2, ..., pk is equal to units of energy.
Of course, walking would be boring if there were no shortcuts. A shortcut is a special path that allows Mike walking from one intersection to another requiring only1 unit of energy. There are exactly n shortcuts in Mike's city, the ith of them allows walking from intersectioni to intersection ai (i ≤ ai ≤ ai + 1) (but not in the opposite direction), thus there is exactly one shortcut starting at each intersection. Formally, if Mike chooses a sequencep1 = 1, p2, ..., pk then for each1 ≤ i < k satisfying pi + 1 = api andapi ≠ pi Mike will spendonly 1 unit of energy instead of|pi - pi + 1| walking from the intersectionpi to intersectionpi + 1. For example, if Mike chooses a sequencep1 = 1, p2 = ap1, p3 = ap2, ..., pk = apk - 1, he spends exactly k - 1 units of total energy walking around them.
Before going on his adventure, Mike asks you to find the minimum amount of energy required to reach each of the intersections from his home. Formally, for each1 ≤ i ≤ n Mike is interested in finding minimum possible total energy of some sequencep1 = 1, p2, ..., pk = i.
The first line contains an integer n (1 ≤ n ≤ 200 000) — the number of Mike's city intersection.
The second line contains n integers a1, a2, ..., an(i ≤ ai ≤ n ,, describing shortcuts of Mike's city, allowing to walk from intersectioni to intersection ai using only1 unit of energy. Please note that the shortcuts don't allow walking in opposite directions (fromai toi).
In the only line print n integers m1, m2, ..., mn, wheremi denotes the least amount of total energy required to walk from intersection1 to intersection i.
3 2 2 3
0 1 2
5 1 2 3 4 5
0 1 2 3 4
7 4 4 4 4 7 7 7
0 1 2 1 2 3 3
In the first sample case desired sequences are:
1: 1; m1 = 0;
2: 1, 2; m2 = 1;
3: 1, 3; m3 = |3 - 1| = 2.
In the second sample case the sequence for any intersection 1 < i is always 1, i and mi = |1 - i|.
In the third sample case — consider the following intersection sequences:
1: 1; m1 = 0;
2: 1, 2; m2 = |2 - 1| = 1;
3: 1, 4, 3; m3 = 1 + |4 - 3| = 2;
4: 1, 4; m4 = 1;
5: 1, 4, 5; m5 = 1 + |4 - 5| = 2;
6: 1, 4, 6; m6 = 1 + |4 - 6| = 3;
7: 1, 4, 5, 7; m7 = 1 + |4 - 5| + 1 = 3.
考虑dfs,遍历每种可能性,一个点只可能去附近的两个点和有捷径相连的点;
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn =200005;
int data[maxn];
int ans[maxn];
int n;
vector<int >vc[maxn];
queue<int >q;
void dfs(int st){
q.push(st);
while(!q.empty()){
int now=q.front();
q.pop();
for(int i=0;i<vc[now].size();i++){
if(ans[vc[now][i]]>ans[now]+1){
ans[vc[now][i]]=ans[now]+1;
q.push(vc[now][i]);
}
}
}
}
int main(){
scanf("%d",&n);
int i,j;
memset(ans,0x3f,sizeof(ans));
for(i=1;i<=n;i++){
scanf("%d",&data[i]);
vc[i].push_back(data[i]);
}
for(i=1;i<=n;i++){
if(i>1)
vc[i].push_back(i-1);
if(i<n)
vc[i].push_back(i+1);
}
ans[1]=0;
dfs(1);
for(i=1;i<=n;i++)
printf("%d ",ans[i]);
printf("\n");
return 0;
}
其实我看到这个数据量,考虑了下贪心,正着贪心一遍,反着贪心一遍,用于处理类似
10 6 2 3 4 10 6 7 8 9 10这种数据;
然后无耻地重复了几次,居然被我水过了。100ms内,比dfs快 ,笑cry;