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+; 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的更多相关文章
- codeforces 689B B. Mike and Shortcuts(bfs)
题目链接: B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input ...
- 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 ...
- codeforces 361 A - Mike and Cellphone
原题: Description While swimming at the beach, Mike has accidentally dropped his cellphone into the wa ...
- codeforces 361 C - Mike and Chocolate Thieves
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Bad ...
- 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 ...
- 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 ...
- Codeforces 689B. Mike and Shortcuts SPFA/搜索
B. Mike and Shortcuts time limit per test: 3 seconds memory limit per test: 256 megabytes input: sta ...
- codeforces 689 Mike and Shortcuts(最短路)
codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...
- CodeForces 689B Mike and Shortcuts (bfs or 最短路)
Mike and Shortcuts 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/F Description Recently ...
随机推荐
- springboot系列之-log
配置文件以application.yml为例说明: Spring Boot默认的日志组件为Logback. 一. 日志配置参数: logging: file: #日志文件,绝对路径或相对路径 path ...
- 关于EEG参考电极
今天搞ADS1299,里面的BIAS偏置运放原来是设置参考电极的. The input multiplexer has EEG-specific functions for the bias driv ...
- python数据结构与算法——字典树
class TrieTree(): def __init__(self): self.root = {} def addNode(self,str): # 树中每个结点(除根节点),包含到该结点的单词 ...
- Java-->Gson解析相较于Json
--> Gson解析jar包: 链接:http://pan.baidu.com/s/1slCeq77 密码:f9ig --> 官方Json解析工具类: package com.drago ...
- kernel/module.c
#include <linux/errno.h>#include <linux/kernel.h>#include <asm/segment.h>#include ...
- my Js
1. __doPostBack是.net自动生成的(当页面中有LinkButton.DropDownList(AutoPostBack)等时:Button和ImageButton不会生成它,也不会调用 ...
- CF 628B New Skateboard --- 水题
CD 628B 题目大意:给定一个数字(<=3*10^5),判断其能被4整除的连续子串有多少个 解题思路:注意一个整除4的性质: 若bc能被4整除,则a1a2a3a4...anbc也一定能被4整 ...
- html+css图片下弹出蒙版
鼠标移入时弹出蒙版!!! html<!DOCTYPE html<html lang="en"<head> <meta charset="UT ...
- 解决MVC中使用BundleConfig.RegisterBundles引用Css及js文件发布后丢失的问题
ASP.NET MVC4,ASP.NET MVC5中对JS和CSS的引用又做了一次变化,在MVC3中我们这样引用资源文件: <link href="@Url.Content(" ...
- SoftEnther VPN 在Window的使用
1.首先下载SoftEnther VPN Client 下载地址 2. 下载后,执行vpngate-client-×××.exe 文件 选择安装一个软件部分: SoftEnther VPN Clien ...