POJ 3171 区间最小花费覆盖 (DP+线段树
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4245 | Accepted: 1429 |
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
题意:给n个区间及其代价值,问要覆盖[M,E]区间至少要花费多少代价;
解法:这是一个dp问题,先列出方程。
F[i]表示取[0,i]这个区间的代价,初始化F[M-1]=0,答案就是F[E].
则方程为F[a[i].T2]=min(F[a[j].T2])+a[i].s (T1-1<=a[j].T2<T2),找min的过程用线段树实现。
将a[i]按T2从小到大排列,逐步更新最小值。
代码:
#include"bits/stdc++.h" #define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, n) for(int i=0;i<n;i++)
using namespace std;
const int NN = 1e6 + ;
int n,s,t;
struct P{int x,y,s;};
P a[NN];
bool cmp(P a,P b){
return a.y<b.y;
}
const ll INF = 0x3fffffffffffffff;
struct SegMin {
int N;
vl is;vl mul;vl add;
ll init;
ll merge(ll a, ll b) {
return min(a, b);
}
void push(int o, int L, int R, ll m, ll a) {
is[o] = is[o] * m + a;
mul[o] = mul[o] * m;
add[o] = add[o] * m + a;
} SegMin(int n, ll init=INF) {
N = ;
while (N < n) N *= ;
this->init = init;
is = vl(N * , init);
mul = vl(N * , );
add = vl(N * );
} SegMin(vl a, ll init=INF) {
int n = a.size();
N = ;
while (N < n) N *= ;
this->init = init;
is = vl(N * );
mul = vl(N * , );
add = vl(N * );
copy(a.begin(), a.end(), is.begin() + N);
for (int i = N - ; i > ; i--) {
is[i] = merge(is[i << ], is[i << | ]);
}
} void update(int l, int r, ll m, ll a) {
if (l < r) update(, , N, l, r, m, a);
} void update(int o, int L, int R, int l, int r, ll m, ll a) {
if (l <= L && R <= r) {
push(o, L, R, m, a);
} else {
int M = (L + R) >> ;
push(o, L, M, R);
if (l < M) update(o << , L, M, l, r, m, a);
if (r > M) update(o << | , M, R, l, r, m, a);
is[o] = merge(is[o << ], is[o << | ]);
}
} void push(int o, int L, int M, int R) {
if (mul[o] != || add[o] != ) {
push(o << , L, M, mul[o], add[o]);
push(o << | , M, R, mul[o], add[o]);
mul[o] = ;
add[o] = ;
}
} ll query(int l, int r) {
if (l < r) return query(, , N, l, r);
return init;
} ll query(int o, int L, int R, int l, int r) {
if (l <= L && R <= r) {
return is[o];
} else {
int M = (L + R) >> ;
push(o, L, M, R);
ll res = init;
if (l < M) res = merge(res, query(o << , L, M, l, r));
if (r > M) res = merge(res, query(o << | , M, R, l, r));
is[o] = merge(is[o << ], is[o << | ]);
return res;
}
}
}; int main(){
ci(n),ci(s),ci(t);//s从1开始
s++,t++;
int ma=;
for(int i=;i<n;i++) ci(a[i].x),ci(a[i].y),ci(a[i].s);
for(int i=;i<n;i++) a[i].x++,a[i].y++,ma=max(ma,a[i].y);
sort(a,a+n,cmp);
SegMin seg(ma+);
seg.update(,ma+,,INF);
seg.update(,s,,); for(int i=;i<n;i++){
if(a[i].y<s) continue;
int L=a[i].x-,R=a[i].y;
ll res=seg.query(L,R)+a[i].s;
res=min(seg.query(R,R+),res);//与前面的最小值取min
seg.update(R,R+,,res);
}
ll ans=seg.query(t,ma+);
if(ans>=INF) puts("-1");//未覆盖到
else pl(ans);
return ;
}
POJ 3171 区间最小花费覆盖 (DP+线段树的更多相关文章
- cf834D(dp+线段树区间最值,区间更新)
题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...
- POJ 2482 Stars in Your Window (线段树+扫描线+区间最值,思路太妙了)
该题和 黑书 P102 采矿 类似 参考链接:http://blog.csdn.net/shiqi_614/article/details/7819232http://blog.csdn.net/ts ...
- HDU 3698 DP+线段树
给出N*M矩阵.每一个点建立灯塔有花费.每一个点的灯塔有连接范围,求每一行都建立一个灯塔的最小花费,要求每相邻两行的灯塔能够互相连接.满足 |j-k|≤f(i,j)+f(i+1,k) DP思路,dp[ ...
- bzoj 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚【dp+线段树】
设f[i]为i时刻最小花费 把牛按l升序排列,每头牛能用f[l[i]-1]+c[i]更新(l[i],r[i])的区间min,所以用线段树维护f,用排完序的每头牛来更新,最后查询E点即可 #includ ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- ZOJ 3349 Special Subsequence 简单DP + 线段树
同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...
- POJ 3468_A Simple Problem with Integers(线段树)
题意: 给定序列及操作,求区间和. 分析: 线段树,每个节点维护两个数据: 该区间每个元素所加的值 该区间元素和 可以分为"路过"该区间和"完全覆盖"该区间考虑 ...
- Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)
Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...
- POJ 2828 Buy Tickets(排队问题,线段树应用)
POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意: 排队买票时候插队. 给出一些数对,分别代表某个人的想要插入的位 ...
随机推荐
- CDSN博客第一天
CDSN博客第一天 今天是CSDN写博客的第一天. 2017/2/11 13:05:45
- Python常用模块(四)
一.re模块 正则表达式时计算机科学的一个概念,正则表达式通常被用来检索,替换那些符合某个模式的文本,大多数程序设计语言都支持利用正则表达式进行字符串操作. 正则就是用一些具有特殊含义的符号组合到一起 ...
- 安全漏洞 : XSS CSRF
https://my.oschina.net/hc24/blog/527099 XSS成因概括 : XSS其实就是Html的注入问题,攻击者A的输入没有经过严格的控制进入了数据库,最终显示给来访的用户 ...
- 黑客伦理(hacker ethic)--《黑客与画家》
使用计算机以及所有有助于了解这个世界本质的事物都不应受到任何限制.任何事情都应该亲手尝试. Access to computers--and anything that might teach you ...
- (一)JavaScript之[数据类型]与[对象]
1].数据类型字符串(String).数字(Number).布尔(Boolean).数组(Array).对象(Object).空(Null).未定义 (Undefined). //极大或极小的数字可以 ...
- 即将要被淘汰的兼容之--CSS Hack
css hack 条件注释法只在IE下生效<!--[if IE]>这段文字只在IE浏览器显示<![endif]-->只在IE6下生效<!--[if IE 6]>这段 ...
- 文件上传PHP
<?php $targetIp = GetIP(); $fileUpload = 'fileUpload'; $frameCount = 'frameCount'; $fileName = $_ ...
- Visual Studio 各个版本汇总
微软开发人员,对开发工具的熟练程度,在一定程度上说明了开发 版本 名称 内部版本 发布日期 支持 .NET Framework 版本 备注 引入 .NET Framework 前[4] 1 Visu ...
- MySQL免安装版中 my-default.ini 的配置
拷贝一份 “my-default.ini” 文件 重命名为 “my.ini” 这样根目录下就有两个.ini文件了 一个是my-default.ini 一个是my.ini 接下来我们只需修改my.i ...
- SSL Labs: Increased Penalty When TLS 1.2 Is Not Supported
https://community.qualys.com/blogs/securitylabs/2015/05/22/ssl-labs-increased-penalty-when-tls-12-is ...