B. Team Training【800/贪心】

33 阅读1分钟

image.png codeforces.com/problemset/… 比较简单的贪心,值越大的人越少,值越小的人越大。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5*4+10;
typedef long long int LL;
LL a[N],t,n,x;
bool cmp(int a,int b)
{
    return a>b;
}
int main(void)
{
    cin>>t;
    while(t--){
        cin>>n>>x;
        for(int i=0;i<n;i++) cin>>a[i];
        sort(a,a+n,cmp);
        int ans=0;
        for(LL i=0;i<n;i++)
        {
            if(a[i]>=x) ans++;
            else
            {
                LL j=i,temp=a[i];
                while(temp*(j-i+1)<x && j+1<n) j++,temp=min(temp,a[j]);
                if(temp*(j-i+1)>=x) ans++;
                i=j;
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}

官方题解代码异常的优美

#include <iostream>
#include <algorithm>

using namespace std;

void solve() {
    int n, x;
    cin >> n >> x;
    int a[n];
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a, a + n);
    reverse(a, a + n);

    int ans = 0;
    for (int i = 0, cnt = 1; i < n; i++, cnt++) {
        cout<<i<<" "<<a[i]<<" "<<cnt<<endl;
        if (a[i] * cnt >= x) {
            ans++;
            cnt = 0;
        }
    }
    cout << ans << endl;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}