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. <转>准备Eclips+python+robot framework环境

    关于python: path添加:C:\Python27;C:\Python27\Scripts; 新加一项:  PYTHON_HOME    C:\Python27 再不行的话,加一个用户变量: P ...

  2. erp验收测试

    软件测试是为了发现错误而执行程序的过程.它不仅是软件开发阶段的有机组成部分,而且在整个软件工程(即软件定义.设计和开发过程)中占据相当大的比重.软件测试是软件质量保证的关键环节,直接影响着软件的质量评 ...

  3. SQL你必须知道的-增删改查与约束

    SQL你必须知道的-增删改查与约束   -- 插入数据    --Insert 语句可以省略表名后的列名,但是不推荐    insert into Class values ('' 高一一班 '', ...

  4. linux 系统维护命令

    cat /home/username/.bash_history :查看用户的历史操作记录 (centos , mac 支持) last -x : 使用root登陆使用last -x可查看用户登陆历史 ...

  5. 【WPF】【火车站点信息查询】

    全文涉及到的是C#和XAML 如果这两门语言并非你喜欢的语言,那可以关闭本网页了 本文介绍的是什么? 一个火车站点信息查询软件 本文涉及到的WPF基本知识 Task async await WebCl ...

  6. POJ 1611 The Suspects (并查集)

    The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...

  7. ssh使用ajax异步通讯. json与对象转换的几个小问题

    首先是hibernate,用ssh做项目的时候,使用hibernate,这个hibernate博大精深,至今只懂皮毛.建对象时候使用它的一对多,多对多联系,. 这样子,对象转json的时候会产生循环依 ...

  8. Linux实战教学笔记08:Linux 文件的属性(下半部分)

    第八节 Linux 文件的属性(下半部分) 标签(空格分隔): Linux教学笔记 ---更多相关资料请点我查看 第1章 链接的概念 在linux系统中,链接可分为两种:一种为硬链接(Hard Lin ...

  9. mybatis generator配置文件

    <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration ...

  10. SOS 调试扩展 (SOS.dll)

    http://blog.csdn.net/cslie/article/details/2158780 SOS 调试扩展 (SOS.dll) 提供公共语言运行时(CLR)内部环境的有关信息,帮助你在Wi ...