题意:
1、给定N个数,和一个权重P,要求,最大值M<=mp(最小值乘以p),求着N个数中最多可以构成完美数列的数字个数
解题:
首先性质,如果a[j] <= a[i] *p,那么对[i,j]内所有数,式子成立
1、先排序,从小到大
2、令i和j为0,并设置一个计数器
3、令j在满足式子的同时,不断往前移,知道不成立,i++,接着循环这个过程,其中不断更新count
4、终止条件是,j到达末尾了
learn && wrong:
1、性质,
2、while里面这个写法不错
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100010;
int main(int argc, char** argv) {
int n,p,a[maxn];
scanf("%d%d",&n,&p);
for(int i = 0;i < n;++i){
scanf("%d",&a[i]);
}
sort(a,a+n);
int i = 0,j = 0, count = 1;
while(i < n && j < n){
//J不断右移,直到恰好不满足条件
while(j < n && a[j] <= (long long) a[i] * p){
j++;
count = max(count,j - i);
}
i++;
}
printf("%d\n",count);
return 0;
}
网友评论