1085 Perfect Sequence (25 分)
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 are supposed to find from the sequence as many numbers as possible to form a perfect subsequence.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers N and p, where N (≤10^5) is the number of integers in the sequence, and p (≤10^9) is the parameter. In the second line there are Npositive integers, each is no greater than 109.
Output Specification:
For each test case, print in one line the maximum number of integers that can be chosen to form a perfect subsequence.
Sample Input:
10 8
2 3 20 4 5 1 6 7 8 9
Sample Output:
8
题意:从一组序列中找出最大的满足条件的序列的长度。
分析:二分法,可以用STL例upper_bound函数。注意乘积可能超过int型表示范围
/** * Copyright(c) * All rights reserved. * Author : Mered1th * Date : 2019-02-26-13.58.21 * Description : A1085 */ #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<string> #include<unordered_set> #include<map> #include<vector> #include<set> using namespace std; ; int a[maxn]; int main(){ #ifdef ONLINE_JUDGE #else freopen("1.txt", "r", stdin); #endif int n,p; scanf("%d%d",&n,&p); ;i<n;i++){ scanf("%d",&a[i]); } sort(a,a+n); ; ;i<n;i++){ ,a+n,(long long)a[i]*p)-a; num=max(num,j-i); } printf("%d\n",num); ; }
另一种方法是双指针,思路就是定义两个指针i,j,均从0开始遍历,j不断右移直到不满足条件,再i++。
/** * Copyright(c) * All rights reserved. * Author : Mered1th * Date : 2019-02-26-14.18.14 * Description : A1085 */ #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<string> #include<unordered_set> #include<map> #include<vector> #include<set> using namespace std; ; int a[maxn]; int main(){ #ifdef ONLINE_JUDGE #else freopen("1.txt", "r", stdin); #endif int n,p; scanf("%d%d",&n,&p); ;i<n;i++){ scanf("%d",&a[i]); } sort(a,a+n); ,j=,num=; while(i<n&&j<n){ while(j<n&&a[j]<=(long long)a[i]*p){ num=max(num,j-i+); j++; } i++; } cout<<num; ; }
1085 Perfect Sequence (25 分)的更多相关文章
- 1085 Perfect Sequence (25 分)
Given a sequence of positive integers and another positive integer p. The sequence is said to be a p ...
- PAT Advanced 1085 Perfect Sequence (25) [⼆分,two pointers]
题目 Given a sequence of positive integers and another positive integer p. The sequence is said to be ...
- 【PAT甲级】1085 Perfect Sequence (25 分)
题意: 输入两个正整数N和P(N<=1e5,P<=1e9),接着输入N个正整数.输出一组数的最大个数使得其中最大的数不超过最小的数P倍. trick: 测试点5会爆int,因为P太大了.. ...
- 1085. Perfect Sequence (25) -二分查找
题目如下: Given a sequence of positive integers and another positive integer p. The sequence is said to ...
- 1085. Perfect Sequence (25)
the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1085 At first ...
- PAT (Advanced Level) 1085. Perfect Sequence (25)
可以用双指针(尺取法),也可以枚举起点,二分终点. #include<cstdio> #include<cstring> #include<cmath> #incl ...
- 1085. Perfect Sequence (25)-水题
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- pat1085. Perfect Sequence (25)
1085. Perfect Sequence (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng Give ...
- PAT 1085 Perfect Sequence[难]
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
随机推荐
- 给tabBarItem加点击效果动画
获取到tabBarItem,添加喜欢的动画 .h文件 @interface JGTabBarController () //记录上一次点击tabbar @property (nonatomic, as ...
- spring入门程序编写
1.导入jar包
- Robolectric测试框架使用笔记
1. 概述 Robolectric(http://robolectric.org/)是一款支持在桌面JVM模拟Android环境的测试框架,通过shadow包下的类来截取view.activity等类 ...
- 利用security.js实现RSA加密
在通常的http协议的网站中直接提交数据可以通过信息抓取从而暴露提交者所提交的信息,特别是注册时的密码和登录时的密码容易被泄露. 那么怎么防止这种现象呢? 很多人会想到加密技术,对没错,本文所讲的就是 ...
- UI基础:UILabel.UIFont 分类: iOS学习-UI 2015-07-01 19:38 107人阅读 评论(0) 收藏
UILabel:标签 继承自UIView ,在UIView基础上扩充了显示文本的功能.(文本框) UILabel的使用步骤 1.创建控件 UILabel *aLabel=[[UILabel alloc ...
- grep命令与正则表达式
搜寻特定字符串the 常用参数:-a 将binary档案以test档案的方式搜寻数据 -c 计算找到'搜寻字符串'的次数 -i 忽略大小写的不同 -n 顺便输出行号 -v 反向选 ...
- Vue 2.0 学习路线
「 Vue很难学吗 」 对于我这种从0.x版本就开始体验 vuejs 的人来说,当然不算难,那时候没各种脚手架和复杂搭配,仅仅是一个mvvm的解决方案库而已,解决了jq带来的繁琐操作dom痛点,所以就 ...
- PyTorch 数据集类 和 数据加载类 的一些尝试
最近在学习PyTorch, 但是对里面的数据类和数据加载类比较迷糊,可能是封装的太好大部分情况下是不需要有什么自己的操作的,不过偶然遇到一些自己导入的数据时就会遇到一些问题,因此自己对此做了一些小实 ...
- Web Components 是什么
/********************************************************************************* * Web Components ...
- 查找单链表的倒数第k个值
刚开始,我想到的是一种笨方法,先遍历单链表,计算出单链表的长度len,然后再从头遍历单链表到第len-k个节点,那么 这个节点既是单链表的倒数第k个节点. 不过这种算法时间复杂度挺高的,还有一种更简单 ...