E. Trains and Statistic

题目连接:

http://www.codeforces.com/contest/675/problem/E

Description

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.

Sample Input

4

4 4 4

Sample Output

6

Hint

题意

你可以从第i个城市买的从i到[i+1,a[i]]的车票,现在Pij表示从i到j的最小车票花费

现在让你求sigma p[i][j]

题解:

考虑 dp[i]表示从i到[i+1,n]的p[i][j]和

那么如果j<=a[i]的话,就+=1,否则就贪心找到[i+1,a[i]]里面a[i]最大的那个车站,转移到这个车站去

dp[i][j] = dp[m][j]+1,这样贪心肯定是最小的

然后根据这个莽一波dp就好了

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e5+7;
  4. typedef pair<int,int> SgTreeDataType;
  5. struct treenode
  6. {
  7. int L , R ;
  8. SgTreeDataType sum;
  9. void update(int v)
  10. {
  11. sum=make_pair(v,L);
  12. }
  13. };
  14. treenode tree[maxn*4];
  15. inline void push_down(int o)
  16. {
  17. }
  18. inline void push_up(int o)
  19. {
  20. if(tree[2*o].sum.first<=tree[2*o+1].sum.first)
  21. tree[o].sum.second=tree[2*o+1].sum.second;
  22. else
  23. tree[o].sum.second=tree[2*o].sum.second;
  24. tree[o].sum.first = max(tree[2*o].sum.first,tree[2*o+1].sum.first);
  25. }
  26. inline void build_tree(int L , int R , int o)
  27. {
  28. tree[o].L = L , tree[o].R = R,tree[o].sum = make_pair(0,0);
  29. if (R > L)
  30. {
  31. int mid = (L+R) >> 1;
  32. build_tree(L,mid,o*2);
  33. build_tree(mid+1,R,o*2+1);
  34. }
  35. }
  36. inline void update(int QL,int QR,int v,int o)
  37. {
  38. int L = tree[o].L , R = tree[o].R;
  39. if (QL <= L && R <= QR) tree[o].update(v);
  40. else
  41. {
  42. push_down(o);
  43. int mid = (L+R)>>1;
  44. if (QL <= mid) update(QL,QR,v,o*2);
  45. if (QR > mid) update(QL,QR,v,o*2+1);
  46. push_up(o);
  47. }
  48. }
  49. inline pair<int,int> query(int QL,int QR,int o)
  50. {
  51. int L = tree[o].L , R = tree[o].R;
  52. if (QL <= L && R <= QR) return tree[o].sum;
  53. else
  54. {
  55. push_down(o);
  56. int mid = (L+R)>>1;
  57. SgTreeDataType res = make_pair(-1,0);
  58. if (QL <= mid)
  59. {
  60. pair<int,int> tmp = query(QL,QR,2*o);
  61. if(res.first<=tmp.first)
  62. res=tmp;
  63. }
  64. if (QR > mid)
  65. {
  66. pair<int,int> tmp = query(QL,QR,2*o+1);
  67. if(res.first<=tmp.first)
  68. res=tmp;
  69. }
  70. push_up(o);
  71. return res;
  72. }
  73. }
  74. long long dp[maxn];
  75. int a[maxn];
  76. int main()
  77. {
  78. int n;
  79. scanf("%d",&n);
  80. for(int i=1;i<=n-1;i++)scanf("%d",&a[i]);
  81. a[n]=n;
  82. build_tree(1,n,1);
  83. for(int i=1;i<=n;i++)update(i,i,a[i],1);
  84. long long ans = 0;
  85. for(int i=n-1;i>=1;i--)
  86. {
  87. pair<int,int> tmp = query(i+1,a[i],1);
  88. int m=tmp.second;
  89. dp[i]=dp[m]-(a[i]-m)+n-i;
  90. ans+=dp[i];
  91. }
  92. cout<<ans<<endl;
  93. }

Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心的更多相关文章

  1. Codeforces Round #353 (Div. 2) E. Trains and Statistic 线段树+dp

    题目链接: http://www.codeforces.com/contest/675/problem/E 题意: 对于第i个站,它与i+1到a[i]的站有路相连,先在求所有站点i到站点j的最短距离之 ...

  2. Codeforces Round #288 (Div. 2) E. Arthur and Brackets [dp 贪心]

    E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input stand ...

  3. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. Codeforces Round #353 (Div. 2)

    数学 A - Infinite Sequence 等差数列,公差是0的时候特判 #include <bits/stdc++.h> typedef long long ll; const i ...

  6. Codeforces Round #353 (Div. 2) C Money Transfers

    题目链接: http://www.codeforces.com/contest/675/problem/C 题意: 给一个数组,每个数与他相邻的数相连,第一个与最后一个相连,每个数的数值可以左右移动, ...

  7. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  8. Codeforces Round #353 (Div. 2) C. Money Transfers (思维题)

    题目链接:http://codeforces.com/contest/675/problem/C 给你n个bank,1~n形成一个环,每个bank有一个值,但是保证所有值的和为0.有一个操作是每个相邻 ...

  9. Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)

    题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...

随机推荐

  1. vs2012 连接oracle11g 及数据的insert及select 的总结

    下载链接Oracle 11g所需的驱动ODTwithODAC1120320_32bit,下载链接为http://www.oracle.com/technetwork/topics/dotnet/uti ...

  2. 2015 Dhaka

    2015 Dhaka A - Automatic Cheater Detection solution 模拟计数. B - Counting Weekend Days solution 模拟计数. C ...

  3. python网络编程--进程池

    一:进程池 进程池内部维护一个进程序列,当使用时,则去进程池中获取一个进程, 如果进程池序列中没有可供使用的进进程,那么程序就会等待,直到进程池中有可用进程为止. 进程池中有两个方法: apply a ...

  4. js函数前加分号和感叹号的作用

    js函数前加分号和感叹号是什么意思?有什么用? 一般看JQuery插件里的写法是这样的 (function($) { //... })(jQuery); 今天看到bootstrap的javascrip ...

  5. 洛谷P2300 合并神犇

    传送门啦 分析: 刚开始读完题后感觉很懵,怎么算都不是3,结果发现题目理解错了.题目要求的是求一个不降的序列,不是递减的(发现自己好傻) 看明白题就好做了吧.经典的区间dp题,合并果子大家应该都做过, ...

  6. Oracle学习笔记:wm_concat函数合并字段

    在Oracle中使用wm_concat(column)可以实现字段的分组合并,逗号分隔. 例如,现有表temp_cwh_test: -- 创建临时表 create table temp_cwh_tes ...

  7. TreeMap和TreeSet在排序时如何比较元素?Collections工具类中的sort()方法如何比较元素?

    TreeSet要求存放的对象所属的类必须实现Comparable接口,该接口提供了比较元素的compareTo()方法,当插入元素时会回调该方法比较元素的大小.TreeMap要求存放的键值对映射的键必 ...

  8. CCF CSP 201703-5 引水入城(50分)

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201703-5 引水入城 问题描述 MF城建立在一片高原上.由于城市唯一的水源是位于河谷地带的 ...

  9. day5模块学习--sys模块

    sys模块 sys模块是处理与系统相关的模块,sys(system),下面来看看sys模块常用的方法: 1.sys.argv         #命令行参数list,第一个元素是程序本身路径 2.sys ...

  10. ASP.NET:MVC中文件上传与地址变化处理

    目录 文件的上传和路径处理必须解决下面列出的实际问题: 1.重复文件处理 2.单独文件上传 3.编辑器中文件上传 4.处理文章中的图片路径 5.处理上传地址的变化 一.上传文件和重复文件处理 文件处理 ...