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太大了.. ...
随机推荐
- LODOP打印控件之LODOP.NewPageA()方法
通过lodop的强制分页方法,当我们一次性需要打印多张单页面的时候,可以通过该方法实现一次性打出,而不是重复调用打印方法,容易出问题 LODOP.NewPageA(); 说明:强制分页,注意是.New ...
- vijos p1729 Knights
描述 在一个N*N的正方形棋盘上,放置了一些骑士.我们将棋盘的行用1开始的N个自然数标记,将列用'A'开始的N个大写英文字母标记.举个例子来说,一个标准的8*8的国际象棋棋盘的行标记为1..8,列标记 ...
- Java数据库编程技术
1. 建立数据库连接 例1.1 使用JDBC-ODBC桥来连接一个Access数据库. 该数据库的名称为FirstExample,在ODBC数据源中的名称为forStudy,用户名和密码均为空. pa ...
- 性能二 fortnite unreal opt
https://replay.unrealsummit.co.kr/data2018/usm2018_42.pdf?ckattempt=1 https://www.unrealengine.com/e ...
- python3 UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f9e0' in position 230: illegal multibyte sequence
最近在保存微博数据到(csv文件)时报错: UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f9e0' in positio ...
- 【cocos2dx开发技巧8】自定义控件-使自定义控件具有RGBA特性
转发,请保持地址:http://blog.csdn.net/stalendp/article/details/9948545 虽然CCNodeRGBA,CCLayerRGBA,sprite等提供颜色和 ...
- C# ftp ListFilesOnServer
public static bool ListFilesOnServer(Uri serverUri) { // The serverUri should start with the ftp:// ...
- 在不重装系统的情况下撤底删除oracle数据库及oralce的相关软件
先从控制面板删除oracle的相关应用及数据库, 删除系统变量 ORACLE_OEM_CLASSPATH=%JAVA_HOME%\lib\ext\access-bridge-64.jar;%JAVA_ ...
- [Javascript] Understand Curry
The act of currying can be described as taking a multivariate function and turning it into a series ...
- svn自助改动password(PHP脚本实现)
#创建脚本文件夹 mkdir -p /var/www/svn/svntools #创建apache配置文件 touch /etc/httpd/conf.d/alias.conf #输入下面内容: Al ...