poj2376 Cleaning Shifts【线段树】【DP】
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 32561 | Accepted: 7972 |
Description
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
* 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
Sample Input
3 10
1 7
3 6
6 10
Sample Output
2
Hint
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】的更多相关文章
- POJ 2376 Cleaning Shifts (线段树优化DP)
题目大意:给你很多条线段,开头结尾是$[l,r]$,让你覆盖整个区间$[1,T]$,求最少的线段数 题目传送门 线段树优化$DP$裸题.. 先去掉所有能被其他线段包含的线段,这种线段一定不在最优解里 ...
- POJ2376 Cleaning Shifts
题意 POJ2376 Cleaning Shifts 0x50「动态规划」例题 http://bailian.openjudge.cn/practice/2376 总时间限制: 1000ms 内存限制 ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- [Usaco2005 Dec]Cleaning Shifts 清理牛棚 (DP优化/线段树)
[Usaco2005 Dec] Cleaning Shifts 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new ...
- 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚 dp/线段树
题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...
- $Poj2376\ Poj3171\ Luogu4644\ Cleaning\ Shifts$ 数据结构优化$DP$
$Poj$ $AcWing$ $Luogu$ $ps:$洛谷题目与$Poj$略有不同,以下$Description$是$Poj$版.题目的不同之处在于洛谷中雇用奶牛的费用不相同,所以不可以 ...
- 洛谷P4644 [USACO2005 Dec]Cleaning Shifts 清理牛棚 [DP,数据结构优化]
题目传送门 清理牛棚 题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness ...
- lightoj1085 线段树+dp
//Accepted 7552 KB 844 ms //dp[i]=sum(dp[j])+1 j<i && a[j]<a[i] //可以用线段树求所用小于a[i]的dp[j ...
随机推荐
- 谷歌修复了 FFmpeg 中上千个 bug
谷歌在科技业界中几乎每天都会创造出新闻素材,它的触手涉及到了生活中的多个领域.最近谷歌将其Google +社交网络与邮件服务Gmail相结合.然而今天谷歌宣布他们修复了FFmpeg的上千个bug. ...
- vncserver的安装和使用
环境:RedHat Linux 6企业版.Xwindows:gnome (红帽默认安装的图形界面) 尽管我们可以使用SSH连接远程通过字符界面来操作Linux,但是对于更多熟悉图形人来说是很不方便的, ...
- PHP实现金额数字转换成大写函数
<?php header("Content-Type:text/html;charset=utf-8"); function num_to_upper($num) { $d ...
- HTML input只能输入数字
onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" onafterpaste="this.value=this.v ...
- 将android程序中的数据库导出到SD卡
private void copyDBToSDcrad() { String DATABASE_NAME = "数据库文件名"; String oldPath = "da ...
- LaTeX公式
在学习机器学习中会接触到大量的数学公式,所以在写博客是会非常的麻烦.用公式编辑器一个一个写会非常的麻烦,这时候我们可以使用LaTeX来插入公式. 写这篇博文的目的在于,大家如果要编辑一些简单的公式,就 ...
- mysql中/*!40000 DROP DATABASE IF EXISTS `top_server`*/;这中注释有什么作用?
需求描述: 今天在进行mysqldump实验,使用--add-drop-databases参数,于是在生成的SQL文件中,就出现了. /*!40000 DROP DATABASE IF EXISTS ...
- fork函数和vfork函数的区别--19
fork()与vfock()都是创建一个进程,那他们有什么区别呢?总结有以下三点区别: 1. fork ():子进程拷贝父进程的数据段,代码段 vfork ( ):子进程与父进程共享数据段 ...
- ios开发之--UICollectionView的使用
最近项目中需要实现一种布局,需要用到UICollectionView,特在此整理记录下! 贴上最终实现的效果图: 1,声明 @interface FirstViewController ()<U ...
- 数组内Merge
数组al[0...mid-1]和al[mid...num-1]两个部分都已经分别排好序.要求合并使得整个数组al有序.请给出合并merge的代码.要求空间复杂度为O(1). /* 数组a[begin, ...