跳到正文

题解

题解:AT_abc431_c [ABC431C] Robot Factory

Night_Echo约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前

思路

本题用到了贪心与双指针的思想。

先将 HiH_i 和 BiB_i 进行排序,然后运用双指针进行头与身体的配对:

  • 如果当前头部重量≤当前身体重量\text{如果当前头部重量} \le \text{当前身体重量},则配对成功,两个指针都后移;
  • 如果头部重量>身体重量\text{如果头部重量} > \text{身体重量},则只移动身体指针。

最后判断:如果 成功配对数≥K\text{成功配对数} \ge K,输出 Yes,否则输出 No。

代码

C++
#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;
}
原文