B.Basketball Together
双指针。
求最多能赢几场,那就从实力大的开始找,如果一个不够就从前面拉壮丁。
#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <limits.h>
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef unsigned long long ULL;
using namespace std;
const int N = 1e5 + 10;
int q[N];
int main()
{
FAST;
int ans = 0;
int n,d;
cin >> n >> d;
for(int i = 0; i < n; i ++)
{
cin >> q[i];
}
//先排序
sort(q,q + n);
//双指针,i找小的,j找大的
int i = 0, j = n - 1;
//限制条件是j >= i。
while(j >= i)
{
//拉了几个壮丁
int s = 1;
//找人,大的不够就从头开始拉壮丁。
//每一轮都会自动 i ++;如果i和j一样了还没大于d,那这轮就要加一次i,最后i会比j大,不符合结果
while(s * q[j] <= d && i <= j)
{
i ++, s++;
}
//如果恰好是i == j - 1 时找到,在循环中会加1变成i == j。
//只有i <= j 才符合条件
if(s * q[j] > d && i <= j) ans ++;
j --;
}
cout << ans;
}