CF 675ETrains and Statistic(线段树 + Dp)
来源:互联网 发布:金仕达期货交易软件 编辑:程序博客网 时间:2024/06/04 01:03
题目:
给你 n 个车站, 第 i 个车站能买到 i+1 到 A[i] 的票, 现在有 P[i][j] 为 车站 i 到 j 所需要的最少的买票次数。 求所有的 P[i][j] 的和。
分析:
首先, 对于在车站 i , 做到最后一站, 我们贪心买票的思路是 在 i+1 到 A[i] 车站中取最大值,在那一站再去买票, 即可最少到达最后一站。在求解过程中, 有很多重复步骤, 故可以Dp从后往前扫利用之前的状态。题目又是求解∑, 故Dpi 就表示 后缀和了。我们便可以很快得出Dp方程了Dp[i] = Dp[m] + n - i - (A[i] - m);而 m = max(A[i+1], A[i+2] ... A[A[i]]);m 用线段树维护就好了。
Code:
/** * @Author: Aoxuets * @DateTime: 2016-06-06 15:58:29 */#include <bits/stdc++.h>#define lson l, m, rt << 1#define rson m+1, r, rt << 1|1using namespace std;typedef long long LL;const int maxn = 100000 + 131;struct Node { int id, val; Node() {} Node(int i, int v) : id(i), val(v) {} bool operator < (const Node a) const { return val == a.val ? id < a.id : val < a.val; }} Sum[maxn << 2];int A[maxn];void PushUp(int rt) { Sum[rt] = max(Sum[rt<<1], Sum[rt<<1|1]);}void Build(int l, int r, int rt) { if(l == r) { Sum[rt] = Node(l, A[l]); return ; } int m = (l + r) >> 1; Build(lson), Build(rson); PushUp(rt);}Node Q_Max(int L, int R, int l, int r, int rt) { if(L <= l && r <= R) { return Sum[rt]; } int m = (l + r) >> 1; Node ret = Node(-1,-1); if(L <= m) ret = max(ret, Q_Max(L, R, lson)); if(R > m) ret = max(ret, Q_Max(L, R, rson)); return ret;}LL Dp[maxn];int main() { int n; cin >> n; for(int i = 1; i < n; ++i) { cin >> A[i]; } A[n] = n; Build(1, n, 1); LL Ans = 0; for(int i = n-1; i >= 1; --i) { int Max = Q_Max(i+1, A[i], 1, n, 1).id; Dp[i] = Dp[Max] + n - i - (A[i]-Max); Ans += Dp[i]; } cout << Ans << endl;}
0 0
- CF 675ETrains and Statistic(线段树 + Dp)
- codeforces 675-E Trains and Statistic 线段树+dp
- codeforces E. Trains and Statistic 线段树优化dp
- codeforces round#353 trians and statistic dp+贪心+线段树
- Codeforces 675E Trains and Statistic【dp+线段树】好题!好题!
- Codeforces Round #353 (Div. 2) E. Trains and Statistic (线段树+dp)
- Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心,DP,线段树
- CF 629 D. Babaei and Birthday Cake 线段树+DP
- CF #343 div2 D Babaei and Birthday Cake dp+线段树维护+(离散)
- cf - 629D Babaei and Birthday Cake(DP+线段树维护)
- codeforces Trains and Statistic DP
- Codeforecs 675E Trains and Statistic 贪心+DP
- 【34.54%】【codeforces 675E】Trains and Statistic
- Codeforces Round #271 (Div. 2)(dp,线段树good)(很好的一场cf)
- CF 256E Lucky Arrays(线段树+DP)
- CF 269D Maximum Waterfall(线段树,DP)
- CF 256E Lucky Arrarys 【线段树+DP】
- 线段树|dp——cf 487 B
- 打开或者关闭wifi
- 山东多校联合模拟赛 Day2
- 在陌生Linux环境查看Tomcat服务的方法
- 传智播客说:大学生们,先让自己值钱,再赚钱!
- java数据结构(二)——栈
- CF 675ETrains and Statistic(线段树 + Dp)
- Face Recognizer
- Foundation库下的NSString,NSArray, NSDictionary……可以被继承吗
- react-native 把图片转化base64字符串
- Unity 手势缩放UI
- windows linux共享
- VC读取文件的文件版本号
- cell自适应高度
- 传智播客助力数以万计的大学生,让就业不再迷茫!