题目链接:

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. js-布尔值

    1.任何JavaScript的值都可以转换为布尔值 下面这些将会转换为false(假值): undefined null 0 -0 NaN "" //空字符串 所有其他值,包括所有 ...

  2. js日期格式化方法 dateFormatFn

    var dateFormatFn=function(val,fmt){ var _this = new Date(val); console.log(_this,_this.getFullYear() ...

  3. HDFS文件读写流程

    一.HDFS HDFS全称是Hadoop Distributed System.HDFS是为以流的方式存取大文件而设计的.适用于几百MB,GB以及TB,并写一次读多次的场合.而对于低延时数据访问.大量 ...

  4. C# 笛卡尔积

    void Main() { string[] str1 = { "a", "b" }; " }; string[] str3 = { "一& ...

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

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

  6. 迭代器、泛型和增强For

    Iterator hasNext  next Iterator 迭代器 Collection提供了一个遍历集合的通用方式,迭代器(Iterator). 获取迭代器的方式是使用Collection定义的 ...

  7. linux C socket

    socket套接字和管道同样可以提供进程内通信.但套接字更胜一筹,不同的进程可以跨越不同的主机(说白了,支持网络通信).使用套接字的知名程序:telnet.rlogin.ftp等. 你需要知道的一些基 ...

  8. Java入门到精通——调错篇之解决MyEclipse 输入注册码后:Enter or update your subscription information.问题

    这几天,我用MyEclipse做例子的时候总是出现下面图上面的提示: 不用看就是注册码到期了要注册.找了好几个注册码总是出现Enter or update your subscription info ...

  9. windows phone版的一个儿教app

    昨天下午看见一个园友写的一篇关于儿教的api,看了也就两三个接口,所以数据处理应该不会太复杂,主要是界面的效果,要求可能比较高.于是我就重新自己写了一个app,实现很简单,花的时间比较多的地方应该是在 ...

  10. scjp考试准备 - 7 - Java构造器

    题目——如下代码的执行结果: class Hello{ String title; int value; public Hello(){ title += " World!"; } ...