戳我直达原题~
本题求一个序列满足 MAX <=MIN *p 的最长子序列个数
排序后暴力搜,有两个重要的代码优化如注释。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include <bits/stdc++.h>typedef long long ll;using namespace std;int main(){ int n; float p; int num[100010]; scanf("%d%f",&n,&p); for(int i = 0; i < n; i++) scanf("%d",num +i); sort(num, num + n); int ans = 1; for(int i = 0; i < n; i++) for(int j = i + ans; j < n; j++) //从比答案长的结果开始搜 if(num[j] <= num[i] * p) ans = j - i + 1; else //此时都不满足,那后面的max值更大更不可能完成,直接剪掉,如果没加这一步会超时 break; printf("%d
",ans);} |