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 ...
随机推荐
- BF算法和KMP算法(javascript版本)
var str="abcbababcbababcbababcabcbaba";//主串 var ts="bcabcbaba";//子串 function BF( ...
- zabbix介绍
zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix组件主要分两个: zabbix-server和zabbix-agent.支持的监控协议有ICM ...
- android任意view爆炸效果--第三方开源--ExplosionField
犹如天女散花一样,爆炸散列,比较有趣.Android ExplosionField在github上的项目主页是:https://github.com/tyrantgit/ExplosionField ...
- 射击比赛 (POJ 1719) 题解
[问题描述] 我们假设射击的目标是一个由R*C(2≤R≤C≤ 1000)个小方格组成的矩形网格.网格中每一列恰有2个白色的小方格和R-2个黑色的小方格.定义网格的行从顶至底编号为1~R,列从左至右编号 ...
- a 标签 跳转4种类型
<a href='' target=''>中的target有4种参数: '_self' , '_parent' , '_top' 和 '_blank' 在没有使用框架 ...
- xtrabackup之Innobackupex全备数据库
一.Xtrabackup是什么: Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品. ...
- 简单快速的开发框架-SRF
1.是什么 SRF(simply and rapid development framework) 一套基于asp.net mvc的开发框架,致力于提供简单.快速的企业应用开发方案,旨在解决企业应用开 ...
- SQL语句基础之 管理数据库,表 和 数据
MySQL中的基本sql语句 MySQL中主要有三个大的对象,第一个是数据库,有了数据库后,我们才能在数据库里面建表,因为Mysql是关系数据库,它的数据都会以记录的形式存到表里,所以第二个是表,然后 ...
- C语言中进制知识总结
1.什么是进制 进制是一种计数的方式,常用的有二进制.八进制.十进制.十六进制.任何数据在计算机内存中都是以二进制的形式存放的. 我对进制的个人理解,二进制数是以2为计算单元,满2进1位的数:八进制数 ...
- Linux软链接与硬链接
1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接]硬连接指通过索引节点 ...