题目链接:

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

题意:

对于第i个站,它与i+1到a[i]的站有路相连,先在求所有站点i到站点j的最短距离之和(1<=i<j<=n)

题解:

这种所有可能都算一遍就会爆的题目,有可能是可以转化为去求每个数对最后答案的贡献,用一些组合计数的方法一般就可以很快算出来。

这里用dp[i]表示第i个站点到后面的所有站的最短距离之和,明显,i+1到a[i]的站,一步就可以到,对于后面的那些点,贪心一下,一定是从max(a[i+1],a[a[i]])的那个点转移过去的,用m表示最大的那个位置,则有:

dp[i]=a[i]-i+dp[m]-(a[i]-m)+n-a[i];

最后求ans=sum(dp[1]...dp[n]);

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #define lson (o<<1)
  5. #define rson ((o<<1)|1)
  6. #define M (l+((r-l)>>1))
  7. using namespace std;
  8.  
  9. const int maxn=;
  10. typedef __int64 LL;
  11.  
  12. int n;
  13. int arr[maxn];
  14. LL dp[maxn];
  15. int posv[maxn<<],maxv[maxn<<];
  16.  
  17. int _p,_v;
  18. void update(int o,int l,int r){
  19. if(l==r){
  20. maxv[o]=_v;
  21. posv[o]=l;
  22. }else{
  23. if(_p<=M) update(lson,l,M);
  24. else update(rson,M+,r);
  25. if(maxv[lson]>maxv[rson]) posv[o]=posv[lson];
  26. else posv[o]=posv[rson];
  27. maxv[o]=max(maxv[lson],maxv[rson]);
  28. }
  29. }
  30.  
  31. int ql,qr,_pos;
  32. void query(int o,int l,int r){
  33. if(ql<=l&&r<=qr){
  34. if(_v<maxv[o]){
  35. _pos=posv[o]; _v=maxv[o];
  36. }
  37. else if(_v==maxv[o]){
  38. _pos=max(_pos,posv[o]);
  39. }
  40. }else{
  41. if(ql<=M) query(lson,l,M);
  42. if(qr>M) query(rson,M+,r);
  43. }
  44. }
  45.  
  46. void init(){
  47. memset(dp,,sizeof(dp));
  48. memset(maxv,,sizeof(maxv));
  49. }
  50.  
  51. int main(){
  52. while(scanf("%d",&n)==&&n){
  53. init();
  54. for(int i=;i<=n;i++){
  55. if(i<n) scanf("%d",&arr[i]);
  56. else arr[i]=n;
  57. _p=i,_v=arr[i]; update(,,n);
  58. }
  59. LL ans=;
  60. for(int i=n-;i>=;i--){
  61. _v=-,ql=i+,qr=arr[i]; query(,,n);
  62. dp[i]=_pos-i+dp[_pos]+n-arr[i];
  63. ans+=dp[i];
  64. }
  65. printf("%I64d\n",ans);
  66. }
  67. return ;
  68. }

Codeforces Round #353 (Div. 2) E. Trains and Statistic 线段树+dp的更多相关文章

  1. Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心

    E. Trains and Statistic 题目连接: http://www.codeforces.com/contest/675/problem/E Description Vasya comm ...

  2. Codeforces Round #426 (Div. 1) B The Bakery (线段树+dp)

    B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

  3. Codeforces Round #426 (Div. 2) D The Bakery(线段树 DP)

     The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard input ...

  4. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  5. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  6. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  7. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  8. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

  9. Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

随机推荐

  1. 网络流量监控工具iftop

    #-------------------网络流量监控工具iftop---------------##! /bin/sh #1.首先安装依赖关系包yum install flex byacc libpc ...

  2. 在SQL中取出字符串中数字部分或在SQL中取出字符部分

    在SQL中取出字符串中数字部分或在SQL中取出字符部分 编写人:CC阿爸 2013-10-18 近来在开发一个项目时,一包含数字的字符串,需要取出中间的数字部分进行排序.经过baidu搜索.并结合自己 ...

  3. [原]Python Web部署方式总结

    不要让服务器裸奔 学过PHP的都了解,php的正式环境部署非常简单,改几个文件就OK,用FastCgi方式也是分分钟的事情.相比起来,Python在web应用上的部署就繁杂的多,主要是工具繁多,主流服 ...

  4. C#之事件初步

    上文简述了委托,所谓的简述,只是说了一下如何使用委托,既然有了委托的基础,便可以稍微一探事件的机制. 事件,实际上是委托类型,事件处理函数如下: public delegate void MyHand ...

  5. Oracle 11g 执行计划管理2

    1.创建测试数据 SQL> conn NC50/NC50 Connected. SQL)); SQL> insert into tab1 select rownum,object_name ...

  6. Server Tomcat v7.0 Server at localhost failed to start解决办法

    今晚搞了下tomcat,在调试的时候发现报了这样一个错误Server Tomcat v7.0 Server at localhost failed to start 首先,确认了端口号8080是不是被 ...

  7. openerp 经典收藏 记录规则 – 销售只能看到自己的客户,经理可以看到全部(转载)

    记录规则 – 销售只能看到自己的客户,经理可以看到全部 原文地址:http://cn.openerp.cn/record_rule/ OpenERP中的权限管理有四个层次: 菜单级别: 即,不属于指定 ...

  8. Python核心编程--学习笔记--6--序列(上)字符串

    本章研究Python中的序列:字符串.列表和元组.因为这些类型其实都是由一些成员共同组成的一个序列整体,所以我们把它们统称为序列.序列的存储结构可以表示为: 1 序列 序列类型有着相同的访问模式:按下 ...

  9. C/C++走过的坑(基础问题篇)

    1.有符号int与无符号int比较 #define TOTOL_ELEMENTS (sizeof(a) / sizeof(a[0]) ); int main() { int a[] = {23,24, ...

  10. Python常用内建模块

    Python常用内建模块 datetime 处理日期和时间的标准库. 注意到datetime是模块,datetime模块还包含一个datetime类,通过from datetime import da ...