1085. 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.
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 9Sample Output:
8
N个数 p
接着无序的N个数
我们要先把它排好序,然后根据要求,从最小的开始,看看当前*p>=max的max能到哪里,最长多少个;输出最长的个数
下面两个代码,就一个地方不一样,一个超时了一个没有超时。AC的在二分查找那里用的是指针,而另一个超时的不是指针。简单的说,AC传过去如果改变了回来的被改变的。而超时的,改变的和原来的没有关系。本来觉得二分查找也不用改变,就用了,结果就超时了,还以为我的二分是多么的不协调。
还要注意一点,当前*p会超过int,如果全部设为int 会一个测试点结果错误
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月17日 20:25 | 答案正确 | 25 | 1085 | C++ (g++ 4.7.2) | 39 | 1072 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 252 | 15/15 |
1 | 答案正确 | 1 | 308 | 2/2 |
2 | 答案正确 | 1 | 180 | 2/2 |
3 | 答案正确 | 1 | 308 | 1/1 |
4 | 答案正确 | 39 | 1072 | 3/3 |
5 | 答案正确 | 1 | 384 | 2/2 |
#include<iostream> #include<vector> #include<algorithm> using namespace std; void readln(vector<long int >*sequence, long int N) { long int index; for (index = 0; index < N; index++) cin >> (*sequence)[index]; } bool cmptobig(const long int &A, const long int &B){ return A < B; } int binaryFound(vector<long int >*sequence, long int goal, long int from, long int to) { long int mid; while (from <=to) { mid = (from + to) / 2; if ((*sequence)[mid] >goal) to = mid-1; else from = mid + 1; } return to; } int main() { long int index, max = 0, front; long int N, p; cin >> N >> p; vector<long int >sequence(N); readln(&sequence, N); sort(sequence.begin(), sequence.end(), cmptobig); for (index = 0, front = 0; N - index>max&&index < N; index++) { long int temp = p*sequence[index]; if (temp < sequence[N - 1]) { front = binaryFound(&sequence, temp, front, N - 1); /*!!注意最后一个只到N-1*/ if (max<front - index+1) max = front - index+1; } else if (max<N - index)max = N - index; } cout << max << endl; system("pause"); return 0; }
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月17日 20:30 | 部分正确 | 22 | 1085 | C++ (g++ 4.7.2) | 1 | 384 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 384 | 15/15 |
1 | 答案正确 | 1 | 308 | 2/2 |
2 | 答案正确 | 1 | 304 | 2/2 |
3 | 答案正确 | 1 | 180 | 1/1 |
4 | 运行超时 | 0/3 | ||
5 | 答案正确 | 1 | 304 | 2/2 |
#include<iostream> #include<vector> #include<algorithm> using namespace std; bool cmptobig(const long int &A, const long int &B){ return A < B; } int binaryFound(vector<long int >sequence, long int goal, long int from, long int to) { long int mid; while (from <=to) { mid = (from + to) / 2; if (sequence[mid] >goal) to = mid-1; else from = mid + 1; } return to; } int main() { long int index, max = 0,front; long int N, p; cin >> N >> p; vector<long int >sequence(N); for (index = 0; index < N; index++) cin >> sequence[index]; sort(sequence.begin(), sequence.end(), cmptobig); for (index = 0,front=0; N - index>max&&index < N; index++) { long int temp = p*sequence[index]; if (temp < sequence[N - 1]) { front = binaryFound(sequence, temp, front, N - 1); /*!!注意最后一个只到N-1*/ if (max<front-index+1) max = front - index+1 ; } else if(max<N-index)max = N - index; } cout << max << endl; system("pause"); return 0; }