POJ 3171 区间最小花费覆盖 (DP+线段树

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

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

Sample Output

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
",x)
  7 #define pl(x) printf("%lld
",x)
  8 #define rep(i, n) for(int i=0;i<n;i++)
  9 using namespace std;
 10 const int NN = 1e6 + 5;
 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 = 1;
 33         while (N < n) N *= 2;
 34         this->init = init;
 35         is = vl(N * 4, init);
 36         mul = vl(N * 4, 1);
 37         add = vl(N * 4);
 38     }
 39 
 40     SegMin(vl a, ll init=INF) {
 41         int n = a.size();
 42         N = 1;
 43         while (N < n) N *= 2;
 44         this->init = init;
 45         is = vl(N * 2);
 46         mul = vl(N * 2, 1);
 47         add = vl(N * 2);
 48         copy(a.begin(), a.end(), is.begin() + N);
 49         for (int i = N - 1; i > 0; i--) {
 50             is[i] = merge(is[i << 1], is[i << 1 | 1]);
 51         }
 52     }
 53 
 54     void update(int l, int r, ll m, ll a) {
 55         if (l < r) update(1, 0, 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) >> 1;
 63             push(o, L, M, R);
 64             if (l < M) update(o << 1, L, M, l, r, m, a);
 65             if (r > M) update(o << 1 | 1, M, R, l, r, m, a);
 66             is[o] = merge(is[o << 1], is[o << 1 | 1]);
 67         }
 68     }
 69 
 70     void push(int o, int L, int M, int R) {
 71         if (mul[o] != 1 || add[o] != 0) {
 72             push(o << 1, L, M, mul[o], add[o]);
 73             push(o << 1 | 1, M, R, mul[o], add[o]);
 74             mul[o] = 1;
 75             add[o] = 0;
 76         }
 77     }
 78 
 79     ll query(int l, int r) {
 80         if (l < r) return query(1, 0, 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) >> 1;
 89             push(o, L, M, R);
 90             ll res = init;
 91             if (l < M) res = merge(res, query(o << 1, L, M, l, r));
 92             if (r > M) res = merge(res, query(o << 1 | 1, M, R, l, r));
 93             is[o] = merge(is[o << 1], is[o << 1 | 1]);
 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=0;
103     for(int i=0;i<n;i++) ci(a[i].x),ci(a[i].y),ci(a[i].s);
104     for(int i=0;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+1);
107     seg.update(0,ma+1,0,INF);
108     seg.update(0,s,0,0);
109 
110     for(int i=0;i<n;i++){
111         if(a[i].y<s) continue;
112         int L=a[i].x-1,R=a[i].y;
113         ll res=seg.query(L,R)+a[i].s;
114         res=min(seg.query(R,R+1),res);//与前面的最小值取min
115         seg.update(R,R+1,0,res);
116     }
117     ll ans=seg.query(t,ma+1);
118     if(ans>=INF) puts("-1");//未覆盖到
119     else pl(ans);
120     return 0;
121 }

转载于:https://www.cnblogs.com/mj-liylho/p/9502012.html

原文地址:https://www.cnblogs.com/twodog/p/12136177.html