dp分类的题,写了用dp的代码超时了,看了网上也没有dp的代码,就用贪心吧。
“如果第i+1个木棒的重量和长度都大于等于第i个处理的木棒,那么将不会耗费时间”。
先按重量升序排好。定义一个temp常量记录当前的长度,然后遍历,如果有长度大于temp的替换掉temp并把该位的长度改成0表示该位已经不耗费时间,所以我们遍历的时候也要判断该位的长度是不是等于0,如果不等0就表示就耗费一个新的单位时间。
#include <bits/stdc++.h>
#define maxn 5002
using namespace std;
struct node{
int x,y;
}l[maxn];
bool cmp(node a,node b){
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>l[i].x>>l[i].y;
int k=0;
sort(l,l+n+1,cmp);
for(int i=1;i<=n;i++){
if(!l[i].y) continue;
k++;
int temp=l[i].y;
for(int j=i+1;j<=n;j++){
if(l[j].y>=temp){
temp=l[j].y;
l[j].y=0;
}
}
}
cout<<k<<endl;
}
}