[BZOJ1672][Usaco2005 Dec]Cleaning Shifts 清理牛棚 线段树优化DP
题意:给你一些区间,每个区间都有一个花费,求覆盖区间 \([S,T]\) 的最小花费
题解
先将区间排序
设 \(f[i]\) 表示决策到第 \(i\) 个区间,覆盖满 \(S\dots R[i]\) 的最小花费
显然 \(f[i]=\min_{R[j]\ge L[i]}f[j]+w[i]\)
按照区间建线段树,插右端点即可
#include<bits/stdc++.h>
#define REP(i,a,b) for(int i(a);i<=(b);++i)
using namespace std;
typedef long long ll;
inline int read(){char c;int w;
while(!isdigit(c=getchar()));w=c&15;
while(isdigit(c=getchar()))w=w*10+(c&15);return w;
}
inline char smax(int&x,const int&y){return x<y?x=y,1:0;}
inline char smin(ll&x,const ll&y){return x>y?x=y,1:0;}
const int N=100005;
struct data{int l,r,w;}a[N];
inline bool cmp(data x,data y){return x.r<y.r;}
int n,s,t;
ll minv[N<<2],f[N];
#define ls o<<1
#define rs o<<1|1
inline void ins(int o,int l,int r,int x,ll w){
smin(minv[o],w);if(l==r)return;int mid=l+r>>1;
x<=mid?ins(ls,l,mid,x,w):ins(rs,mid+1,r,x,w);
}
inline ll query(int o,int l,int r,int x,int y){
if(x>r||y<l)return minv[0];
if(x<=l&&r<=y)return minv[o];
int mid=l+r>>1;
return min(query(ls,l,mid,x,y),query(rs,mid+1,r,x,y));
}
int main(){
n=read(),s=read()+1,t=read()+1;
REP(i,1,n)a[i]=(data){read()+1,read()+1,read()};
sort(a+1,a+1+n,cmp);
memset(f,0x3f,sizeof f);memset(minv,0x3f,sizeof minv);
#define all 1,s-1,t
ins(all,s-1,0);f[s-1]=0;
REP(i,1,n)if(a[i].r>=s&&a[i].l<=t){
f[i]=query(all,max(s-1,a[i].l-1),min(a[i].r-1,t))+a[i].w;
ins(all,min(a[i].r,t),f[i]);
}
ll ans=minv[0];
for(int i=n;i;--i)if(a[i].r>=t){
smin(ans,f[i]);
}else break;
if(ans==minv[0])puts("-1");else cout<<ans;
return 0;
}
[BZOJ1672][Usaco2005 Dec]Cleaning Shifts 清理牛棚 线段树优化DP的更多相关文章
- BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 414 Solved: ...
- BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树
BZOJ_1672_[Usaco2005 Dec]Cleaning Shifts 清理牛棚_动态规划+线段树 题意: 约翰的奶牛们从小娇生惯养,她们无法容忍牛棚里的任何脏东西.约翰发现,如果要使这群 ...
- [Usaco2005 Dec]Cleaning Shifts 清理牛棚 (DP优化/线段树)
[Usaco2005 Dec] Cleaning Shifts 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new ...
- BZOJ 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
题目 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec Memory Limit: 64 MB Description Farm ...
- P4644 [Usaco2005 Dec]Cleaning Shifts 清理牛棚
P4644 [Usaco2005 Dec]Cleaning Shifts 清理牛棚 你有一段区间需要被覆盖(长度 <= 86,399) 现有 \(n \leq 10000\) 段小线段, 每段可 ...
- 【BZOJ1672】[Usaco2005 Dec]Cleaning Shifts 清理牛棚 动态规划
[BZOJ1672][Usaco2005 Dec]Cleaning Shifts Description Farmer John's cows, pampered since birth, have ...
- [Usaco2005 Dec]Cleaning Shifts 清理牛棚
题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...
- 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚 dp/线段树
题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...
- 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚
题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...
随机推荐
- ArcGIS api for javascript——地理编码任务-反向地理编码
描述 反向地理编码确定地图上给出点的地址.本例展示了如何通过ArcGIS JavaScript API做反向地理编码. 反向地理编码和常规的地理编码请求都使用Locator类和ArcGIS Serve ...
- Use Uncertainty As a Driver
 Use Uncertainty As a Driver Kevlin Henney ConFRonTEd WiTH TWo opTionS, most people think that the ...
- Visual Studio2008 和2010 执行程序出现的黑框马上消失解决方法
1 在程序最后加 system("PAUSE"); 要注意包括头文件#include"stdlib.h" //system须要调用这个 ...
- 百度地图SDK for Android【Demo兴趣点搜索】
百度地图SDK为开发人员提供了便捷的检索服务. 今天我将为大家介绍Poi检索相关的内容. 首先,我们要构建一个最主要的地图应用.详细介绍请參考:百度地图SDK for Android[ ...
- 并查集树数据结构hdu1325
我的解法就是去构造了一棵树 以数组的存储方式 数组的值存放节点的根. 排除空树 剩下的就是出现环和多根节点的情况 也就是排除森林和有一个节点多个入度的情况 排除森林就用到了并查集 也就是便利数组让其仅 ...
- 从C10K到C10M高性能网络的探索与实践
在高性能网络的场景下,C10K是一个具有里程碑意义的场景,15年前它给互联网领域带来了非常大的挑战.发展至今,我们已经进入C10M的场景进行网络性能优化. 这期间有怎样的发展和趋势?环绕着各类指标分别 ...
- HDU 2435 There is a war Dinic 最小割
题意是有n座城市,n号城市不想让1号城市可达n号,每条道路有一条毁坏的代价,1号还可以修一条不能毁坏的道路,求n号城市所需的最小代价最大是多少. 毁坏的最小代价就直接求一遍最大流,就是最小割了.而可以 ...
- hashCode 和 equals 方法
hashCode 和 equals 方法 hashCode()和equals()定义在Object类中,这个类是所有java类的基类,所以所有的java类都继承这两个方法. 使用hashCode()和 ...
- ps切图时常用的操作与快捷键
一:两种切片方法 第一种: 1.使用切片工具划分好你要切的模块 2.点击'存储为web所有格式',在存储之前可以修改图片的品质来改变文件的大小. 3.在存储时切片有三种选择方式,按照自己的需要选择. ...
- C++中冒号(:)的作用
摘于:http://blog.csdn.net/zimingjushi/article/details/6549390 (1)表示机构内位域的定义(即该变量占几个bit空间) typedef stru ...