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太大了.. ...
随机推荐
- C结构体
什么是结构体? 简单的来说,结构体就是个能够包含不同数据类型的一个结构,他是一种能够自己定义的数据类型,他的特点和数组主要有两点不同,首先结构体能够在一个结构中声明不同的数据类型,第二相同结构的结构体 ...
- centos6.8安装具有ngx_cache_purge模块的nginx1.10.3
CentOS-6.8 安装 Nginx1.10.3Nginx 环境准备:安装Nginx需要完成以下依赖的安装 1.gcc 安装:yum install gcc-c++ 2.PCRE pcre-deve ...
- Android开发之Navigationdrawer导航抽屉功能的实现(源码分享)
导航抽屉(navigationdrawer)是一个从屏幕左边滑入的面板,用于显示应用的主要导航项目.用户能够通过在屏幕左边缘滑入或者触摸操作栏的应用图标打开导航抽屉. 导航抽屉覆盖在内容之上,但不覆盖 ...
- Servlet监听器统计在线人数
监听器的作用是监听Web容器的有效事件,它由Servlet容器管理,利用Listener接口监听某个执行程序,并根据该程序的需求做出适应的响应. 例1 应用Servlet监听器统计在线人数. (1)创 ...
- 第六章 JVM垃圾收集器(2)
上一章记录了几种常见的垃圾收集器,见<第五章 JVM垃圾收集器(1)> 1.G1 说明: 从上图来看,G1与CMS相比,仅在最后的"筛选回收"部分不同(CMS是并发清除 ...
- 你可能不知道的5 个强大的HTML5 API 函数
HTML5提供了一些非常强大的JavaScript和HTML API,来帮助开发者构建精彩的桌面和移动应用程序.本文将介绍5个新型的API,希望对你的开发工作有所帮助. 1. 全屏API(Fulls ...
- JSON与XML
XML——这种用于表示客户端与服务器间数据交换有效负载的格式,几乎已经成了Web services的同义词.我们知道AJAX技术能够使得每一次请求更加迅捷,对于每一次请求返回的不是整个页面,也仅仅是所 ...
- android设置横屏和竖屏的方法
方法一:在AndroidManifest.xml中配置 假设不想让软件在横竖屏之间切换,最简单的办法就是在项目的AndroidManifest.xml中找到你所指定的activity中加上androi ...
- Cass环境下光标无显示
先安装CAD2004,十字光标正常显示:再安装CASS7.0,光标就不显示了.现在不清楚是CAD的问题,还是CASS的问题,多半是后者.重新配置了CASS环境也不行. 于是,打开CAD选项,显示,窗口 ...
- Yahoo团队总结的关于网站性能优化的经验(转)
英文原文:http://developer.yahoo.com/performance/rules.html 中文原文:http://www.ha97.com/2710.html 1.尽量减少HTTP ...