Codeforces Round #353 (Div. 2)E. Trains and Statistic(ST表+贪心)
来源:互联网 发布:网络录音电话 编辑:程序博客网 时间:2024/05/22 16:44
题意:有n个车站,每个车站有一个可达区间[i+1,ai],问所有车站的最小换站距离之和。
分析:设i为起点,k为[i+1,a[i]]中a[k]最大的车站编号,则对于任意j>ai,选择从k换站一定是最好的。
#include <set>#include <map>#include <stack>#include <queue>#include <deque>#include <cmath>#include <vector>#include <string>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define L(i) i<<1#define R(i) i<<1|1#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-9#define maxn 1000100#define MOD 1000000007int n,a[maxn];int st[maxn][30];long long dp[maxn];void init(){ for(int j = 1; j < 20; j++) for(int i = 1; i+(1<<j)-1 < n; i++) st[i][j] = a[st[i][j-1]]>a[st[i+(1<<(j-1))][j-1]]?st[i][j-1]:st[i+(1<<(j-1))][j-1];}int solve(int x,int y){ int len = log2(y-x+1); return a[st[x][len]] > a[st[y-(1<<len)+1][len]]?st[x][len]:st[y-(1<<len)+1][len];}int main(){ int t; while(scanf("%d",&n) != EOF) { for(int i = 1; i < n; i++) { scanf("%d",&a[i]); st[i][0] = i; } init(); long long ans = 0; dp[n] = 0; for(int i = n-1; i > 0; i--) { if(a[i] == n) dp[i] = (long long)(n-i); else { int k = solve(i+1,a[i]); dp[i] = dp[k] + (1ll)*(k - i + n - a[i]); } ans += dp[i]; } printf("%lld\n",ans); } return 0;}
0 0
- Codeforces Round #353 (Div. 2)E. Trains and Statistic(ST表+贪心)
- Codeforces Round #353 (Div. 2)E. Trains and Statistic
- Codeforces Round #353 (Div. 2) E. Trains and Statistic
- Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心,DP,线段树
- Codeforces Round #353 (Div. 2) E. Trains and Statistic (线段树+dp)
- Codeforces Round #353 (Div. 2) E. Trains and Statistic(求d[i][j]的和的最小值(1<=i<=n,i+1<=j<=n))
- 【34.54%】【codeforces 675E】Trains and Statistic
- Codeforecs 675E Trains and Statistic 贪心+DP
- codeforces E. Trains and Statistic 线段树优化dp
- codeforces 675-E Trains and Statistic 线段树+dp
- codeforces Trains and Statistic DP
- 【Codeforces Round #325 (Div. 2)】D. Phillip and Trains
- Codeforces Round #325 (Div. 2) D. Phillip and Trains
- Codeforces Round #325 (Div. 2)-D. Phillip and Trains
- codeforces round#353 trians and statistic dp+贪心+线段树
- Codeforces 675E Trains and Statistic【dp+线段树】好题!好题!
- Codeforces 586D Phillip and Trains dfs —— Round #325 (Div. 2)
- Codeforces Round #325 (Div. 2) D. Phillip and Trains (BFS)
- Android中Adapter的源码分析以及其中的陷阱
- HDU 5122 K.Bro Sorting(2014亚洲区北京站现场赛)
- C#实现百度地图瓦片下载器
- spoj 001
- 算法导论之线性时间排序(2)
- Codeforces Round #353 (Div. 2)E. Trains and Statistic(ST表+贪心)
- scope上绑定的函数找不到
- 好坑的题目
- 二进制与16进制的对应关系
- 算法导论之线性时间排序(3)
- 梯度(Gradient Descent) 方向梯度 (directional derivative) 以及 gradient descent梯度下降算法(1)
- hdu5831 Rikka with Parenthesis II 2016 Multi-University Training Contest 8 解题报告
- HDU1796 How many integers can you find(容斥)
- Linux的根文件系统