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的最短距离之和(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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- 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. 这题 ...
- 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 ...
随机推荐
- c3p0配置xml
c3p0-config.xml <c3p0-config> <default-config> <property name="automaticTestTabl ...
- C#使用结构来传递多个参数
当参数超过5个时,建议用结构来传递多个参数. 示例代码如下: public struct MyStruct { public string str; public int number; } clas ...
- mariadb的explain分析及InnoDB存储引擎
id: 当前查询语句中,每个SELECT语句的编号, id: 1 表示简单类型的查询 复杂类型的查询有三种:简单子查询,用于FROM中的子查询,联合查询:UNION 注意:UNION查询的分 ...
- Java 连接MongoDB
1.驱动 通过java连接MongoDB需要一个java版的驱动 下载地址:http://mongodb.github.io/mongo-java-driver/ 2.连接MongoDB 通过 com ...
- php header 函数详解
网页的缓存是由 HTTP消息头中的“Cache-control”来控制的,常见的取值有private.no-cache.max-age.must- revalidate等,默认为private.其作用 ...
- Android VideoView简单播放视频
给Android VideoView一个文件目录,就可以直接播放智能设备中的视频文件,现在以播放事先用手机拍好并重命名的视频文件test.mp4为例.(1) 需要在布局文件中写一个ViedoView: ...
- CentOS7.0安装与配置Tomcat-7
解决权限不够 #chmod a+x filename 安装说明 安装环境:CentOS-7.0.1406安装方式:源码安装 软件:apache-tomcat-7.0.29.tar.gz 下载地址:ht ...
- 成长记录 if语句输出 由大到小的数字
#include<stdio.h> void main() { float a,b,c,d,e,f,g,t; scanf("%f,%f,%f,%f,%f,%f,%f", ...
- CK表达式编辑器
1. 什么是表达式编辑器? 这个工具允许技术员传入一系列的参数,由用户编辑一个公式返回一种特定的结果.之所以需要使用表达式编辑器,就是因为用户编辑的公式经常变,技术员无法想出一办法来适应用户 ...
- JSAPI_Ticket签名
1.微信 JS 接口签名校验工具 2.具体开发 2.1 获取access_token,然后jsapi_ticket /** * 获取access_token,然后jsapi_ticket */ pri ...