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 (≤10^5) is the number of integers in the sequence, and p (≤10^9) is the parameter. In the second line there are Npositive 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
题意:从一组序列中找出最大的满足条件的序列的长度。
分析:二分法,可以用STL例upper_bound函数。注意乘积可能超过int型表示范围
/**
* Copyright(c)
* All rights reserved.
* Author : Mered1th
* Date : 2019-02-26-13.58.21
* Description : A1085
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<unordered_set>
#include<map>
#include<vector>
#include<set>
using namespace std;
;
int a[maxn];
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
int n,p;
scanf("%d%d",&n,&p);
;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
;
;i<n;i++){
,a+n,(long long)a[i]*p)-a;
num=max(num,j-i);
}
printf("%d\n",num);
;
}
另一种方法是双指针,思路就是定义两个指针i,j,均从0开始遍历,j不断右移直到不满足条件,再i++。
/**
* Copyright(c)
* All rights reserved.
* Author : Mered1th
* Date : 2019-02-26-14.18.14
* Description : A1085
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<unordered_set>
#include<map>
#include<vector>
#include<set>
using namespace std;
;
int a[maxn];
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
int n,p;
scanf("%d%d",&n,&p);
;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
,j=,num=;
while(i<n&&j<n){
while(j<n&&a[j]<=(long long)a[i]*p){
num=max(num,j-i+);
j++;
}
i++;
}
cout<<num;
;
}
1085 Perfect Sequence (25 分)的更多相关文章
- 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 ...
- 【PAT甲级】1085 Perfect Sequence (25 分)
题意: 输入两个正整数N和P(N<=1e5,P<=1e9),接着输入N个正整数.输出一组数的最大个数使得其中最大的数不超过最小的数P倍. trick: 测试点5会爆int,因为P太大了.. ...
- 1085. Perfect Sequence (25) -二分查找
题目如下: Given a sequence of positive integers and another positive integer p. The sequence is said to ...
- 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 ...
- pat1085. Perfect Sequence (25)
1085. Perfect Sequence (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng Give ...
- PAT 1085 Perfect Sequence[难]
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
随机推荐
- DevExpress WPF入门指南:绑定编辑器对话框
绑定编辑器对话框 每个Smart Tag属性既可以设置也可以绑定.如下图所示,点击绑定按钮打开绑定对话框: 如果属性已经绑定,binging按钮会显示为黄色,绑定的文本会显示在相应的属性行. 绑定So ...
- node-webkit 入门
下载node-webkit 点击这里: https://github.com/rogerwang/node-webkit 找到Downloads这一小节,然后下载对应平台的node-webkit预编译 ...
- 轻松玩转AI 与PDF文件的转化(完美解决字体问题)
经过漫长而坚苦卓绝的研究查阅了网上无数资料下载了众多相关软件进行试验终于,找到搞定PDF文件的方便并且有效的办法PDF文件!你这个魔鬼!退去吧!!!! 难点一: 如何修改客户常常会提供不知道从哪里搞来 ...
- Hihocoder1883 : 生成树问题(并查集+树剖+线段树)
描述 有一个无向图,有n个点,m1条第一类边和m2条第二类边.第一类边有边权,第二类边无边权.请为第二类的每条边定义一个边权,使得第二类边可能全部出现在该无向图的最小生成树上,同时要求第二类边的边权总 ...
- BZOJ3757: 苹果树【树上莫队】
Description 神犇家门口种了一棵苹果树.苹果树作为一棵树,当然是呈树状结构,每根树枝连接两个苹果,每个苹果都可以沿着一条由树枝构成的路径连到树根,而且这样的路径只存在一条.由于这棵苹果树 ...
- Passing the Message 单调栈两次
What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten ar ...
- hdu1114 dp(完全背包)
题意:已知空钱罐质量和满钱罐质量(也就是知道钱罐里的钱的质量),知道若干种钱币每种的质量以及其价值,钱币都是无限个,问最少钱罐中有多少钱. 这个题在集训的时候学长给我们做过,所以你会做是应该的,由于已 ...
- getParameter、getParameterValues、getParameterMap用法详解
首先request中的参数parameter是一个map表,如下例 map={username=[xxx],password=[xxx],hobby=[eat,drink]} (1)当调用getPar ...
- 分部视图在ASP.NET MVC中的应用
概述: 在ASP.NET Web Form的开发经验中,对于User Control使用比较频繁,可以减少重复的代码,利于页面模块化,这个概念也被引入了ASP.NET MVC.即“分部视图”. 1.创 ...
- Aria2+yaaw+Chrome插件BaiduExporter实现百度网盘下载
Aria2+yaaw+Chrome插件BaiduExporter实现百度网盘下载 这篇博客主要为了解决百度网盘下载限速以及linux等操作系统无百度客户端的问题.主要包括两个方面第一个是aria2的安 ...