PAT-A 1085. Perfect Sequence
来源:互联网 发布:程序员推荐书单 编辑:程序博客网 时间:2024/05/16 08:29
1085. Perfect Sequence
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
程序代码:
#include<stdio.h>#include<stdlib.h>#define MAX 100000long long num[MAX];void sort(long long n[],int len);void quicksort(long long r[],int start,int end);int comp(const void* a,const void* b){ if(*(long long*)a<*(long long*)b) return -1; if(*(long long*)a==*(long long*)b) return 0; if(*(long long*)a>*(long long*)b) return 1;}int main(){ int N,i,j; long long p; long long sum ; long long m; int max = 0; int count=0; scanf("%d%lld",&N,&p); for(i=0;i<N;i++) scanf("%lld",&num[i]); qsort(num,N,sizeof(long long),comp); for(i=0;i<N;i++) { sum = num[i]*p; for(j=i+count;j<N;j++) { if(num[j]<=sum) { count =j-i+1; } else break; } if(count > max) max = count; } printf("%d",max); return 0;}void sort(long long n[],int len){ int i,j; long long tmp; for(i=len-1;i>0;i--) for(j=0;j<i;j++) { if(n[j]>n[j+1]) { tmp = n[j]; n[j]=n[j+1]; n[j+1]=tmp; } } }void quicksort(long long r[],int start,int end){ int i=start; int j=end; long long temp=r[start]; if(i<j) { while(i!=j) { while(i<j&&r[j]>=temp) j--; if(i<j) { r[i]=r[j]; i++; } while(i<j&&r[i]<temp) i++; if(i<j) { r[j]=r[i]; j--; } } r[i]=temp; quicksort(r,start,i-1); quicksort(r,i+1,end); } }
- PAT-A 1085. Perfect Sequence
- PAT A 1085. Perfect Sequence (25)
- PAT-A 1085. Perfect Sequence (25)
- PAT-A-1085. Perfect Sequence (25)
- Pat(A) 1085. Perfect Sequence (25)
- PAT 1085. Perfect Sequence
- 【PAT】1085. Perfect Sequence
- 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
- PAT 1085. Perfect Sequence (25)(二分查找)
- 4-1 单链表逆转 (20分)
- 2016 Multi-University Training Contest 5
- mybatis简单查询
- 关于Activity生命周期的一些小总结
- 杭电ACM2005 年月日的输入
- PAT-A 1085. Perfect Sequence
- oracle数据库批量解锁表
- Python——BeautifulSoup
- wei 老师的 文件复制
- 数组
- la4287(有向图的强连通分量和DAG)
- SQL语句返回多表联合查询的结果集的数据条数
- nyoj 599 奋斗的蜗牛
- HDU 3938 Portal