Codeforces Round #353 (Div. 2) E. Trains and Statistic (线段树+dp)
来源:互联网 发布:三星i9300软件下载 编辑:程序博客网 时间:2024/05/22 08:03
Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i + 1 to ai inclusive. No tickets are sold at the last station.
Let ρi, j be the minimum number of tickets one needs to buy in order to get from stations i to station j. As Vasya is fond of different useless statistic he asks you to compute the sum of all values ρi, j among all pairs 1 ≤ i < j ≤ n.
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of stations.
The second line contains n - 1 integer ai (i + 1 ≤ ai ≤ n), the i-th of them means that at the i-th station one may buy tickets to each station from i + 1 to ai inclusive.
Print the sum of ρi, j among all pairs of 1 ≤ i < j ≤ n.
44 4 4
6
52 3 5 5
17
In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6.
Consider the second sample:
- ρ1, 2 = 1
- ρ1, 3 = 2
- ρ1, 4 = 3
- ρ1, 5 = 3
- ρ2, 3 = 1
- ρ2, 4 = 2
- ρ2, 5 = 2
- ρ3, 4 = 1
- ρ3, 5 = 1
- ρ4, 5 = 1
Thus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17.
题解:有n个车站,,每个车站都有一个a[i],从该车站可以花费1的钱到达[ i+1, a[i] ]之间的任意车站,现在让你求d[i][j]的
和的最小值。
设dp[i]表示从i点出发到i+1,i+2,...,n的最小值。那么有dp[i]=dp[m]-(a[i]-m)+n-i,且m是i+1~a[i]之间的数,且a[m]最大
的。因为i < m < a[i] < a[m] < n。所以n-i表示的是 i~n每个站至少买一次票。a[i]-m表示m~a[i]这些站中,m到这些站就
买了一次票,再买一次就重复了,所以要减去重复的。
AC代码:
#pragma comment(linker, "/STACK:102400000,102400000")//#include<bits/stdc++.h>#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<cstring>#include<vector>#include<map>#include<cmath>#include<queue>#include<set>#include<stack>#include <utility> using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int,int> pii;typedef vector<int> vi;#define mst(a) memset(a, 0, sizeof(a))#define M_P(x,y) make_pair(x,y)#define in freopen("in.txt","r",stdin) #define rep(i,j,k) for (int i = j; i <= k; i++) #define per(i,j,k) for (int i = j; i >= k; i--) #define lson l , mid , rt << 1 #define rson mid + 1 , r , rt << 1 | 1 const int lowbit(int x) { return x&-x; } const double eps = 1e-8; const int INF = 1e9+7; const ll inf =(1LL<<62) ;const int MOD = 1e9+7; const ll mod = (1LL<<32);const int N =1e5+7; const int M=100010;const ll MAX=1e18;//const int maxn=1001; template <class T1, class T2>inline void getmax(T1 &a, T2 b) {if (b>a)a = b;} template <class T1, class T2>inline void getmin(T1 &a, T2 b) {if (b<a)a = b;}int read(){int v = 0, f = 1;char c =getchar();while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();}while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();return v*f;}ll dp[N],ans;int maxn[N*4], a[N];int n;void pushup(int rt){ if( a[maxn[rt<<1|1]] < a[maxn[rt<<1]]) maxn[rt]=maxn[rt<<1]; else maxn[rt]=maxn[rt<<1|1];}int query(int L,int R,int l,int r,int rt){ if(L<=l && R>=r) return maxn[rt]; int mid=(l+r)>>1, p=-1, q=-1; if(L<=mid) p=query(L,R,lson); if(R>mid) q=query(L,R,rson); if(p==-1) return q; if(q==-1) return p; return a[p]>a[q]?p:q;}void build(int l,int r,int rt){ if(l==r) { maxn[rt]=l; return; } int mid=(l+r)>>1; build(lson); build(rson); pushup(rt);}void solve(){ dp[n] =0; int m=0; for(int i=n-1;i>=1;--i) { m=query(i+1,a[i],1,n,1); dp[i]=dp[m]-(a[i]-m)+(n-i); ans+=dp[i]; }}int main(){ cin>>n; for(int i=1;i<n;i++){ cin>>a[i]; } a[n]=n; build(1,n,1); solve(); cout<<ans;}
- Codeforces Round #353 (Div. 2) E. Trains and Statistic (线段树+dp)
- Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心,DP,线段树
- Codeforces Round #353 (Div. 2)E. Trains and Statistic
- Codeforces Round #353 (Div. 2) E. Trains and Statistic
- codeforces E. Trains and Statistic 线段树优化dp
- codeforces 675-E Trains and Statistic 线段树+dp
- Codeforces Round #353 (Div. 2)E. Trains and Statistic(ST表+贪心)
- Codeforces 675E Trains and Statistic【dp+线段树】好题!好题!
- codeforces Trains and Statistic DP
- codeforces round#353 trians 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
- Codeforces Round #353 (Div. 2) E 线段树+dp
- Codeforecs 675E Trains and Statistic 贪心+DP
- Codeforces Round #271 (Div. 2) E Pillars(dp+线段树优化)
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
- Codeforces Round #271 (Div. 2) E. Pillars(线段树优化DP)
- Codeforces Round #271 (Div. 2) E. Pillars(线段树+DP)
- 循环语句
- GUi组件篇1
- JSP动作元素
- PATB1024:科学记数法
- ubuntu下jdk安装
- Codeforces Round #353 (Div. 2) E. Trains and Statistic (线段树+dp)
- Hadoop 2.6 MapReduce运行原理详解
- 入侵防御系统
- 生成验证码的几种方式
- noip模拟赛(一)魔法树
- 简单说说项目中资源存储与调用方式
- favicon.ico网站图标不显示
- 你真的了解一段Java程序的生命史吗
- 5.Eclipse 4 Application Model