pat甲级1085
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 (≤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
第一种方法:
用max记录当前的最长序列长度,因为是要找出最大长度,因此对于每个i,只需要让j从i + max开始。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; vector<int> v; int main()
{
int N, i, j;
long p;
cin >> N >> p;
v.resize(N);
for (i = ; i < N; i++) cin >> v[i];
sort(v.begin(), v.end());
int t, max = ;
for (i = ; i < N; i++)
{
for (j = i + max; j < N && v[j] <= v[i] * p; j++);
if (j - i > max) max = j - i;
}
cout << max;
return ;
}
关于lower_bound()和upper_bound()的使用:
在从小到大的排序好的数组中:
(lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
来源:CSDN
原文:https://blog.csdn.net/qq_40160605/article/details/80150252 )
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; vector<int> v; int main()
{
int N, i, j;
long p;
cin >> N >> p;
v.resize(N);
for (i = ; i < N; i++) cin >> v[i];
sort(v.begin(), v.end());
int t, max = ;
for (i = ; i < N; i++)
{
t = upper_bound(v.begin() + i, v.end(), v[i] * p) - (v.begin() + i);
if (t > max) max = t;
}
cout << max;
return ;
}
参考:
https://www.liuchuo.net/archives/1908
pat甲级1085的更多相关文章
- PAT 甲级 1085 Perfect Sequence
https://pintia.cn/problem-sets/994805342720868352/problems/994805381845336064 Given a sequence of po ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级题分类汇编——杂项
本文为PAT甲级分类汇编系列文章. 集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- PAT甲级1119. Pre- and Post-order Traversals
PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...
- PAT甲级1114. Family Property
PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...
随机推荐
- CodeForces - 124B-Permutations(DFS)
You are given n k-digit integers. You have to rearrange the digits in the integers so that the diffe ...
- HDU-Fibonacci Again(打表找规律)
There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2). ...
- Windows 常见DOS命令
1. 查看网络连接及路由状况 netstat -a 查看开启了哪些端口,常用netstat -an netstat -n 查看端口的网 ...
- Hello World 十大秘密
#include <stdio.h> int main(int argc, char* argv[], char* env[]) { printf("Hello World\n& ...
- python3 迭代器笔记
#迭代器import syslist=[1,2,3,4]it=iter(list)while True: try: print(next(it)) except StopIteration: sys. ...
- my10_使用binlog2sql闪回DML操作
下载git clone https://github.com/danfengcao/binlog2sql.git 原理使用python连接到指定的库,读取要恢复表的表结构和对应的binlog日志,在b ...
- component: resolve => require(['../pages/home.vue'], resolve)
component: resolve => require(['../pages/home.vue'], resolve) vue 路由的懒加载 import Vue from 'vue' im ...
- jquery 操作 checkbox select
1.prop方法获取.设置checked属性 <input type="checkbox" name="checkboxMain" onclick=&qu ...
- php错误等级
E_ALL - 所有的错误和警告(不包括 E_STRICT) E_ERROR - 致命性的运行时错误 E_WARNING - 运行时警告(非致命性错误) E_PARSE - 编译时解析错误 ...
- etc
小小的注意点们 交换两个变量的值时, 如果使用异或运算符, 需要先判断两个数是否相等 if (a == b) return; a ^= b; b = a ^ b; a ^= b; 取一个数组的中间位置 ...