Codeforces Round #353 (Div. 2) E. Trains and Statistic

原创 2016年05月31日 16:02:11

Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i + 1 to ai inclusive. No tickets are sold at the last station.

Let ρi, j be the minimum number of tickets one needs to buy in order to get from stations i to station j. As Vasya is fond of different useless statistic he asks you to compute the sum of all values ρi, j among all pairs 1 ≤ i < j ≤ n.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of stations.

The second line contains n - 1 integer ai (i + 1 ≤ ai ≤ n), the i-th of them means that at the i-th station one may buy tickets to each station from i + 1 to ai inclusive.

Output

Print the sum of ρi, j among all pairs of 1 ≤ i < j ≤ n.

Examples
Input
4
4 4 4
Output
6
Input
5
2 3 5 5
Output
17
Note

In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6.

Consider the second sample:

  • ρ1, 2 = 1
  • ρ1, 3 = 2
  • ρ1, 4 = 3
  • ρ1, 5 = 3
  • ρ2, 3 = 1
  • ρ2, 4 = 2
  • ρ2, 5 = 2
  • ρ3, 4 = 1
  • ρ3, 5 = 1
  • ρ4, 5 = 1

Thus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17.

题意:有n个车站,每个车站有一个可达区间[i+1,ai],问所有车站的最小换站距离之和。


分析:设i为起点,k为[i+1,a[i]]中a[k]最大的车站编号,则对于任意j>ai,选择从k换站一定是最好的。


#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
#define got(x) (1<<x) 
using namespace std;
long long ans,dp[100007];
int n,a[100007],Max[100007][28];
int gotmax(int x,int y)
{
	int l = log2(y-x+1);
	return a[Max[x][l]] < a[Max[y-got(l)+1][l]] ? Max[y-got(l)+1][l] : Max[x][l];
}
int main()
{
	scanf("%d",&n);
	for(int i = 1;i < n;i++)
	{
		scanf("%d",&a[i]);
		Max[i][0] = i;
	}
	for(int i = 1;got(i) < n;i++)
	 for(int j = 1;j+got(i)-1 < n;j++)
	  Max[j][i] = a[Max[j][i-1]] < a[Max[j+got(i-1)][i-1]] ? Max[j+got(i-1)][i-1]:Max[j][i-1];
	dp[n] = 0;
	for(int i = n-1;i;i--) 
	{
		if(a[i] == n)
		{
			dp[i] = 1ll*(n - i);
			ans += dp[i];
			continue;
		}
		int k = gotmax(i+1,a[i]);
		dp[i] = dp[k] + 1ll*(n - i - a[i] + k);
		ans += dp[i];
	}
	cout<<ans<<endl;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Codeforces Round #353 (Div. 2) E. Trains and Statistic(求d[i][j]的和的最小值(1<=i<=n,i+1<=j<=n))

E. Trains and Statistic Vasya commutes by train every day. There are n train stations in the city, a...

codeforces 675 A~E(Round #353 Div. 2) python and C++ 解法

PROBLEM LISTofficial tutorialExplanation I use python in Problem A, B & E. while in C and D, I ...

Codeforces 675E Trains and Statistic【dp+线段树】好题!好题!

E. Trains and Statistic time limit per test 2 seconds memory limit per test 256 megabytes ...

codeforces E. Trains and Statistic 线段树优化dp

E. Trains and Statistic time limit per test 2 seconds memory limit per test 256 megabytes inpu...

Codeforces 586D Phillip and Trains dfs —— Round #325 (Div. 2)

原题见CF 586D给一个3*n的地图,人s从最左边一列的某个位置出发,每次先向右走一步,再选择上、下、不动。接着连续的字母表示火车,均向左走两步。如此循环,人和火车不能相撞。问人是否能走到地图的最右...

Codeforces Round #325 (Div. 2)-D. Phillip and Trains

原题链接 D. Phillip and Trains time limit per test1 second memory limit per test256 megabytes inputs...

Codeforces Round #325 (Div. 2) D. Phillip and Trains (BFS)

一开始看这题,还以为是DP,后来写完记忆化搜索之后发现结果怎么也不对,仔细一想,无论时间复杂度还是状态转移好像都不想DP。    后来想了一下,发现应该是BFS(DFS的盲目性太大,而BFS则可以在最...

【Codeforces Round #325 (Div. 2)】D. Phillip and Trains

Codeforces Round #325 (Div. 2) 题目D. Phillip and Trains

codeforces round#353 trians and statistic dp+贪心+线段树

题目描述:有n个车站,第i(1                     sum = Σp(i , j) (1 思路:在车站i处,可以用一张车票到达[i + 1 , a[i]]中的一站,那...
  • jijijix
  • jijijix
  • 2016年10月11日 21:53
  • 83

Codeforces Round #297 (Div. 2)E. Anya and Cubes

题目链接:http://codeforces.com/problemset/problem/525/E 题意: 给定n个数,k个感叹号,常数S 下面给出这n个数。 目标: ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #353 (Div. 2) E. Trains and Statistic
举报原因:
原因补充:

(最多只允许输入30个字)