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
原创粉丝点击