思路
本题用到了贪心与双指针的思想。
先将 和 进行排序,然后运用双指针进行头与身体的配对:
- ,则配对成功,两个指针都后移;
- ,则只移动身体指针。
最后判断:如果 ,输出 Yes,否则输出 No。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN=2e5+10;
int n,m,k,h[MAXN],b[MAXN],ans;
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
cin>>h[i];
}
for(int i=1;i<=m;i++){
cin>>b[i];
}
sort(h+1,h+n+1);//排序
sort(b+1,b+m+1);
int i=1,j=1;//h,b的下标从1开始,所以i,j初始值为1,i为头指针,j为身体指针
while(i<=n && j<=m){//双指针
if(h[i]<=b[j]){
ans++;//答案数+1
i++;
j++;
}
else{
j++;
}
}
if(ans>=k){
cout<<"Yes";
}
else{
cout<<"No";
}
return 0;
}