Cleaning Shifts
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4245   Accepted: 1429

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

  1. 3 0 4
  2. 0 2 3
  3. 3 4 2
  4. 0 0 1

Sample Output

  1. 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.

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从小到大排列,逐步更新最小值。

代码:

  1. #include"bits/stdc++.h"
  2.  
  3. #define ll long long
  4. #define vl vector<ll>
  5. #define ci(x) scanf("%d",&x)
  6. #define pi(x) printf("%d\n",x)
  7. #define pl(x) printf("%lld\n",x)
  8. #define rep(i, n) for(int i=0;i<n;i++)
  9. using namespace std;
  10. const int NN = 1e6 + ;
  11. int n,s,t;
  12. struct P{int x,y,s;};
  13. P a[NN];
  14. bool cmp(P a,P b){
  15. return a.y<b.y;
  16. }
  17. const ll INF = 0x3fffffffffffffff;
  18. struct SegMin {
  19. int N;
  20. vl is;vl mul;vl add;
  21. ll init;
  22. ll merge(ll a, ll b) {
  23. return min(a, b);
  24. }
  25. void push(int o, int L, int R, ll m, ll a) {
  26. is[o] = is[o] * m + a;
  27. mul[o] = mul[o] * m;
  28. add[o] = add[o] * m + a;
  29. }
  30.  
  31. SegMin(int n, ll init=INF) {
  32. N = ;
  33. while (N < n) N *= ;
  34. this->init = init;
  35. is = vl(N * , init);
  36. mul = vl(N * , );
  37. add = vl(N * );
  38. }
  39.  
  40. SegMin(vl a, ll init=INF) {
  41. int n = a.size();
  42. N = ;
  43. while (N < n) N *= ;
  44. this->init = init;
  45. is = vl(N * );
  46. mul = vl(N * , );
  47. add = vl(N * );
  48. copy(a.begin(), a.end(), is.begin() + N);
  49. for (int i = N - ; i > ; i--) {
  50. is[i] = merge(is[i << ], is[i << | ]);
  51. }
  52. }
  53.  
  54. void update(int l, int r, ll m, ll a) {
  55. if (l < r) update(, , N, l, r, m, a);
  56. }
  57.  
  58. void update(int o, int L, int R, int l, int r, ll m, ll a) {
  59. if (l <= L && R <= r) {
  60. push(o, L, R, m, a);
  61. } else {
  62. int M = (L + R) >> ;
  63. push(o, L, M, R);
  64. if (l < M) update(o << , L, M, l, r, m, a);
  65. if (r > M) update(o << | , M, R, l, r, m, a);
  66. is[o] = merge(is[o << ], is[o << | ]);
  67. }
  68. }
  69.  
  70. void push(int o, int L, int M, int R) {
  71. if (mul[o] != || add[o] != ) {
  72. push(o << , L, M, mul[o], add[o]);
  73. push(o << | , M, R, mul[o], add[o]);
  74. mul[o] = ;
  75. add[o] = ;
  76. }
  77. }
  78.  
  79. ll query(int l, int r) {
  80. if (l < r) return query(, , N, l, r);
  81. return init;
  82. }
  83.  
  84. ll query(int o, int L, int R, int l, int r) {
  85. if (l <= L && R <= r) {
  86. return is[o];
  87. } else {
  88. int M = (L + R) >> ;
  89. push(o, L, M, R);
  90. ll res = init;
  91. if (l < M) res = merge(res, query(o << , L, M, l, r));
  92. if (r > M) res = merge(res, query(o << | , M, R, l, r));
  93. is[o] = merge(is[o << ], is[o << | ]);
  94. return res;
  95. }
  96. }
  97. };
  98.  
  99. int main(){
  100. ci(n),ci(s),ci(t);//s从1开始
  101. s++,t++;
  102. int ma=;
  103. for(int i=;i<n;i++) ci(a[i].x),ci(a[i].y),ci(a[i].s);
  104. for(int i=;i<n;i++) a[i].x++,a[i].y++,ma=max(ma,a[i].y);
  105. sort(a,a+n,cmp);
  106. SegMin seg(ma+);
  107. seg.update(,ma+,,INF);
  108. seg.update(,s,,);
  109.  
  110. for(int i=;i<n;i++){
  111. if(a[i].y<s) continue;
  112. int L=a[i].x-,R=a[i].y;
  113. ll res=seg.query(L,R)+a[i].s;
  114. res=min(seg.query(R,R+),res);//与前面的最小值取min
  115. seg.update(R,R+,,res);
  116. }
  117. ll ans=seg.query(t,ma+);
  118. if(ans>=INF) puts("-1");//未覆盖到
  119. else pl(ans);
  120. return ;
  121. }

