POJ 3171
题目大意:
给定一个区间范围[M,E],接下来有n行输入。每行输入三个数值:T1,T2,S,表示覆盖区间[T1,T2]
的代价为S,要求你求出覆盖区间[M,E]的最小代价,假设不能覆盖,则输出-1.
解题思路:
先将区间按右端点进行排序,这样我们就能得到dp状态的定义和转移方程:
dp[i]:表示覆盖[M,cow[i].T2]的最小覆盖代价.
dp[i] = cow[i].cost (cow[i].l == M) 或者 dp[i] = min(dp[j~i]) + cow[i].cost(cow[j].T2 >= cow[i].T1-1)
因为右端点可能有重合的情况,所以我们的dp[i]中可能存放的并非最小覆盖代价。当然了,
我们能够对右端点进行离散化。然后去重,这样dp数组里面存放的就是最优解,只是这添加了
编程的复杂度,代码量也加大不少.事实上,我们依旧能够用上述dp定义。最后dp[j~i]cow[j].T2==cow[i].T2)
的最小值就能够了.关于dp[i] = min(dp[j~i]) + cow[i].cost,是个RMQ问题,这里我用的是线段树实现的.
以下是解题代码。代码中对空间进行了优化。所以代码中并没有dp数组。其值都存放在线段树中.
#include<stdio.h>
#include<algorithm>
#define MAX_N 11000
#define INF 100000000000
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
struct Node
{
int l,r,c;
bool operator < (const Node &tmp) const
{
return r < tmp.r;
}
}cow[MAX_N];
long long tree[MAX_N<<2];
void build(int l,int r,int rt)
{
tree[rt] = INF ;
if( l == r)
return;
int m = l + ( r - l ) / 2 ;
build(lson);
build(rson);
}
long long query(int L,int R,int l,int r,int rt)
{
if(L <= l && r <= R)
return tree[rt];
int m = l + ( r - l ) / 2 ;
long long tmp = INF;
if(L <= m)
tmp = query(L,R,lson);
if(R > m)
tmp = min(tmp,query(L,R,rson));
return tmp;
}
void update(int pos,long long val,int l,int r,int rt)
{
if(l == r)
{
tree[rt] = val ;
return;
}
int m = l + ( r - l ) / 2 ;
if(pos <= m)
update(pos,val,lson);
else
update(pos,val,rson);
tree[rt] = min(tree[rt<<1],tree[rt<<1|1]);
}
//去掉不满足条件的区间
int init(int n)
{
int cnt = 0 , r_max = cow[0].r ;
for(int i=1;i<n;++i)
{
if(cow[i].r != cow[i-1].r)
r_max = cow[cnt].r ;
if(cow[i].l - 1 <= r_max)
cow[++cnt] = cow[i];
}
return cnt;
}
int Bin(int key,int l,int r)
{
while(l <= r)
{
int m = l + ( r - l ) / 2 ;
if(cow[m].r < key)
l = m + 1 ;
else
r = m - 1 ;
}
return l;
}
int main()
{
int n,m,e;
while(~scanf("%d%d%d",&n,&m,&e))
{
int l_min = 90000 , r_max = 0 ;
for(int i=0;i<n;++i)
{
scanf("%d%d%d",&cow[i].l,&cow[i].r,&cow[i].c);
l_min = min(l_min,cow[i].l);
r_max = max(r_max,cow[i].r);
}
sort(cow,cow+n);
n = init(n) ;
if(l_min > m || r_max < e || cow[n].r < e)
{
printf("-1\n");
continue;
}
build(0,n,1);
for(int i=0;i<=n;++i)
{
long long tmp;
if(cow[i].l == m)
tmp = (long long)cow[i].c ;
else
tmp = query(Bin(cow[i].l-1,0,i),i,0,n,1) + cow[i].c;
update(i,tmp,0,n,1);
}
printf("%I64d\n",query(Bin(cow[n].r,0,n),n,0,n,1));
}
return 0;
}
POJ 3171的更多相关文章
- POJ 3171 Cleaning Shifts(DP+zkw线段树)
[题目链接] http://poj.org/problem?id=3171 [题目大意] 给出一些区间和他们的价值,求覆盖一整条线段的最小代价 [题解] 我们发现对区间右端点排序后有dp[r]=min ...
- poj 3171 Cleaning Shifts(区间的最小覆盖价值)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2743 Accepted: 955 De ...
- POJ 3171 Cleaning Shifts
Description Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. Th ...
- POJ 3171 DP
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3563 Accepted: 1205 D ...
- POJ 3171.Cleaning Shifts-区间覆盖最小花费-dp+线段树优化(单点更新、区间查询最值)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4721 Accepted: 1593 D ...
- POJ 3171 区间最小花费覆盖 (DP+线段树
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4245 Accepted: 1429 D ...
- POJ 3171 区间覆盖最小值&&线段树优化dp
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4715 Accepted: 1590 D ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
随机推荐
- 24SpringMvc中的重定向和转发-解释return "redirect:/cargo/contractproduct/tocreate.action";
我在做JK项目时发现:我们在增加一个货物时.会先根据提交的Action()进入到一个新增页面 //跳转到新增的页面 @RequestMapping("/cargo/contractprodu ...
- virtualbox pxe启动
在设置——系统中更改启动顺序,优先使用网路启动,如果你的网络环境支持PXE启动,就可以使用网络安装了. 转自: http://blog.csdn.net/huanghuibai/article/det ...
- MATLAB中TXT数据文件读取并写入元胞数组的方法与步骤
一. TXT数据文件读取 Data = load('train.txt'); %简单的文件读取,这时在工作区可以看到导入的大数据变量Data 二.大数据变量Data装入元胞数组中 D = cell ...
- 『Spring.NET+NHibernate+泛型』框架搭建之DAO(三)★
本节内容介绍Nhibernate所封装的数据库訪问层.只是我增加了泛型进行封装.大概思路:首先,我们有一个接口层,另一个相应的实现层.在接口层中我们先定义一个父接口,父接口中定义每个接口都可能会用到的 ...
- android4.4上全屏界面实现禁止状态栏下拉
附上我改动的方法:PhoneWindowManager.java里面的改动 --- a/frameworks/base/policy/src/com/android/internal/policy/i ...
- querySelectorAll 和getElementsByClassName的区别
querySelectorAll 返回的是映射 改变其值不会改变document 而getElementsByClassName 改变它就会改变document 摘自JavaScript权威指南(jQ ...
- Python背景知识——学习笔记
诞生于1989圣诞节,阿姆斯特丹.Guido van Rossum(吉多·范罗苏姆). Python Python:解释型.面向对象.动态数据类型 的 高级程序设计语言. 解释型语言:运行的时候将程序 ...
- Android API Guides---Bluetooth
Bluetooth Android平台包含蓝牙网络协议栈,它同意设备以无线方式与其他蓝牙设备进行数据交换的支持.应用程序框架提供了訪问通过Android蓝牙API的蓝牙功能.这些API使应用程序无线方 ...
- PDF.NET数据开发框架实体类操作实例
PDF.NET数据开发框架实体类操作实例(MySQL)的姊妹篇,两者使用了同一个测试程序,不同的只是使用的类库和数据库不同,下面说说具体的使用过程. 1,首先在App.config文件中配置数据库连接 ...
- JZOJ.5306【NOIP2017模拟8.18】棋盘游戏
Description 这个游戏上在一个无限大的棋盘上, 棋盘上只有一颗棋子在位置(x,y)(x,y>=0)棋盘的左下角是(0,0)Amphetamine每次都是第一个移动棋子,然后Amphet ...