//每块巧克力j的重量为w[j],当每个i孩子分得巧克力h[i]>w[j],才上场
//贪心算法
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e6;
int w[maxn],h[maxn];
int main(){
int n,m;
int k;
cin>>n;for(k=0;k<n;k++)
{scanf("%d",&w[k]);}
cin>>m;for(k=0;k<m;k++)
{scanf("%d",&h[k]);}
sort(h,h+m);sort(w+n);
int i=0; j=0; res=0;
while(i<m && j<n){
if(h[i]<=w[j]){
res++;i++;j++;
}esle{
j++;
}
}
return 0;
cout<<res<<endl;
}
//贪心算法
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e6;
int w[maxn],h[maxn];
int main(){
int n,m;
int k;
cin>>n;for(k=0;k<n;k++)
{scanf("%d",&w[k]);}
cin>>m;for(k=0;k<m;k++)
{scanf("%d",&h[k]);}
sort(h,h+m);sort(w+n);
int i=0; j=0; res=0;
while(i<m && j<n){
if(h[i]<=w[j]){
res++;i++;j++;
}esle{
j++;
}
}
return 0;
cout<<res<<endl;
}
全部评论
相关推荐
点赞 评论 收藏
分享