思路:数据很小,直接dfs暴力就能过。
按右端点从小到大排序,以便保证当前这个点的r>=上一个选的点的l的同时,也大于之前选的所有点.
path数组存储选不选当前点。
或者也可以,二进制枚举,把每个任务选不选看成一个状态,最多有(1<<20)-1种状态
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int N=110;
struct Ac
{
int l, r, val;
bool operator < (const Ac &t) const
{
return r!=t.r?r<t.r:l<t.l;
}
};
void dfs(int u);
int T;
int m;
int res;
int path[N];
Ac news[N];
signed main()
{
cin>>T;
while(T--)
{
scanf("%d", &m);
for(int i=0;i<m;i++) scanf("%d%d%d", &news[i].l, &news[i].r, &news[i].val);
sort(news, news+m);
res=0;
memset(path, 0, sizeof path);
dfs(0);
cout<<res<<endl;
}
}
void dfs(int u)
{
if(u==m)
{
int cnt=0, f=-1;//f存储上一个选的点的下标是哪个
for(int i=0;i<m;i++)
if(path[i]==1)
{
if(f==-1) cnt+=news[i].val, f=i;
else if(news[i].l>=news[f].r) cnt+=news[i].val, f=i;
}
res=max(res, cnt);
return;
}
path[u]=1;//选
dfs(u+1);
path[u]=2;//不选
dfs(u+1);
}