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
题目要求从给定序列中选取子序列,使得序列的最小值m、最大值M满足:M≤m*p,其中p为一个给定的正整数,输出能找到的最长子序列长度。
这道题一个很自然的思路就是设立一个头指针cur1,尾指针cur2,将序列按照升序排列,让cur2从最后一个元素向前指,cur1遍历从第一个元素到cur2的位置,找到合适的m时停下,记录长度,这样会有一个case超时,解决方法是让cur1从前到后遍历,cur2采用二分查找。
如果找到的位置使得M<m*p,说明M还可以更大,可以继续查找右半部分;如果M>m*p,说明M偏大,应该去左半部分找更小的;如果M=m*p,说明找到了合适的位置。在查找结束后,记录长度即可。
这段代码参考了Yangsongtao1991。
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std; int main()
{
int n,p;
scanf("%d%d",&n,&p);
vector<long> seq(n);
for(int i = 0; i < n; i++)
scanf("%ld",&seq[i]);
sort(seq.begin(),seq.end());
int maxcount = 0, down = 1;
for(int i = 0; i < n; i++)
{
long mp = p * seq[i];
if(mp >= seq[n-1]) // 如果最大的元素都≤m*p,则从当前位置到最后全部计数。
{
if(maxcount < n - i){
maxcount = n - i;
}
break;
}
int up = n-1;
while(up > down)
{
// 二分查找,结束条件为上界≤下界,根据mid处的乘积判定。
// 现在是确定了m,要找M,如果找到的位置<mp,说明M可能可以更大,向右找;如果>mp,说明M偏大,向左找。
// 如果当前位置恰好满足,则说明已经找到了最长满足要求的位置。
int mid = (up + down)/2;
if(seq[mid] > mp)
up = mid;
else if(seq[mid] < mp)
down = mid + 1;
else
{
down = mid + 1;
break;
}
}
if(down - i > maxcount)
maxcount = down - i;
}
printf("%d\n",maxcount);
return 0;
}
1085. Perfect Sequence (25) -二分查找的更多相关文章
- 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 be a p ...
- 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 ...
- 1085. Perfect Sequence (25)-水题
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- 【PAT甲级】1085 Perfect Sequence (25 分)
题意: 输入两个正整数N和P(N<=1e5,P<=1e9),接着输入N个正整数.输出一组数的最大个数使得其中最大的数不超过最小的数P倍. trick: 测试点5会爆int,因为P太大了.. ...
- pat1085. Perfect Sequence (25)
1085. Perfect Sequence (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng Give ...
- 1085 Perfect Sequence (25 分)
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
- PAT 1085 Perfect Sequence[难]
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
随机推荐
- SPOJ - DISUBSTR 多少个不同的子串
694. Distinct Substrings Problem code: DISUBSTR Given a string, we need to find the total number o ...
- 例10-4 uva10791(唯一分解)
题意:求最小公倍数为n的数的和的最小值. 如12:(3,4),(2,6),(1,12)最小为7 要想a1,a2,a3……an的和最小,要保证他们两两互质,只要存在不互质的两个数,就一定可以近一步优化 ...
- Elasticsearch 创建、更新、删除文档、处理冲突
----创建新文档---- 1._index,_type和_id的组合可以唯一标识一个文档,所以确保一个新文档的最简单的办法就是,使用索引请求的POST形式让elsticsearch自动生成唯一_id ...
- MySQL插件实现浅析——插件的调用
一. MySQL中的动态插件 最初想到这个问题是在学习mysql半同步复制相关问题的时候,为何在mysql运行时install半同步插件并开启后就能起到作用,他是如何让事务停下来等待的.安装插件的时候 ...
- Windows上安装scapy
1. 环境: (1) 操作系统:win7 .server2012 (2) Python版本:Python3.6-64bit (3) 依赖模块Npcap(推荐) 或WinPcap. ps:从logo ...
- java 获得当前时间前指定几个小时的时间?
//@2016-12-13 获取当前时间,指定前面多少小时的时间 //返回格式YYYYMMDDHHMMSS public static String getBeforeHourTime(int iho ...
- c++ 文件操作详解
C++ 通过以下几个类支持文件的输入输出: ofstream: 写操作(输出)的文件类 (由ostream引申而来) ifstream: 读操作(输入)的文件类(由istream引申而来) fstre ...
- ACM 数塔
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 已经告诉你了,这是个DP的题 ...
- Node.js Domain 模块
Node.js Domain(域) 简化异步代码的异常处理,可以捕捉处理try catch无法捕捉的异常.引入 Domain 模块 语法格式如下: var domain = require(" ...
- Node.js Net 模块
Node.js Net 模块提供了一些用于底层的网络通信的小工具,包含了创建服务器/客户端的方法,我们可以通过以下方式引入该模块: var net = require("net") ...