the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1085

At first, i decide to write the code as follows:

/*
firstly: sort the array using the algorithm "sort"
secondly: traverse all the possible answer and find the most suitable one.
*/
#include<iostream>
#include<vector>
#include<algorithm> using namespace std; int main()
{
long long int n,p;
vector<long long int> v;
while (cin>>n>>p)
{
long long int temp;
while (n--)
{
cin>>temp;
v.push_back(temp);
}
sort(v.begin(),v.end());
long long int max = -;
for (int i = ; i < v.size(); i++)
{
long long int count = ;
for (int j = i + ; j < v.size(); j++)
{
if (v[i] * p >= v[j])
count += ;
else break;
}
if (count >= max)
{
max = count ;
}
}
cout<<max<<endl;
}
}

but the result of one example is time running out, so i evaluate the time complexity of the

code. i think the most time-consuming part is the double circulation,whose time complexity is

O(n*2). then i searched the web and found a better solution. its url is http://blog.csdn.net/nan327347465/article/details/39104721. he uses the binary search and successfully reduces the

tiem complexity down to O(n * log (n)).

1085. Perfect Sequence (25)的更多相关文章

  1. 1085. Perfect Sequence (25) -二分查找

    题目如下: Given a sequence of positive integers and another positive integer p. The sequence is said to ...

  2. 1085 Perfect Sequence (25 分)

    Given a sequence of positive integers and another positive integer p. The sequence is said to be a p ...

  3. PAT Advanced 1085 Perfect Sequence (25) [⼆分,two pointers]

    题目 Given a sequence of positive integers and another positive integer p. The sequence is said to be ...

  4. PAT (Advanced Level) 1085. Perfect Sequence (25)

    可以用双指针(尺取法),也可以枚举起点,二分终点. #include<cstdio> #include<cstring> #include<cmath> #incl ...

  5. 1085. Perfect Sequence (25)-水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  6. 【PAT甲级】1085 Perfect Sequence (25 分)

    题意: 输入两个正整数N和P(N<=1e5,P<=1e9),接着输入N个正整数.输出一组数的最大个数使得其中最大的数不超过最小的数P倍. trick: 测试点5会爆int,因为P太大了.. ...

  7. pat1085. Perfect Sequence (25)

    1085. Perfect Sequence (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng Give ...

  8. 1085 Perfect Sequence (25 分)

    1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...

  9. PAT 1085 Perfect Sequence[难]

    1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...

随机推荐

  1. JQuery WEB前段开发

    JQuery WEB前段开发 Jquery是继prototype之后又一个优秀的Javascript框架.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, S ...

  2. 使用python的logging模块

    一.从一个使用场景开始 开发一个日志系统, 既要把日志输出到控制台, 还要写入日志文件 import logging # 创建一个logger logger = logging.getLogger(' ...

  3. bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊(LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2002 [题意] 给定n个数的序列,i可以跳到i+k[i],需要能够修改k并可以查询跳出 ...

  4. Educational Codeforces Round 14

    A - Fashion in Berland 水 // #pragma comment(linker, "/STACK:102c000000,102c000000") #inclu ...

  5. Standalone HBase

    This is the default mode. Standalone mode is what is described in the quickstart section. In standal ...

  6. Unity3d自定义脚本模板

    这是一个小技巧,打开Unity安装目录,如: C:\Program Files (x86)\Unity\Editor\Data\Resources\ScriptTemplates /* * * Tit ...

  7. VS2015创建的C++程序在Debug模式下不能调试

    如题,不能调试的问题,解决办法: 1.修改工程属性页 2.修改VS2015托管兼容模式 工具->选项->调试,勾选使用托管兼容

  8. google proto buffer安装和简单示例

    1.安装 下载google proto buff. 解压下载的包,并且阅读README.txt,根据里面的指引进行安装. $ ./configure $ make $ make check $ mak ...

  9. HDU 4861 Couple doubi (数论 or 打表找规律)

    Couple doubi 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/D Description DouBiXp has a ...

  10. eclipse中如何设置tomcat启动时间

    现象:在eclipse中启动tomcat总是提示“Server Tomcat v5.5 Server @ localhost was unable to start within 45 seconds ...