http://codeforces.com/contest/675/problem/E

题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i]。定义p[i][j]为从i到j所需要买的最小票数。问sigma(p)的和是多少。

思路:感觉我的dp定义能力还是太弱了啊,我刚开始还定义成dp[i]表示1~i-1到i所需要的最小花费和,后来发现这样子我转移不了啊!!

于是重新定义dp[i]表示从i出发到i+1~n所需要的最小票数,然后这样定义就能很好的解决问题啦。然后我们每次贪心的选取j = i+1~a[i]中的a[j]跑的最远的那个,然后进行转移就好了,具体的用线段树维护一下区间吧。

TAT感觉是不难的题目

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e5 + ;
int a[maxn];
int n;
struct Tree{
int lpos, rpos;
Tree(int l = , int r = ): lpos(l), rpos(r){}
}tree[maxn << ]; inline void push_up(int o){
int lb = o << , rb = o << | ;
tree[o].rpos = max(tree[lb].rpos, tree[rb].rpos);
if (tree[lb].rpos >= tree[rb].rpos) {
tree[o].lpos = tree[lb].lpos;
}
else tree[o].lpos = tree[rb].lpos;
} void build_tree(int l, int r, int o){
if (l == r){
tree[o].rpos = a[l];
tree[o].lpos = l;
return ;
}
int mid = (l + r) / ;
if (l <= mid) build_tree(l, mid, o << );
if (r > mid) build_tree(mid + , r, o << | );
push_up(o);
} Tree query(int l ,int r, int ql, int qr, int o){
if (ql <= l && qr >= r){
return tree[o];
}
Tree ans, re;
ans.lpos = re.lpos = ans.rpos = re.rpos = ;
int mid = (l + r) / ;
if (ql <= mid)
ans = query(l, mid, ql, qr, o << );
if (qr > mid)
re = query(mid + , r, ql, qr, o << | );
if (ans.rpos < re.rpos) ans = re;
return ans;
} LL dp[maxn];///定义从i开始走到n所需要的最小花费
int main(){
scanf("%d", &n);
a[n] = n;
for (int i = ; i <= n - ; i++) scanf("%d", a + i);
memset(tree, , sizeof(tree));
build_tree(, n, );
LL ans = ;
for (int i = n - ; i > ; i--){
Tree pos = query(, n, i + , a[i], );
dp[i] = n - i + dp[pos.lpos] - (a[i] - pos.lpos);
ans += 1LL * dp[i];
}
printf("%lld\n", ans);
return ;
}

线段树+dp+贪心 Codeforces Round #353 (Div. 2) E的更多相关文章

  1. 贪心 Codeforces Round #236 (Div. 2) A. Nuts

    题目传送门 /* 贪心:每一次选取最多的线段,最大能放置nuts,直到放完为止,很贪婪! 题目读不懂多读几遍:) */ #include <cstdio> #include <alg ...

  2. 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know

    题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...

  3. 贪心 Codeforces Round #301 (Div. 2) B. School Marks

    题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...

  4. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  5. 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges

    题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...

  6. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  7. 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

    题目传送门 /* 找规律/贪心:ans = n - 01匹配的总数,水 */ #include <cstdio> #include <iostream> #include &l ...

  8. 字符串处理/贪心 Codeforces Round #307 (Div. 2) B. ZgukistringZ

    题目传送门 /* 题意:任意排列第一个字符串,使得有最多的不覆盖a/b字符串出现 字符串处理/贪心:暴力找到最大能不覆盖的a字符串,然后在b字符串中动态得出最优解 恶心死我了,我最初想输出最多的a,再 ...

  9. 贪心 Codeforces Round #173 (Div. 2) B. Painting Eggs

    题目传送门 /* 题意:给出一种方案使得abs (A - G) <= 500,否则输出-1 贪心:每次选取使他们相差最小的,然而并没有-1:) */ #include <cstdio> ...

随机推荐

  1. Retrofit,Rxjava,OkHttp3的配置

    这几个库的版本都更新了,和以前的使用略有不同,这是两篇介绍的博客:http://www.jianshu.com/p/91ac13ed076d ,https://drakeet.me/retrofit- ...

  2. js--冒泡排序[由小到大]

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  3. Algorithm 算法

    http://www.cnblogs.com/baiboy/category/723479.html 记下来,有空去看 随笔分类 - Algorithm   [项目总结]自然语言处理在现实生活中运用 ...

  4. 更改自身web项目的图标(默认为tomcat的小喵咪)

    在页面<head>标签中加入 <link rel="shortcut icon" href="img/11.png" type="i ...

  5. ckplayer 实现

    <div id="flashcontent"></div> <div id="video" style="positio ...

  6. LightOJ 1337 F - The Crystal Maze (bfs)

    Description You are in a plane and you are about to be dropped with a parasuit in a crystal maze. As ...

  7. Python 跳出多重循环

    Python 本身没有“break n” 和“goto” 的语法,这也造成了Python 难以跳出多层(特定层数)循环.下面是几个跳出多层(特定层数)循环的tip. 1.自定义异常   class g ...

  8. JS全选

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  9. JQ和其他框架一起使用方法

    时下,越来越多的javascripe框架不断崛起,同时开源网站系统也之间增多.网站建设过程中当使用一些开源的网站程序时,免不了会在javascript上产生冲突.也许网站的开发者习惯使用jQuery, ...

  10. ajax 假上传文件

    1. <form name="certForm" id="certForm" method="post" action="x ...