PAT (Basic Level) 1030 完美数列

题意

给定一个数组,选择尽可能多的数字构成完美序列。
完美序列:序列中,最大值 <= 最小值 * M,M为给定数字。

思路

sort一下,用尺取的方法来。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n, p;
	cin >> n >> p;
	vector<int> a(n);
	for (int& e : a) cin >> e;
	sort(a.begin(), a.end());
	int ans = 0;
	for (int i = 0, j = 0; i < n; ++i) {
		while (j < n && 1LL * a[i] * p >= a[j]) j++;
		ans = max(ans, j - i);
	}
	cout << ans << '\n';
	return 0;
} 

HINT

不定时更新更多题解,Basic Level 全部AC代码,详见 link ! ! !

发布了31 篇原创文章 · 获赞 15 · 访问量 751

猜你喜欢

转载自blog.csdn.net/abcdefbrhdb/article/details/104565535
今日推荐