http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=25
//贪心 #include<iostream> #include<algorithm> using namespace std; struct Node { int len; int wei; }; bool comp(Node a,Node b) { if(a.len!=b.len) return a.len>b.len; else return a.wei>b.wei; } int main() { int n,t; cin>>t; while(t--) { Node infor[5001]; int i,j,t,hash[5001],max,minj; cin>>n; for(i=1;i<=n;i++) scanf("%d%d",&infor[i].len,&infor[i].wei); sort(infor+1,infor+n+1,comp); hash[0] = infor[1].wei; t=1; for(i=2;i<=n;i++) { max = 10001; for(j=0;j<t;j++) { if(infor[i].wei<=hash[j] && hash[j]-infor[i].wei < max) { max = hash[j]-infor[i].wei; minj = j; } } if(max == 10001) { hash[t++]=infor[i].wei; continue; } hash[minj] = infor[i].wei; } cout<<t<<endl; } return 0; }
|