A1085. 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
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
long long num[];
bool cmp(long long a, long long b){
return a < b;
}
int binSearch(long long num[], int low, int high, long long m, long long p){
long long ans = p * m;
int mid = high;
while(low < high){
mid = (low + high) / ;
if(num[mid] > ans){
high = mid;
}else low = mid + ;
}
return low;
}
int main(){
int N, p, index, ans = -;
scanf("%d%d", &N, &p);
for(int i = ; i < N; i++)
scanf("%lld", &num[i]);
sort(num, num + N, cmp);
for(int i = ; i < N; i++){
index = binSearch(num, i + , N, num[i], p);
index--;
if(index - i + > ans){
ans = index - i + ;
}
}
printf("%d", ans);
cin >> N;
return ;
}
总结:
1、题意:给出一个数列和一个数字p,求满足M <= mp 的最长子数列, M与m分别为最大最小值。先对原数列排序一下,对比条件,显然M越大、m越小可以得到的子数列长度越长。因此可以从排好序的数列的两端往中间搜索答案,但是如果暴力二重循环会出现超时。因此,只能从左端递增遍历m, 而右端的M改用二分搜索,将复杂度变为 n*logn。
2、二分搜索M,目标是找到序列中尽可能靠右的且满足 M <= mp 的M, 即最后一个满足所给条件的M。这个可以转化为搜索第一个满足 M > mp 的num[i]。 则num[i - 1]即为最后一个满足M <= mp的M。
3、本题还可以使用两点法做,即使用两个指针,一遍扫描。如果a[j] <= a[i]* p成立,那么在[i, j]之内的任意k,都有a[k] <= a[i] *p成立,基于这一点设置i、j两个指针,j先递增,发现不等式不成立之后,i再向前走一步,如此反复。双指针法不仅可以一头一尾,也可以在同方向使用。代码如下:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
long long num[];
bool cmp(long long a, long long b){
return a < b;
} int main(){
int N, p, index, ans = -;
scanf("%d%d", &N, &p);
for(int i = ; i < N; i++)
scanf("%lld", &num[i]);
sort(num, num + N, cmp);
int i = , j = ;
while(i < N && j < N){
long long temp = num[j] - num[i] * p;
if(temp <= ){
ans = max(ans, j - i);
j++;
}else{
i++;
}
}
printf("%d", ans + );
cin >> N;
return ;
}
4、二分搜索:
1)搜索满足某条件的元素。
int binSearch(int num[], int low, int high, int x){
int mid;
while(low <= high){ //当搜索区间为空时结束
mid = low + (high - low) / ;
if(num[mid] == x)
return mid;
else if(num[mid] > x)
high = mid - ;
else low = mid + ;
}
return -;
}
2)搜索第一个满足某条件的元素,在这个情况下要注意,被排好序的序列一定是从左到右先不满足条件,然后满足;另外,传入的high = N时,在搜索不到满足条件的数时,会返回N(假想N位置有数字); 返回的是low而不是mid。
/*返回第一个大于等于x的元素。num[N]数组应从小到大排序。
如果传入的high = N,则当序列中所有元素都小于x时,会返回N */
int binSearch(int num[], int low, int high, int x){
int mid;
while(low < high){ //当low == high时结束搜索
mid = low + (high - low) / ;
if(num[mid] >= x)
high = mid; //mid有可能就是结果,所以区间上限不能漏掉mid
else low = mid + ;
}
return low; //返回low而不是mid
}
A1085. Perfect Sequence的更多相关文章
- PAT甲级——A1085 Perfect Sequence
Given a sequence of positive integers and another positive integer p. The sequence is said to be a p ...
- PAT_A1085#Perfect Sequence
Source: PAT A1085 Perfect Sequence (25 分) Description: Given a sequence of positive integers and ano ...
- 1085 Perfect Sequence (25 分)
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 ...
- 1085. Perfect Sequence
Given a sequence of positive integers and another positive integer p. The sequence is said to be a “ ...
- PAT 甲级 1085 Perfect Sequence
https://pintia.cn/problem-sets/994805342720868352/problems/994805381845336064 Given a sequence of po ...
- 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 ...
- pat1085. Perfect Sequence (25)
1085. Perfect Sequence (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng Give ...
随机推荐
- C#_反射机制
一:反射的定义 审查元数据并收集关于它的类型信息的能力.元数据(编译以后的最基本数据单元)就是一大堆的表,当编译程序集或者模块时,编译器会创建一个类定义表,一个字段定义表,和一个方法定义表等. Sys ...
- django通用权限控制框架
在web项目中根据不同的用户肯定会限制其不同的权限,利用以下模块可以满足日常几乎所有的权限控制 permission_hook.py # 自定义权限控制,必须返回True/false ,True表 ...
- ARM-GPIO
操作GPIO有三种方法: 调用库函数读取IO的输入电平:uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef*GPIOx,uint16_t GPIO_pin): 操作寄 ...
- Gerrit上分支操作记录(创建分支、删除分支)
Git分支对于一个项目的代码管理而言,是十分重要的!许多久用git的朋友可能已经掌握的很牢固了,但对于一些初涉git的童鞋来说,可能还不是很熟悉.在此,我将自己的一些操作经历做一梳理,希望能帮助到有用 ...
- WIFI探针技术
1.WIFI 探针定义 WIFI 探针是一种能够主动识别 Android 和 IOS 设备,感知用户行为轨迹的精准数据收集前端,基于 WIFI探测技术.移动互联网和云计算等先进技术自动识别探针附近的智 ...
- “数学口袋精灵”第二个Sprint计划(第九天)
第九天进度 任务分配: 冯美欣:欢迎界面背景音乐发现bug(一开始进入游戏可以播放音乐,进入游戏界面,再返回欢迎界面时,音乐播放不出来),仍在解决中: 吴舒婷:改进ui与音效 1.进度条.金黄色: 2 ...
- Maximal Binary Matrix CodeForces - 803A (贪心+实现)
题目链接 题意有点坑: 给你一个N*N的矩阵,让你填入K个1,使之整个矩阵关于左上到右下的对角线对称,并且这个要求这个矩阵的字典序最大. 对矩阵的字典序的定义是从每一行的第一个元素开始比较,大着为字典 ...
- HDU 2029 Palindromes _easy version
http://acm.hdu.edu.cn/showproblem.php?pid=2029 Problem Description “回文串”是一个正读和反读都一样的字符串,比如“level”或者“ ...
- What is the best Java email address validation method?
https://stackoverflow.com/questions/624581/what-is-the-best-java-email-address-validation-method htt ...
- Oracle的一般监听问题解决
1. 无监听的解决办法: Windows的情况下重启之后或者是一些异常状态时会造成服务没有正常启动起来, 解决办法: 打开服务 方法1 任务管理器-服务界面 或者是 运行-services.msc 打 ...