【PAT】 1085. Perfect Sequence (25)
来源:互联网 发布:mac safari怎么截图 编辑:程序博客网 时间:2024/05/15 23:44
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 82 3 20 4 5 1 6 7 8 9Sample Output:
8
分析:(1)int*int有可能会超出int范围,所以用long long (2)使用二分查找法,否则会超时
代码如下:
#include <iostream>#include <vector>#include <algorithm>using namespace std;//vec 采用“引用”则不会超时,如果用传参则会超时。 int binaryFind(vector<long long >& vec, long long max, int i){int left = i;int right = vec.size()-1; int mid; while(left < right){mid = (left+right)/2;if(vec[mid] > max){right = mid - 1;}else if(vec[mid] < max){left = mid + 1;}else{return mid;}}return vec[left]>max? left-1:left; }int main(int argc, char** argv) {long long n, p, i, val;scanf("%lld%lld",&n,&p);vector<long long> vec(n);for(i=0; i<n; i++){scanf("%lld",&vec[i]);}sort(vec.begin(), vec.end()); long long max; int cnt=0, pos;for(i=0; i<n; i++){max = p*vec[i];pos = binaryFind(vec, max, i); if(pos-i+1 > cnt){ cnt = pos-i+1;}}printf("%d\n",cnt);return 0;}
- pat 1085. Perfect Sequence (25)
- PAT 1085. Perfect Sequence (25)
- pat 1085. Perfect Sequence (25)
- PAT 1085. Perfect Sequence (25)
- 【PAT】1085. Perfect Sequence (25)
- PAT 1085. Perfect Sequence (25)
- pat 1085. Perfect Sequence (25)
- PAT 1085. Perfect Sequence (25)
- 【PAT】 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25) PAT
- PAT 1085. Perfect Sequence
- 【PAT】1085. Perfect Sequence
- PAT Perfect Sequence (25)
- PAT A 1085. Perfect Sequence (25)
- PAT 1085. Perfect Sequence (25)(二分查找)
- PAT甲级练习1085. Perfect Sequence (25)
- PAT-A 1085. Perfect Sequence (25)
- PAT-A-1085. Perfect Sequence (25)
- 管道引起的变量赋值问题
- 一个 Xcode 7 缺少 *.dylib库的解决方法
- dvfs概述(1)
- ORACLE 多表关联 UPDATE 语句
- IO的阻塞与非阻塞、同步与异步以及Java网络IO交互方式
- 【PAT】 1085. Perfect Sequence (25)
- 什么是前端、什么是后端
- JAVA 编程
- 第一次发博文
- 有符号数 、无符号数 浅析
- android异步下载图片缓存到sdcard
- NYOJ——84 阶乘的0
- 一个非常有用的函数——COALESCE 多行合并成一行
- MTK Android flash 配置