原题:

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+; ll fun(ll x)
{
ll sum=;
for(ll i=; i*i*i<=x; i++){
sum += x/(i*i*i);
}
return sum;
} int main()
{
ll l=,r=10e15,mid,ans=-;//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)/;
ll types=fun(mid);
if(types < m_types)
l=mid+;
else if(types > m_types)
r=mid-;
else{ //相等也要取左半部分,因为要找最小的n(找最大的也有方法)
ans=mid;
r=mid-;
}
}
cout<<ans<<endl;
return ;
}

codeforces 361 B - Mike and Shortcuts的更多相关文章

  1. codeforces 689B B. Mike and Shortcuts(bfs)

    题目链接: B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input ...

  2. codeforces 361 E - Mike and Geometry Problem

    原题: Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him ...

  3. codeforces 361 A - Mike and Cellphone

    原题: Description While swimming at the beach, Mike has accidentally dropped his cellphone into the wa ...

  4. codeforces 361 C - Mike and Chocolate Thieves

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u   Description Bad ...

  5. Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs

    B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...

  6. Codeforces Round #361 (Div. 2)——B. Mike and Shortcuts(BFS+小坑)

    B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  7. Codeforces 689B. Mike and Shortcuts SPFA/搜索

    B. Mike and Shortcuts time limit per test: 3 seconds memory limit per test: 256 megabytes input: sta ...

  8. codeforces 689 Mike and Shortcuts(最短路)

    codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...

  9. CodeForces 689B Mike and Shortcuts (bfs or 最短路)

    Mike and Shortcuts 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/F Description Recently ...

随机推荐

  1. StatisticalOutlierRemoval源码

    源代码 * * Software License Agreement (BSD License) * * Point Cloud Library (PCL) - www.pointclouds.org ...

  2. sql语句与 数据库

    create  table  wyx( xh int primary key, xm varchar(20) not null, nl int, zcrq timestamp default curr ...

  3. 深入C#中get与set的详解(转)

    转自:http://www.jb51.net/article/37960.htm 释一:属性的访问器包含与获取(读取或计算)或设置(写)属性有关的可执行语句.访问器声明可以包含 get 访问器或 se ...

  4. 鼠标滚动事件兼容性 wheel、onwheel

    wheelEvent = "onwheel" in document.createElement("div") ? "wheel" : // ...

  5. Keepalived 双机热备

    使用 Keepalived 做双机热备非常简单,经常和 LVS 搭配来实现高可用负载平衡方案. 1. Master / Slave 首先准备两台测试服务器和一个虚拟IP. Server A: 192. ...

  6. [转]N种内核注入DLL的思路及实现

    内核注入,技术古老但很实用.现在部分RK趋向无进程,玩的是SYS+DLL,有的无文件,全部存在于内存中.可能有部分人会说:“都进内核了.什么不能干?”.是啊,要是内核中可以做包括R3上所有能做的事,软 ...

  7. Python Queue队列

    queue is especially useful in threaded programming when information must be exchanged safely between ...

  8. mac idea 设置

    鼠标悬停显示文档注释:preferences->Editor->General:勾选 show quick documentation on mouse move 智能提示模糊搜索:pre ...

  9. PHP中float变量转换为int时,结果有误的问题!

    先上例子: <?php $money = 100; $rate = 1.15; $result = $money * $rate; var_dump( intval( $result ) ); ...

  10. Python招聘需求与技能体系

    目前国内的招聘Python,基本都是偏向web后台开发,偶有高大上的数据挖掘&机器学习. 这是之前(2012年)找工作整理的一些JD,在梳理几年来的笔记,顺带理一理 可以以此建立自己的技能体系 ...