POJ 3171 区间最小花费覆盖 (DP+线段树的更多相关文章

  1. cf834D(dp+线段树区间最值,区间更新)

    题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...

  2. POJ 2482 Stars in Your Window (线段树+扫描线+区间最值,思路太妙了)

    该题和 黑书 P102 采矿 类似 参考链接:http://blog.csdn.net/shiqi_614/article/details/7819232http://blog.csdn.net/ts ...

  3. HDU 3698 DP+线段树

    给出N*M矩阵.每一个点建立灯塔有花费.每一个点的灯塔有连接范围,求每一行都建立一个灯塔的最小花费,要求每相邻两行的灯塔能够互相连接.满足 |j-k|≤f(i,j)+f(i+1,k) DP思路,dp[ ...

  4. 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 ...

  5. 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 ...

  6. ZOJ 3349 Special Subsequence 简单DP + 线段树

    同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...

  7. POJ 3468_A Simple Problem with Integers(线段树)

    题意: 给定序列及操作,求区间和. 分析: 线段树,每个节点维护两个数据: 该区间每个元素所加的值 该区间元素和 可以分为"路过"该区间和"完全覆盖"该区间考虑 ...

  8. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

  9. POJ 2828 Buy Tickets(排队问题,线段树应用)

    POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意:  排队买票时候插队.  给出一些数对,分别代表某个人的想要插入的位 ...

随机推荐

  1. cf1059D. Nature Reserve(三分)

    题意 题目链接 Sol 欲哭无泪啊qwq....昨晚一定是智息了qwq 说一个和标算不一样做法吧.. 显然\(x\)轴是可以三分的,半径是可以二分的. 恭喜你获得了一个TLE的做法.. 然后第二维的二 ...

  2. 转:解决Arcsde用户锁定的问题

    采用arcgis平台做GIS应用的人,可能偶尔碰到sde用户锁定(Arccatalog 或应用程序异常退出的时比较多)的问题,往往咱们解决的办法是重启sde服务.如果一个服务器上有多个连接时,重启服务 ...

  3. c++ 处理utf-8字符串

    c++的字符串中的每一个元素都是一个字节.所以在装入utf8字符串的时候,其实是按照一定的规则编码的. 字符的8位中 如果0开头 则自己就是一个单位. 1字节 0xxxxxxx  2字节 110xxx ...

  4. python模块详解 XML

    XML模块 XML是实现不同语言或程序之间进行数据交换的协议,和json一样. XML格式: <?xml version="1.0" encoding="UTF-8 ...

  5. centos7.4 系统安装指导

    centos7 系统安装指导 安装前规划 下载安装文件 安装过程设置 安装后系统基本设置 安装前规划 CentOS 7.x系列只有64位系统,没有32位. 生产服务器建议安装CentOS-7-x86_ ...

  6. JavaScript基础:比较运算符——==与 ===;!=与!==

    var x=10, y="10", m=15 x==y;//返回true x===y;//返回false x!=y;//返回false x!==y;//返回true//同理cons ...

  7. ALPS语言学校(西雅图)|ALPS Language School (Seattle)

    http://www.swliuxue.com/school-3879.html 所属国家: 美国 所在省洲: 华盛顿州 所在城市: 华盛顿州 建校时间: 1992年 学校类型: 院校 学校类别: 私 ...

  8. windows服务器间文件同步搭建步骤搜集

    Rsync https://www.cnblogs.com/janas/p/3321087.html https://yq.aliyun.com/ziliao/110867 subersion协议 h ...

  9. UE4的蓝图都能做什么

    创建关卡脚本 蓝图具有和虚幻3中Kismet一样的功能,每个关卡都由自己的蓝图,他可以: 引用和操控actors 控制使用Matinee的过场 管理关卡流,存档点以及其他关卡相关的系统 和关卡中的类蓝 ...

  10. ffmpeg 安装和参数介绍

    0.说明: 1).configure,这一步一般用来生成 Makefile,为下一步的编译做准备,你可以通过在 configure 后加上参数来对安装进行控制,比如代码:./configure –pr ...