#include <iostream> #include <algorithm> #define MAX 5010 using namespace std; /****************************************************************************************************************** 本题是一个比较简答的贪心算法题目,关键在于贪心策略的选择: 1.先按照木条的长度(或者重量)进行升序排序,如果木条的长度(或者质量)时,按照其质量(或者长度)进行升序排序。 2.对所有木条进行排序后,依次进行选择满足题目条件的木条。 由于题目要求当且仅当 l<=l' and w<=w'时,才不会消耗时间,所以可以证明以上贪心策略是正确的。 ******************************************************************************************************************/ struct Node { int l; int w; bool vis; //变量访问记录,防止二次访问 }node[MAX]; bool cmp(Node x,Node y) { if(x.l<y.l) return true; else if(x.l==y.l) return x.w<y.w; return false; } int main() { int T,n; cin>>T; while(T--) { int num=0; cin>>n; for(int i=0;i<n;i++){ cin>>node[i].l>>node[i].w; node[i].vis=false; //初始化 vis 均为未访问变量 } sort(node,node+n,cmp); for(int i=0;i<n;i++){ if(node[i].vis!=true){ //假如未访问,访问过便跳过 num++; //第一次木棍时间为1 minute,并且改变访问标记为已访问 node[i].vis=true; int temp=node[i].w; //若 weight为升序,则更新weight,并将升序排列的 weight更新为已访问 for(int j=i+1;j<n;j++){ if(node[j].w>=temp&&node[j].vis!=true){ node[j].vis=true; temp=node[j].w; } } } } cout<<num<<endl; } return 0; }