LoKwongho

mm

1085. Perfect Sequence

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

知识点:

剪枝,遍历的时候,如果 list[i]*p < list[j] 的话,就剪枝

另外,比当前完美序列长度小的就不用遍历了,j 直接从 i+cnt 开始

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int main(){
 7     vector<int> list;
 8     long long p,tmp;
 9     int n; scanf("%d %lld",&n,&p);
10     
11     for(int i=0;i<n;i++){
12         scanf("%lld",&tmp);
13         list.push_back(tmp);
14     }
15     sort(list.begin(),list.end());
16     
17     int cnt=0;
18     
19     for(int i=0;i<n;i++){
20         for(int j=i+cnt;j<n;j++){
21             if(list[i]*p>=list[j]){
22                 if(j-i>cnt)
23                     cnt=j-i;
24             }else{
25                 break;
26             }
27         }
28     }
29     printf("%d",cnt+1);
30 }

 

posted on 2018-11-13 09:12  iojafekniewg  阅读(172)  评论(0编辑  收藏  举报

导航

My Email guangho2743##foxmail.com : )