思路:
按照length排序,如果length一样,按照weight排序,然后暴力枚举。
坑点:题目中的right after 代表着每个树枝最多使之后的一个树枝的花费变成0;
了解了这一点就没有什么难的了。。
AC代码:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <string.h>
#define pi acos(-1.0)
using namespace std;
struct pai{
int len,wei;
}p[5020];
int cnt;
bool vis[5020];
bool cmp(const pai &a,const pai &b){
if(a.len == b.len){
return a.wei < b.wei;
}
return a.len < b.len;
}
int main()
{
int t;
cin>>t;
int n;
while(t--){
cnt = 0;
memset(vis,false,sizeof(vis));
scanf("%d",&n);
for(int i = 1;i <= n;i++){
scanf("%d%d",&p[i].len,&p[i].wei);
}
sort(p+1,p+1+n,cmp);
int we;
for(int i = 1;i <= n;i++){
if(vis[i] == true)continue;
we = p[i].wei;
vis[i] = true;
cnt++;
for(int j = i+1;j <= n;j++){
if(vis[j] == true)continue;
if(p[j].wei >= we){
we = p[j].wei;
vis[j] = true;
}
}
}
printf("%d\n",cnt);
}
return 0;
}