1085. Perfect Sequence (25)

102 篇文章 0 订阅
37 篇文章 0 订阅

 

1085. Perfect Sequence (25)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CAO, Peng

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
N个数  p
接着无序的N个数

我们要先把它排好序,然后根据要求,从最小的开始,看看当前*p>=max的max能到哪里,最长多少个;输出最长的个数
下面两个代码,就一个地方不一样,一个超时了一个没有超时。AC的在二分查找那里用的是指针,而另一个超时的不是指针。简单的说,AC传过去如果改变了回来的被改变的。而超时的,改变的和原来的没有关系。本来觉得二分查找也不用改变,就用了,结果就超时了,还以为我的二分是多么的不协调。
还要注意一点,当前*p会超过int,如果全部设为int 会一个测试点结果错误

 评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
8月17日 20:25答案正确251085C++ (g++ 4.7.2)391072datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确125215/15
1答案正确13082/2
2答案正确11802/2
3答案正确13081/1
4答案正确3910723/3
5答案正确13842/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部分正确221085C++ (g++ 4.7.2)1384datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确138415/15
1答案正确13082/2
2答案正确13042/2
3答案正确11801/1
4运行超时  0/3
5答案正确13042/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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值