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 ...
随机推荐
- 网络流量监控工具iftop
#-------------------网络流量监控工具iftop---------------##! /bin/sh #1.首先安装依赖关系包yum install flex byacc libpc ...
- 在SQL中取出字符串中数字部分或在SQL中取出字符部分
在SQL中取出字符串中数字部分或在SQL中取出字符部分 编写人:CC阿爸 2013-10-18 近来在开发一个项目时,一包含数字的字符串,需要取出中间的数字部分进行排序.经过baidu搜索.并结合自己 ...
- [原]Python Web部署方式总结
不要让服务器裸奔 学过PHP的都了解,php的正式环境部署非常简单,改几个文件就OK,用FastCgi方式也是分分钟的事情.相比起来,Python在web应用上的部署就繁杂的多,主要是工具繁多,主流服 ...
- C#之事件初步
上文简述了委托,所谓的简述,只是说了一下如何使用委托,既然有了委托的基础,便可以稍微一探事件的机制. 事件,实际上是委托类型,事件处理函数如下: public delegate void MyHand ...
- Oracle 11g 执行计划管理2
1.创建测试数据 SQL> conn NC50/NC50 Connected. SQL)); SQL> insert into tab1 select rownum,object_name ...
- Server Tomcat v7.0 Server at localhost failed to start解决办法
今晚搞了下tomcat,在调试的时候发现报了这样一个错误Server Tomcat v7.0 Server at localhost failed to start 首先,确认了端口号8080是不是被 ...
- openerp 经典收藏 记录规则 – 销售只能看到自己的客户,经理可以看到全部(转载)
记录规则 – 销售只能看到自己的客户,经理可以看到全部 原文地址:http://cn.openerp.cn/record_rule/ OpenERP中的权限管理有四个层次: 菜单级别: 即,不属于指定 ...
- Python核心编程--学习笔记--6--序列(上)字符串
本章研究Python中的序列:字符串.列表和元组.因为这些类型其实都是由一些成员共同组成的一个序列整体,所以我们把它们统称为序列.序列的存储结构可以表示为: 1 序列 序列类型有着相同的访问模式:按下 ...
- C/C++走过的坑(基础问题篇)
1.有符号int与无符号int比较 #define TOTOL_ELEMENTS (sizeof(a) / sizeof(a[0]) ); int main() { int a[] = {23,24, ...
- Python常用内建模块
Python常用内建模块 datetime 处理日期和时间的标准库. 注意到datetime是模块,datetime模块还包含一个datetime类,通过from datetime import da ...