题目链接:

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. windwos异地备份Mysql数据库

    @echo off@title Mysqlbackup echo MySQL数据库备份 echo ***********************echo Today %date%echo Time % ...

  2. C#中判断文件夹中存在某个txt文本

    strFileName="D:\\strarray.txt"; if (File.Exists(strFileName))//判断文件是否存在 { }

  3. JS模块化工具requirejs教程(一):初识requirejs

    随着网站功能逐渐丰富,网页中的js也变得越来越复杂和臃肿,原有通过script标签来导入一个个的js文件这种方式已经不能满足现在互联网开发模式,我们需要团队协作.模块复用.单元测试等等一系列复杂的需求 ...

  4. JSON.stringify 语法实例讲解

    语法:  JSON.stringify(value [, replacer] [, space]) value:是必选字段.就是你输入的对象,比如数组,类等. replacer:这个是可选的.它又分为 ...

  5. SQL Server自增长列插入指定值 -- SET IDENTITY_INSERT ON|OFF(转)

    想要将值插入到自动编号(或者说是标识列,IDENTITY)中去,需要设定 SET IDENTITY_INSERT 示例: 1.首先建立一个有标识列的表:CREATE TABLE products (i ...

  6. 用Python的smtp模块发邮件的示例

    # -*- coding: UTF-8 -*- import smtplib from email.mime.multipart import MIMEMultipart from email.mim ...

  7. 集合删数 (vijos 1545) 题解

    [问题描述] 一个集合有如下元素:1是集合元素:若P是集合的元素,则2 * P +1,4*P+5也是集合的元素,取出此集合中最小的K个元素,按从小到大的顺序组合成一个多位数,现要求从中删除M个数位上的 ...

  8. Oracle Database Cloud Services

    Oracle 开始也把数据库服务作为PaaS 服务,好吧 Oracle 叫做 DBaaS,数据库服务 https://cloud.oracle.com/database?tabID=138367891 ...

  9. Map和HashMap

    通过查询JDK帮助文档,我们可以得知Map的说明.方法等 import java.util.Map; import java.util.HashMap; class Test{ public stat ...

  10. 线程操作API

    线程操作API 1.currentThread 2.getId() .getName().getPriority().getStart.isAlive().isDaemon().isInterrupt ...