【34.54%】【codeforces 675E】Trains and Statistic
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
【题解】
设dp[i]为i到i+1..n这些点的”总共”需要邮票的张数;
转移方程
dp[i] = dp[pos]+n-i-(a[i]-pos);
这里的pos,是(i+1..a[i])这个区间里面的a的值最大的下标;
表示的是从i开始往后转移的方式;
首先
从i->(i+1..a[i])都是只要一张票的。(这肯定是最优的)。所以不用从i到pos再转移到(pos+1..a[i]);
直接到pos+1..a[i]就可以了;
而a[i]之后的位置。则如果要到a[i]..a[pos]这段。则需要从i到pos再转移到。如下图。
上图中0->1、2、3、4、5、6这些都只要买一张票;
而dp[pos]包含了从pos->4、5的两张票;
如果再加上了dp[pos],则会重复;
因此要减去(a[i]-pos);
n-i+a[i]-pos
对应:
pos->a[i]后面的点;
然后从i连若干条边到pos;
因为有n-i+a[i]-pos个点。
所以总共要加n-i+a[i]-pos条边;
为什么选a最大的pos;
因为a[pos]是最大的。所以从pos可以走到更远的地方
如图,如果选择的是4。它最远能到8。则如果要到9就的再从8再买一张票到9;
而如果选择pos,则可以直接买一张票到9;这就节省了一张。
画图多理解下吧。
最后累加dp[1..n];
dp[n] = 0;
一开始dp[n-1]=1;
从n-2开始往前处理
获取某个区间内的最大值用ST算法就好(线段树is ok)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#define LL long long
using namespace std;
const int MAXN = 200000;
const int MAX = 17;
int n, a[MAXN],rmq[MAXN][MAX+3],pre[MAX+3];
int need[MAXN];
LL dp[MAXN],ans = 0;
void input(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
scanf("%d", &n);
for (int i = 1; i <= n-1; i++)
scanf("%d", &a[i]),rmq[i][0] = i;
pre[0] = 1;
for (int i = 1; i <= MAX; i++)
pre[i] = pre[i - 1] * 2;
int now = 1;
need[1] = 0;
for (int i = 2; i <= n; i++)
if (i == pre[now])
need[i] = need[i - 1] + 1,now++;
else
need[i] = need[i - 1];
for (int l = 1;pre[l]<=n;l++)
for (int i = 1; i + pre[l]-1<= n; i++)
{
int left = rmq[i][l-1];
int right = rmq[i + pre[l - 1]][l-1];
if (a[left] > a[right])
rmq[i][l] = left;
else
rmq[i][l] = right;
}
dp[n-1] = 1;
ans = 1;
for (int i = n - 2; i >= 1; i--)
{
int xy = need[a[i] - i+1];
int left = rmq[i][xy], right = rmq[a[i] - pre[xy] + 1][xy];
int pos;
if (a[left] > a[right])
pos = left;
else
pos = right;
dp[i] = dp[pos] + n - i - (a[i] - pos);
ans += dp[i];
}
printf("%I64d\n", ans);
return 0;
}
【34.54%】【codeforces 675E】Trains and Statistic的更多相关文章
- codeforces 675E E. Trains and Statistic(线段树+dp)
题目链接: E. Trains and Statistic time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【34.57%】【codeforces 557D】Vitaly and Cycle
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【24.34%】【codeforces 560D】Equivalent Strings
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【34.88%】【codeforces 569C】Primes or Palindromes?
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【codeforces 793D】Presents in Bankopolis
[题目链接]:http://codeforces.com/contest/793/problem/D [题意] 给你n个点, 这n个点 从左到右1..n依序排; 然后给你m条有向边; 然后让你从中选出 ...
- 【codeforces 799D】Field expansion
[题目链接]:http://codeforces.com/contest/799/problem/D [题意] 给你长方形的两条边h,w; 你每次可以从n个数字中选出一个数字x; 然后把h或w乘上x; ...
- 【22.73%】【codeforces 606D】Lazy Student
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- bzoj1191 超级英雄
Description 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目,只有当选手正确回 ...
- CSS面试题总结2(转)
1.你最喜欢的图片替换方法是什么,你如何选择使用. 图像替代,就是像我们在平时将文本添加到文本中,然后通过css隐藏文本并在它的位置上显示一个背景图片,这样,搜索引擎仍然可以搜到HTML文本,即使我们 ...
- 寒哥教你学iOS - 经验漫谈
http://www.jianshu.com/p/cb54054d3add 寒哥教你学iOS - 经验漫谈 字数2848 阅读1896 评论19 喜欢43 顺便来个广告 iOS开发者 群1734993 ...
- 洛谷 1463[SDOI2005] 反素数ant
题目描述 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数1,2,4,6 ...
- c++第四次作业:继承
继承与派生 基本概念和语法 概念 继承与派生是同一过程从不同角度看 保持已有的特性而构造新类的过程称为继承. 在已有类的基础上新增自己的特性而产生新类的过程为派生. 被继承的已有类称为基类(父类) 派 ...
- C++五:重载 多态
C++五:重载与多态 一:概述 多态是指同样的消息被不同类型的对象接收导致不同的行为,即接口的多种不同的实现方式.多态可分为静态多态与动态多态.多态类型可分为四类:重载多态,强制多态,包含多态,参 ...
- A.The beautiful values of the palace 南京网络赛
A对于知道了解主席树性质的人来说,的确算是一个模板题目 题目在于给一个螺旋矩阵,以及一些权值,问在二维区间内权值和是多少? 对于螺旋矩阵权值来说,计算每个点的值,只需要O1计算即可.我们可以通过计算内 ...
- C++ 结构体的定义
struct 结构体名称{ 数据类型 A: 数据类型 B; }结构体变量名; 相当于: struct 结构体名称{ 数据类型 A: 数据类型 B; }; struct 结构体名 ...
- Redis正确使用的十个技巧
http://www.jb51.net/article/73376.htm Redis 在当前的技术社区里是非常热门的.从来自 Antirez 一个小小的个人项目到成为内存数据存储行业的标准,Redi ...
- CSS像素设置为整数,渲染结果像素带有小数
今天进行网页设计学习时,CSS代码设计width:300px,但实际显示像素如下图所示为199.99px. 最后发现是浏览器运用了缩放,恢复100%的缩放后,像素显示为整数.