首页 > 其他分享 >PAT_A 1085 Perfect Sequence

PAT_A 1085 Perfect Sequence

时间:2023-10-20 10:35:12浏览次数:31  
标签:Perfect 1085 PAT sequence int ll long ++ scanf

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

总结

二分 #two_pointers 可以用二分、two pointers两种方法。

  1. 二分有两种做法:
    • 二分答案;
    • 对排序后的每一个a[i],在a[i+1]~a[n-1]内查找第一个超过a[i]*p的数的位置j。
  2. two pointers:i=j=0,让j在符合条件的情况下不断增大,不能增大时则令i++,时间复杂度只需 $o(n)$ 。
// 二分答案
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,p, e[100000+5];
int main(){
	scanf("%lld%lld", &n, &p);
	for(int i = 0; i < n; i++) scanf("%lld", &e[i]);
	sort(e, e+n);
	int lo = 0, hi = n, mi=0;
	while(hi > lo){
		mi = (hi + lo)>>1;
		int en = 0;
		for(int i = 0; i + mi < n; i++){
			if(e[i + mi] <= e[i]*p){
				en = 1;
				break;
			}
		}
		if(en) lo = mi+1;
		else hi = mi;
	}
	printf("%d", mi+1);
	return 0;
}
//二分数组
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,p, e[100000+5];
int main(){
	scanf("%lld%lld", &n, &p);
	for(int i = 0; i < n; i++) scanf("%lld", &e[i]);
	sort(e, e+n);
	int r = 1;
	for(int i = 0; i < n; i++){
		int j = upper_bound(e+i+1, e+n, (ll)e[i]*p) - e;
		r = max(r, j-i);
	}
	printf("%d", r);
	return 0;
}
//two pointers
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,p, e[100000+5];
int main(){
	scanf("%lld%lld", &n, &p);
	for(int i = 0; i < n; i++) scanf("%lld", &e[i]);
	sort(e, e+n);
	int r = 0;
	int i = 0, j = 0;
	while(i < n && j < n){
		while(j < n && e[j] <= e[i]*p){
			r=max(r, j-i+1);
			j++;
		}
		i++;
	}
	printf("%d", r);
	return 0;
}

标签:Perfect,1085,PAT,sequence,int,ll,long,++,scanf
From: https://www.cnblogs.com/Afinis/p/17776457.html

相关文章

  • python sys.path介绍
    pythonsys.path介绍介绍当我们导入模块时,python解释器会通过sys.path中的环境变量搜索。sys.path是一个列表,里面包含已添加到环境变量中的路径。使用sys.path.append({路径})可以往里面添加自定义的环境变量。使用当我们想要导入某个文件中的文件失败时,可以将其文件夹路......
  • Java资源文件获取方法详解:从 Classpath 到 Web 应用程序
    在Java开发中,访问和读取资源文件是一个常见的需求。这些资源可以是配置文件、图像、音频、视频、文本文件等。在Java中,获取资源文件有多种方式,包括直接通过类路径(Classpath)访问,或者通过Web应用程序的上下文路径(ContextPath)访问。以下我们将详细探讨这些方法。通过类路径(Classpath)......
  • 华为路由器配置NAT,PAT
    NAT概述NAT(NetworkAddressTranslation)又称为网络地址转换,用于实现私有网络和公有网络之间的互访。私有网络地址和公有网络地址私有网络地址(以下简称私网地址)是指内部网络或主机的IP地址,公有网络地址(以下简称公网地址)是指在互联网上全球唯一的IP地址。IANA(InternetAssigne......
  • Git-error: invalid path
    gitcheckoutisp原因是Win和Linux文件格式不一致,要么切换到Linux环境下操作,要么gitconfigcore.protectNTFSfalse查了下官方手册,官方原话:Ifsettotrue,donotallowcheckoutofpathsthatwouldcauseproblemswiththeNTFSfilesystem大概意思是说NTFS有个路径保......
  • PAT_A 1038 Recover the Smallest Number
    Givenacollectionofnumbersegments,youaresupposedtorecoverthesmallestnumberfromthem.Forexample,given{32,321,3214,0229,87},wecanrecovermanynumberssuchlike32-321-3214-0229-87or0229-32-87-321-3214withrespecttodifferentor......
  • [ABC207F] Tree Patrolling 题解
    [ABC207F]TreePatrolling弱智DP题,设\(f(i,j,0/1/2)\)表示在点\(i\),子树中有\(j\)个点被覆盖,且\(i\)点自身状态是未被覆盖/被自身覆盖/被某个儿子覆盖,然后树上背包更新就行了。代码:#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintmo......
  • JsonPath使用(Java)
    JsonPath使用(Java)Java有一些类似于jq的语法库和工具。其中一个叫做JsonPath,它允许使用类似于jq的语法来查询和操作JSON数据。可以使用JsonPath来提取特定的JSON字段、过滤数据、执行计算等操作。另外,还有一些其他的Java库和框架也提供了类似的功能,比如FastJson,Gson和Jackson。这......
  • SpringCloudGateway网关整合swagger3+Knife4j3,basePath丢失请求404问题
    很多人都是照着别人的文章粘代码,我也是粘的,但是这样粘也会有问题,我搞这个Knife4j3的时候遇到两个问题,这里记录一下:第一个是basePath丢失,第二个解决basePath丢失完又引发了会引起application/json数据类型参数示例的问题。在集成SpringCloudGateway网关的时候,会出现没有basePat......
  • PAT_A1067 Sort with Swap(0, i)
    Givenanypermutationofthenumbers{0,1,2,..., N−1},itiseasytosorttheminincreasingorder.Butwhatif Swap(0,*) istheONLYoperationthatisallowedtouse?Forexample,tosort{4,0,2,1,3}wemayapplytheswapoperationsinthefollowi......
  • 【git】git pull更新项目报错git error:invalid path
    1、报错内容error:invalidpath'xxxxxxx'原因是某分支下的文件名格式不支持,最终导致在gitclone的时候找不到这个文件路径导致的! 2、解决方法gitconfigcore.protectNTFSfalse作用是关掉NTFS下的路径保护机制,防止文件系统出错,这样就不存在找不到文件路径了......