1085. Perfect Sequence (25)-PAT甲级真题
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.
Input Specification:
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.
Output Specification:
For each test case, print in one line the maximum number of integers that can be chosen to form a perfect subsequence.
Sample Input:
10 8
2 3 20 4 5 1 6 7 8 9
Sample Output:
8
题目大意:给定一个正整数数列,和正整数p,设这个数列中的最大值是M,最小值是m,如果M <= m * p,则称这个数列是完美数列。现在给定参数p和一些正整数,请你从中选择尽可能多的数构成一个完美数列。输入第一行给出两个正整数N(输入正数的个数)和p(给定的参数),第二行给出N个正整数。在一行中输出最多可以选择多少个数可以用它们组成一个完美数列
分析:简单题。首先将数列从小到大排序,设当前结果为result = 0,当前最长长度为temp = 0;从i = 0~n,j从i + result到n,【因为是为了找最大的result,所以下一次j只要从i的result个后面开始找就行了】每次计算temp若大于result则更新result,最后输出result的值~
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {int n;long long p;scanf("%d%lld", &n, &p);vector<int> v(n);for (int i = 0; i < n; i++)cin >> v[i];sort(v.begin(), v.end());int result = 0, temp = 0;for (int i = 0; i < n; i++) {for (int j = i + result; j < n; j++) {if (v[j] <= v[i] * p) {temp = j - i + 1;if (temp > result)result = temp;} else {break;}}}cout << result;return 0;
}
1085. Perfect Sequence (25)-PAT甲级真题相关推荐
- 1078. Hashing (25)-PAT甲级真题
1078. Hashing (25) The task of this problem is simple: insert a sequence of distinct positive intege ...
- 1020. Tree Traversals (25) PAT甲级真题
之前我看了这道题,实在是看不懂网上的解题答案,他们的具体思路基本上就是通过后续遍历和中序遍历,直接推出层次遍历. 我苦思冥想了半天,是在没看懂这种思路,于是想了一个笨点的但是也比较好理解的思路,通过后 ...
- 1040. Longest Symmetric String (25)-PAT甲级真题
Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...
- 1006. Sign In and Sign Out (25)-PAT甲级真题
At the beginning of every day, the first person who signs in the computer room will unlock the door, ...
- 1007. Maximum Subsequence Sum (25)-PAT甲级真题(最大连续子序列和、动态规划dp)
Given a sequence of K integers { N1, N2, -, NK }. A continuous subsequence is defined to be { Ni, Ni ...
- 1121. Damn Single (25)-PAT甲级真题
"Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are suppo ...
- 1090. Highest Price in Supply Chain (25)-PAT甲级真题
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- 1106. Lowest Price in Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- 1114. Family Property (25)-PAT甲级真题(并查集)
This time, you are supposed to help us collect the data for family-owned property. Given each person ...
最新文章
- Umbra 3:次世代的遮挡裁剪
- [转]Oracle 服务器名配置
- springboot热部署工具
- P1420 最长连号(python3实现)
- 嵌入式linux设备驱动程序是,嵌入式Linux设备驱动开发之:按键驱动程序实例-嵌入式系统-与非网...
- MPLS ××× Carrier Supporting Carrier Option AB(二)
- SQL2000系统表、存储过程、函数的功能介绍及应用
- 同样学软件测试,为什么有些人一面试就拿一堆大厂offer?
- Android图形之HWC(二十四)
- 二进制算法——模二运算
- QQ空间批量删除留言
- 【微信JSSDK】PHP版微信录音文件下载
- 全国超能吃辣的省份,连四川和湖南都不是对手
- JS黄金分割法实现随机漂亮颜色!
- 英语兔音标学习之双元音
- 服务器故障英文邮件,全球邮
- 打开网络邻居有很多计算机,教你网上邻居无法看到其它共享电脑的解决方法
- R包estimate评估肿瘤组织中基质及免疫细胞浸润水平
- iOS_隐藏系统音量(MPVolumeView初探)
- 高校课堂机器人工程方向教学设计不足与工作反思