1085. Perfect Sequence (25)

题目链接:http://www.patest.cn/contests/pat-a-practise/1085
题目:

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

分析:
第一个给出一个数目和一个参数,要求输出满足条件的组成满足条件的最多的数量
比如10和8中,在第二个数组中只有当20和1都去除的时候,得到最多的8个数组成“完美数列”
所以我们先对数组进行一下排序,然后按照从小到达统计出所有可能的情况,找出最大的数量值
参考了: http://blog.csdn.net/nan327347465/article/details/39104721
上面说到long long,改后就好了,另外上面还说到二分查找,我这里用了不同的方法。
证明了测试点5是用非常大的用到了long long。
测试点4应该是数据量很大的。

AC代码:
#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
int main(){
 freopen("F://Temp/input.txt", "r", stdin);
 int N, p;
 cin >> N >> p;
 long long *input = new long long[N];
 for (int i = 0; i < N; ++i){
  cin >> input[i];
 }
 sort(input, input + N);//排序
 int max = 1;
 int cur_count = 2;
 int idx_t = 1;
 int idx_s = 0;
 for (; idx_s < N - 1; ++idx_s){
  --cur_count;
  for (; idx_t < N; ++idx_t){
   if (input[idx_t] <= input[idx_s] * p)
    ++cur_count;
   else{
    break;
   }
  }
  if (cur_count > max)max = cur_count;
 }
 cout << max << endl;
 return 0;
}


截图:

——Apie陈小旭
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值