Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4715   Accepted: 1590

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 ]有一个权值w ,  问覆盖区间[ m , e ]的最小权值是多少。
解析: 首先按照右端点从小到大排序,然后dp[i][j] 表示 考虑前i个区间,覆盖区间[m,j]的最小值。
考虑当前区间[ li , ri ]容易想到 dp[i][ri] 的最小值就是 dp[ri]=min(dp[ri],min(dp[ li -1, ri ])+wi) 我们可以线段树查找区间最小值进行优化,
当我们确定了dp[ri] 其实更新ri这一个点值就可以了 因为下一个区间[ Li+1,Ri+1 ] 询问最小值的时候,一定会询问到dp[ri],询问不到,说明 Li+1是大于 ri 的,覆盖的区间已经断开了。
AC代码
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n")
#define debug(a,b) cout<<a<<" "<<b<<" "<<endl
#define ffread(a) fastIO::read(a)
using namespace std;
typedef long long ll;
const int maxn = 1e5+;
const ll inf = 1e12;
const ll mod = ;
const double epx = 1e-;
const double pi = acos(-1.0);
//head-----------------------------------------------------------------
ll minn[maxn*];
void PushUp(int rt)
{
minn[rt]=min(minn[rt<<],minn[rt<<|]);
}
void update(int x,ll val,int l,int r,int rt)
{
if(l==x&&r==x)
{
minn[rt]=val;
return;
}
int mid=(l+r)>>;
if(x<=mid)
update(x,val,l,mid,rt<<);
else
update(x,val,mid+,r,rt<<|);
PushUp(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
return minn[rt];
}
int mid=(l+r)>>;
ll ans=1e12;
if(L<=mid) ans=min(ans,query(L,R,l,mid,rt<<));
if(R>mid) ans=min(ans,query(L,R,mid+,r,rt<<|));
return ans;
}
struct node
{
ll l,r,val;
}qujian[maxn];
bool cmp(node a,node b)
{
return a.r<b.r;
}
int main()
{
ll n,m,e;
scanf("%lld%lld%lld",&n,&m,&e);
m+=,e+=;
for(int i=;i<n;i++)
{
scanf("%lld%lld%lld",&qujian[i].l,&qujian[i].r,&qujian[i].val);
qujian[i].l+=,qujian[i].r+=;
}
sort(qujian,qujian+n,cmp);
for(int i=;i<=1e5;i++)
update(i,inf,,1e5,);
update(m-,,,1e5,);
for(int i=;i<n;i++)
{
if(qujian[i].r>=m)
{
ll tempmin=query(qujian[i].l-,qujian[i].r,,1e5,);
if(tempmin==inf)
continue;
ll temp=tempmin+qujian[i].val;
ll before=query(qujian[i].r,qujian[i].r,,1e5,);
if(before>temp)
update(qujian[i].r,temp,,1e5,);
}
}
ll ans=query(e,1e5,,1e5,);
if(ans==inf)
printf("-1\n");
else
printf("%lld\n",ans);
}

POJ 3171 区间覆盖最小值&&线段树优化dp的更多相关文章

  1. POJ 1769 Minimizing maximizer (线段树优化dp)

    dp[i = 前i中sorter][j = 将min移动到j位置] = 最短的sorter序列. 对于sorteri只会更新它右边端点r的位置,因此可以把数组改成一维的,dp[r] = min(dp[ ...

  2. POJ 2376 Cleaning Shifts (线段树优化DP)

    题目大意:给你很多条线段,开头结尾是$[l,r]$,让你覆盖整个区间$[1,T]$,求最少的线段数 题目传送门 线段树优化$DP$裸题.. 先去掉所有能被其他线段包含的线段,这种线段一定不在最优解里 ...

  3. 洛谷$P2605\ [ZJOI2010]$基站选址 线段树优化$dp$

    正解:线段树优化$dp$ 解题报告: 传送门$QwQ$ 难受阿,,,本来想做考试题的,我还造了个精妙无比的题面,然后今天讲$dp$的时候被讲到了$kk$ 先考虑暴力$dp$?就设$f_{i,j}$表示 ...

  4. 2021.12.08 P1848 [USACO12OPEN]Bookshelf G(线段树优化DP)

    2021.12.08 P1848 [USACO12OPEN]Bookshelf G(线段树优化DP) https://www.luogu.com.cn/problem/P1848 题意: 当农夫约翰闲 ...

  5. [AGC011F] Train Service Planning [线段树优化dp+思维]

    思路 模意义 这题真tm有意思 我上下楼梯了半天做出来的qwq 首先,考虑到每K分钟有一辆车,那么可以把所有的操作都放到模$K$意义下进行 这时,我们只需要考虑两边的两辆车就好了. 定义一些称呼: 上 ...

  6. 4.11 省选模拟赛 序列 二分 线段树优化dp set优化dp 缩点

    容易想到二分. 看到第一个条件容易想到缩点. 第二个条件自然是分段 然后让总和最小 容易想到dp. 缩点为先:我是采用了取了一个前缀最小值数组 二分+并查集缩点 当然也是可以直接采用 其他的奇奇怪怪的 ...

  7. 【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp

    题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a varian ...

  8. D - The Bakery CodeForces - 834D 线段树优化dp···

    D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...

  9. Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...

随机推荐

  1. NodeJS基础入门-Event

    大多数Node.js核心API都采用惯用的异步事件驱动架构,其中某些类型的对象(触发器)会周期性地触发命名事件来调用函数对象(监听器). 例如,net.Server对象会在每次有新连接时触发事件;fs ...

  2. RSA非对称加密算法实现过程

    RSA非对称加密算法实现过程 非对称加密算法有很多,RSA算法就是其中比较出名的算法之一,下面是具体实现过程 <?php /** */ class Rsa { /** * private key ...

  3. 【Linux命令】nohup和&差异,查看进程和终止进程!

    最近在开发dueros的技能,官方提供的PHPSDK中有多个实力,而运行实例的命令如下是 nohup php -S 0.0.0.0:8029 myindex.php & 从命令来看,肯定是在8 ...

  4. Lecture 3

    surface models 1. The two main methods of creating surface models are interpolation and triangulatio ...

  5. Apache ant 配置

    ANT_HOME C:\Program Files(D)\apache-ant-1.10.1Path %ANT_HOME%/binant -v

  6. webpack 环境搭建+实现热更新

    让我们一起构建一个小的app 为了便于你更好的了解Webpack带来的好处,我们将会构建一个非常小的app并将资源文件打包.在这个教程中我推荐基于Node4或Node5和NPM3来进行开发,这样就避免 ...

  7. ICCID

     ICCID:Integrate circuit card identity 集成电路卡识别码(固化在手机SIM卡中) ICCID为IC卡的唯一识别号码,共有20位数字+英文组成,其编码格式为:XXX ...

  8. 零基础自学用Python 3开发网络爬虫

    原文出处: Jecvay Notes (@Jecvay) 由于本学期好多神都选了Cisco网络课, 而我这等弱渣没选, 去蹭了一节发现讲的内容虽然我不懂但是还是无爱. 我想既然都本科就出来工作还是按照 ...

  9. ubuntu14.04修改mysql默认编码

    修改文件为/etc/mysql/my.cnf [client] default-character-set = utf8 (ps:client的设置没变) [mysqld] lower_case_ta ...

  10. BZOJ1189 [HNOI2007]紧急疏散evacuate 【二分 + 网络流】

    题目 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一 块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一扇门,人们可以从 ...