题面
这个写题面的风气是谁带起来的。。。
Description
给出\(N\)件单位时间任务,对于第\(i\)件任务,如果要完成该任务,需要占用\([S_i,T_i]\)间的某个时刻,且完成后会有\(V_i\)的收益。求最大收益。
\(N\leq 5000,1 \leq S_i \leq T_i \leq 10^8,1 \leq V_i \leq 10^8\)
澄清:一个时刻只能做一件任务,做一个任务也只需要一个时刻。
Input
第一行一个整数\(N\),表示可供选择的任务个数。
接下来的第二到第\(N+1\)行,每行三个数,其中第\(i+1\)行依次为\(S_i,T_i,V_i\)
Output
输出最大收益
Sample Input
4
1 1 2
2 2 2
1 2 3
1 3 1
Sample Output
6
HINT
共有四个任务,其中第一个任务只能在时刻\(1\)完成,第二个任务只能在时刻\(2\)做,第三个任务只能在时刻\(1\)或时刻\(2\)做,第四个任务可以在\([1,3]\)内任一时刻完成,四个任务的价值分别为\(2,2,3\)和\(1\)。一种完成方案是:时刻\(1\)完成第一个任务,时刻\(2\)完成第三个任务,时刻\(3\)完成第四个任务,这样得到的总收益为\(2+3+1=6\),为最大值。
题解
第一眼:线段树优化连边的裸题
刚准备打,突然发现:
\(1 \leq S_i \leq T_i \leq 10^8\)
这™用个鬼的线段树啊
经过一番寻找,在网上找到了一篇论文
大家可以去看一下,这里只提示大家用类似匈牙利算法贪心
这里还有代码
代码
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define RG register
#define clear(x, y) memset(x, y, sizeof(x))
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while(ch != '-' && (!isdigit(ch))) ch = getchar();
if(ch == '-') w = -1, ch = getchar();
while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
const int maxn(5010);
struct node { int l, r, val; } p[maxn];
int n, val[maxn], match[maxn];
long long ans;
inline bool cmp_1(const node &a, const node &b) { return a.l < b.l; }
inline bool cmp_2(const node &a, const node &b) { return a.val > b.val; }
int hungary(int x, int y)
{
if(val[y] > p[x].r) return 0;
if(!match[y]) return (match[y] = x, 1);
if(p[match[y]].r < p[x].r) return hungary(x, y + 1);
else if(hungary(match[y], y + 1)) return (match[y] = x, 1);
return 0;
}
int main()
{
n = read();
for(RG int i = 1; i <= n; i++) p[i] = (node) {read(), read(), read()};
std::sort(p + 1, p + n + 1, cmp_1);
for(RG int i = 1; i <= n; i++) val[i] = std::max(val[i - 1] + 1, p[i].l);
for(RG int i = 1, j = 1; i <= n; i++)
{
while(j < n && val[j] < p[i].l) ++j;
p[i].l = j;
}
std::sort(p + 1, p + n + 1, cmp_2);
for(RG int i = 1; i <= n; i++)
if(hungary(i, p[i].l)) ans += p[i].val;
printf("%lld\n", ans);
return 0;
}