【34.54%】【codeforces 675E】Trains and Statistic
来源:互联网 发布:盛势网络剧台湾未删减 编辑:程序博客网 时间:2024/05/21 06:52
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
【题解】
设dp[i]为i到i+1..n这些点的”总共”需要邮票的张数;
转移方程
dp[i] = dp[pos]+n-i-(a[i]-pos);
这里的pos,是(i+1..a[i])这个区间里面的a的值最大的下标;
表示的是从i开始往后转移的方式;
首先
从i->(i+1..a[i])都是只要一张票的。(这肯定是最优的)。所以不用从i到pos再转移到(pos+1..a[i]);
直接到pos+1..a[i]就可以了;
而a[i]之后的位置。则如果要到a[i]..a[pos]这段。则需要从i到pos再转移到。如下图。
上图中0->1、2、3、4、5、6这些都只要买一张票;
而dp[pos]包含了从pos->4、5的两张票;
如果再加上了dp[pos],则会重复;
因此要减去(a[i]-pos);
n-i+a[i]-pos
对应:
pos->a[i]后面的点;
然后从i连若干条边到pos;
因为有n-i+a[i]-pos个点。
所以总共要加n-i+a[i]-pos条边;
为什么选a最大的pos;
因为a[pos]是最大的。所以从pos可以走到更远的地方
如图,如果选择的是4。它最远能到8。则如果要到9就的再从8再买一张票到9;
而如果选择pos,则可以直接买一张票到9;这就节省了一张。
画图多理解下吧。
最后累加dp[1..n];
dp[n] = 0;
一开始dp[n-1]=1;
从n-2开始往前处理
获取某个区间内的最大值用ST算法就好(线段树is ok)
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <queue>#include <vector>#include <stack>#define LL long longusing namespace std;const int MAXN = 200000;const int MAX = 17;int n, a[MAXN],rmq[MAXN][MAX+3],pre[MAX+3];int need[MAXN];LL dp[MAXN],ans = 0;void input(int &r){ r = 0; char t = getchar(); while (!isdigit(t)) t = getchar(); int sign = 1; if (t == '-')sign = -1; while (!isdigit(t)) t = getchar(); while (isdigit(t)) r = r * 10 + t - '0', t = getchar(); r = r*sign;}int main(){ //freopen("F:\\rush.txt", "r", stdin); scanf("%d", &n); for (int i = 1; i <= n-1; i++) scanf("%d", &a[i]),rmq[i][0] = i; pre[0] = 1; for (int i = 1; i <= MAX; i++) pre[i] = pre[i - 1] * 2; int now = 1; need[1] = 0; for (int i = 2; i <= n; i++) if (i == pre[now]) need[i] = need[i - 1] + 1,now++; else need[i] = need[i - 1]; for (int l = 1;pre[l]<=n;l++) for (int i = 1; i + pre[l]-1<= n; i++) { int left = rmq[i][l-1]; int right = rmq[i + pre[l - 1]][l-1]; if (a[left] > a[right]) rmq[i][l] = left; else rmq[i][l] = right; } dp[n-1] = 1; ans = 1; for (int i = n - 2; i >= 1; i--) { int xy = need[a[i] - i+1]; int left = rmq[i][xy], right = rmq[a[i] - pre[xy] + 1][xy]; int pos; if (a[left] > a[right]) pos = left; else pos = right; dp[i] = dp[pos] + n - i - (a[i] - pos); ans += dp[i]; } printf("%I64d\n", ans); return 0;}
- 【34.54%】【codeforces 675E】Trains and Statistic
- codeforces 675-E Trains and Statistic 线段树+dp
- codeforces E. Trains and Statistic 线段树优化dp
- Codeforces Round #353 (Div. 2)E. Trains and Statistic
- Codeforces Round #353 (Div. 2) E. Trains and Statistic
- codeforces Trains and Statistic DP
- Codeforces 675E Trains and Statistic【dp+线段树】好题!好题!
- Codeforecs 675E Trains and Statistic 贪心+DP
- 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))
- CodeForces #325 D. Phillip and Trains
- codeforces 585B Phillip and Trains(bfs)
- 【33.33%】【codeforces 586D】Phillip and Trains
- Codeforces 586D Phillip and Trains
- Codeforces 586D Phillip and Trains
- CodeForces 586DPhillip and Trains(搜索)
- Nim 博弈1: hiho1163
- mac使用python实现网页截图
- PHP + jQuery Ajax文件无刷新上传文件
- Linux学习--第二天--分区、格式化、系统安装、vmware、远程管理工具
- ubuntu安装tensorflow时使用pip安装时出现SSLError[已解决]
- 【34.54%】【codeforces 675E】Trains and Statistic
- eclipse快捷键
- yii框架中省市三级联动
- 选择排序
- struts学习笔记:在jsp页面显示结果的三种方式
- 51nod 1201
- Java Web技术经验总结(一)
- 3-6 静态数据成员与静态成员函数
- 5pdshell15软件使用、遇到的错、页面编写案例、