您的位置:首页 > 产品设计 > UI/UE

1085. Perfect Sequence (25)

2015-09-08 22:17 459 查看
题目链接: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
;
 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陈小旭
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: