线段树+dp+贪心 Codeforces Round #353 (Div. 2) E
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的更多相关文章
- 贪心 Codeforces Round #236 (Div. 2) A. Nuts
题目传送门 /* 贪心:每一次选取最多的线段,最大能放置nuts,直到放完为止,很贪婪! 题目读不懂多读几遍:) */ #include <cstdio> #include <alg ...
- 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...
- 贪心 Codeforces Round #301 (Div. 2) B. School Marks
题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones
题目传送门 /* 找规律/贪心:ans = n - 01匹配的总数,水 */ #include <cstdio> #include <iostream> #include &l ...
- 字符串处理/贪心 Codeforces Round #307 (Div. 2) B. ZgukistringZ
题目传送门 /* 题意:任意排列第一个字符串,使得有最多的不覆盖a/b字符串出现 字符串处理/贪心:暴力找到最大能不覆盖的a字符串,然后在b字符串中动态得出最优解 恶心死我了,我最初想输出最多的a,再 ...
- 贪心 Codeforces Round #173 (Div. 2) B. Painting Eggs
题目传送门 /* 题意:给出一种方案使得abs (A - G) <= 500,否则输出-1 贪心:每次选取使他们相差最小的,然而并没有-1:) */ #include <cstdio> ...
随机推荐
- Linux增加swap分区大小
1. 查看当前分区情况 free -m 2. 增加 swap 大小, 2G 左右 dd if=/dev/zero of=/var/swap bs=1024 count=2048000 3. 设置交换文 ...
- 简单的Android之apk包反编译方法
网上相关的文章一大堆了,我只是总结下自己的反编译方法和工具 工具下载地址: http://download.csdn.net/detail/zsjangel/7104663 下载上面的三个工具的压缩包 ...
- javascript 延时执行函数
延时执行函数,貌似有些多此一举, 也许还是有点用 记在这儿 var test = { delay : function(lifetime){ var data; setTimeout(function ...
- [妙味DOM]第五课:事件深入应用
知识点总结 鼠标拖拽原理: 1.鼠标按下后开始移动,鼠标抬起停止移动,即onmousedown中要包括onmousemove和onmouseup 2.获取位置的计算:获取鼠标的当前位置-鼠标在物体中的 ...
- javascript动画:velocity.js学习
第二章:基础知识 一.velocity和jQuery: Velocity函数是独立于jQuery的,但两者可以结合使用.通常这么做的好处是可以利用jQuery的链式操作:当你先用jQuery选择了一个 ...
- Java 反射 Array动态创建数组
Java 反射 Array动态创建数组 @author ixenos 注:java.lang.reflect.Array 是个反射工具包,全是静态方法,创建数组以多维数组为基准,一维数组只是特殊实现 ...
- 浙大 pat 1023题解
1023. Have Fun with Numbers (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- openwrt拦截snmp报文
SNMP使用的协议为UDP,默认端口为161和162. 使用iptables 命令如下: iptables -A INPUT -p udp -m udp --dport 161:162 -j DROP ...
- webapi中的Route的标签的命名参数name的使用
Route Names In Web API, every route has a name. Route names are useful for generating links, so that ...
- 图片拉伸:resizableImageWithCapInsets
iOS 5.0 在iOS 5.0中,UIImage有一个新方法可以处理图片的拉伸问题 - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)ca ...