Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32561   Accepted: 7972

Description

Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T.

Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval.

Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.

Input

* Line 1: Two space-separated integers: N and T

* Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.

Output

* Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

Sample Input

3 10
1 7
3 6
6 10

Sample Output

2

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

INPUT DETAILS:

There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10.

OUTPUT DETAILS:

By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.

Source

https://www.cnblogs.com/wyboooo/p/9808378.html

和poj3171基本一样,改一下输入和范围即可。

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const int maxn = + ;
const int maxtime = 1e6 + ;
struct node{
int st, ed, cost;
}cow[maxn];
bool cmp(node a, node b)
{
return a.ed < b.ed;
}
LL tree[maxtime << ];//区间中f[]最小值
int n, L, R; void pushup(int rt)
{
tree[rt] = min(tree[rt << ], tree[rt << |]);
} void build(int rt, int l, int r)
{
if(l == r){
tree[maxn] = inf;
return;
}
int mid = (l + r) / ;
build(rt<<, l, mid);
build(rt<<|, mid + , r);
pushup(rt);
} void update(int x, LL val, int l, int r, int rt)
{
if(l == r){
tree[rt] = min(tree[rt], val);
return;
}
int m = (l + r) / ;
if(x <= m){
update(x, val, l, m, rt<<);
}
else{
update(x, val, m + , r, rt<<|);
}
pushup(rt);
} LL query(int L, int R, int l, int r, int rt)
{
if(L <= l && R >= r){
return tree[rt];
}
int m = (l + r) / ;
LL ans = inf;
if(L <= m){
ans = min(ans, query(L, R, l, m, rt<< ));
}
if(R > m){
ans = min(ans, query(L, R, m + , r, rt<<|));
}
pushup(rt);
return ans;
} int main()
{
while(scanf("%d%d", &n, &R) != EOF){
R+=;
memset(tree, 0x7f, sizeof(tree));
for(int i = ; i <= n; i++){
scanf("%d%d", &cow[i].st, &cow[i].ed);
cow[i].st+=;cow[i].ed+=;
cow[i].cost = ;
}
sort(cow + , cow + + n, cmp); build(, , R); update(, , , R, );
//cout<<"yes"<<endl;
//int far = L;
bool flag = true;
for(int i = ; i <= n; i++){
/*if(cow[i].st > far + 1){
flag = false;
// break;
}*/
int a = max(, cow[i].st - );
int b = min(R, cow[i].ed);
//cout<<a<<" "<<b<<endl;
LL f = query(a, b, , R, );
f += cow[i].cost;
//cout<<f<<endl;
update(b, f, , R, );
//far = max(far, cow[i].ed);
//cout<<far<<endl;
}
//cout<<"yes"<<endl; LL ans = query(R, R, , R, );
if(ans >= inf){
printf("-1\n");
}
else{
printf("%lld\n", ans); //else{
// printf("-1\n");
} } }

poj2376 Cleaning Shifts【线段树】【DP】的更多相关文章

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

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

  2. POJ2376 Cleaning Shifts

    题意 POJ2376 Cleaning Shifts 0x50「动态规划」例题 http://bailian.openjudge.cn/practice/2376 总时间限制: 1000ms 内存限制 ...

  3. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  4. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  5. [Usaco2005 Dec]Cleaning Shifts 清理牛棚 (DP优化/线段树)

    [Usaco2005 Dec] Cleaning Shifts 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new ...

  6. 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚 dp/线段树

    题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...

  7. $Poj2376\ Poj3171\ Luogu4644\ Cleaning\ Shifts$ 数据结构优化$DP$

    $Poj$    $AcWing$    $Luogu$ $ps:$洛谷题目与$Poj$略有不同,以下$Description$是$Poj$版.题目的不同之处在于洛谷中雇用奶牛的费用不相同,所以不可以 ...

  8. 洛谷P4644 [USACO2005 Dec]Cleaning Shifts 清理牛棚 [DP,数据结构优化]

    题目传送门 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness ...

  9. lightoj1085 线段树+dp

    //Accepted 7552 KB 844 ms //dp[i]=sum(dp[j])+1 j<i && a[j]<a[i] //可以用线段树求所用小于a[i]的dp[j ...

随机推荐

  1. 【转】MFC 迅雷七窗体特效,使用DWM实现Aero Glass效果

    从Windows Vista开始,Aero Glass效果被应用在了Home Premium以上的系统中(Home Basic不具有该效果).这种效果是由DWM(Desktop Window Mana ...

  2. IoC最大的好处是什么?

    IoC最大的好处是什么?因为把对象生成放在了XML里定义,所以当我们需要换一个实现子类将会变成很简单(一般这样的对象都是实现于某种接口的),只要修改XML就可以了,这样我们甚至可以实现对象的热插拨(有 ...

  3. 搭建局域网SVN代码服务器

    1.安装Subversion,安装好后,在控制台输入“svn help”,如果成功安装,则会有很多命令打印输出:2.svnadmin create F:\Java_workspace\Reposito ...

  4. 在GIT中创建一个空分支

    ref:  https://segmentfault.com/a/1190000004931751

  5. html5移动端根据百度地图api获取详细地址

    <script type="text/javascript" src="js/BMap.js" ></script> <scrip ...

  6. Spring配置文件加载流程

    http://blog.csdn.net/dy_paradise/article/details/6038990

  7. Android v4包中的 SwipeRefreshLayout 官方的下拉刷新组件

    SwipeRefreshLayout在v4包下,相应的v4Demo中也有相应的样例.假设没有请下载最新support-v4 SwipeRefreshLayout 仅仅能有一个直接子View,可能是一个 ...

  8. poj 3414 Pots(广搜BFS+路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...

  9. VCL 中的 Windows API 函数(6): BeginDeferWindowPos

    BeginDeferWindowPos 和 DeferWindowPos.EndDeferWindowPos 是一组一起使用的函数, 可对一组窗口的位置.大小.Z 序等进行调整, 在 ExtCtrls ...

  10. XP 终端服务组件 恢复补丁包 terminal service patch

    terminal 终端服务组件恢复包 下载地址(点击) winconnect server xp软件 下载地址(点击)