Codeforces Round #353 (Div. 2) E. Trains and Statistic
来源:互联网 发布:淘宝卖家微淘入口 编辑:程序博客网 时间:2024/05/01 21:34
Vasya commutes by train every day. There are n train stations in the city, and at thei-th station it's possible to buy only tickets to stations fromi + 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 stationsi 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 pairs1 ≤ i < j ≤ n.
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), thei-th of them means that at the i-th station one may buy tickets to each station from i + 1 to ai inclusive.
Print the sum of ρi, j among all pairs of1 ≤ i < j ≤ n.
44 4 4
6
52 3 5 5
17
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 is6, 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
- Codeforces Round #353 (Div. 2) E. Trains and Statistic
- Codeforces Round #353 (Div. 2) E. Trains and Statistic (线段树+dp)
- Codeforces Round #353 (Div. 2)E. Trains and Statistic(ST表+贪心)
- Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心,DP,线段树
- Codeforces Round #353 (Div. 2) E. Trains and Statistic(求d[i][j]的和的最小值(1<=i<=n,i+1<=j<=n))
- 【34.54%】【codeforces 675E】Trains and Statistic
- codeforces E. Trains and Statistic 线段树优化dp
- codeforces 675-E Trains and Statistic 线段树+dp
- codeforces Trains and Statistic DP
- 【Codeforces Round #325 (Div. 2)】D. Phillip and Trains
- Codeforces Round #325 (Div. 2) D. Phillip and Trains
- Codeforces Round #325 (Div. 2)-D. Phillip and Trains
- Codeforces 675E Trains and Statistic【dp+线段树】好题!好题!
- Codeforces 586D Phillip and Trains dfs —— Round #325 (Div. 2)
- Codeforces Round #325 (Div. 2) D. Phillip and Trains (BFS)
- Codeforecs 675E Trains and Statistic 贪心+DP
- Codeforces Round #353 (Div. 2)E
- 第12周课后实践(1)
- 关于FM1208之类的CPU卡的总结说明
- 新眼保操
- Linux下安装配置FastDFS
- 学习MyBatis过程中的记录之实体类与表的对应关系
- Codeforces Round #353 (Div. 2) E. Trains and Statistic
- ARM平台device tree是如何工作的?(一)
- 总结libevent安装方法
- 第十四周项目一 排序函数模板
- shell 两数相减取正值
- 【JAVA】JAVA调用浏览器打开链接代码
- ViewPager小圆点的绘制及用法
- 【JavaWeb开发】HTML/JSP中嵌入CSS样式表(相对路径)
- 【Andorid学习笔记】eclipse-bundle包SDK目录结构