Codeforces Round #353 (Div. 2) E

原创 2016年05月30日 20:23:01


Trains and Statistic

题意:有n个车站,第i个车站可以买一张票到i+1和ai之间,p(i,j)表示从i到j需要买的最小票数,求p(i,j)之和


#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <bitset>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;

#define LL long long
#define ULL unsigned long long
#define eps 1e-9
#define N (200000 + 10)
#define M (50000 + 10)
#define pii pair<int,int>
#define MP make_pair
#define inf 0x3f3f3f3f
#define md (ll+rr>>1)
#define lson ll, md, ls
#define rson md + 1, rr, rs
#define ls i<<1
#define rs i<<1|1
#define mod 1000000007
#define Pi acos(-1.0)


int a[N];

LL ans;


LL dp[N];
LL mi[N<<2];
int n;

void update(int x, LL v, int ll, int rr, int i) {
	if(ll == rr) {
		mi[i] = v;
		return;
	}
	if(x <= md) update(x, v, lson);
	else update(x, v, rson);
	mi[i] = min(mi[ls], mi[rs]);
}

LL query(int l, int r, int ll, int rr, int i) {
	if(l == ll && r == rr) {
		return mi[i];
	}
	if(r <= md) return query(l, r, lson);
	else if(l > md) return query(l, r, rson);
	else return min(query(l, md, lson), query(md+1, r, rson));
}

void gao() {
	update(n, n, 1, n, 1);
	for(int i = n-1; i >= 1; --i) {
		LL k = query(i+1, a[i], 1, n, 1);
		dp[i] = k  - a[i] + n - i;
		update(i, dp[i]+i, 1, n, 1);
		ans += dp[i];
	}

}


int main() {
	scanf("%d", &n);
	int x;
	for(int i = 1; i < n; ++i) {
		scanf("%d", &x);
		a[i] = x;
	}
	ans = 0;
	gao();
	printf("%I64d\n", ans);
}

Codeforces Round #353 (Div. 2) E. Trains and Statistic

Vasya commutes by train every day. There are n train stations in the city, and at the i-th station ...

Codeforces Round #353 (Div. 2) E. Trains and Statistic(求d[i][j]的和的最小值(1<=i<=n,i+1<=j<=n))

E. Trains and Statistic Vasya commutes by train every day. There are n train stations in the city, a...

codeforces 675 A~E(Round #353 Div. 2) python and C++ 解法

PROBLEM LISTofficial tutorialExplanation I use python in Problem A, B & E. while in C and D, I ...

Codeforces Round #353 (Div. 2) A. Infinite Sequence 思维题

被cha了,(┬_┬), 原因是用 (b - a) * c >= 0 来表示(b-a) 与 c同号, 或b - a == 0.这里int * int 溢出了,以后还是基本上不要这样,写了。 老老实实的...

Codeforces Round #353 (Div. 2) D. Tree Construction (构造二叉搜索树)

D. Tree Construction time limit per test 2 seconds memory limit per test 256 megabytes input s...

Codeforces Round #353 (Div. 2) D. Tree Construction

During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how ...

Codeforces Round #353 (Div. 2) C. Money Transfers 环、贪心、前缀和推广、好题

首先最多是ans = n-1次,这个时候只有sum总是0; 否则, 只有 有一个小区间是 0, 则ans就少1次; 所以要找到,尽可能多的sum == 0 的区间。 当时一直不知道,环该怎么处理, 结...

Codeforces Round #353 (Div. 2) C. Money Transfers

There are n banks in the city where Vasya lives, they are located in a circle, such that any two ban...

Codeforces Round #353 (Div. 2) B. Restoring Painting __ map or set 、思维题

自己画一个3*3的方格图, 然后标上 a, b, c, d 然后发现左上角标上x, 中间标上y,然后剩余3个空格可以表示出来。 故可以O(n)的来做 扫一遍,过程中用ans[][][][]来表示那个状...

Codeforces Round #353 (Div. 2) Editorial

**675A - Infinite Sequence**#include using namespace std;int main() { int a, b, c; cin >> a ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #353 (Div. 2) E
举报原因:
原因补充:

(最多只允许输入30个字)