PAT (Advanced Level) 1085. Perfect Sequence (25) 贪心算法
来源:互联网 发布:淘宝客cnzz建站工具 编辑:程序博客网 时间:2024/06/05 09:58
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 82 3 20 4 5 1 6 7 8 9Sample Output:
8
排序后使用贪心算法。
/*2015.7.30cyq*/#include <iostream>#include <vector>#include <algorithm>#include <fstream>using namespace std;//ifstream fin("case1.txt");//#define cin finint main(){long long N,p;cin>>N>>p;vector<long long> ivec(N);for(int i=0;i<N;i++)cin>>ivec[i];sort(ivec.begin(),ivec.end());int low=0;int high=0;int len=0;while(1){while(high<N&&ivec[high]<=ivec[low]*p)high++;if(high-low>len)len=high-low;if(high==N)break;while(low<high&&ivec[low]*p<ivec[high])low++;}cout<<len;return 0;}
- PAT (Advanced Level) 1085. Perfect Sequence (25) 贪心算法
- 【PAT】【Advanced Level】1085. Perfect Sequence (25)
- PAT Advanced Level 1085. Perfect Sequence (25)(Java and C++)
- PAT (Advanced Level) Practise 1085 Perfect Sequence (25)
- PAT (Advanced Level) Practise 1085-Perfect Sequence (25)
- Pat(Advanced Level)Practice--1085(Perfect Sequence)
- 1085. Perfect Sequence (25)【二分查找】——PAT (Advanced Level) Practise
- 【PAT】【Advanced Level】1051. Pop Sequence (25)
- pat 1085. Perfect Sequence (25)
- PAT 1085. Perfect Sequence (25)
- pat 1085. Perfect Sequence (25)
- PAT 1085. Perfect Sequence (25)
- 【PAT】1085. Perfect Sequence (25)
- PAT 1085. Perfect Sequence (25)
- pat 1085. Perfect Sequence (25)
- PAT 1085. Perfect Sequence (25)
- 【PAT】 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25) PAT
- 关于@property的一点理解
- Java compiler level does not match解决方法
- html以及css中注意的属性
- VIM配置文件
- 【C语言探索之旅】 第二部分第十课:练习题和习作
- PAT (Advanced Level) 1085. Perfect Sequence (25) 贪心算法
- 数组及排序
- 安装ubuntu后
- iOS开发UI篇—UITableview控件基本使用
- SVD奇异值分解
- 通用的事件侦听器函数实现(JS)
- 限制文本框输入只能为字母`数字`.`/号的实现
- pcap文件格式分析
- 秘Java虚拟机——内存管理与垃圾回收