Save More Mice 贪心

在这里插入图片描述
题意 :

  • 0位置有一个猫,n位置有老鼠洞,每次任选一个老鼠向右移动1个位置,然后猫也向右移动一个位置,如果猫所在的位置有老鼠,这些老鼠全部会被抓住,老鼠碰到洞就不会被抓,问最多有多少老鼠不被抓住

思路 :

  • 贪心,每次让最近的老鼠先进洞,所有不被抓住的老鼠到洞的距离和小于等于猫到洞的距离
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <unordered_set>
 
using namespace std;
 
typedef long long ll;
 
const int N = 4e5 + 10;
 
ll a[N];
 
int main()
{
    
    
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    int _;
    cin >> _;
 
    while (_ -- )
    {
    
    
        int n, k;
        cin >> n >> k;
        for (int i = 1; i <= k; i ++ ) cin >> a[i];
        
        sort(a + 1, a + k + 1);
        
        ll cnt = 0, sum = n - 1;
        for (int i = k; i >= 1; i -- )
        {
    
    
            if (sum >= n - a[i])
            {
    
    
                sum -= n - a[i];
                cnt ++ ;
            }
            else
                break;
        }
        
        cout << cnt << endl;
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_51448653/article/details/121254043