Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2743   Accepted: 955

Description

Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now require their barn to be immaculate. Farmer John, the most obliging of farmers, has no choice but hire some of the cows to clean the barn. 



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

Line 1: Three space-separated integers: N, M, and E. 



Lines 2..N+1: Line i+1 describes cow i's schedule with three space-separated integers: T1, T2, and S.

Output

Line 1: a single integer that is either the minimum total salary to get the barn cleaned or else -1 if it is impossible to clean the barn.

Sample Input

3 0 4
0 2 3
3 4 2
0 0 1

Sample Output

5

Hint

Explanation of the sample: 



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(区间的最小覆盖价值)的更多相关文章

  1. POJ 3171 Cleaning Shifts(DP+zkw线段树)

    [题目链接] http://poj.org/problem?id=3171 [题目大意] 给出一些区间和他们的价值,求覆盖一整条线段的最小代价 [题解] 我们发现对区间右端点排序后有dp[r]=min ...

  2. POJ 2376 Cleaning Shifts 区间覆盖问题

    http://poj.org/problem?id=2376 题目大意: 给你一些区间的起点和终点,让你用最小的区间覆盖一个大的区间. 思路: 贪心,按区间的起点找满足条件的并且终点尽量大的. 一开始 ...

  3. POJ 3171 Cleaning Shifts

    Description Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. Th ...

  4. POJ 2376 Cleaning Shifts(轮班打扫)

    POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Farmer ...

  5. POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)

    Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...

  6. poj 2376 Cleaning Shifts 贪心 区间问题

    <pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS   Memory ...

  7. poj 2376 Cleaning Shifts 最小区间覆盖

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40751   Accepted: 9871 ...

  8. poj 2376 Cleaning Shifts

    http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  9. POJ 2376 Cleaning Shifts 贪心

    Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...

随机推荐

  1. SGU 226.Colored graph(最短路)

    时间限制:0.25s 空间限制:4M 题意: 给出一个n个节点,m条边的图,每条边都有标记了编号为1,2,3三种颜色之一,现在求从1号节点到n号节点的一条最短路径的长度,要求该路径中相邻的边没有相同的 ...

  2. java数据类型学习

    java数据类型基本分为两类: 一类为基本数据类型: 数值类型: 整数类型:byte.short.int.long 浮点类型:float.double 字符类型:char 布尔类型:boolean 一 ...

  3. javascript定义类的方法总结

    1.构造函数法 类是对象的模板,定义了对象共有的方法属性数据 等,在javascript中一个函数就是一个对象,也可以看做一个类的构造方法. 所以我们可以像以下方式定义类: //1.经典的构造方法 Q ...

  4. windows下实现uboot的tftp下载功能

    一.原理分析 带有uboot的开发板实际上充当的就是tftp客户端,而PC机扮演的角色就是tftp服务器端,而tftp下载功能实际上就是文件传输.tftp服务器可以建立在虚拟机linux下,也可以建立 ...

  5. bzoj3864: Hero meet devil

    Description There is an old country and the king fell in love with a devil. The devil always asks th ...

  6. [转]Uploading and Downloading VHDs to Windows Azure

    The article shows how to download and upload VHD to Azure. http://michaelwasham.com/windows-azure-po ...

  7. WIN版的Jenkins Master加入LINUX的SLAVE节点,并作C++程序的集成交付

    这次深撸了一下JENKINS的配置,不敢说完全通了. 但对于整个体系,有了更新认识. 将LINUX作为SLAVE节点加入WIN的JENKINS里,网上有很多教程,依作即可. 在将相关任务分配给这个节点 ...

  8. NXP LPC11xx I2C Slave 从机程序

    /**************************************************************************** * $Id:: i2cslave.c 363 ...

  9. 一句话改变TGraphicControl控件的left坐标的前世今生

    稍微用脑子想了一下,图形控件没有句柄,因此先把自己的坐标改一改,然后只要把父控件的某些区域Invalidate一下就可以了,WM_PAINT消息一来,父控件就会重绘所有子图形控件,就达到了相应的效果. ...

  10. If one session has a shared or exclusive lock on record R in an index, another session cannot insert

    If one session has a shared or exclusive lock on record R in an index, another session cannot insert ...