Codeforces 675E Trains and Statistic - 线段树 - 动态规划
题目传送门
快速的vjudge通道
快速的Codeforces通道
题目大意
有$n$个火车站,第$i$个火车站出售第$i + 1$到第$a_{i}$个火车站的车票,特殊地,第$n$个火车站不出售车票。
记$\rho_{i, j}$表示从第$i$个火车站出发,到第$j$个火车站最少要购买的车票数。
求$\sum_{i = 1}^{n - 1}\sum_{j=i + 1}^{n}\rho_{i,j}$。
对于在$[i + 1, a_{i}]$中的火车站,肯定直接购买一张从$i$出发的火车票,然后就可以到达了。
对于这个区间之外的呢?那么我们一定会贪心地选择先到达一个火车站$p$,$p$满足$i < p \leqslant a_{i}, a_{p} \geqslant a_{k}( i < k \leqslant a_{i})$。
考虑在$a_{i}$之后的某个火车站$k$,如果我们想要到达它,经过的路线就是$i \rightarrow p \rightarrow \cdots \rightarrow k$.
因此$\rho_{i, k} = \rho_{p, k} + 1\ \ \ (k > a_{i})$。
令$f[i] = \sum_{j = i+1}^{n}\rho_{i, j}$,那么转移的时候区间加一,然后减去多算的一段。
找$p$可以用线段树。
Code
/**
* Codeforces
* Problem#675E
* Accepted
* Time: 61ms
* Memory: 7896k
*/
#include <bits/stdc++.h>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean; #define pii pair<int, int>
#define fi first
#define sc second
#define ll long long typedef class SegTreeNode {
public:
pii val;
SegTreeNode *l, *r; SegTreeNode():l(NULL), r(NULL) { } void pushUp() {
val = max(l->val, r->val);
}
}SegTreeNode; SegTreeNode pool[];
SegTreeNode *top = pool; SegTreeNode* newnode() {
return top++;
} typedef class SegTree {
public:
SegTreeNode *rt; void build(SegTreeNode*& p, int l, int r, int* ar) {
p = newnode();
if (l == r) {
p->val = pii(ar[l], l);
return ;
}
int mid = (l + r) >> ;
build(p->l, l, mid, ar);
build(p->r, mid + , r, ar);
p->pushUp();
} pii query(SegTreeNode* p, int l, int r, int ql, int qr) {
if (ql == l && r == qr)
return p->val;
int mid = (l + r) >> ;
if (qr <= mid)
return query(p->l, l, mid, ql, qr);
else if (ql > mid)
return query(p->r, mid + , r, ql, qr);
pii a = query(p->l, l, mid, ql, mid), b = query(p->r, mid + , r, mid + , qr);
return max(a, b);
}
}SegTree; int n;
int *ar;
ll *f;
ll res = ;
SegTree st; inline void init() {
scanf("%d", &n);
ar = new int[(n + )];
f = new ll[(n + )];
for (int i = ; i < n; i++)
scanf("%d", ar + i);
} inline void solve() {
st.build(st.rt, , n, ar);
f[n] = ;
for (int i = n - , j; i; i--) {
j = st.query(st.rt, , n, i + , ar[i]).sc;
f[i] = f[j] + n - i - (ar[i] - j);
res += f[i];
}
printf(Auto"\n", res);
} int main() {
init();
solve();
return ;
}
Codeforces 675E Trains and Statistic - 线段树 - 动态规划的更多相关文章
- codeforces 675E Trains and Statistic 线段树+贪心统计
分析:这个题刚看起来无从下手 但是我们可以先简化问题,首先可以固定起点i,求出i+1到n的最小距离 它可以到达的范围是[i+1,a[i]],贪心的想,我们希望换一次车可以到达的距离尽量远 即:找一个k ...
- Codeforces 675E Trains and Statistic(DP + 贪心 + 线段树)
题目大概说有n(<=10W)个车站,每个车站i卖到车站i+1...a[i]的票,p[i][j]表示从车站i到车站j所需买的最少车票数,求所有的p[i][j](i<j)的和. 好难,不会写. ...
- codeforces 675E E. Trains and Statistic(线段树+dp)
题目链接: E. Trains and Statistic time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- CodeForces 675E Trains and Statistic
贪心,递推,线段树,$RMQ$. 假设我们记$ans[i]$是以$i$点为起点对答案的贡献,那么答案就是$\sum\limits_{i = 1}^n {ans[i]}$. $ans[i]$怎么计算呢? ...
- 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的最短距离之 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- Codeforces 750E New Year and Old Subsequence - 线段树 - 动态规划
A string t is called nice if a string "2017" occurs in t as a subsequence but a string &qu ...
- codeforces 22E XOR on Segment 线段树
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...
- Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
随机推荐
- 在PHP5.3以上版本运行ecshop出现的问题及解决方案
ecshop 问题一:商城首页报错 Strict Standards: Only variables should be passed by reference in D:\wamp\ecshop\ ...
- Version Control,Git的下载与安装
一.什么是Version Control(版本控制系统)? ——来自百度百科 以Git为例,是一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本管理.Git 是 Linus ...
- D Cloud of Hashtags Codeforces Round #401 (Div. 2)
Cloud of Hashtags [题目链接]Cloud of Hashtags &题意: 给你一个n,之后给出n个串,这些串的总长度不超过5e5,你要删除最少的单词(并且只能是后缀),使得 ...
- MySQL.配置MariaDB的字符集
配置MariaDB的字符集 环境: 操作系统:CentOS Linux release 7.x mariadb安装及配置 yum install mariadb-server mariadb #安装 ...
- 初识gispro
因为之前一直用的arcmap,由于项目中用到三维数据的服务发布,需要用到gispro.Gispro与arcmap用法还是有些不同.仅用此文来记录一些简易操作. Gispro简介 ArcGIS Pro是 ...
- 20171130-2-python orm
https://www.cnblogs.com/pycode/p/mysql-orm.html https://www.cnblogs.com/Hiberniane/archive/2011/01/3 ...
- django 网站的搭建(1)
使用 python django 模块来搭建自己的博客网站. 本人环境:阿里云centos7+django1.10+python3.5 使用工具:putty + winscp 1.首先安装python ...
- Lua用一维数组存储一个n阶方阵,输出这个方阵的正对角线上的数的和与反对角线上的数的和的差的绝对值。
arr = {, , , , , , , , -} function diagonalDifference(arr) dimesion = math.sqrt(#arr) arr1 = {} sum1 ...
- GAN的文献综述
1.Conditional Generative Adversarial Netwoks Describe GAN: Generative adversarial nets were recently ...
- winscp 怎么用私钥文件登录的,以.ppk结尾的密钥文件
Winscp默认用帐号密码登录,用私钥文件登录需要在高级选项里面的SSH--验证里面选择文件. Winscp使用的是putty作为SSH登录工具,而puttygen所生成的是以.ppk结尾的密钥文件. ...