PAT乙级 完美数列(25)

题意:

构造一个完美数列,符合最大值<=最小值*p,参数p会在输入中给出,构造一个符合这个规则的最长序列.

先用sort快排一下整个序列,之后从最小值开始计算,利用最小值*p这个数值在序列中二分查找,时间复杂度nlogn.

#include "iostream"
#include "string.h"
#include "string"
#include "vector"
#include "algorithm"
using namespace std;

//M <= m * p
int binary_search(vector array,int n,int value,int sub)
{
	int left = 0;
	int right = n-1;
	while(left<=right)
	{
		int middle = left + ((right-left)>>1);

		if(array[middle]>value && array[middle-1]value) 
				return middle + 1 - sub;
		}

		if(array[middle]>value)
		{
			right = middle - 1;
		}
		else if(array[middle]>count>>p;
	vector num;

	/*
	//test
	count = 100000;
	p = 42;
	for(i=1;i<=50000;i++)
	{
		num.push_back(i);
	}
	for(i=100000;i>50000;i--)
	{
		num.push_back(i);
	}
	*/

	for(i=0;i>temp;
		num.push_back(temp);
	}

	sort(num.begin(),num.end());


	for(i=0;i0&&num[i] == num[i-1])
			continue;
		if(max>count-i-1)
			break;

		//test
		//cout<<"参数:"<max)
			max = temp;
		
	}
	cout<


你可能感兴趣的:(数据结构算法学习)