我的思路是让最大的数除p的minp,看看小于minp的有多少,记下来;让最小的乘p得maxp,看看大于maxp的有多少。最后比较取最小的数,用总数减去即可得最多的数。
代码如下
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(long a, long b) {
return a > b;
};
int main()
{
vector<long> arr;
long rec = 0L, p = 0L;
int N = 0, cnt_front = 0, cnt_back = 0;
cin >> N >> p;
for(long i = 0L; i < N; i++)
{
cin >> rec;
arr.push_back(rec);
}
sort(arr.begin(), arr.begin() + N, cmp);
int minp = arr[0] / p;
int maxp = arr[N - 1] * p;
int k = N - 1;
int j = 0;
if(arr[0] % p == 0)
--minp;
while(minp >= arr[k--])
++cnt_back;
while(maxp < arr[j++])
++cnt_front;
int min = (cnt_front > cnt_back) ? cnt_back : cnt_front;
cout << N - min;
return 0;
}