你知道吗,SQ Class的人都很喜欢打CS。(不知道CS是什么的人不用参加这次比赛)。
描述
今天,他们在打一张叫DUSTII的地图,万恶的恐怖分子要炸掉藏在A区的SQC论坛服务器!我们SQC的人誓死不屈,即将于恐怖分子展开激战,准备让一个人守着A区,这样恐怖分子就不能炸掉服务器了。(一个人就能守住??这人是机械战警还是霹雳游侠?)
但是问题随之出现了,由于DustII中风景秀丽,而且不收门票,所以n名反恐精英们很喜欢在这里散步,喝茶。他们不愿意去单独守在荒无人烟的A区,在指挥官的一再命令下,他们终于妥协了,但是他们每个人都要求能继续旅游,于是给出了自己的空闲时间,而且你强大的情报系统告诉了你恐怖份子计划的进攻时间(从s时刻到e时刻)。
当然,精明的SQC成员不会为你免费服务,他们还要收取一定的佣金(注意,只要你聘用这个队员,不论他的执勤时间多少,都要付所有被要求的佣金)。身为指挥官的你,看看口袋里不多的资金(上头真抠!),需要安排一个计划,雇佣一些队员,让他们在保证在进攻时间里每时每刻都有人员执勤,花费的最少资金。
格式
输入格式
第一行是三个整数n(1≤n≤10000),s和e(1≤s≤e≤90000)。
接下来n行,描述每个反恐队员的信息:空闲的时间si, ei(1≤si≤ei≤90000)和佣金ci(1≤ci≤300000)。
输出格式
一个整数,最少需支付的佣金,如果无解,输出“-1”。
样例1
样例输入1
3 1 5
1 3 3
4 5 2
1 1 1
样例输出1
5
限制
提示
敌人从1时刻到4时刻要来进攻,一共有3名反恐队员。第1名从1时刻到3时刻有空,要3元钱(买糖都不够??)。以此类推。
一共要付5元钱,选用第1名和第2名。
来源
SQ CLASS公开编程竞赛2008——Problem D
Source: WindTalker, liuichou, royZhang
题目大意
数轴上有一些区间,每个区间有一个费用。要求选择一些区间将$[s, t]$覆盖,问最小的总费用。
Solution#1 Dynamic Programming & Heap Optimization
dp是显然的。 用$f[i]$表示将$[s, i]$覆盖的最小费用。
考虑转移。显然通过能够覆盖点$i$的线段进行转移,因此有
$f[i] = \max_{线段j能覆盖点i}\left\{ f[l_{j} - 1] + w_{j}\right\}$
显然可以用一个堆维护右边的那一坨。
Code
1 /** 2 * Vijos 3 * Problem#1404 4 * Accepted 5 * Time: 98ms 6 * Memory: 3.48m 7 */ 8 #include <bits/stdc++.h> 9 #ifndef WIN32 10 #define Auto "%lld" 11 #else 12 #define Auto "%I64d" 13 #endif 14 using namespace std; 15 typedef bool boolean; 16 17 #define ll long long 18 19 typedef class Segment { 20 public: 21 int l, r; 22 int w; 23 24 Segment(int l = 0, int r = 0, int w = 0):l(l), r(r), w(w) { } 25 26 boolean operator < (Segment b) const { 27 return l < b.l; 28 } 29 }Segment; 30 31 typedef class Heap { 32 public: 33 priority_queue<ll, vector<int>, greater<int> > que; 34 priority_queue<ll, vector<int>, greater<int> > del; 35 36 Heap() { } 37 38 ll top() { 39 while (!del.empty() && que.top() == del.top()) 40 que.pop(), del.pop(); 41 return que.top(); 42 } 43 44 void push(ll x) { 45 que.push(x); 46 } 47 48 void remove(ll x) { 49 del.push(x); 50 } 51 52 boolean empty() { 53 return (que.size() == del.size()); 54 } 55 }Heap; 56 57 int n, s, t; 58 Segment* ss; 59 vector<Segment*> *g; 60 Heap h; 61 ll *f; 62 63 inline void init() { 64 scanf("%d%d%d", &n, &s, &t); 65 ss = new Segment[(n + 1)]; 66 g = new vector<Segment*>[(t - s + 5)]; 67 f = new ll[(t - s + 5)]; 68 t = t - s + 1; 69 for (int i = 1; i <= n; i++) { 70 scanf("%d%d%d", &ss[i].l, &ss[i].r, &ss[i].w); 71 ss[i].l = max(ss[i].l - s, 0) + 1; 72 ss[i].r = max(ss[i].r - s, 0) + 1; 73 if (ss[i].r > t) ss[i].r = t; 74 } 75 } 76 77 inline void solve() { 78 int p = 1; 79 f[0] = 0; 80 sort (ss + 1, ss + n + 1); 81 for (int i = 1; i <= t; i++) { 82 while (p <= n && ss[p].l == i) { 83 h.push(f[ss[p].l - 1] + ss[p].w); 84 g[ss[p].r].push_back(ss + p); 85 p++; 86 } 87 if (h.empty()) { 88 puts("-1"); 89 return; 90 } 91 f[i] = h.top(); 92 for (int j = 0; j < (signed)g[i].size(); j++) 93 h.remove(f[g[i][j]->l - 1] + g[i][j]->w); 94 g[i].clear(); 95 } 96 printf(Auto"\n", f[t]); 97 } 98 99 int main() { 100 init(); 101 solve(); 102 return 0; 103 }