A1085 Perfect Sequence (25 point(s))

求M≤m×p最大长度M-m

1. 原文

原题

Given a sequence of positive integers and another positive integer p. The sequence is said to be a perfect sequence if Mm×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:

1
2
10 8
2 3 20 4 5 1 6 7 8 9

Sample output:

1
8

2. 解析

求M≤m×p M-m最大

数组排序,对于每个当前i 查找满足num[i]*p>=num[j]的最大j,j不回溯。记录最大j-i

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<cstdio>
#include<algorithm>
using namespace std;
long long num[100010]={};
int main()
{
int n;
long long p;
scanf("%d%lld",&n,&p);
for (int i = 0; i < n; ++i)
{
scanf("%lld",&num[i]);
}
sort(num,num+n);
int i=0;
int j=0;
int ans=-1;
for (; i < n; ++i)
{
for (; j < n; ++j)
{
if (num[j]>num[i]*p)
{
break;
}
}
if (j-i>ans)
{
ans=j-i;
}
}
printf("%d\n", ans);
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1085/
  • 发布时间: 2019-09-03 12:31
  • 更新时间: 2021-10-29 14:04
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!