当前位置:网站首页>B1030 完美数列
B1030 完美数列
2022-07-27 05:01:00 【叶辰 .】
1030 完美数列 (25 分)
给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列。
现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列。
输入格式:
输入第一行给出两个正整数 N 和 p,其中 N(≤10的5次方)是输入的正整数的个数,p(≤10的9次方)是给定的参数。第二行给出 N 个正整数,每个数不超过 10的9次方。
输出格式:
在一行中输出最多可以选择多少个数可以用它们组成一个完美数列。
输入样例
10 8
2 3 20 4 5 1 6 7 8 9
输出样例
8
题目分析:
- 在存入数组数据之后,及时使用sort排序,方便以后的操作
- 测试点4超时问题:可以先存入一个临时变量j,j是当前数列中的个数最大值。做完while循环之后及时更新count的值
- 注意mp的数值范围
代码如下:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,p,count=0;
cin>>n>>p;
vector<int> a;
for(int i=0;i<n;i++){
int x;
cin>>x;
a.push_back(x);
}
sort(a.begin(),a.end());
int j=0;//优化代码所耗费的时间------测试点4超时的关键
for(int i=0;i<n-count;i++){
long long int mp=(long long)a[i]*p;//注意取的mp的值int存不下-------测试点5
while(j<n&&a[j]<=mp){
j++;
}
count=max(count,j-i);
}
cout<<count;
return 0;
}
边栏推荐
- Deep Qt5 signal slot new syntax
- Could not autowire. No beans of ‘userMapper‘ type found.
- Raspberry pie output PWM wave to drive the steering gear
- JVM上篇:内存与垃圾回收篇十--运行时数据区-直接内存
- feign调用丢失请求头问题解决及原理分析
- 【牛客讨论区】第七章:构建安全高效的企业服务
- OFDM 十六讲 2- OFDM and the DFT
- kali系统arp介绍(断网嗅探密码抓包)
- Card drawing program simulation
- 深入 Qt5 信号槽新语法
猜你喜欢

JVM上篇:内存与垃圾回收篇七--运行时数据区-堆

Detailed description of polymorphism

Network protocol details: IP

MQ message queue is used to design the high concurrency of the order placing process, the generation scenarios and solutions of message squeeze, message loss and message repetition

JVM Part 1: memory and garbage collection part 14 -- garbage collector
![[search] - multi source BFS + minimum step model](/img/42/11b5b89153ab48d837707988752268.png)
[search] - multi source BFS + minimum step model

Another skill is to earn 30000 yuan a month+

MySQL download and installation & perfect uninstall

精选用户故事|洞态在聚水潭的误报率几乎为0,如何做到?

OFDM 十六讲 2- OFDM and the DFT
随机推荐
When using Photoshop, the prompt "script error -50 general Photoshop error appears“
再一个技巧,每月稳赚3万+
Hiding skills of Photoshop clipping tool
来自“飞人”乔丹的启示!奥尼尔开启的另一个“赛场”
使用Druid连接池创建DataSource(数据源)
精选用户故事|洞态在聚水潭的误报率几乎为0,如何做到?
集成开发环境Pycharm的安装及模板设置
Constraints of MySQL table
Event filter
Install pyGame
使用ngrok做内网穿透
一、MySQL基础
2、 MySQL advanced
OFDM 16 lecture 2-ofdm and the DFT
Final Cut Pro Chinese tutorial (1) basic understanding of Final Cut Pro
A math problem cost the chip giant $500million
树莓派输出PWM波驱动舵机
C中文件I/O的使用
Solution and principle analysis of feign call missing request header
Mysql表的约束