关闭

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

标签: ACM好题
314人阅读 评论(0) 收藏 举报
分类:

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;
}


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

【Codeforces Round 375 (Div 2) E】【欧拉回路Fleury算法 或网络流】One-Way Reform 每条边定向使得最多的点满足入度=出度

E. One-Way Reform time limit per test 2 seconds memory limit per test 256 megabytes ...
  • snowy_smile
  • snowy_smile
  • 2016-10-04 10:20
  • 738

Codeforces Round #406 (Div. 2):C. Berzerk(记忆化搜索解决博弈问题)

C. Berzerk time limit per test 4 seconds memory limit per test 256 megabytes input standard in...
  • Jaihk662
  • Jaihk662
  • 2017-03-24 15:23
  • 805

【专题】线段树

单点更新 最最基础的线段树,只更新叶子节点,然后把信息用PushUP(int r)这个函数更新上来。 hdu1166 敌兵布阵 线段树 hdu 1166 敌兵布阵 单点更新区间求和 hdu1754 ...
  • cyendra
  • cyendra
  • 2013-05-12 17:33
  • 739

【CF 140E】New Year Garland(第二类斯特林(Stirling)数+DP+容斥)

【CF 140E】New Year Garland(第二类斯特林(Stirling)数+DP+容斥) E. New Year Garlandtime limit per test5 secondsme...
  • ChallengerRumble
  • ChallengerRumble
  • 2016-08-08 14:45
  • 689

数学推公式——较难——Codeforces Round #187 (Div. 2)

题目链接: http://codeforces.com/contest/315/problem/C C. Sereja and Contest time limi...
  • CS_liuqing
  • CS_liuqing
  • 2013-06-16 15:34
  • 2001

Codeforces Round #244 (Div. 2)(强连通分量,后缀数组)

A. Police Recruits #include #include #include #include using namespace std; int n; int main() { ...
  • u010660276
  • u010660276
  • 2014-05-23 20:08
  • 422

Codeforces Round #248 (Div. 2) B题 【数据结构:树状数组】

题目链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),其中有m(1 ≤ m ≤...
  • u013912596
  • u013912596
  • 2014-05-24 18:18
  • 708

Codeforces Round #312 (Div. 2) (第三题是位运算,好题)

比赛:点击打开链接 558A - Lala Land and Apple Trees 分析:从0坐标分开,负半轴一个数组,正半轴一个数组,来记录果树的左边和数量,可以用结构体数组来存储数据,其...
  • loveyou11111111
  • loveyou11111111
  • 2015-08-31 16:30
  • 158

【打CF,学算法——三星级】CodeForces 689C Mike and Chocolate Thieves (二分)

题目链接:CF 689C 题面: C. Mike and Chocolate Thieves time limit per test 2 seconds memory limit ...
  • David_Jett
  • David_Jett
  • 2016-07-08 10:55
  • 795

Codeforces Round #303 (Div. 2) E 最短路迪杰斯特拉(小根堆实现)

链接:戳这里 E. Paths and Trees time limit per test3 seconds memory limit per test256 megabytes...
  • libin66
  • libin66
  • 2016-07-22 17:29
  • 289
    个人资料
    • 访问:102997次
    • 积分:4450
    • 等级:
    • 排名:第7801名
    • 原创:363篇
    • 转载:2篇
    • 译文:0篇
    • 评论:42条
    最新评论