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

1085. Perfect Sequence (25)

时间:2015-12-06 12:53:37      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

自己想的比较好的一个算法,时间大大节省

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CAO, Peng

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
 
    1. #include <iostream>
    2. #include <stdio.h>
    3. #include <vector>
    4. #include <algorithm>
    5. #pragma warning(disable:4996)
    6. using namespace std;
    7. vector<long long int> num;
    8. int main(void) {
    9. int n, p, temp;
    10. cin >> n >> p;
    11. while (n--)
    12. {
    13. scanf("%d", &temp);
    14. num.push_back(temp);
    15. }
    16. sort(num.begin(), num.end());
    17. int count = 0, max = 0;
    18. n = num.size();
    19. for (int i = 0; i < n; i++) {
    20. count=0;
    21. if (n - max <= i) {
    22. break;
    23. }
    24. if (num[i + max] <= num[i] * p) {
    25. for (int j = i; j < n; j++) {
    26. if (num[j] <= num[i] * p) {
    27. count++;
    28. }
    29. else
    30. break;
    31. }
    32. }
    33. if (count > max)
    34. max = count;
    35. }
    36. cout << max;
    37. return 0;
    38. }





1085. Perfect Sequence (25)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023267.html

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