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. 如何使用jmeter做关联

    1.适用场景 从上一个接口的返回值中获取值传递给下一个接口使用 2.添加JSON Extractor 在需求提取的参数上添加--后置处理器--JSON Extractor 从登录接口的返回值中取use ...

  2. Oracle rownum的理解

    核心过程分三步: 从表中取出行(无索引的话,顺序取出). 根据当前结果集,为当前行添加rownum. 条件筛选,如通过则添加到结果集中. 完.

  3. ESP8266入门学习笔记1:资料获取

    乐鑫官网:https://www.espressif.com/zh-hans/products/hardware/esp8266ex/overview 乐鑫资料:https://www.espress ...

  4. 在SCIKIT中做PCA 逆变换 -- 新旧特征转换

    PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分量,常用于高维数据的降 ...

  5. The 2018 ACM-ICPC Chinese Collegiate Programming Contest Take Your Seat

    /* 证明过程如下 :第一种情况:按1到n的顺序上飞机,1会随意选一个,剩下的上去时若与自己序号相同的座位空就坐下去,若被占了就也会随意选一个.求最后一个人坐在应坐位置的概率 */ #include ...

  6. 逻辑与(&)和短路与(&&)的关系

    逻辑与(&)和短路与(&&)在运算上对条件的结果判断不会产生影响,但会对条件判断的运算有影响.关键在于,逻辑与(&)在运算时会连续运算所有需要判断的命令.但短路与当遇到 ...

  7. Sublime插件开发——简单的代码模板插件

    最近一段一直使用sublime进行golang开发,整体感觉很不错,虽然比不上eclipse之类IDE强大,但是用起来很轻巧便捷,开发golang完全做够了.由于有一部分代码复用率很高,经常要用到,而 ...

  8. Linux中TTY是什么意思

    终端是一种字符型设备,它有多种类型,通常使用tty来简称各种类型的终端设备.tty是Teletype的缩写.Teletype是最早出现的一种终端 设备,很象电传打字机(或者说就是),是由Telety ...

  9. lambda遍历的精简

    本文转自 http://it.deepinmind.com/java%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/2014/03/15/Java%E5%8 ...

  10. Android强制更新

    代码改变世界 Android版本强制更新 package com.lianpos.util; import android.content.Context; import android.conten ...