A1085. Perfect Sequence (25)
来源:互联网 发布:优化相机 编辑:程序博客网 时间:2024/05/16 04:38
</pre><pre name="code" class="cpp">#include<cstdio><span style="white-space:pre"></span>//两点法,为什么必须去掉下面的判断语句,当j = n-1时#include<algorithm>using namespace std;int main(){long long n,p;scanf("%lld %lld",&n,&p);long long a[n];for(int i = 0;i < n;i++)scanf("%lld",&a[i]);sort(a,a+n);int i = 0,j = 0,maxN = 1;while(i<n&& j < n){while(a[i]*p>=a[j]&&j<n) j++;//if(a[i]*p<a[j]){maxN = max(maxN,j-i);i++;//}}printf("%d\n",maxN);return 0;}
//二分法
#include<cstdio>#include<algorithm>//二分法using namespace std;int binarySearch(long long a[],int s,long long n,long long x){if(a[n-1]<=x) return n; <span style="white-space:pre"></span>//这个没有考虑到int l = s+1,r = n-1;while(l<r){int mid = (l+r)/2;if(a[mid]>x)r = mid;elsel = mid+1;}return l;}int main(){long long n,p;scanf("%lld %lld",&n,&p);long long a[n];for(int i = 0;i < n;i++)scanf("%lld",&a[i]);sort(a,a+n);int maxN = 1;//记录最大值for(int i = 0;i < n;i++){maxN = max(maxN,binarySearch(a,i,n,a[i]*p)-i);/*int l = i+1,r = n-1;while(l<r){int mid = (l+r)/2;if(a[i]*p>=a[mid]){//符合要求,可向右走if(mid-i>max)max = mid-i;l = mid+1;}else {//偏大,向左r = mid;}}*/}printf("%d\n",maxN);return 0;}
0 0
- A1085. Perfect Sequence (25)
- PAT-A1085. Perfect Sequence (25)
- pat-a1085. Perfect Sequence (25)
- PAT A1085. Perfect Sequence (25)
- PAT A1085 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)
- PAT Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- 1085. Perfect Sequence (25)
- A Bug's Life(分组并查集 奇偶表)
- array_splice()函数
- 我的金工实习心得(二):铸工第一天
- LintCode-Copy Books
- Java(8-10)
- A1085. Perfect Sequence (25)
- 事件响应的优先级、stopProgapation禁止下层组件响应
- C语言-Struct
- To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
- JVM学习笔记(三)------内存管理和垃圾回收
- 交叉排序
- Android Drawable、Bitmap、byte[]之间的转换
- 确定天数 SDUT 2520
- HDu 1513 LCS