poj 3171 Cleaning Shifts(区间的最小覆盖价值)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2743 | Accepted: 955 |
Description
Farmer John has N (1 <= N <= 10,000) cows who are willing to do some cleaning. Because dust falls continuously, the cows require that the farm be continuously cleaned during the workday, which runs from second number M to second number E during the day (0 <=
M <= E <= 86,399). Note that the total number of seconds during which cleaning is to take place is E-M+1. During any given second M..E, at least one cow must be cleaning.
Each cow has submitted a job application indicating her willingness to work during a certain interval T1..T2 (where M <= T1 <= T2 <= E) for a certain salary of S (where 0 <= S <= 500,000). Note that a cow who indicated the interval 10..20 would work for 11
seconds, not 10. Farmer John must either accept or reject each individual application; he may NOT ask a cow to work only a fraction of the time it indicated and receive a corresponding fraction of the salary.
Find a schedule in which every second of the workday is covered by at least one cow and which minimizes the total salary that goes to the cows.
Input
Lines 2..N+1: Line i+1 describes cow i's schedule with three space-separated integers: T1, T2, and S.
Output
Sample Input
3 0 4
0 2 3
3 4 2
0 0 1
Sample Output
5
Hint
FJ has three cows, and the barn needs to be cleaned from second 0 to second 4. The first cow is willing to work during seconds 0, 1, and 2 for a total salary of 3, etc.
Farmer John can hire the first two cows.
给出n个小区间[l,r],更新这段区间的代价为c,求覆盖一段区间[m,e]的最小值。
线段树+DP
首先应该将小区间排个序,对于每一个区间[l,r],它能够在[l-1,r]区间上覆盖,找到这段区
间的最小代价。最小代价加上这段区间的代价与r点的代价比較。更新包括r点的区间
段就能够了。
区间查询用线段树。
代码:
//94ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=100000+100;
const int inf=199999999;
struct node
{
int minc;
} tree[maxn<<2];
struct Cow
{
int l;
int r;
int c;
} cow[maxn];
bool cmp(Cow a,Cow b)
{
if(a.l==b.l)
return a.r<b.r;
return a.l<b.l;
}
void build(int rt,int l,int r)//建树
{
tree[rt].minc=inf;
if(l==r)
return;
int m=(l+r)>>1;
build(rt<<1,l,m);
build(rt<<1|1,m+1,r);
}
void update(int rt,int k,int v,int l,int r)//更新所有包括k的区间
{
if(l==r)
{
tree[rt].minc=min(tree[rt].minc,v);
return;
}
int m=(l+r)>>1;
if(k<=m)
update(rt<<1,k,v,l,m);
else
update(rt<<1|1,k,v,m+1,r);
tree[rt].minc=min(tree[rt<<1].minc,tree[rt<<1|1].minc);
}
int query(int rt,int l,int r,int L,int R)//查找L,R区间内的最小值
{
if(l>=L&&r<=R)
return tree[rt].minc;
int mid=(l+r)>>1;
int temp = inf;
if(L<=mid)
temp=query(rt<<1,l,mid,L,R);
if(R>mid)
temp=min(temp,query(rt<<1|1,mid+1,r,L,R));
return temp;
}
int main()
{
int n,m,e;
while(~scanf("%d%d%d",&n,&m,&e))
{
for(int i=0; i<n; i++)
scanf("%d%d%d",&cow[i].l,&cow[i].r,&cow[i].c);
int sign=1;
sort(cow,cow+n,cmp);
build(1,m-1,e);
int cur=m-1;
update(1,cur,0,m-1,e);//将m-1点赋为0.
for(int i=0; i<n; i++)
{
if(cow[i].l>cur+1)//不能所有覆盖
{
sign=0;
break;
}
int temp=query(1,m-1,e,cow[i].l-1,cow[i].r);
update(1,cow[i].r,temp+cow[i].c,m-1,e);
cur=max(cur,cow[i].r); }
if(sign)
printf("%d\n",query(1,m-1,e,e,e));
else
printf("-1\n");
}
return 0;
}
poj 3171 Cleaning Shifts(区间的最小覆盖价值)的更多相关文章
- POJ 3171 Cleaning Shifts(DP+zkw线段树)
[题目链接] http://poj.org/problem?id=3171 [题目大意] 给出一些区间和他们的价值,求覆盖一整条线段的最小代价 [题解] 我们发现对区间右端点排序后有dp[r]=min ...
- POJ 2376 Cleaning Shifts 区间覆盖问题
http://poj.org/problem?id=2376 题目大意: 给你一些区间的起点和终点,让你用最小的区间覆盖一个大的区间. 思路: 贪心,按区间的起点找满足条件的并且终点尽量大的. 一开始 ...
- POJ 3171 Cleaning Shifts
Description Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. Th ...
- POJ 2376 Cleaning Shifts(轮班打扫)
POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Farmer ...
- POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)
Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...
- poj 2376 Cleaning Shifts 贪心 区间问题
<pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS Memory ...
- poj 2376 Cleaning Shifts 最小区间覆盖
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40751 Accepted: 9871 ...
- poj 2376 Cleaning Shifts
http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 2376 Cleaning Shifts 贪心
Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
随机推荐
- 【HOJ1356】【Miller_rabin素性测试】Prime Judge
Given a positive integer, your job is writing a program to determine whether it is a prime number or ...
- PHPCMS 使用图示和PHPCMS二次开发教程(转)
PHPCMS V9 核心文件说明 模块与控制器 模块: phpcms v9框架中的模块,位于phpcms/modules目录中 每一个目录称之为一个模块.即url访问中的m. 访问content模块示 ...
- Java学习----到底调用哪一个方法(多态)
public class Father { public void print() { System.out.println("Father:print()"); } } publ ...
- 小心DriveInfo类IsReady属性的较大延迟问题
当某些驱动器调用IsReady属性来判断是否准备好时,会有性能问题,会非常慢,特别是网络驱动器断开的时候,这个属性会有30秒左右的延迟,这对程序执行是非常大的开销,请慎重调用
- 51中的C语言数据类型
在标准C语言中基本的数据类型为char,int,short,long,float 和double,而在C51编译器中int和short相同,float和double相同. 说明: (1)类型修饰符si ...
- Java中异常处理和设计
在程序设计中,进行异常处理是非常关键和重要的一部分.一个程序的异常处理框架的好坏直接影响到整个项目的代码质量以及后期维护成本和难度.试想一下,如果一个项目从头到尾没有考虑过异常处理,当程序出错从哪里寻 ...
- Node.js npm
Node程序包管理器(NPM)提供了以下两个主要功能: 在线存储库的Node.js包/模块,可搜索 search.nodejs.org 命令行实用程序来安装Node.js的包,做版本管理和Node.j ...
- [BZOJ 2243] [SDOI 2011] 染色 【树链剖分】
题目链接:BZOJ - 2243 题目分析 树链剖分...写了200+行...Debug了整整一天+... 静态读代码读了 5 遍 ,没发现错误,自己做小数据也过了. 提交之后全 WA . ————— ...
- C++ Primer 随笔 Chapter 13 复制控制
1.复制控制包含的内容:复制构造函数.赋值操作符.析构函数 2.复制构造函数: a. 定义:只有单个形参,而且该形参是对本类类型的引用,这样的构造函数被成为复制构造函数 b. 适用情况: (1)根据一 ...
- shell command使用技巧
1窗口可以merge 2.可以通过 control+t打开窗口