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 ...
随机推荐
- selenium打开带有扩展的chrome
每当用跑用例失败的时候,第一反应就是查看元素定位是不是正确,帮助定位的扩展是必不可少的,但是selenium一般打开的是不带扩展的干净的浏览器,如果操作步骤很长的话,就得手动去执行直到那一步去检查元素 ...
- C++ 使用Htmlcxx解析Html内容(VS编译库文件)
1.下载Htmlcxx,http://sourceforge.net/projects/htmlcxx/ 2.解压htmlcxx-0.85.tar.gz 3.打开htmlcxx.vcproj,注意是h ...
- [GRYZ2015]工业时代
试题描述 小FF的第一片矿区已经开始运作了, 他着手开展第二片矿区……小FF的第二片矿区, 也是”NewBe_One“计划的核心部分, 因为在这片矿区里面有全宇宙最稀有的两种矿物,科学家称其为NEW矿 ...
- C语言反转字符串
也是面腾讯的一道编程题=,= 这题比较简单 代码如下: #include <stdio.h> #include <string.h> // 非递归实现字符串反转 char *r ...
- POJ 1004 解题报告
1.题目描述: http://poj.org/problem?id=1004 2.解题过程 这个题目咋一看很简单,虽然最终要解出来的确也不难,但是还是稍微有些小把戏在里面,其中最大的把戏就是float ...
- HttpServletRequest 的使用
1. 从HttpServletRequest中读取请求参数 InputStream inStream = request.getInputStream(); ByteArrayOutputStream ...
- redis 应用
前段使用JQueryMobile进行展示. 实现了用户注册,登陆,列表基本功能 非常简洁. 如果想了解Redis存储,Express的处理可以提供一些基础的示范. 下载地址: https://gith ...
- 应用数据存储到sdcard上一定要规范,android4.4.2有新规范
如果你的android设备有内部存储空间,即通常所说的机身存储(这就是指主要外部存储),那么你从外部插入SD卡就是一个二级外部存储设备. 最新的Android 4.4系统中,外置存储卡(SD卡)被称为 ...
- Homework-10 BASIC
对于本次作业: 我的整体思路如下: 1.首先修改二维数组求最大子数组和的C语言代码,加入分步骤的当前最优解边界值,局部最优解的记录,使之支持分步执行,连续执行,回滚等功能. 2.将程序改写为Javas ...
- HIVE删除表数据
HIVE只有INSERT,没有UPDATE跟DELETE,所以通过其他的语句产生DETELE效果. 在HDFS上或者本地服务器上新建空的文件XXXXX, 然后执行: LOAD DATA LOCAL I ...