鱼C论坛

 找回密码
 立即注册
查看: 1966|回复: 0

[技术交流] PTA A_1085 Perfect Sequence

[复制链接]
发表于 2020-1-29 11:30:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 798236606 于 2020-1-29 15:32 编辑

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805381845336064

解:
1.二分法
  1. #include <cstdio>
  2. #include <algorithm>

  3. using namespace std;

  4. typedef long long int LL;

  5. int a[100010];

  6. int main()
  7. {
  8.     int n, p;
  9.         
  10.     scanf("%d %d", &n, &p);
  11.         
  12.     for (int i = 0; i < n; i++)
  13.         scanf("%d", a + i);
  14.                
  15.     sort(a, a + n);
  16.    
  17.     int maxlen = 0;        
  18.     for (int i = 0; i < n; i++)
  19.     {
  20.         LL limit = (LL)a[i] * (LL)p;
  21.         int pa = upper_bound(a + i, a + n, limit) - a - i;

  22.         if (pa > maxlen)
  23.             maxlen = pa;
  24.     }
  25.         
  26.     printf("%d", maxlen);
  27.         
  28.     return 0;
  29. }
复制代码
2.two pointers
  1. #include <cstdio>
  2. #include <algorithm>

  3. using namespace std;

  4. typedef long long int LL;

  5. int a[100010];

  6. int main(void)
  7. {
  8.         int n, p;
  9.         
  10.         scanf("%d %d", &n, &p);
  11.         
  12.         for (int i = 0; i < n; i++)
  13.                 scanf("%d", a + i);
  14.                
  15.         sort(a, a + n);

  16.         int i = 0, j = 0, maxlen = 1;
  17.         
  18.         for (i = 0; i < n; i++)
  19.         {
  20.                 LL limits = (LL)a[i] * (LL)p;
  21.                
  22.                 while (j < n && a[j] <= limits)
  23.                         j++;

  24.                 maxlen = max(j - i, maxlen);
  25.         }
  26.         
  27.         printf("%d", maxlen);
  28.         
  29.         return 0;
  30. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 20:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表