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. 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage“提示信息

        在安装过vs2015之后出现未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage“提示信息在VS的安装目录下,找到 ...

  2. “System.BadImageFormatException”类型的未经处理的异常在 PurchaseDevices.Access.dll 中发生 其他信息: 未能加载文件或程序集“System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139”或它的某一个依赖项。试图加载格式不正确

    引用sqlite的程序集时,有时会报如下异常:  "System.BadImageFormatException"类型的未经处理的异常在 PurchaseDevices.Acces ...

  3. hdu1025

    #include<stdio.h>const int MAXN=500010;int a[MAXN],b[MAXN]; //用二分查找的方法找到一个位置,使得num>b[i-1] 并 ...

  4. hdu1008

    //c++// #includeusing namespace std;int main(){int n,j,t,start;while (cin >> n,n){start =0;t = ...

  5. display: inline-block兼容性写法

    display:inline-block;*display:inline;*zoom:1;

  6. 【瞎搞搞之】 window_x64微信小程序环境搭建

    所需文件地址如下: http://pan.baidu.com/s/1nv0IHhn(ylk7)   1.下载微信开发工具0.7.0_x64 安装完成后,打开程序,进行微信扫码登录 2.下载微信开发工具 ...

  7. Markdown 基础

    How to use Markdown H1 text. H2 text. H3 text. H4 text. H5 text. H6 text. Text This is italic text. ...

  8. VLAN学习

    IEEE于1999年颁布了标准化VLAN实现方案的 IEEE 802.1Q 协议标准草案,对带有VLAN标识的报文结构进行了统一规定. 传统的以太网数据帧在目的MAC和源MAC地址之后封装的是上层协议 ...

  9. ActiveMQ in Action(5) - Clustering

    关键字: activemq 2.5 Clustering    ActiveMQ从多种不同的方面提供了集群的支持.2.5.1 Queue consumer clusters    ActiveMQ支持 ...

  10. 归并排序的go语言与C++实现对比

    最近对go语言发生了兴趣,发现go语言语法简洁,非常适合算法的描述和实现,于是对归并排序进行了实现. 例子中需要排序的队列是长度为100的从100到1的数列,排序算法是正序排序,排序正确的话,结果应当 ...