Codeforces 675E Trains and Statistic【dp+线段树】好题!好题!
2017-03-15 18:52
375 查看
E. Trains and Statistic
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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.
Output
Print the sum of ρi, j among all pairs of
1 ≤ i < j ≤ n.
Examples
Input
Output
Input
Output
Note
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个车站,现在给出从1~N-1号车站买的票最远可以到达的站点编号,保证a【i】>=i+1.
现在设定p【i】【j】表示从i到j需要购买的最少票数。
求Σp【i】【j】(1<=i<j<=n);
思路(思路源自:http://www.cnblogs.com/zhangchengc919/p/5507077.html):
(这题确实有点难度啊,想了很久也没相对正确方向);
1、首先我们设定dp【i】表示p【i】【n】。
那么如果我们逆序处理的话,有dp【i】=dp【temp】+1;(希望下一步的下一步尽可能的远);
这里temp是a【i+1】,a【i+2】,a【i+3】.................a【a【i】】中的最大值的位子。
2、现在我们要求的是Σ,那么我们设定dp【i】表示Σp【j】【k】(i<=j<k<=n);
那么就有dp【i】=dp【temp】+(n-i)-(a【i】-temp);
查询temp位子可以logn线段树查询,那么总时间复杂度O(nlogn);
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<climits>
using namespace std;
#define ll __int64
#define lson l, m, rt<<1
#define rson m+1, r, (rt<<1)|1
int tree[111111*4];
int posn[111111*4];
void pushup(int rt)
{
if (tree[rt<<1] > tree[rt<<1|1])
{
tree[rt] = tree[rt<<1];
posn[rt] = posn[rt<<1];
}
else
{
tree[rt] = tree[rt<<1|1];
posn[rt] = posn[rt<<1|1];
}
}
void build(int l, int r, int rt)
{
if (l == r)
{
tree[rt]=0;
posn[rt] = l;
}
else
{
int m = (l + r) >> 1;
build(lson);
build(rson);
pushup(rt);
}
}
void update(int p, int val, int l, int r, int rt)
{
if (l == r)
{
tree[rt] = val;
}
else
{
int m = (l + r) >> 1;
if (p <= m)
{
update(p, val, lson);
}
else
{
update(p, val, rson);
}
pushup(rt);
}
}
int query(int L, int R, int l, int r, int rt, int *pos)
{
if (L <= l && r <= R)
{
*pos = posn[rt];
return tree[rt];
}
else
{
int m = (l + r) >> 1;
int ret1 = INT_MIN;
int ret2 = INT_MIN;
int pa, pb;
int *pos1 = &pa;
int *pos2 = &pb;
if (L <= m)
{
ret1 = query(L, R, lson, pos1);
}
if (R > m)
{
ret2 = query(L, R, rson, pos2);
}
if (ret1 > ret2)
{
*pos = pa;
}
else
{
*pos = pb;
ret1 = ret2;
}
return ret1;
}
}
int a[105000];
ll dp[105000];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(dp,0,sizeof(dp));
ll ans=0;
build(1,n,1);
for(int i=1;i<=n-1;i++)scanf("%d",&a[i]);
update(n,n,1,n,1);
for(int i=n-1;i>=1;i--)
{
int temp;
query(i+1,a[i],1,n,1,&temp);
dp[i]=dp[temp]+(n-i)-(a[i]-temp);
ans+=dp[i];
update(i,a[i],1,n,1);
}
printf("%I64d\n",ans);
}
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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.
Output
Print the sum of ρi, j among all pairs of
1 ≤ i < j ≤ n.
Examples
Input
4 4 4 4
Output
6
Input
5 2 3 5 5
Output
17
Note
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个车站,现在给出从1~N-1号车站买的票最远可以到达的站点编号,保证a【i】>=i+1.
现在设定p【i】【j】表示从i到j需要购买的最少票数。
求Σp【i】【j】(1<=i<j<=n);
思路(思路源自:http://www.cnblogs.com/zhangchengc919/p/5507077.html):
(这题确实有点难度啊,想了很久也没相对正确方向);
1、首先我们设定dp【i】表示p【i】【n】。
那么如果我们逆序处理的话,有dp【i】=dp【temp】+1;(希望下一步的下一步尽可能的远);
这里temp是a【i+1】,a【i+2】,a【i+3】.................a【a【i】】中的最大值的位子。
2、现在我们要求的是Σ,那么我们设定dp【i】表示Σp【j】【k】(i<=j<k<=n);
那么就有dp【i】=dp【temp】+(n-i)-(a【i】-temp);
查询temp位子可以logn线段树查询,那么总时间复杂度O(nlogn);
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<climits>
using namespace std;
#define ll __int64
#define lson l, m, rt<<1
#define rson m+1, r, (rt<<1)|1
int tree[111111*4];
int posn[111111*4];
void pushup(int rt)
{
if (tree[rt<<1] > tree[rt<<1|1])
{
tree[rt] = tree[rt<<1];
posn[rt] = posn[rt<<1];
}
else
{
tree[rt] = tree[rt<<1|1];
posn[rt] = posn[rt<<1|1];
}
}
void build(int l, int r, int rt)
{
if (l == r)
{
tree[rt]=0;
posn[rt] = l;
}
else
{
int m = (l + r) >> 1;
build(lson);
build(rson);
pushup(rt);
}
}
void update(int p, int val, int l, int r, int rt)
{
if (l == r)
{
tree[rt] = val;
}
else
{
int m = (l + r) >> 1;
if (p <= m)
{
update(p, val, lson);
}
else
{
update(p, val, rson);
}
pushup(rt);
}
}
int query(int L, int R, int l, int r, int rt, int *pos)
{
if (L <= l && r <= R)
{
*pos = posn[rt];
return tree[rt];
}
else
{
int m = (l + r) >> 1;
int ret1 = INT_MIN;
int ret2 = INT_MIN;
int pa, pb;
int *pos1 = &pa;
int *pos2 = &pb;
if (L <= m)
{
ret1 = query(L, R, lson, pos1);
}
if (R > m)
{
ret2 = query(L, R, rson, pos2);
}
if (ret1 > ret2)
{
*pos = pa;
}
else
{
*pos = pb;
ret1 = ret2;
}
return ret1;
}
}
int a[105000];
ll dp[105000];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(dp,0,sizeof(dp));
ll ans=0;
build(1,n,1);
for(int i=1;i<=n-1;i++)scanf("%d",&a[i]);
update(n,n,1,n,1);
for(int i=n-1;i>=1;i--)
{
int temp;
query(i+1,a[i],1,n,1,&temp);
dp[i]=dp[temp]+(n-i)-(a[i]-temp);
ans+=dp[i];
update(i,a[i],1,n,1);
}
printf("%I64d\n",ans);
}
}
相关文章推荐
- Codeforces 833B - The Bakery(DP+线段树)
- CodeForces - 833B The Bakery(dp+线段树的巧妙运用)
- Codeforces 834D - The Bakery 【DP+线段树】
- Codeforces 834D The Bakery(dp+线段树)
- CodeForces 833B Round#426 Div1B&Div2D The Bakery: DP+线段树
- Codeforces 834D The Bakery【Dp+线段树】好题~
- Codeforces 834D The Bakery【Dp+线段树】
- Codeforces 833B - The Bakery 【DP+线段树】
- codeforces 340D D. Bubble Sort Graph(dp+线段树)
- Codeforces 834D The Bakery【dp+线段树】
- 【codeforces 675E】【DP+线段树维护】【给出n个车站,并告诉你第i个车站有从i+1到a[i]个车站的直达票,问你所有车站到其余各个车站的最小车票花费和】
- CodeForces 629 D.Babaei and Birthday Cake(dp+线段树)
- Codeforces 671D. Roads in Yusland(树形DP+线段树)
- CodeForces - 946G Almost Increasing Array [DP+线段树]
- CodeForces 833 B.The Bakery(dp+线段树)
- Codeforces 483B - Friends and Presents(二分)
- CodeForces 387B George and Round
- CodeForces-546D.Soldier and Number Game
- Codeforces 756C Nikita and stack
- codeforces—— 439A —— Devu, the Singer and Churu, the Joker