1085.PerfectSequence(25)
时间限制300ms内存限制65536kB代码长度限制16000B判题程序Standard作者CAO,PengGivenasequenceofpositiveintegersandanotherpositiveintegerp.Thesequenceissaidtobea"perfectsequence"ifM<=m*pwhereMandmarethemaximumandminimumnumbersinthesequence,respectively.Nowgivenasequenceandaparameterp,youaresupposedtofindfromthesequenceasmanynumbersaspossibletoformaperfectsubsequence.InputSpecification:Eachinputfilecontainsonetestcase.Foreachcase,thefirstlinecontainstwopositiveintegersNandp,whereN(<=105)isthenumberofintegersinthesequence,andp(<=109)istheparameter.InthesecondlinethereareNpositiveintegers,eachisnogreaterthan109.OutputSpecification:Foreachtestcase,printinonelinethemaximumnumberofintegersthatcanbechosentoformaperfectsubsequence.SampleInput:
108
23204516789
SampleOutput:
8
N个数p
接着无序的N个数
我们要先把它排好序,然后根据要求,从最小的开始,看看当前*p>=max的max能到哪里,最长多少个;输出最长的个数
下面两个代码,就一个地方不一样,一个超时了一个没有超时。AC的在二分查找那里用的是指针,而另一个超时的不是指针。简单的说,AC传过去如果改变了回来的被改变的。而超时的,改变的和原来的没有关系。本来觉得二分查找也不用改变,就用了,结果就超时了,还以为我的二分是多么的不协调。
还要注意一点,当前*p会超过int,如果全部设为int会一个测试点结果错误
评测结果
| 时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|
8月17日20:25 | 答案正确 | 25 | 1085 | C++(g++4.7.2) | 39 | 1072 | datrilla |
测试点
| 测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|
0 | 答案正确 | 1 | 252 | 15/15 |
1 | 答案正确 | 1 | 308 | 2/2 |
2 | 答案正确 | 1 | 180 | 2/2 |
3 | 答案正确 | 1 | 308 | 1/1 |
4 | 答案正确 | 39 | 1072 | 3/3 |
5 | 答案正确 | 1 | 384 | 2/2 |
#include<iostream>#include<vector>#include<algorithm>usingnamespacestd;voidreadln(vector<longint>*sequence,longintN){longintindex;for(index=0;index<N;index++)cin>>(*sequence)[index];}boolcmptobig(constlongint&A,constlongint&B){returnA<B;}intbinaryFound(vector<longint>*sequence,longintgoal,longintfrom,longintto){longintmid;while(from<=to){mid=(from+to)/2;if((*sequence)[mid]>goal)to=mid-1;elsefrom=mid+1;}returnto;}intmain(){longintindex,max=0,front;longintN,p;cin>>N>>p;vector<longint>sequence(N);readln(&sequence,N);sort(sequence.begin(),sequence.end(),cmptobig);for(index=0,front=0;N-index>max&&index<N;index++){longinttemp=p*sequence[index];if(temp<sequence[N-1]){front=binaryFound(&sequence,temp,front,N-1);/*!!注意最后一个只到N-1*/if(max<front-index+1)max=front-index+1;}elseif(max<N-index)max=N-index;}cout<<max<<endl;system("pause");return0;}
评测结果
| 时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|
8月17日20:30 | 部分正确 | 22 | 1085 | C++(g++4.7.2) | 1 | 384 | datrilla |
测试点
| 测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|
0 | 答案正确 | 1 | 384 | 15/15 |
1 | 答案正确 | 1 | 308 | 2/2 |
2 | 答案正确 | 1 | 304 | 2/2 |
3 | 答案正确 | 1 | 180 | 1/1 |
4 | 运行超时 | | | 0/3 |
5 | 答案正确 | 1 | 304 | 2/2 |
#include<iostream>#include<vector>#include<algorithm>usingnamespacestd;boolcmptobig(constlongint&A,constlongint&B){returnA<B;}intbinaryFound(vector<longint>sequence,longintgoal,longintfrom,longintto){longintmid;while(from<=to){mid=(from+to)/2;if(sequence[mid]>goal)to=mid-1;elsefrom=mid+1;}returnto;}intmain(){longintindex,max=0,front;longintN,p;cin>>N>>p;vector<longint>sequence(N);for(index=0;index<N;index++)cin>>sequence[index];sort(sequence.begin(),sequence.end(),cmptobig);for(index=0,front=0;N-index>max&&index<N;index++){longinttemp=p*sequence[index];if(temp<sequence[N-1]){front=binaryFound(sequence,temp,front,N-1);/*!!注意最后一个只到N-1*/if(max<front-index+1)max=front-index+1;}elseif(max<N-index)max=N-index;}cout<<max<<endl;system("pause");return0;}