codeforces 361 B - Mike and Shortcuts

原题: 

Description
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 from 1 to n. Mike starts walking from his house located at the intersection number 1 and goes along some sequence of intersections. Walking from intersection number i 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 only 1 unit of energy. There are exactly n shortcuts in Mike's city, the ith of them allows walking from intersection i 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 sequence p1 = 1, p2, ..., pk then for each 1 ≤ i < k satisfying pi + 1 = api and api ≠ pi Mike will spend only 1 unit of energy instead of |pi - pi + 1| walking from the intersection pi to intersection pi + 1. For example, if Mike chooses a sequence p1 = 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 each 1 ≤ i ≤ n Mike is interested in finding minimum possible total energy of some sequence p1 = 1, p2, ..., pk = i.

Input
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 intersection i to intersection ai using only 1 unit of energy. Please note that the shortcuts don't allow walking in opposite directions (from ai to i).

Output
In the only line print n integers m1, m2, ..., mn, where mi denotes the least amount of total energy required to walk from intersection 1 to intersection i.

Sample Input
Input
3
2 2 3
Output
0 1 2 
Input
5
1 2 3 4 5
Output
0 1 2 3 4 
Input
7
4 4 4 4 7 7 7
Output
0 1 2 1 2 3 3 
Hint
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.
原题

 

提示: 题目很长,配合着hint和案例就容易理解了。

  这是一个 一维数组 组成的链表(向量?) ,一个点可以向前、后、捷径 3个方向跳转,权值都是1.记得用最大值初始化dis【】数组。找比dis[now]更小的dis[now]替换之。

这里再说下这个bfs的复杂度,表示不会看这个题目的复杂度,好难分析。囧

 

代码:

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

#define  MAX(x,y) (((x)>(y)) ? (x) : (y))
#define  MIN(x,y) (((x) < (y)) ? (x) : (y))
#define ABS(x) ((x)>0?(x):-(x))
#define ll long long
const int inf = 0x7fffffff;
const int maxn=1e15+10;

ll fun(ll x)
{
    ll sum=0;
    for(ll i=2; i*i*i<=x; i++){
        sum += x/(i*i*i);
    }
    return sum;
}

int main()
{
    ll l=1,r=10e15,mid,ans=-1;//fun(10e14) == 10e15;  所以最大的n可能取值为10e15 (极端有10e15种方法的时候)
    ll m_types;
    cin>>m_types;
    while(l<=r){  //l=minimum n,  r=maximum n ;  =要加  方便找到最小的那个数
        mid=l+(r-l)/2;
        ll types=fun(mid);
        if(types < m_types)
            l=mid+1;
        else if(types > m_types)
                r=mid-1;
            else{   //相等也要取左半部分,因为要找最小的n(找最大的也有方法)
                ans=mid;
                r=mid-1;
            }
    }
    cout<<ans<<endl;
    return 0;
}

 

posted @ 2016-07-11 20:28  Shawn_Ji  阅读(257)  评论(0编辑  收藏  举报