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
知识点:
剪枝,遍历的时候,如果 list[i]*p < list[j] 的话,就剪枝
另外,比当前完美序列长度小的就不用遍历了,j 直接从 i+cnt 开始
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; int main(){
vector<int> list;
long long p,tmp;
int n; scanf("%d %lld",&n,&p); for(int i=;i<n;i++){
scanf("%lld",&tmp);
list.push_back(tmp);
}
sort(list.begin(),list.end()); int cnt=; for(int i=;i<n;i++){
for(int j=i+cnt;j<n;j++){
if(list[i]*p>=list[j]){
if(j-i>cnt)
cnt=j-i;
}else{
break;
}
}
}
printf("%d",cnt+);
}
1085. Perfect Sequence的更多相关文章
- 1085 Perfect Sequence (25 分)
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
- PAT 1085 Perfect Sequence
PAT 1085 Perfect Sequence 题目: Given a sequence of positive integers and another positive integer p. ...
- PAT 1085 Perfect Sequence[难]
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
- 1085. Perfect Sequence (25) -二分查找
题目如下: Given a sequence of positive integers and another positive integer p. The sequence is said to ...
- PAT 甲级 1085 Perfect Sequence
https://pintia.cn/problem-sets/994805342720868352/problems/994805381845336064 Given a sequence of po ...
- 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 ...
- 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 ...
随机推荐
- go语言中的数组
package main; import ( "fmt" ) func main() { //声明一个数组 var a [3]int; a = [3]int{1, 2, 3}; / ...
- const修饰符用法
1. 将一个对象设置为不可修改 const int a = 100; 2. 指向const对象的指针 const int* p = 3;可以通过指针来修改指针所指向的值,但是不能通过指针*p修改对像的 ...
- Python.SQLAlchemy.0
1. SQLAlchemy and You http://lucumr.pocoo.org/2011/7/19/sqlachemy-and-you/ 2. Overview http://docs.s ...
- 探索未知种族之osg类生物---器官初始化四
上一节我们对完成了对osg生物内部非常重要器官graphicsContext的初始化工作.这样就可保证我们场景中至少有一个graphicContext存在,不至于刚出生就面临夭折.我们根据上一节中os ...
- javascript 高级程序设计 七
引言:好几天没有写随笔了,项目有点紧,恰好今天项目遇到了比较大阻塞,就只好来写一篇随笔来压压惊. 1.Date类型 创建一个新的Date:(除了new Date()外) var someDate = ...
- Ubuntu12.04软件安装指南
更新升级源 首先编辑软件源,在终端输入下面命令: sudo gedit /etc/apt/sources.list 较快速的升级源有163,台湾源,科大源,搜狐源等,大家将新的升级源全部覆盖原文件so ...
- MySQL学习笔记-事务相关话题
事务机制 事务(Transaction)是数据库区别于文件系统的重要特性之一.事务会把数据库从一种一致状态转换为另一个种一致状态.在数据库提交工作时,可以确保其要么所有修改都已经保存了,要么所有修改都 ...
- swift NSdata 转换 nsstring
result = NSString(data: data, encoding: NSUTF8StringEncoding) 做HTTP 请求时 遇到 打印结果看 所以~~~
- SQL Server 查询中文字段返回为空
昨晚维护系统数据时,遇到个奇怪现象.明明数据库里有数据,查询结果就是返回为空.具体情况是这样的,查询工作日志表里关于工作描述的情况,以中文内容匹配工作描述字段,其中匹配内容里包含有空格. 尝试去掉第一 ...
- window下文件在Linux下文件乱码解决
在使用iconv转换文件的字符编码时,如果遇到类似“iconv: illegal input sequence at position”的错误,原因是需要转换的字符编码没有涵盖文件中的字符,比如,将一 ...