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
#include
#include
#include
#include
#include
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;
}


截图:
1085. Perfect Sequence (25)_第1张图片
——Apie陈小旭

你可能感兴趣的:(PAT)