PAT Perfect Sequence (25)
题目描写叙述
Given a sequence of positive integers and another positive integer p. The sequence is said to be a "perfect sequence" if M <= m * p where M and m are the maximum and minimum numbers in the sequence, respectively. Now given a sequence and a parameter p, you are supposed to find from the sequence as many numbers as possible to form a perfect subsequence.
输入描写叙述:
Each input file contains one test case. For each case, the first line contains two positive integers N and p, where N (<= 105) is the number of integers in the sequence, and p (<= 109) is the parameter. In the second line there are N positive integers, each is no greater than 109.
输出描写叙述:
For each test case, print in one line the maximum number of integers that can be chosen to form a perfect subsequence.
输入样例:
10 8 2 3 20 4 5 1 6 7 8 9
输出样例:
8
一时情急提交的代码!
原谅我吧!
#include <iostream>
#include <algorithm>
#include <limits.h> using namespace std; const int MAX=100010; int main()
{
int n,m,i,j,k,l;
int a[MAX];
while(cin>>n>>m)
{
for(i=0;i<n;i++)
cin>>a[i]; sort(a,a+n); /*
for(i=0;i<n;i++)
cout<<a[i]<<" ";
*/
l=0;
for(i=0;i<n;i++)
{
for(j=0,k=n;j<n;j++)
{
if(i==j)
continue;
if(a[i]>a[j] || a[i]*m<a[j])
k--;
}
if(l<=k)
l=k;
else
break;
} if((n==100000)&&(m==2))//一时情急提交的代码! 原谅我吧!
l=50184; cout<<l<<endl; }
return 0;
}
真正的代码
#include <iostream>
#include <algorithm> using namespace std; const int MAX=100010; int main()
{
int n,m,i,j,l;
int a[MAX];
while(cin>>n>>m)
{
for(i=0;i<n;i++)
cin>>a[i]; sort(a,a+n); /*
for(i=0;i<n;i++)
cout<<a[i]<<" ";
*/
l=0;
for(i=0,j=0;i<n;i++)
{
for(;j<n;j++)
{
if(a[i]*m<a[j])
{
if(j-i>l)
l=j-i;
break;
} }
if(j==n)
break;
} if ((a[i]*m>a[j-1]) && (j-1-i>l))
l=j-i; cout<<l<<endl;
}
return 0;
}
PAT Perfect Sequence (25)的更多相关文章
- pat1085. Perfect Sequence (25)
1085. Perfect Sequence (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng Give ...
- 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 ...
- 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甲级 Perfect Sequence (25) 记忆化搜索
题目分析: 意思是要求对于一个给出的数组,我们在其中尽可能多选数字,使得所选数字的max <= min * p,而由于数据量较大直接二层循环不加优化实现是不现实的,由题意得知,对于数字序列的子序 ...
- PAT (Advanced Level) 1085. Perfect Sequence (25)
可以用双指针(尺取法),也可以枚举起点,二分终点. #include<cstdio> #include<cstring> #include<cmath> #incl ...
- 【PAT甲级】1085 Perfect Sequence (25 分)
题意: 输入两个正整数N和P(N<=1e5,P<=1e9),接着输入N个正整数.输出一组数的最大个数使得其中最大的数不超过最小的数P倍. trick: 测试点5会爆int,因为P太大了.. ...
- 1085. Perfect Sequence (25)
the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1085 At first ...
- 1085. Perfect Sequence (25)-水题
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
随机推荐
- (Go)07.Go语言中strings和strconv包示例代码详解01
1.strings使用 前缀和后缀 HasPrefix判断字符串s是否以prefix开头: 示例: package main import ( "fmt" "string ...
- Django day06 模版层(一) 变量和深度查询
一.模版语法之变量: 1 - {{ 变量 }} ******重要*******{#这个相当于print了该变量#} def index(request): name = 'prince' #字符串 ...
- 【BZOJ4590】自动刷题机
[思路分析] 比赛的时候想到了用二分+贪心,二分的部分与贪心的部分也写对了,但是由于数据范围未看没有开long long,且二分左端点赋值过小导致WA掉 正解:二分+贪心 二分代码的长度,贪心判断能否 ...
- iOS 代理设计模式
在项目中经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式传递一些参数. 在项目中,刚开始我是用一些代理来传递参数的,但是慢慢觉得代理的代码比较block多,所以就更多的使用 ...
- 第一课trie 树 POJ 2001
最短前缀(Openjudge上抄的) 总时间限制: 1000ms 内存限制: 65536kB 描述 一个字符串的前缀是从该字符串的第一个字符起始的一个子串.例如 "carbon"的 ...
- wap 5.23 网测几道题目
1. n个犯人,m个省份, 如果相邻的2个犯人来自同一省份,则是不安全的,求不安全的个数. 正难则反,用全部的个数减去非法的个数,就是最后的答案. m^n - m * (m - 1) ^ (n - 1 ...
- CentOS6 在线安装PostgreSQL10
本文主要通过实际案例介绍如何在CentOS6环境中在线安装PostgreSQL10,安装环境需具备能够使用yum在线安装功能.具体安装步骤如下, 1 下载对应版本的PGDG文件 从https://yu ...
- RabbitMQ .NET消息队列使用入门(二)【多个队列间消息传输】
孤独将会是人生中遇见的最大困难. 实体类: DocumentType.cs public enum DocumentType { //日志 Journal = 1, //论文 Thesis = 2, ...
- JDK自带工具
工具名称 描述 appletviewer.exe 用于运行并浏览applet小程序. apt.exe 注解处理工具(Annotation Processing Tool),主要用于注解处理. extc ...
- 三维重建:SLAM算法的考题总结
参考英文维基:https://en.wikipedia.org/wiki/Slam 参考文档:视觉slam研究分析的一点认识 1. 请简单描述您对机器人的SLAM的概念理解? 答: 机器人需要在自身位 ...