PTA甲级1085 Perfect Sequence (25分)
首先,先贴柳神的博客
想要刷好PTA,强烈推荐柳神的博客,和算法笔记
题目原文
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
题目大意:
① 给你N个正整数,然后在给你一个P,要你任意的整合出一个数列,要求这些数列的最小的数m,和最大的数M,满足下面的要求
② M≤m×p,要你求出,这个数列最多能有多少
思路如下
① 先排序,然后在双重循环的解法肯定是不可以的,因为这样时间复杂度会超过
② 这题可以用upper_bound这个函数来解,就是求出第一个超过的数,这样他的邻接的那个数就肯定是最大的那个数了.
代码一览
算法笔记上的
#include<iostream>
#include<cstdio>
#include<algorithm>
const int maxn = 100010;
int n, p, a[maxn];
int binarySearch(int i, long long x) {
if (a[n - 1] <= x) return n;
int l = i + 1, r = n - 1, mid;
while (l < r) {
mid = (l + r) / 2;
if (a[mid] <= x) l = mid + 1;
else r = mid;
}
return l;
}
using namespace std;
int main(void) {
scanf("%d%d", &n, &p);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n); //递增排序
int ans = 1; //最大长度,初值为1(表示有至少有一个数)
for (int i = 0; i < n; i++) {
int j = binarySearch(i, (long long)a[i] * p);
ans = max(ans, j - i);
}
printf("%d", ans); //输出结果
return 0;
}
自己改了一下子的
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(void) {
int N,Max=1,P;
scanf("%d%d", &N,&P);
vector<int> data(N);
for (int i = 0; i < N; i++) scanf("%d", &data[i]);
sort(data.begin(), data.end());
for (int i = 0; i < N / 2; i++) {
int t=upper_bound(data.begin(), data.end(), (long long)data[i] * P)-data.begin()-i;
if (t > Max) Max = t;
}
printf("%d", Max);
return 0;
}
原文地址:https://www.cnblogs.com/a-small-Trainee/p/12574942.html
- 中国大数据行业发展现状及趋势预测
- 【腾讯云的1001种玩法】十分钟轻松搞定云架构 · 负载均衡的几种类型
- 常用的Hadoop 文件查看工具
- 机器学习之——LINE及LargeVis可视化算法
- 开发人员看测试之细说JBehave
- 智能合约中存在的3种最常见的误解
- O'ReillyAI系列:将学习速率可视化来优化神经网络
- 再下一城,腾讯黑科技介入新零售
- 微信团队广发内部体验邀请,小程序将大火!
- 胖虎科技获1亿元融资 域名“我爱胖虎”创意十足!
- 高挺:区块链在金融领域的三个应用方向
- 条码支付迎来分级限额制 支付宝、微信纷纷响应
- 关键基础设施威胁预警,HDD声波攻击可致蓝屏
- 集成学习之随机森林通俗理解
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- C语言小笔记(1)
- 这一次搞懂Spring的XML解析原理
- 这一次搞懂Spring自定义标签以及注解解析原理
- 这一次搞懂Spring的Bean实例化原理
- 单片机入门学习十三 STM32单片机学习十 通用定时器
- Spring IOC原理补充(循环依赖、Bean作用域等)
- 这一次搞懂Spring代理创建及AOP链式调用过程
- 这一次搞懂Spring事务注解的解析
- 这一次搞懂Spring事务是如何传播的
- 这一次搞懂SpringMVC原理
- 这一次搞懂Spring Web零xml配置原理以及父子容器关系
- 这一次搞懂SpringBoot核心原理(自动配置、事件驱动、Condition)
- 全网最深分析SpringBoot MVC自动配置失效的原因
- Mybatis源码初探——优雅精良的骨架
- 深入Mybatis源码——配置解析