poj3171 Cleaning Shifts【线段树(单点修改区间查询)】【DP】
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4422 | Accepted: 1482 |
Description
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
Lines 2..N+1: Line i+1 describes cow i's schedule with three space-separated integers: T1, T2, and S.
Output
Sample Input
3 0 4
0 2 3
3 4 2
0 0 1
Sample Output
5
Hint
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.
Source
题意:
一个[L, R]的区间,有n头牛,每头牛可以清理一个固定区间,需要花费一定的价格。现在要清理[L,R]这个区间,需要花费最少的价格是多少。
思路:
用f[x]表示清理区间[L, x]需要花费的最小价格。
对于一头牛,他可以清理的区间是[a,b],那么因为中间不能间断,所以f[b] = min(f[x]) + c其中x是属于[a-1,b]区间的。
每次状态转移都要取一个区间最小值,并且更新一个点的值,所以用上线段树来维护。
首先将所有的牛按照结束的区间进行排序,然后DP。
因为时间是从0开始,所以我给所有的时间都加了2,这样a-1就还是从1开始。
要注意的是update()时,并不是直接将值改为val,而是取较小值,这里WA了一会。
#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 = 1e5 + ;
const int maxtime = ;
struct node{
int st, ed, cost;
}cow[maxn];
bool cmp(node a, node b)
{
return a.ed < b.ed;
}
LL tree[maxn << ];//区间中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%d", &n, &L, &R) != EOF){
L+=;R+=;
memset(tree, 0x7f, sizeof(tree));
for(int i = ; i <= n; i++){
scanf("%d%d%d", &cow[i].st, &cow[i].ed, &cow[i].cost);
cow[i].st+=;cow[i].ed+=;
}
sort(cow + , cow + + n, cmp); build(, , R); update(L - , , , R, );
//cout<<"yes"<<endl;
int far = L;
bool flag = true;
for(int i = ; i <= n; i++){
if(cow[i].st > far + ){
flag = false;
// break;
}
int a = max(L - , 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");
} } }
poj3171 Cleaning Shifts【线段树(单点修改区间查询)】【DP】的更多相关文章
- HDU 1166 敌兵布阵 <线段树 单点修改 区间查询>
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- I Hate It(线段树点修改区间查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 I Hate It Time Limit: 9000/3000 MS (Java/Others) ...
- HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)
HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...
- NYOJ-568/1012//UVA-12299RMQ with Shifts,线段树单点更新+区间查询
RMQ with Shifts 时间限制:1000 ms | 内存限制:65535 KB 难度:3 -> Link1 <- -> Link2 <- 以上两题题意是一样 ...
- Ocean的礼物(线段树单点修改)
题目链接:http://oj.ismdeep.com/contest/Problem?id=1284&pid=0 A: Ocean的礼物 Time Limit: 5 s Memory ...
- 校内模拟赛T5:连续的“包含”子串长度( nekameleoni?) —— 线段树单点修改,区间查询 + 尺取法合并
nekameleoni 区间查询和修改 给定N,K,M(N个整数序列,范围1~K,M次查询或修改) 如果是修改,则输入三个数,第一个数为1代表修改,第二个数为将N个数中第i个数做修改,第三个数为修改成 ...
- HDU - 1166 敌兵布阵 方法一:(线段树+单点修改,区间查询和) 方法二:利用树状数组
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...
- [线段树]区间修改&区间查询问题
区间修改&区间查询问题 [引言]信息学奥赛中常见有区间操作问题,这种类型的题目一般数据规模极大,无法用简单的模拟通过,因此本篇论文将讨论关于可以实现区间修改和区间查询的一部分算法的优越与否. ...
- HDU 1166敌兵布阵+NOJv2 1025: Hkhv love spent money(线段树单点更新区间查询)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
随机推荐
- 质量管理是SQA(软件质量保证)人员的职责
质量管理是SQA(软件质量保证)人员的职责
- MJRefresh原理分析
MJRefresh是流行的下拉刷新控件.前段时间为了修复一个BUG.读了它的源代码.本文总结一下实现的原理 下拉刷新的基本原理 大部分的下拉刷新控件.都是用contentInset实现的.默认情况下. ...
- jvm载入过程
类载入过程 类从被载入到虚拟机内存中開始,到卸载出内存为止,它的整个生命周期包含:载入.验证.准备.解析.初始化.使用和卸载七个阶段.它们開始的顺序例如以下图所看到的: 当中类载入的过程包含了载入.验 ...
- 用json在java和C#之间传递base64的问题。。。
记录下..唉.... java代码: 导入这个 commons-codec-1.8.jar (下载链接: http://files.cnblogs.com/files/gaocong/jar%E5%8 ...
- 【Java集合的详细研究6】Java 数组
Java 语言中提供的数组是用来存储固定大小的同类型元素. 声明数组变量 double[] myList; // 首选的方法 或 double myList[]; // 效果相同,但不是首选方法 创建 ...
- 使用jquery.uploadify动态传递自己的参数
上传碰到这个问题在上传文件的时候同时上传文件的类型..上网找了半天.总于解决了..分享一下了..直接例子了.. html <%@ page language="java" i ...
- 搭建基于 HDFS 碎片文件存储服务
安装 JDK HDFS 依赖 Java 环境,这里我们使用 yum 安装 JDK 8,在终端中键入如下命令: yum -y install java-1.8.0-openjdk* 使用如下命令查看下 ...
- when an event of selector will be fired
OP_READ Operation-set bit for read operations. Suppose that a selection key's interest set contains ...
- 配置Java的jdk环境变量
1.classpath E:\Java\jdk1..0_20\jre\lib\rt.jar;.;E:\Tomcat\lib; 2.JAVA_HOME E:\Java\jdk1..0_20; 3.Pat ...
- org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Can
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Can ...