PAT 1085 Perfect Sequence
PAT 1085 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 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 (<= 105) is the number of integers in the sequence, and p (<= 109) is the parameter. In the second line there are N positive 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
地址:http://pat.zju.edu.cn/contests/pat-a-practise/1085
注意题意,说的是从数组中取任意多个数字满足完美子序列,所以对数字的要求没有顺序性。我的方法是,先对数组做一个排序,时间为O(nlogn),然后在用线性的时间找到这个序列,如果该题用O(n^2)的算法则会有一个case超时。现在来说说如何在线性时间找到这个序列:首先,用下标i表示当前找到的最大值,用下标j表示当前找到的最小值。从j=0开始,i可以从0一直遍历到第一不满足data[0]*p >= data[i],此时,说明序列0到i-1是满足完美子序列,也就是以data[0]为最小值的最大完美子序列,我们把其个数记为count;然后j加一,此时最小值变为data[1],这是data[1]*p >= data[0]*p>=data[i],也就是说原来的count个也都满足,但由于data[0]比data[1]要小,所以不满足data[1]为最小值,故count减一,然后在从当前的i开始往后继续找,找到第一个不满足data[1]*p>=data[i],此时的count为以data[1]为最小值的最大完美子序列的个数,然后j继续加一,count减一,i继续向前遍历,如此循环直到j走到底。由于i,j在遍历时都没有回头,故时间复杂度为线性的。代码:
#include <stdio.h>
#include <algorithm>
using namespace std; int main()
{
long long n,p;
long long data[];
while(scanf("%lld%lld",&n,&p) != EOF){
for(int i = ; i < n; ++i){
scanf("%lld",&data[i]);
}
sort(data,data+n);
int result = ;
int count = ;
int i = , j = ;
long long sum;
while(i < n){
sum = data[j] * p;
while(i < n && data[i] <= sum){
++count;
++i;
}
if(count > result)
result = count;
++j;
--count; }
printf("%d\n",result);
}
return ;
}
PAT 1085 Perfect Sequence的更多相关文章
- PAT 1085 Perfect Sequence[难]
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
- 1085 Perfect Sequence (25 分)
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
- PAT 甲级 1085 Perfect Sequence
https://pintia.cn/problem-sets/994805342720868352/problems/994805381845336064 Given a sequence of po ...
- 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 ...
- 1085. Perfect Sequence (25) -二分查找
题目如下: Given a sequence of positive integers and another positive integer p. The sequence is said to ...
- 1085. Perfect Sequence
Given a sequence of positive integers and another positive integer p. The sequence is said to be a “ ...
- 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 Level) 1085. Perfect Sequence (25)
可以用双指针(尺取法),也可以枚举起点,二分终点. #include<cstdio> #include<cstring> #include<cmath> #incl ...
- 【PAT甲级】1085 Perfect Sequence (25 分)
题意: 输入两个正整数N和P(N<=1e5,P<=1e9),接着输入N个正整数.输出一组数的最大个数使得其中最大的数不超过最小的数P倍. trick: 测试点5会爆int,因为P太大了.. ...
随机推荐
- P值(P-value),“差异具有显著性”和“具有显著差异”
郑冰刚提到P值,说P值的定义(着重号是笔者加的,英文是从WikiPedia摘来的): P值就是当原假设为真时,比所得到的样本观察结果更极端的结果出现的概率. The P-value is the pr ...
- 数学图形(1.2)Sin曲线
相关软件参见:数学图形可视化工具,使用自己定义语法的脚本代码生成数学图形.该软件免费开源.QQ交流群: 367752815 Sin曲线 vertices = x = *PI) to (*PI) y = ...
- Binary Tree Postorder Traversal leetcode java
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
- JavaScript操作XML (一)
JavaScript操作XML是通过XML DOM来完成的.那么什么是XML DOM呢?XML DOM 是: 用于 XML 的标准对象模型 用于 XML 的标准编程接口 中立于平台和语言 W3C 的标 ...
- 如果不用jQuery,Ajax你还能写出多少?
许久之前发过一篇关于Ajax的博客,通篇讲的都是通过jQuery编写Ajax,可能因为jQuery在这方面做的实在太好,以至于突然发现不用jQuery的话自己都模糊了Ajax的写法,这里重温一下. F ...
- EasyUI-Tooltip(提示框)学习
引子: if($("#BLUETOOTH_a")){ $("#BLUETOOTH_a").tooltip({ position: 'right', conten ...
- C++初始化列表和大括号中构造的差别
C++的对象构造函数有两种初始化的方法: 1.初始化列表 2.大括号中面赋值 这两种推荐使用另外一种.原因在于使用初始化列表仅仅须要进行一次初始化.而使用大括号内赋值的话首先须要调用默认构造函数初始化 ...
- file not found app文件
昨天svn迁移.然后又一次check out之后编译遇到这个错误. Ld Build/Products/Debug-iphonesimulator/wiseCloudCrmTests.xctest/w ...
- [置顶] ios 无限循环翻页源码例子
原创文章,转载请注明出处:http://blog.csdn.net/donny_zhang/article/details/9923053 demo功能:ios 无限循环翻页源码例子.iphone 6 ...
- [HTML5] Using the focus event to improve navigation accessibility (nextElementSibling)
For a menu item, when we tab onto it, we want this element get 'focus' event, so that the submenu wi ...