码迷,mamicode.com
首页 > 其他好文 > 详细

PAT1085. Perfect Sequence

时间:2015-02-22 12:11:53      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

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

思路:利用two pointer的思想可以减少很多时间,这种思想很宝贵。
技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 #define MAX 100010
 6 long long data[MAX];
 7 int main(int argc, char *argv[])
 8 {
 9     int N;
10     long long p;
11     scanf("%d%lld",&N,&p);
12     for(int i=0;i<N;i++)
13     {
14         scanf("%lld",&data[i]);
15     }
16     sort(data,data+N);
17     //two point两个指针的思想
18     int count=0;
19     int j=0;
20     for(int i=0;i<N;i++) 
21     {
22         while(j<N)
23         {
24             if(data[j]<=data[i]*p)
25             {
26                 if(j-i+1>count)
27                   count=j-i+1;
28                 j++;
29             }
30             else
31                break;
32         }        
33     }
34     printf("%d\n",count);
35     return 0;
36 }
View Code

 

PAT1085. Perfect Sequence

标签:

原文地址:http://www.cnblogs.com/GoFly/p/4297360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!