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. jQuery插件实现左右无缝轮播

    前段时间学习jQuery的时候,在网上找了个SuperSlide插件,做轮播图demo,感觉对于新人而言,还是挺容易上手的,代码量也少. 直接贴代码吧. 1.HTML <!DOCTYPE htm ...

  2. deepin2014.1安装搜狗后却找不到图标及配置

    点开Input Method Configration; 点左下角添加输入法; 将Only  Ohow Current Language前 的勾去掉,选择出现的搜狗输入法. FYI.

  3. 5、判断、循环、数组综合练习案例(迷你DVD)

    迷你dvd代码如下: package com.manager; import java.util.Scanner; public class DVDManage { public static voi ...

  4. LeetCode OJ 74. Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  5. 2016NEFU集训第n+3场 E - New Reform

    Description Berland has n cities connected by m bidirectional roads. No road connects a city to itse ...

  6. 基于C++的顺序表的实现

    顺序表,是数据结构中按顺序方式存储的线性表,又称向量.具有方便检索的特点.以下,是笔者学习是基于C++实现的顺序表代码,贴上来当网页笔记用. #include <iostream> usi ...

  7. Openjudge-计算概论(A)-点与正方形的关系

    描述: 有一个正方形,四个角的坐标(x,y)分别是(1,-1),(1,1),(-1,-1),(-1,1),x是横轴,y是纵轴.写一个程序,判断一个给定的点是否在这个正方形内.输入输入坐标x,y输出ye ...

  8. InnoDB表要建议用自增列做主键

    InnoDB引擎表是基于B+树的索引组织表(IOT): 每个表都需要有一个聚集索引(clustered index): 所有的行记录都存储在B+树的叶子节点(leaf pages of the tre ...

  9. 存储过程sql语句

    select count(virtualacc) into v_count  from T_ATMMONITOR WHERE virtualacc = v_number; 用于存储过程中,是把coun ...

  10. tomcat各目录(文件)作用

    以tomcat7.0.50为例,主目录下有bin,conf,lib,logs,temp,webapps,work 7个文件夹 bin目录主要是用来存放tomcat的命令,主要有两大类,一类是以.sh结 ...