PAT (Basic Level) Practice (中文)- 1030 完美数列(25 分)

简介: PAT (Basic Level) Practice (中文)- 1030 完美数列(25 分)

题目链接:点击打开链接


题目大意:略。


解题思路:先存储排序后,用最小的 a[0] 来从尾到头逐个比较,直到可以大于等于 a[i],记录长度 cnt 并 break。然后从第二个小的数 a[1] 去做重复的事情,为了优化比较次数,直接从 a[i+cnt] 比较,因为从这个开始比较成功的话,才可以更新掉最终 cnt,因为在这之前的比较没必要。


AC 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
ll a[maxn];
int main()
{
    ll n,p;
    while(~scanf("%lld%lld",&n,&p))
    {
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        sort(a,a+n);
        int s,e;
        for(int i=n-1;i>0;i--)
            if(a[0]*p>=a[i]){ e=i; break; }
        int cnt=e-0+1,rs=cnt;
        for(int i=1;i<n;i++)
        {
            while(i+cnt<n)
                if(a[i]*p>=a[i+cnt]) cnt++;
                else break;
            rs=max(rs,cnt);
        }
        printf("%d\n",rs);
    }
    return 0;
}
目录
相关文章
|
10月前
|
C语言 C++
PAT (Basic Level) Practice (中文)1099 性感素数(20分)
“性感素数”是指形如 (p, p+6) 这样的一对素数。之所以叫这个名字,是因为拉丁语管“六”叫“sex”(即英语的“性感”)。(原文摘自 http://mathworld.wolfram.com/SexyPrimes.html) 现给定一个整数,请你判断其是否为一个性感素数。
85 0
|
存储 测试技术
PAT (Basic Level) Practice (中文) 1004 成绩排名 (20 分)
PAT (Basic Level) Practice (中文) 1004 成绩排名 (20 分)
63 0
PAT (Basic Level) Practice (中文) B1046 划拳 (15 分)
PAT (Basic Level) Practice (中文) B1046 划拳 (15 分)
59 0
|
C语言
PAT (Basic Level) Practice (中文) B1026 程序运行时间 (15 分)
PAT (Basic Level) Practice (中文) B1026 程序运行时间 (15 分)
95 0
|
测试技术
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
76 0
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
64 0
|
算法
PAT (Basic Level) Practice (中文)1028. 人口普查(20分)
PAT (Basic Level) Practice (中文)1028. 人口普查(20分)
76 0
|
人工智能 测试技术
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
71 0
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
|
测试技术
PAT (Basic Level) Practice (中文)1012 数字分类 (20 分)+易错测试点
PAT (Basic Level) Practice (中文)1012 数字分类 (20 分)+易错测试点
97 0
PAT (Basic Level) Practice (中文)1012 数字分类 (20 分)+易错测试点
PAT (Basic Level) Practice (中文) 1010 一元多项式求导 (25 分)
PAT (Basic Level) Practice (中文) 1010 一元多项式求导 (25 分)
72 0

热门文章

最新文章