Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心
E. Trains and Statistic
题目连接:
http://www.codeforces.com/contest/675/problem/E
Description
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.
Sample Input
4
4 4 4
Sample Output
6
Hint
题意
你可以从第i个城市买的从i到[i+1,a[i]]的车票,现在Pij表示从i到j的最小车票花费
现在让你求sigma p[i][j]
题解:
考虑 dp[i]表示从i到[i+1,n]的p[i][j]和
那么如果j<=a[i]的话,就+=1,否则就贪心找到[i+1,a[i]]里面a[i]最大的那个车站,转移到这个车站去
dp[i][j] = dp[m][j]+1,这样贪心肯定是最小的
然后根据这个莽一波dp就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
typedef pair<int,int> SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum;
void update(int v)
{
sum=make_pair(v,L);
}
};
treenode tree[maxn*4];
inline void push_down(int o)
{
}
inline void push_up(int o)
{
if(tree[2*o].sum.first<=tree[2*o+1].sum.first)
tree[o].sum.second=tree[2*o+1].sum.second;
else
tree[o].sum.second=tree[2*o].sum.second;
tree[o].sum.first = max(tree[2*o].sum.first,tree[2*o+1].sum.first);
}
inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].sum = make_pair(0,0);
if (R > L)
{
int mid = (L+R) >> 1;
build_tree(L,mid,o*2);
build_tree(mid+1,R,o*2+1);
}
}
inline void update(int QL,int QR,int v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].update(v);
else
{
push_down(o);
int mid = (L+R)>>1;
if (QL <= mid) update(QL,QR,v,o*2);
if (QR > mid) update(QL,QR,v,o*2+1);
push_up(o);
}
}
inline pair<int,int> query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].sum;
else
{
push_down(o);
int mid = (L+R)>>1;
SgTreeDataType res = make_pair(-1,0);
if (QL <= mid)
{
pair<int,int> tmp = query(QL,QR,2*o);
if(res.first<=tmp.first)
res=tmp;
}
if (QR > mid)
{
pair<int,int> tmp = query(QL,QR,2*o+1);
if(res.first<=tmp.first)
res=tmp;
}
push_up(o);
return res;
}
}
long long dp[maxn];
int a[maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n-1;i++)scanf("%d",&a[i]);
a[n]=n;
build_tree(1,n,1);
for(int i=1;i<=n;i++)update(i,i,a[i],1);
long long ans = 0;
for(int i=n-1;i>=1;i--)
{
pair<int,int> tmp = query(i+1,a[i],1);
int m=tmp.second;
dp[i]=dp[m]-(a[i]-m)+n-i;
ans+=dp[i];
}
cout<<ans<<endl;
}
Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心的更多相关文章
- 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 Round #288 (Div. 2) E. Arthur and Brackets [dp 贪心]
E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input stand ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- Codeforces Round #353 (Div. 2)
数学 A - Infinite Sequence 等差数列,公差是0的时候特判 #include <bits/stdc++.h> typedef long long ll; const i ...
- Codeforces Round #353 (Div. 2) C Money Transfers
题目链接: http://www.codeforces.com/contest/675/problem/C 题意: 给一个数组,每个数与他相邻的数相连,第一个与最后一个相连,每个数的数值可以左右移动, ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- Codeforces Round #353 (Div. 2) C. Money Transfers (思维题)
题目链接:http://codeforces.com/contest/675/problem/C 给你n个bank,1~n形成一个环,每个bank有一个值,但是保证所有值的和为0.有一个操作是每个相邻 ...
- Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)
题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...
随机推荐
- aarch64_c1
CBFlib-0.9.5.15-3.fc26.aarch64.rpm 2017-02-06 06:55 393K fedora Mirroring Project CBFlib-devel-0.9.5 ...
- LINUX修改、增加IP的方法,一张网卡绑定多个IP/漂移IP【转】
临时增加IP命令:ifconfig eth0:1 ip地址 netmask 子网码 broadcast 广播地址 gateway 网关 ifconfig eth0:1 10.1.104.65 net ...
- avalonJS-源码阅读(一)
写angularJS源码阅读系列的时候,写的太垃圾了.一个月后看,真心不忍直视,以后有机会的话得重写.这次写avalonJS,希望能在代码架构层面多些一点,少上源码.多写思路. avalon暴露句柄方 ...
- 【不知道是啥的NOIP模拟赛】网络入侵
题意是这样的: 给你一棵树,每个边有一个初始的0/1边权.你希望把它弄成一个给定的样子. 你每次可以选一条树链取反,然后问你最少要操作几次. ----------------------------- ...
- java基础56 HTML5的标签知识(网页知识)
本文知识点(目录): 1.html常用标签 2.html实体标签 3.html媒体标签 4.html超链接标签 5.html图片标签 6.html标个标签 7.html框 ...
- IntelliJ IDEA + Tomcat ;On Upate Action 与 On Frame Deactivation
On Upate Action 与 On Frame Deactivation 这两个选项的设置,依赖于 项目的部署方式 是war包 还是 exploded ,看下面的gif: 这里实在是太灵活了, ...
- Baidu软件研发工程师笔试题整理
Hadoop Map/Reduce Hadoop Map/Reduce是一个使用简易的软件框架,基于它写出来的应用程序能够运行在由上千个商用机器组成的大型集群上,并以一种可靠容错的方式并行处理上T级别 ...
- VS2015的对象浏览器的使用
用vs开发这么久了,还是第一次用上对象浏览器的功能,第一次用有一点懵逼,记录一下. 这个图标是项目 这是代表类,下面可以展开看到基类 在右边可以看到这个类的方法和成员 这个代表结构体 同样的右边显示成 ...
- day5模块学习 -- time、datetime时间模块
1.定义 模块:用来从逻辑上组织python(变量,函数,类,逻辑:实现一个功能)代码,本质就是.py结尾的python文件(文件名:test.py,对应的模块名test) 包:用来从逻辑上组织模块的 ...
- 【BZOJ】2395: [Balkan 2011]Timeismoney
题解 最小乘积生成树! 我们把,x的总和和y的总和作为x坐标和y左边,画在坐标系上 我们选择两个初始点,一个是最靠近y轴的A,也就是x总和最小,一个是最靠近x轴的B,也就是y总和最小 连接两条直线,在 ...