1085. Perfect Sequence (25) -- 二分法
来源:互联网 发布:重庆知行卫生学校电话 编辑:程序博客网 时间:2024/05/22 16:39
题目地址
https://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 82 3 20 4 5 1 6 7 8 9
Sample Output:
8
ac代码(二分法思路)
#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <sstream>#include <list>#include <stack> #include <map> #include <set> #include <iterator> using namespace std;// freopen("in.txt", "r", stdin);#define INF 0x7ffffffftypedef long long int LL;LL n, p;vector<LL> v;// 找到第一个大于val的数 返回下标int bsearch(int left, int right ,LL val){ if(left > right) return -1; if(val > v[right]) return right + 1; if(val < v[left]) return -1; int tmpL = left; int tmpR = right; while(left < right) // left == right 将退出 { int mid = (right - left) / 2 + left; if(val == v[mid]) { int j = mid; while(v[j] <= val) j ++; return j; // 第一个大于 val的数的下标 }else if(val > v[mid]) { left = mid + 1; }else{ right = mid - 1; } } int j = min(left, right); int k; for(k = j ; j < tmpR;j ++) { if(v[j] > val) { break; } } return k;}int main( ){ //freopen("in.txt","r",stdin); while(scanf("%lld%lld", &n, &p) != EOF) { v.resize(n); for(int i=0;i<n;i++) { scanf("%lld", &v[i]); } sort(v.begin(), v.end()); int l = 0; int r = n - 1; if(v[r] <= v[l] * p) { printf("%d\n", n); }else{ int maxLen = 1; for(int i=0;i<n-1;i++) { LL val = v[i] * p; // int t = bsearch(i + 1, n-1 , val); if(t != -1) { if(t - i > maxLen) { maxLen = t - i; } } } printf("%d\n", maxLen); } } return 0;}
超时代码
#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <sstream>#include <list>#include <stack> #include <map> #include <set> #include <iterator> using namespace std;// freopen("in.txt", "r", stdin);#define INF 0x7ffffffftypedef long long int LL;LL n, p;vector<LL> v;int main( ){ //freopen("in.txt","r",stdin); while(scanf("%lld%lld", &n, &p) != EOF) { v.resize(n); for(int i=0;i<n;i++) { scanf("%lld", &v[i]); } sort(v.begin(), v.end()); int l = 0; int r = n - 1; if(v[r] <= v[l] * p) { printf("%lld\n", n); }else{ int maxLen = -1; for(int i=0;i<n-1;i++) { if(n - 1 - i + 1 < maxLen) break; int cnt = 1; for(int j = i+1;j < n;j++) { if(v[j] <= v[i] * p) { cnt ++; } } if(cnt > maxLen) { maxLen = cnt; } } printf("%d\n", maxLen); } } return 0;}
- 1085. Perfect Sequence (25) -- 二分法
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 搜狐刷阅读量方法,搜狐自媒体刷阅读量方法
- HQL代码笔记整理
- listview实现的tree树结构
- (二十三)Qt中的容器类简介
- oracle JDNI 配置
- 1085. Perfect Sequence (25) -- 二分法
- centos6.4下tuxedo11gR1安装及jolt连接测试
- Android小技巧—Activity管理
- 深入了解 JavaScript 中的 for 循环
- 就这样,我只用一天就开发出了一款APP
- 母函数详解及模板
- 托福写作1-opinions on food that are easier to prepare
- Java 基础(3)-----Java基本语法二
- UC刷阅读量的方法,uc阅读量可很不好刷