[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 ...
随机推荐
- HDU 3001 三进制状压DP
N个城市,M条道路,每条道路有其经过的代价,每一个城市最多能够到达两次,求走全然部城市最小代价,起点随意. 三进制状压.存储每一个状态下每一个城市经过的次数. 转移方程: dp[i+b[k]][k]= ...
- Launcher3实现壁纸居中
Launcher3的wallpaper显示是动态的,与Launcher预置桌面数目有关,让壁纸居中,仅仅能确保第一次开机时壁纸居中,后面用户改动桌面数目后,就无法达到了.怎样要在默认桌面数目配置时居中 ...
- UVA 11991 Easy Problem from Rujia Liu?【STL】
题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142">https://uv ...
- Gonet2 游戏server框架解析之gRPC提高(5)
上一篇blog是关于gRPC框架的基本使用,假设说gRPC仅仅是远程发几个參数,那和一个普通的http请求也没多大区别了. 所以今天我就来学习一下gRPC高级一点的用法. 流! 流能够依据用法,分为单 ...
- C++中的namespace详解
原文链接:http://blog.csdn.net/yao_zhuang/article/details/1853625 namespace中文意思是命名空间或者叫名字空间,传统的C++只有一个全局的 ...
- 硬件时间,操作系统时间,Windows 和linux 双系统时间差8小时问题说明
1.硬件时间:硬件时钟是存储在主板上CMOS里的时间即BIOS时间,关机后该时钟依然运行,主板的电池为它供电.对应于嵌入式设备有一个RTC模块.硬件时钟即RTC时钟.信息比较少没时区.夏令时的概念. ...
- Felx之菜单导航
Felx之菜单导航 环境搭建:MyEclipse 6.5+Flex Builder 3 Plug-in <?xml version="1.0" encoding=" ...
- Vue常见面试题汇总
Vue框架常见面试题 1.active-class是哪个组件的属性?嵌套路由怎么定义? 答:vue-router模块的router-link组件. 2.怎么定义vue-router的动态路由?怎么 ...
- 【Codeforces Round #424 (Div. 2) A】Unimodal Array
[Link]:http://codeforces.com/contest/831/problem/A [Description] 让你判断一个数列是不是这样一个数列: 一开始是严格上升 然后开始全都是 ...
- Maintaining processor resources during architectural events
In one embodiment of the present invention, a method includes switching between a first address spac ...