题目链接:

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]);

#include<iostream>
#include<cstdio>
#include<cstring>
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M (l+((r-l)>>1))
using namespace std; const int maxn=;
typedef __int64 LL; int n;
int arr[maxn];
LL dp[maxn];
int posv[maxn<<],maxv[maxn<<]; int _p,_v;
void update(int o,int l,int r){
if(l==r){
maxv[o]=_v;
posv[o]=l;
}else{
if(_p<=M) update(lson,l,M);
else update(rson,M+,r);
if(maxv[lson]>maxv[rson]) posv[o]=posv[lson];
else posv[o]=posv[rson];
maxv[o]=max(maxv[lson],maxv[rson]);
}
} int ql,qr,_pos;
void query(int o,int l,int r){
if(ql<=l&&r<=qr){
if(_v<maxv[o]){
_pos=posv[o]; _v=maxv[o];
}
else if(_v==maxv[o]){
_pos=max(_pos,posv[o]);
}
}else{
if(ql<=M) query(lson,l,M);
if(qr>M) query(rson,M+,r);
}
} void init(){
memset(dp,,sizeof(dp));
memset(maxv,,sizeof(maxv));
} int main(){
while(scanf("%d",&n)==&&n){
init();
for(int i=;i<=n;i++){
if(i<n) scanf("%d",&arr[i]);
else arr[i]=n;
_p=i,_v=arr[i]; update(,,n);
}
LL ans=;
for(int i=n-;i>=;i--){
_v=-,ql=i+,qr=arr[i]; query(,,n);
dp[i]=_pos-i+dp[_pos]+n-arr[i];
ans+=dp[i];
}
printf("%I64d\n",ans);
}
return ;
}

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. Sublime Text 2 常用快捷键

    Sublime Text 2 常用的快捷键 (不包含插件快捷键) Ctrl+P 打开文件搜索框,可以直接输入文件名搜索,或者输入@funcName 可以直接到函数定义处,输入#key 可以直接查找,输 ...

  2. Spring Richclient — 企业级富客户端开发框架介绍,第 1 部分

    Spring Richclient — 企业级富客户端开发框架介绍,第 1 部分 http://www.ibm.com/developerworks/cn/java/j-lo-spring-richc ...

  3. 05-Tom猫(UIImageView的简单运用)

    ViewController.h文件中: @interface ViewController : UIViewController @property (weak, nonatomic) IBOutl ...

  4. set 赋值(转载)

    名著<C#设计模式>第9章“观察者模式”涉及了标准的事件处理流程,作者在探讨属性值变更时给出一个如下示例代码(P73-74): pulbic abstract class TpeakFun ...

  5. [leetcode]_Roman to Integer

    题目:给定一个罗马数字串,转换为一个整数. 一开始没理解,以为是string to int.后来理解:罗马数字与阿拉伯数字的映射关系,见下图: 至此,题目的意思才掌握明白,用程序模拟这张表. 无可置否 ...

  6. wordpress学习-plugins-001

    plugins-插件 Akismet(Automattic Kismet)是应用广泛的一个垃圾留言过滤系统,其作者是大名鼎鼎的WordPress创始人Matt Mullenweg,Akismet也是W ...

  7. JSP标记

    JSP标记是JSP页面中很重要的组成部分,JSP标记包括指令标记.动作标记和自定义标记.其中自定义标记主要讲述与Tag文件有关的Tag标记. 一 指令标记page Page指令标记,简称page指令, ...

  8. DevExpress LookUpEdit和ComboBoxEdit部分用法

    LookUpEdit 1.绑定列 (注意点:LookUpEdit1的FieldName要和绑定的列明一致) 方式一: LookUpEdit1.Properties.DisplayMember = &q ...

  9. Oracle中Clob类型处理解析:ORA-01461:仅可以插入LONG列的LONG值赋值

    感谢原作者:破剑冰-Oracle中Clob类型处理解析 上一篇分析:ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值 最近为Clob字段在插入数据时发现当字符的字节数(一个半角字符一 ...

  10. 用Java简单实现C#的参数为Action<T> Function<T,boolean>扩展方法

    直接上代码 Blog.Java public class Blog { public Blog(int id,String name) { Id=id; Name=name; } public int ...