1085. Perfect Sequence (25)
来源:互联网 发布:linux 激活网卡 编辑:程序博客网 时间:2024/06/06 03:44
1085. Perfect Sequence (25)
notice that the last case could give a great number which could make the product of a[i] and p overflow, you should take the long long type to convert this product;
#include <stdio.h>#include <stdlib.h>int comp(const void*a,const void*b){ return *((int*)a)-*((int*)b);}int main(){ int n,p,maxlen=0; int a[100000+1]; scanf("%d %d",&n,&p); for(int i=0;i<n;++i) scanf("%d",&a[i]); int i=0,j=n-1; qsort(a,n,sizeof(int),comp); while((long long)a[i]*p<a[j])//find i which could satisfy this condition:a[i]*p>=a[j] ++i; if(maxlen<j-i+1)//get maxlen; maxlen=j-i+1; while(i>0)//when i=0,it suggests that current lenth is the maxlen;else do loop { --j;//reduce the j,find a less a[j] and check if could find a longer lenth; while(i>=0&&(long long)a[i]*p>=a[j])//in this range at grater than zero and reduce i,to check if could find... --i; ++i;//a[i]*p<a[j],or i=-1,so we add 1 to i;and then in the range between i and j,satisify the condition if(maxlen<j-i+1) maxlen=j-i+1;//if i=0,will end this loop; } printf("%d",maxlen); return 0;}
0 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)
- “基于云平台的移动终端实时渲染”学习参考
- poj2592 Instantaneous Transference
- Java集合类
- eclipse + maven + cxf +spring
- JQuery的表单验证之JQuery.validate插件
- 1085. Perfect Sequence (25)
- 在Web上运行Linux—js/linux模拟器
- 块设备驱动(2)
- Java中static的使用
- UIViewController生命周期
- socket同时读写问题
- Activity 启动过程全解析
- Spark2 DataFrameStatFunctions探索性数据统计分析
- C++杂记:“error LNK1169: 找到一个或多个多重定义的符号”的解决方法