PAT 甲级 A1085 (2019/02/20)

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 100010;
int n, p, a[MAXN];
int binarySearch(int i, long long x){
    if(a[n - 1] <= x) //如果最大的数比x小,则返回n
        return n;
    int left = i + 1;
    int right = n - 1;          //在区间[i+1, n-1]内查找
    int mid;
    while(left < right){
        mid = (left + right) / 2;
        if(a[mid] <= x)         //若区间中间的数小于x,则这个数在mid后面
            left = mid + 1;//左端点记为mid+1
        else                    //若区间中间的数大于x,则这个数在mid前面
            right = mid;   //右端点记为mid
    }
    return left;
}
int main(){
    scanf("%d %d", &n, &p);
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
    }
    sort(a, a + n);//不加cmp,默认从小到大
    int count = 1;
    for(int i = 0; i < n; i++){
        int  j = binarySearch(i, (long long)a[i] * p);
        count = max(count, j - i);
    }
    printf("%d", count);
    return 0;
}

原文地址:https://www.cnblogs.com/zjsaipplp/p/10425232.html

时间: 2024-11-09 04:15:50

PAT 甲级 A1085 (2019/02/20)的相关文章

PAT 甲级 A1010 (2019/02/20)

#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; LL Map[256]; // 0 ~ 9, a ~ z 与 0 ~ 35 的对应 LL Y = 1; // 定义LL型的Y LL inf = (1LL << 63) - Y; // long long 的最大值 2^63 - 1,注意括号 void init

PAT 甲级 A1037 (2019/02/20)

#include<cstdio> #include<algorithm> using namespace std; const int MAXN = 100010; //段错误,数组开辟的太小 int a[MAXN], b[MAXN]; int main(){ int n1, n2; scanf("%d", &n1); for(int i = 0; i < n1; i++){ scanf("%d", &a[i]); }

PAT 甲级 A1044 (2019/02/20)

#include<cstdio> const int MAXN = 100010; int sum[MAXN]; int n, S, nearS = 100000010; int upper_bound(int L, int R, int x) { int left = L, right = R, mid; while(left < right) { mid = (left + right) / 2; if(sum[mid] > x) { right = mid; } else {

PAT 甲级 A1067 (2019/02/20)

#include<cstdio> #include<algorithm> using namespace std; const int MAXN = 100010; int a[MAXN]; //存放各个数字当前所处的位置编号 int main(){ int n, num, ans = 0;//表示总计交换次数 scanf("%d", &n); int surplus = n - 1;//存放除0以外不在本位上的数的个数 for(int i = 0; i

PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】

Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting victims lose their entire life savings. To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone c

PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now given

PAT甲级——A1085 Perfect Sequence

Given a sequence of positive integers and another positive integer p. The sequence is said to be a perfect sequence if M≤m×p where M and m are the maximum and minimum numbers in the sequence, respectively. Now given a sequence and a parameter p, you

PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)

1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1]. The first one who bets on a unique number wins. For example

PAT 甲级 1108 Finding Average (20分)

1108 Finding Average (20分) The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [−] and is a