1085. Perfect Sequence (25)
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)的更多相关文章
- 1085. Perfect Sequence (25) -二分查找
题目如下: Given a sequence of positive integers and another positive integer p. The sequence is said to ...
- 1085 Perfect Sequence (25 分)
Given a sequence of positive integers and another positive integer p. The sequence is said to be a p ...
- 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 ...
- PAT (Advanced Level) 1085. Perfect Sequence (25)
可以用双指针(尺取法),也可以枚举起点,二分终点. #include<cstdio> #include<cstring> #include<cmath> #incl ...
- 1085. Perfect Sequence (25)-水题
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- 【PAT甲级】1085 Perfect Sequence (25 分)
题意: 输入两个正整数N和P(N<=1e5,P<=1e9),接着输入N个正整数.输出一组数的最大个数使得其中最大的数不超过最小的数P倍. trick: 测试点5会爆int,因为P太大了.. ...
- pat1085. Perfect Sequence (25)
1085. Perfect Sequence (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng Give ...
- 1085 Perfect Sequence (25 分)
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
- PAT 1085 Perfect Sequence[难]
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
随机推荐
- 如何从ST网站找到对应的固件库
ST官方网站改版后,基本上很难搜索到固件库的地址,找了半天才找到固件库的下载地址,通过此方法可以找到其他需要的资源,故记下来方便大家. 下载的网站地址为: Home>Tools and Soft ...
- ORA-15124 数据库启动阶段报错
重新进行启动数据库的时候报错: SQL> startup nomount; ORA-15124: ASM file name '+KEL/ipap/controlfile/control02.c ...
- Tkinter教程之Canvas篇(4)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1812091 '''Tkinter教程之Canvas篇(4)''''''22.绘制弧形'''# ...
- syscolumns、sysconstraints、sysobjects
1.根据表名查询对象ID SELECT OBJECT_ID('Production.Product') 结果:1429580131 不能作为输入参数:列名.约束名 能作为输入参数:表名 2.根据对象I ...
- jedis连接池详解(Redis)
转自:http://tianxingzhe.blog.51cto.com/3390077/1684306 原子性(atomicity): 一个事务是一个不可分割的最小工作单位,事务中包括的诸操作要么都 ...
- java console ( mac osx ) 命令行编码
方法 vi ~/.bash_profile #添加新行, UTF-8表示你平台的编码方式 #例如你是GBK.GB18030的 #替换成你平台console可现实字符编码即可 export JAVA_T ...
- 浏览器插件-ActiveX
浏览器插件:B/S模式下通过在客户端浏览器安装插件调用外设或者处理特殊格式数据. 常用插件有身份证阅读器.sim卡阅读器.银行卡校验插件.手写板插件.小键盘插件: 处理表格数据的华表插件.图片合成插件 ...
- Gradle – Spring 4 MVC Hello World Example
In this tutorial, we will show you a Gradle + Spring 4 MVC, Hello World Example (JSP view), XML conf ...
- Spring AOP + AspectJ in XML configuration example
For those don't like annotation or using JDK 1.4, you can use AspectJ in XML based instead. Review l ...
- Spring入门(11)-Spring与Junit整合
POM配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3. ...