PTA乙级题 1030. 完美数列(25)
【题目链接】
先上大佬代码。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cmp(const void* a,const void* b){
return *(double*) a>*(double*) b?1:-1;
}
int main(){
int n,p;
scanf("%d %d",&n,&p);
double a[100100];
for(int i=0;i<n;i++){
scanf("%lf",&a[i]);
}
int max = 0;
int count = 0;
qsort(a,n,sizeof(a[0]),cmp);
double temp;
int i,j;
for(i=0;i<n;i++){
temp = a[i]*p;
for(j=count;j<n;j++){
if(a[j]>temp){
break;
}
if(j-i>=max)
max = j-i+1;
}
count = j;
}
printf("%d",max);
}
下面是自己写的代码,一个点运行超时,很无奈。
int main()
{
int n,i,j,temp,max=0,x;
double flag,p;
scanf("%d %lf",&n,&p);
int a[n];
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for (i=0;i<n-1;i++)
{
for (j=0;j<n-1-i;j++)
{
if (a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for (i=0;i<n;i++)
{
flag=p*a[i];
for (j=n-1;j>=0;j--)
{
if (a[j]<=flag)
goto a;
}
a:
x=j-i+1;
max=(max>x?max:x);
}
for (i=n-1;i>=0;i--)
{
flag=a[i]/p;
for (j=0;j<n;j++)
{
if (a[j]>=flag)
goto b;
}
b:
x=i-j+1;
max=(max>x?max:x);
}
printf("%d",max);
return 0;
}